TORCS  1.3.10
The Open Racing Car Simulator
Loading...
Searching...
No Matches
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-2026 Eric EspiƩ, Bernhard Wymann
6 email : berniw@bluewin.ch
7
8 ***************************************************************************/
9
10/***************************************************************************
11 * *
12 * This program is free software; you can redistribute it and/or modify *
13 * it under the terms of the GNU General Public License as published by *
14 * the Free Software Foundation; either version 2 of the License, or *
15 * (at your option) any later version. *
16 * *
17 ***************************************************************************/
18
21
22#include <stdio.h>
23#include <stdlib.h>
24#include <tgfclient.h>
25#include <raceinit.h>
26#include <graphic.h>
27#include <musicplayer/musicplayer.h>
28#include <portability.h>
29
30#include "soundconfig.h"
31
32static float LabelColor[] = {1.0, 0.0, 1.0, 1.0};
33
34// list of options.
35static const char *soundOptionList[] = {
37 //GR_ATT_SOUND_STATE_PLIB,
39};
40
41static const int nbOptions = sizeof(soundOptionList) / sizeof(soundOptionList[0]);
42static int curOption = 0;
43
44static const char *menuMusicList[] = {
47};
48static int curOptionMenuMusic = 0;
49
50// gui label id.
51static int SoundOptionId;
53
54// volume
55static float VolumeValue = 100.0f;
56//static int VolumeValueId;
57
58// gui screen handles.
59static void *scrHandle = NULL;
60static void *prevHandle = NULL;
61
62
63// Read sound configuration.
64static void readSoundCfg(void)
65{
66 const char *optionName;
67 int i;
68 const int BUFSIZE = 1024;
69 char buf[BUFSIZE];
70
71 snprintf(buf, BUFSIZE, "%s%s", GetLocalDir(), GR_SOUND_PARM_CFG);
72
73 void *paramHandle = GfParmReadFile(buf, GFPARM_RMODE_REREAD | GFPARM_RMODE_CREAT);
74 optionName = GfParmGetStr(paramHandle, GR_SCT_SOUND, GR_ATT_SOUND_STATE, soundOptionList[0]);
75
76 for (i = 0; i < nbOptions; i++) {
77 if (strcmp(optionName, soundOptionList[i]) == 0) {
78 curOption = i;
79 break;
80 }
81 }
82
83 VolumeValue = GfParmGetNum(paramHandle, GR_SCT_SOUND, GR_ATT_SOUND_VOLUME, "%", 100.0f);
84 if (VolumeValue>100.0f) {
85 VolumeValue = 100.0f;
86 }
87 if (VolumeValue < 0.0f) {
88 VolumeValue = 0.0f;
89 }
90
91 GfParmReleaseHandle(paramHandle);
92
94
95 // Read Menu music optons
96 snprintf(buf, BUFSIZE, "%s%s", GetLocalDir(), MM_SOUND_PARM_CFG);
99
100 if (strcmp(optionName, MM_VAL_SOUND_ENABLED) == 0) {
103 } else {
106 }
107
108 GfParmReleaseHandle(paramHandle);
109}
110
111
112// Save the choosen values in the corresponding parameter file.
113static void saveSoundOption(void)
114{
115 const int BUFSIZE = 1024;
116 char buf[BUFSIZE];
117
118 snprintf(buf, BUFSIZE, "%s%s", GetLocalDir(), GR_SOUND_PARM_CFG);
119 void *paramHandle = GfParmReadFile(buf, GFPARM_RMODE_REREAD | GFPARM_RMODE_CREAT);
122 GfParmWriteFile(NULL, paramHandle, "sound");
123 GfParmReleaseHandle(paramHandle);
124
125 // Write Menu music optons
126 snprintf(buf, BUFSIZE, "%s%s", GetLocalDir(), MM_SOUND_PARM_CFG);
129 GfParmWriteFile(NULL, paramHandle, "sound");
130 GfParmReleaseHandle(paramHandle);
131
132 if (curOptionMenuMusic == 1) {
134 } else {
136 }
137
138 // Return to previous screen.
140 return;
141}
142
143
144// Toggle sound state openal/plib/disabled.
145static void changeSoundState(int delta)
146{
147 if (delta < 0) {
148 curOption--;
149 if (curOption < 0) {
150 curOption = nbOptions - 1;
151 }
152 } else {
153 curOption++;
154 if (curOption == nbOptions) {
155 curOption = 0;
156 }
157 }
159}
160
161
162// Toggle menu music state between enabled and disabled.
163static void changeMenuMusicState(void)
164{
165 if (curOptionMenuMusic == 0) {
167 } else {
169 }
170
172}
173
174
175// Volume
176/*
177static void changeVolume(void * )
178{
179 char *val;
180 const int BUFSIZE = 1024;
181 char buf[BUFSIZE];
182 val = GfuiEditboxGetString(scrHandle, VolumeValueId);
183 sscanf(val, "%g", &VolumeValue);
184 snprintf(buf, BUFSIZE, "%g", VolumeValue);
185 GfuiEditboxSetString(scrHandle, VolumeValueId, buf);
186}
187*/
188
189static void onActivate(void)
190{
191 readSoundCfg();
192}
193
194
195// Sound menu
196void * SoundMenuInit(void *prevMenu)
197{
198 int x, y, x2, x3, x4, dy;
199// char buf[1024];
200
201
202 // Has screen already been created?
203 if (scrHandle) {
204 return scrHandle;
205 }
206
207 prevHandle = prevMenu;
208
210 GfuiTitleCreate(scrHandle, "Sound Configuration", 0);
211 GfuiScreenAddBgImg(scrHandle, "data/img/splash-qrdrv.png");
212
213 x = 20;
214 x2 = 200;
215 x3 = 340;
216 x4 = (x2+x3)/2;
217 y = 400;
218 dy = 30;
219
220 y -= dy;
221
223 GfuiGrButtonCreateAction(scrHandle, "data/img/arrow-left.png", "data/img/arrow-left.png",
224 "data/img/arrow-left.png", "data/img/arrow-left-pushed.png",
225 x2, y-5, GFUI_ALIGN_HL_VB, 1,
226 [] { changeSoundState(-1); }, tGfuiAction(), tGfuiAction());
227
228 GfuiGrButtonCreateAction(scrHandle, "data/img/arrow-right.png", "data/img/arrow-right.png",
229 "data/img/arrow-right.png", "data/img/arrow-right-pushed.png",
230 x3, y-5, GFUI_ALIGN_HR_VB, 1,
231 [] { changeSoundState(1); }, tGfuiAction(), tGfuiAction());
232
235
236 y -= dy;
237
239 GfuiGrButtonCreateAction(scrHandle, "data/img/arrow-left.png", "data/img/arrow-left.png",
240 "data/img/arrow-left.png", "data/img/arrow-left-pushed.png",
241 x2, y-5, GFUI_ALIGN_HL_VB, 1,
243
244 GfuiGrButtonCreateAction(scrHandle, "data/img/arrow-right.png", "data/img/arrow-right.png",
245 "data/img/arrow-right.png", "data/img/arrow-right-pushed.png",
246 x3, y-5, GFUI_ALIGN_HR_VB, 1,
248
251
252 /*
253 y -= dy;
254 GfuiLabelCreate(scrHandle, "Volume:", GFUI_FONT_MEDIUM, x, y, GFUI_ALIGN_HL_VB, 0);
255
256
257 snprintf(buf, "%f", VolumeValue);
258 VolumeValueId = GfuiEditboxCreate(scrHandle, buf, GFUI_FONT_MEDIUM_C,
259 x2+10, y+2, x4-x2+20, 16, NULL, (tfuiCallback)NULL, changeVolume);
260
261
262*/
265
267 [prevMenu] { GfuiScreenActivate(prevMenu); }, tGfuiAction(), tGfuiAction());
268
270 GfuiAddKeyAction(scrHandle, 27, "Cancel Selection", [prevMenu] { GfuiScreenActivate(prevMenu); }, tGfuiAction());
271 GfuiAddSKeyAction(scrHandle, GLUT_KEY_F12, "Screen-Shot", [] { GfuiScreenShot(NULL); }, tGfuiAction());
272 GfuiAddSKeyAction(scrHandle, GLUT_KEY_LEFT, "Previous Option in list", [] { changeSoundState(-1); }, tGfuiAction());
273 GfuiAddSKeyAction(scrHandle, GLUT_KEY_RIGHT, "Next Option in list", [] { changeSoundState(1); }, tGfuiAction());
274
275 readSoundCfg();
276
277 return scrHandle;
278}
static Vector y[4]
Definition Convex.cpp:56
static void * prevHandle
static void * scrHandle
static void onActivate(void)
static float LabelColor[]
#define GR_ATT_SOUND_STATE_DISABLED
Definition graphic.h:47
#define GR_ATT_SOUND_VOLUME
Definition graphic.h:48
#define GR_SCT_SOUND
Definition graphic.h:43
#define GR_ATT_SOUND_STATE_OPENAL
Definition graphic.h:46
#define GR_SOUND_PARM_CFG
Definition graphic.h:42
#define GR_ATT_SOUND_STATE
Definition graphic.h:44
void * GfuiScreenCreateExAction(float *bgColor, const tGfuiAction &onActivate, const tGfuiAction &onDeactivate, int mouseAllowed)
Create a screen.
Definition gui.cpp:619
void GfuiLabelSetText(void *scr, int id, const char *text)
Change the text of a label.
Definition guilabel.cpp:193
int GfuiTitleCreate(void *scr, const char *text, int maxlen)
Add a Title to the screen.
Definition guilabel.cpp:150
void GfuiAddKeyAction(void *scr, unsigned char key, const char *descr, const tGfuiAction &onKeyPressed, const tGfuiAction &onKeyReleased)
Add a Keyboard callback to a screen.
Definition gui.cpp:809
void GfuiScreenAddBgImg(void *scr, const char *filename)
Add an image background to a screen.
Definition gui.cpp:1058
void GfuiAddSKeyAction(void *scr, int key, const char *descr, const tGfuiAction &onKeyPressed, const tGfuiAction &onKeyReleased)
Add a Special Keyboard shortcut to the screen.
Definition gui.cpp:895
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:121
void GfuiLabelSetColor(void *scr, int id, float *color)
Change the color of a label.
Definition guilabel.cpp:221
int GfuiGrButtonCreateAction(void *scr, const char *disabled, const char *enabled, const char *focused, const char *pushed, int x, int y, int align, int mouse, const tGfuiAction &onPush, const tGfuiAction &onFocus, const tGfuiAction &onFocusLost)
Add a graphical button to a screen.
Definition guibutton.cpp:58
void GfuiScreenActivate(void *screen)
Activate a screen and make it current.
Definition gui.cpp:508
int GfuiButtonCreateAction(void *scr, const char *text, int font, int x, int y, int width, int align, int mouse, const tGfuiAction &onPush, const tGfuiAction &onFocus, const tGfuiAction &onFocusLost)
Add a button to a screen.
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:2453
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:2623
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:2534
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:2737
void GfParmReleaseHandle(void *parmHandle)
Release given parameter set handle parmHandle.
Definition params.cpp:1963
int GfParmWriteFile(const char *file, void *parmHandle, const char *name)
Write parameter set into file.
Definition params.cpp:1732
void * GfParmReadFile(const char *file, int mode)
Read parameter set from file and return handle to parameter set.
Definition params.cpp:1173
void GfuiScreenShot(void *)
Save a screen shot in png format.
Definition gui.cpp:998
void stopMenuMusic()
void startMenuMusic()
#define MM_ATT_SOUND_ENABLE
Definition musicplayer.h:25
#define MM_SOUND_PARM_CFG
Definition musicplayer.h:23
#define MM_SCT_SOUND
Definition musicplayer.h:24
#define MM_VAL_SOUND_ENABLED
Definition musicplayer.h:26
#define MM_VAL_SOUND_DISABLED
Definition musicplayer.h:27
static void changeMenuMusicState(void)
static const char * menuMusicList[]
static float VolumeValue
static int curOptionMenuMusic
static int SoundOptionId
static void saveSoundOption(void)
static void readSoundCfg(void)
static void onActivate(void)
static int MenuMusicOptionId
static void changeSoundState(int delta)
void * SoundMenuInit(void *prevMenu)
static const int nbOptions
static int curOption
static const char * soundOptionList[]
char * GetLocalDir(void)
Definition tgf.cpp:230
#define GFPARM_RMODE_CREAT
Allow an empty in-memory set on file-open failure; does not write a file.
Definition tgf.h:266
#define GFPARM_RMODE_REREAD
Clear shared cached data and reread the file.
Definition tgf.h:265
#define GFUI_FONT_LARGE
Definition tgfclient.h:248
#define GFUI_FONT_MEDIUM
Definition tgfclient.h:249
#define GFUI_FONT_MEDIUM_C
Definition tgfclient.h:253
#define GFUI_ALIGN_HL_VB
Definition tgfclient.h:71
#define GFUI_MOUSE_UP
Definition tgfclient.h:82
std::function< void()> tGfuiAction
Definition tgfclient.h:107
#define GFUI_ALIGN_HR_VB
Definition tgfclient.h:77
#define GFUI_ALIGN_HC_VB
Definition tgfclient.h:74