TORCS  1.3.9
The Open Racing Car Simulator
soundconfig.cpp
Go to the documentation of this file.
1 /***************************************************************************
2 
3  file : soundconfig.cpp
4  created : Thu Dec 12 15:12:07 CET 2004
5  copyright : (C) 2004 Eric Espi�, Bernhard Wymann, baseed on simuconfig.cpp
6  email : berniw@bluewin.ch
7  version : $Id$
8 
9  ***************************************************************************/
10 
11 /***************************************************************************
12  * *
13  * This program is free software; you can redistribute it and/or modify *
14  * it under the terms of the GNU General Public License as published by *
15  * the Free Software Foundation; either version 2 of the License, or *
16  * (at your option) any later version. *
17  * *
18  ***************************************************************************/
19 
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <tgfclient.h>
28 #include <raceinit.h>
29 #include <graphic.h>
31 #include <portability.h>
32 
33 #include "soundconfig.h"
34 
35 static float LabelColor[] = {1.0, 0.0, 1.0, 1.0};
36 
37 // list of options.
38 static const char *soundOptionList[] = {
40  //GR_ATT_SOUND_STATE_PLIB,
42 };
43 
44 static const int nbOptions = sizeof(soundOptionList) / sizeof(soundOptionList[0]);
45 static int curOption = 0;
46 
47 static const char *menuMusicList[] = {
50 };
51 static int curOptionMenuMusic = 0;
52 
53 // gui label id.
54 static int SoundOptionId;
55 static int MenuMusicOptionId;
56 
57 // volume
58 static float VolumeValue = 100.0f;
59 //static int VolumeValueId;
60 
61 // gui screen handles.
62 static void *scrHandle = NULL;
63 static void *prevHandle = NULL;
64 
65 
66 // Read sound configuration.
67 static void readSoundCfg(void)
68 {
69  const char *optionName;
70  int i;
71  const int BUFSIZE = 1024;
72  char buf[BUFSIZE];
73 
74  snprintf(buf, BUFSIZE, "%s%s", GetLocalDir(), GR_SOUND_PARM_CFG);
75 
76  void *paramHandle = GfParmReadFile(buf, GFPARM_RMODE_REREAD | GFPARM_RMODE_CREAT);
77  optionName = GfParmGetStr(paramHandle, GR_SCT_SOUND, GR_ATT_SOUND_STATE, soundOptionList[0]);
78 
79  for (i = 0; i < nbOptions; i++) {
80  if (strcmp(optionName, soundOptionList[i]) == 0) {
81  curOption = i;
82  break;
83  }
84  }
85 
86  VolumeValue = GfParmGetNum(paramHandle, GR_SCT_SOUND, GR_ATT_SOUND_VOLUME, "%", 100.0f);
87  if (VolumeValue>100.0f) {
88  VolumeValue = 100.0f;
89  }
90  if (VolumeValue < 0.0f) {
91  VolumeValue = 0.0f;
92  }
93 
94  GfParmReleaseHandle(paramHandle);
95 
97 
98  // Read Menu music optons
99  snprintf(buf, BUFSIZE, "%s%s", GetLocalDir(), MM_SOUND_PARM_CFG);
102 
103  if (strcmp(optionName, MM_VAL_SOUND_ENABLED) == 0) {
105  curOptionMenuMusic = 1;
106  } else {
108  curOptionMenuMusic = 0;
109  }
110 
111  GfParmReleaseHandle(paramHandle);
112 }
113 
114 
115 // Save the choosen values in the corresponding parameter file.
116 static void saveSoundOption(void *)
117 {
118  const int BUFSIZE = 1024;
119  char buf[BUFSIZE];
120 
121  snprintf(buf, BUFSIZE, "%s%s", GetLocalDir(), GR_SOUND_PARM_CFG);
122  void *paramHandle = GfParmReadFile(buf, GFPARM_RMODE_REREAD | GFPARM_RMODE_CREAT);
125  GfParmWriteFile(NULL, paramHandle, "sound");
126  GfParmReleaseHandle(paramHandle);
127 
128  // Write Menu music optons
129  snprintf(buf, BUFSIZE, "%s%s", GetLocalDir(), MM_SOUND_PARM_CFG);
132  GfParmWriteFile(NULL, paramHandle, "sound");
133  GfParmReleaseHandle(paramHandle);
134 
135  if (curOptionMenuMusic == 1) {
136  startMenuMusic();
137  } else {
138  stopMenuMusic();
139  }
140 
141  // Return to previous screen.
143  return;
144 }
145 
146 
147 // Toggle sound state openal/plib/disabled.
148 static void changeSoundState(void *vp)
149 {
150  if (vp == 0) {
151  curOption--;
152  if (curOption < 0) {
153  curOption = nbOptions - 1;
154  }
155  } else {
156  curOption++;
157  if (curOption == nbOptions) {
158  curOption = 0;
159  }
160  }
162 }
163 
164 
165 // Toggle menu music state between enabled and disabled.
166 static void changeMenuMusicState(void *vp)
167 {
168  if (curOptionMenuMusic == 0) {
169  curOptionMenuMusic = 1;
170  } else {
171  curOptionMenuMusic = 0;
172  }
173 
175 }
176 
177 
178 // Volume
179 /*
180 static void changeVolume(void * )
181 {
182  char *val;
183  const int BUFSIZE = 1024;
184  char buf[BUFSIZE];
185  val = GfuiEditboxGetString(scrHandle, VolumeValueId);
186  sscanf(val, "%g", &VolumeValue);
187  snprintf(buf, BUFSIZE, "%g", VolumeValue);
188  GfuiEditboxSetString(scrHandle, VolumeValueId, buf);
189 }
190 */
191 
192 static void onActivate(void * /* dummy */)
193 {
194  readSoundCfg();
195 }
196 
197 
198 // Sound menu
199 void * SoundMenuInit(void *prevMenu)
200 {
201  int x, y, x2, x3, x4, dy;
202 // char buf[1024];
203 
204 
205  // Has screen already been created?
206  if (scrHandle) {
207  return scrHandle;
208  }
209 
210  prevHandle = prevMenu;
211 
212  scrHandle = GfuiScreenCreateEx((float*)NULL, NULL, onActivate, NULL, (tfuiCallback)NULL, 1);
213  GfuiTitleCreate(scrHandle, "Sound Configuration", 0);
214  GfuiScreenAddBgImg(scrHandle, "data/img/splash-qrdrv.png");
215 
216  x = 20;
217  x2 = 200;
218  x3 = 340;
219  x4 = (x2+x3)/2;
220  y = 400;
221  dy = 30;
222 
223  y -= dy;
224 
225  GfuiLabelCreate(scrHandle, "Sound System:", GFUI_FONT_MEDIUM, x, y, GFUI_ALIGN_HL_VB, 0);
226  GfuiGrButtonCreate(scrHandle, "data/img/arrow-left.png", "data/img/arrow-left.png",
227  "data/img/arrow-left.png", "data/img/arrow-left-pushed.png",
228  x2, y-5, GFUI_ALIGN_HL_VB, 1,
229  (void*)-1, changeSoundState,
230  NULL, (tfuiCallback)NULL, (tfuiCallback)NULL);
231 
232  GfuiGrButtonCreate(scrHandle, "data/img/arrow-right.png", "data/img/arrow-right.png",
233  "data/img/arrow-right.png", "data/img/arrow-right-pushed.png",
234  x3, y-5, GFUI_ALIGN_HR_VB, 1,
235  (void*)1, changeSoundState,
236  NULL, (tfuiCallback)NULL, (tfuiCallback)NULL);
237 
240 
241  y -= dy;
242 
244  GfuiGrButtonCreate(scrHandle, "data/img/arrow-left.png", "data/img/arrow-left.png",
245  "data/img/arrow-left.png", "data/img/arrow-left-pushed.png",
246  x2, y-5, GFUI_ALIGN_HL_VB, 1,
247  (void*)-1, changeMenuMusicState,
248  NULL, (tfuiCallback)NULL, (tfuiCallback)NULL);
249 
250  GfuiGrButtonCreate(scrHandle, "data/img/arrow-right.png", "data/img/arrow-right.png",
251  "data/img/arrow-right.png", "data/img/arrow-right-pushed.png",
252  x3, y-5, GFUI_ALIGN_HR_VB, 1,
253  (void*)1, changeMenuMusicState,
254  NULL, (tfuiCallback)NULL, (tfuiCallback)NULL);
255 
258 
259  /*
260  y -= dy;
261  GfuiLabelCreate(scrHandle, "Volume:", GFUI_FONT_MEDIUM, x, y, GFUI_ALIGN_HL_VB, 0);
262 
263 
264  snprintf(buf, "%f", VolumeValue);
265  VolumeValueId = GfuiEditboxCreate(scrHandle, buf, GFUI_FONT_MEDIUM_C,
266  x2+10, y+2, x4-x2+20, 16, NULL, (tfuiCallback)NULL, changeVolume);
267 
268 
269 */
271  NULL, saveSoundOption, NULL, (tfuiCallback)NULL, (tfuiCallback)NULL);
272 
274  prevMenu, GfuiScreenActivate, NULL, (tfuiCallback)NULL, (tfuiCallback)NULL);
275 
276  GfuiAddKey(scrHandle, 13, "Save", NULL, saveSoundOption, NULL);
277  GfuiAddKey(scrHandle, 27, "Cancel Selection", prevMenu, GfuiScreenActivate, NULL);
278  GfuiAddSKey(scrHandle, GLUT_KEY_F12, "Screen-Shot", NULL, GfuiScreenShot, NULL);
279  GfuiAddSKey(scrHandle, GLUT_KEY_LEFT, "Previous Option in list", (void*)0, changeSoundState, NULL);
280  GfuiAddSKey(scrHandle, GLUT_KEY_RIGHT, "Next Option in list", (void*)1, changeSoundState, NULL);
281 
282  readSoundCfg();
283 
284  return scrHandle;
285 }
int GfParmSetStr(void *handle, const char *path, const char *key, const char *val)
Set a string parameter in the parameter set handle.
Definition: params.cpp:2477
static void * prevHandle
Definition: soundconfig.cpp:63
#define GFUI_MOUSE_UP
Definition: tgfclient.h:80
static int curOption
Definition: soundconfig.cpp:45
void * GfParmReadFile(const char *file, int mode)
Read parameter set from file and return handle to parameter set.
Definition: params.cpp:1157
static int SoundOptionId
Definition: soundconfig.cpp:54
int GfuiButtonCreate(void *scr, const char *text, int font, int x, int y, int width, int align, int mouse, void *userDataOnPush, tfuiCallback onPush, void *userDataOnFocus, tfuiCallback onFocus, tfuiCallback onFocusLost)
Add a button to a screen.
Definition: guibutton.cpp:248
#define MM_ATT_SOUND_ENABLE
Definition: musicplayer.h:25
char * GetLocalDir(void)
Definition: tgf.cpp:231
void GfParmReleaseHandle(void *parmHandle)
Release given parameter set handle parmHandle.
Definition: params.cpp:1834
void GfuiAddKey(void *scr, unsigned char key, const char *descr, void *userData, tfuiCallback onKeyPressed, tfuiCallback onKeyReleased)
Add a Keyboard callback to a screen.
Definition: gui.cpp:742
static int MenuMusicOptionId
Definition: soundconfig.cpp:55
#define GR_SCT_SOUND
Definition: graphic.h:45
#define MM_VAL_SOUND_DISABLED
Definition: musicplayer.h:27
int GfuiLabelCreate(void *scr, const char *text, int font, int x, int y, int align, int maxlen)
Add a label to a screen.
Definition: guilabel.cpp:142
static int curOptionMenuMusic
Definition: soundconfig.cpp:51
static void changeMenuMusicState(void *vp)
void GfuiScreenAddBgImg(void *scr, const char *filename)
Add an image background to a screen.
Definition: gui.cpp:961
#define MM_SOUND_PARM_CFG
Definition: musicplayer.h:23
int GfParmWriteFile(const char *file, void *parmHandle, const char *name)
Write parameter set into file.
Definition: params.cpp:1610
#define GFUI_ALIGN_HR_VB
Definition: tgfclient.h:75
#define GR_ATT_SOUND_STATE_DISABLED
Definition: graphic.h:49
const char * GfParmGetStr(void *parmHandle, const char *path, const char *key, const char *deflt)
Get a string parameter from the parameter set handle.
Definition: params.cpp:2311
The Gaming Framework API (client part).
static const char * menuMusicList[]
Definition: soundconfig.cpp:47
#define GFUI_FONT_LARGE
Definition: tgfclient.h:168
void * GfuiScreenCreateEx(float *bgColor, void *userDataOnActivate, tfuiCallback onActivate, void *userDataOnDeactivate, tfuiCallback onDeactivate, int mouseAllowed)
Create a screen.
Definition: gui.cpp:578
static const int nbOptions
Definition: soundconfig.cpp:44
void GfuiLabelSetText(void *scr, int id, const char *text)
Change the text of a label.
Definition: guilabel.cpp:212
static float LabelColor[]
Definition: soundconfig.cpp:35
#define GR_SOUND_PARM_CFG
Definition: graphic.h:44
#define GFPARM_RMODE_REREAD
reread the parameters from file and release the previous ones
Definition: tgf.h:266
void GfuiLabelSetColor(void *scr, int id, float *color)
Change the color of a label.
Definition: guilabel.cpp:239
Graphic Module Interface Definition.
static void * scrHandle
Definition: soundconfig.cpp:62
void * SoundMenuInit(void *prevMenu)
#define GR_ATT_SOUND_VOLUME
Definition: graphic.h:50
static void saveSoundOption(void *)
#define MM_SCT_SOUND
Definition: musicplayer.h:24
void(* tfuiCallback)(void *)
Definition: tgfclient.h:105
void GfuiScreenActivate(void *screen)
Activate a screen and make it current.
Definition: gui.cpp:467
void GfuiAddSKey(void *scr, int key, const char *descr, void *userData, tfuiCallback onKeyPressed, tfuiCallback onKeyReleased)
Add a Special Keyboard shortcut to the screen.
Definition: gui.cpp:816
int GfParmSetNum(void *handle, const char *path, const char *key, const char *unit, tdble val)
Set a numerical parameter in the parameter set handle.
Definition: params.cpp:2586
#define GFUI_ALIGN_HC_VB
Definition: tgfclient.h:72
#define GFUI_ALIGN_HL_VB
Definition: tgfclient.h:69
tdble GfParmGetNum(void *handle, const char *path, const char *key, const char *unit, tdble deflt)
Get a numerical parameter from the parameter set handle.
Definition: params.cpp:2392
void GfuiScreenShot(void *)
Save a screen shot in png format.
Definition: gui.cpp:913
static Vector y[4]
Definition: Convex.cpp:56
#define GR_ATT_SOUND_STATE_OPENAL
Definition: graphic.h:48
int GfuiTitleCreate(void *scr, const char *text, int maxlen)
Add a Title to the screen.
Definition: guilabel.cpp:170
#define GR_ATT_SOUND_STATE
Definition: graphic.h:46
int GfuiGrButtonCreate(void *scr, const char *disabled, const char *enabled, const char *focused, const char *pushed, int x, int y, int align, int mouse, void *userDataOnPush, tfuiCallback onPush, void *userDataOnFocus, tfuiCallback onFocus, tfuiCallback onFocusLost)
Add a graphical button to a screen.
Definition: guibutton.cpp:62
#define GFUI_FONT_MEDIUM_C
Definition: tgfclient.h:173
static void readSoundCfg(void)
Definition: soundconfig.cpp:67
static const char * soundOptionList[]
Definition: soundconfig.cpp:38
#define GFUI_FONT_MEDIUM
Definition: tgfclient.h:169
static void onActivate(void *)
void startMenuMusic()
Definition: musicplayer.cpp:80
static float VolumeValue
Definition: soundconfig.cpp:58
void stopMenuMusic()
Definition: musicplayer.cpp:90
static void changeSoundState(void *vp)
#define GFPARM_RMODE_CREAT
Create the file if doesn&#39;t exist.
Definition: tgf.h:267
#define MM_VAL_SOUND_ENABLED
Definition: musicplayer.h:26