1 /* --------------------------------------------------------------------
2 EXTREME TUXRACER
3
4 Copyright (C) 2010 Extreme Tux Racer Team
5
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; either version 2
9 of the License, or (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15 ---------------------------------------------------------------------*/
16
17 /*
18 If you want to add a new option, do this:
19 First add the option to the TParam struct (game_config.h).
20
21 Then edit the below functions:
22
23 - LoadConfigFile. Use
24 SPIntN for integer and boolean values
25 SPStrN for strings.
26 The first value is always 'line', the second defines the tag within the
27 brackets [ ], and the last value is the default.
28
29 - SetConfigDefaults. These values are used as long as no options file exists.
30 It's a good idea to use the same values as the defaults in LoadConfigFile.
31
32 - SaveConfigFile. See the other entries; it should be self-explanatory.
33 If an options file exists, you will have to change any value at runtime
34 on the configuration screen to overwrite the file. Then you will see the
35 new entry.
36 */
37
38 #ifdef HAVE_CONFIG_H
39 #include <etr_config.h>
40 #endif
41
42 #include "config_screen.h"
43 #include "spx.h"
44 #include "translation.h"
45 #include "particles.h"
46 #include "audio.h"
47 #include "ogl.h"
48 #include "gui.h"
49 #include "font.h"
50 #include "winsys.h"
51
52 CGameConfig GameConfig;
53 static std::string res_names[NUM_RESOLUTIONS];
54
55 static TCheckbox* fullscreen;
56 static TUpDown* language;
57 static TUpDown* resolution;
58 static TUpDown* mus_vol;
59 static TUpDown* sound_vol;
60 static TUpDown* detail_level;
61 static TWidget* textbuttons[2];
62 static TLabel* descriptions[5];
63
SetConfig()64 void SetConfig() {
65 if (mus_vol->GetValue() != param.music_volume ||
66 sound_vol->GetValue() != param.sound_volume ||
67 language->GetValue() != param.language ||
68 resolution->GetValue() != param.res_type ||
69 detail_level->GetValue() != param.perf_level ||
70 fullscreen->checked != param.fullscreen) {
71
72 if (resolution->GetValue() != param.res_type || fullscreen->checked != param.fullscreen) {
73 // these changes require a new VideoMode
74 param.res_type = resolution->GetValue();
75 param.fullscreen = fullscreen->checked;
76 Winsys.SetupVideoMode(param.res_type);
77 init_ui_snow(); // Reinitialize UI snow to avoid ugly snow-free stripes at the borders
78 }
79
80 // the followind config params don't require a new VideoMode
81 // they only must stored in the param structure (and saved)
82 param.music_volume = mus_vol->GetValue();
83 Music.SetVolume(param.music_volume);
84 param.sound_volume = sound_vol->GetValue();
85 param.perf_level = detail_level->GetValue();
86 FT.SetFontFromSettings();
87 if (param.language != language->GetValue()) {
88 param.language = language->GetValue();
89 Trans.ChangeLanguage(param.language);
90 }
91 SaveConfigFile();
92 }
93 State::manager.RequestEnterState(*State::manager.PreviousState());
94 }
95
Keyb(sf::Keyboard::Key key,bool release,int x,int y)96 void CGameConfig::Keyb(sf::Keyboard::Key key, bool release, int x, int y) {
97 if (release) return;
98
99 switch (key) {
100 case sf::Keyboard::U:
101 param.ui_snow = !param.ui_snow;
102 break;
103 case sf::Keyboard::Escape:
104 State::manager.RequestEnterState(*State::manager.PreviousState());
105 break;
106 case sf::Keyboard::Return:
107 if (textbuttons[0]->focussed())
108 State::manager.RequestEnterState(*State::manager.PreviousState());
109 else if (textbuttons[1]->focussed())
110 SetConfig();
111 break;
112 default:
113 KeyGUI(key, release);
114 break;
115 }
116 }
117
Mouse(int button,int state,int x,int y)118 void CGameConfig::Mouse(int button, int state, int x, int y) {
119 if (state == 1) {
120 TWidget* focussed = ClickGUI(x, y);
121
122 if (focussed == textbuttons[0])
123 State::manager.RequestEnterState(*State::manager.PreviousState());
124 else if (focussed == textbuttons[1])
125 SetConfig();
126 }
127 }
128
Motion(int x,int y)129 void CGameConfig::Motion(int x, int y) {
130 MouseMoveGUI(x, y);
131
132 if (param.ui_snow) push_ui_snow(cursor_pos);
133 }
134
135 // ------------------ Init --------------------------------------------
136
137 static TArea area;
138 static int dd;
139 static int columnAnchor;
140
Enter()141 void CGameConfig::Enter() {
142 Winsys.ShowCursor(!param.ice_cursor);
143
144 for (int i=0; i<NUM_RESOLUTIONS; i++)
145 res_names[i] = Winsys.GetResName(i);
146
147 int framewidth = 550 * Winsys.scale;
148 area = AutoAreaN(30, 80, framewidth);
149 FT.AutoSizeN(4);
150 dd = FT.AutoDistanceN(3);
151 if (dd < 36) dd = 36;
152 int rightpos = area.right -48;
153
154 ResetGUI();
155 unsigned int siz = FT.AutoSizeN(5);
156 fullscreen = AddCheckbox(area.left, area.top, framewidth-16, Trans.Text(31));
157 fullscreen->checked = param.fullscreen;
158
159 resolution = AddUpDown(rightpos, area.top+dd*1, 0, NUM_RESOLUTIONS-1, (int)param.res_type);
160 mus_vol = AddUpDown(rightpos, area.top+dd*2, 0, 100, param.music_volume, 2, true);
161 sound_vol = AddUpDown(rightpos, area.top+dd*3, 0, 100, param.sound_volume, 2, true);
162 language = AddUpDown(rightpos, area.top+dd*4, 0, (int)Trans.languages.size() - 1, (int)param.language);
163 detail_level = AddUpDown(rightpos, area.top+dd*5, 1, 4, param.perf_level, 2, true);
164
165 textbuttons[0] = AddTextButton(Trans.Text(28), area.left+50, AutoYPosN(80), siz);
166 float len = FT.GetTextWidth(Trans.Text(8));
167 textbuttons[1] = AddTextButton(Trans.Text(15), area.right-len-50, AutoYPosN(80), siz);
168
169 columnAnchor = 0;
170 for (int i = 0; i < 5; i++) {
171 descriptions[i] = AddLabel(Trans.Text(32 + i), area.left, area.top + dd*(i + 1), colWhite);
172 columnAnchor = std::max(columnAnchor, (int)descriptions[i]->GetSize().x);
173 }
174 columnAnchor += area.left + 20*Winsys.scale;
175
176 Music.Play(param.config_music, true);
177 }
178
Loop(float time_step)179 void CGameConfig::Loop(float time_step) {
180 ScopedRenderMode rm(GUI);
181 Winsys.clear();
182
183 if (param.ui_snow) {
184 update_ui_snow(time_step);
185 draw_ui_snow();
186 }
187
188 DrawGUIBackground(Winsys.scale);
189
190 FT.AutoSizeN(4);
191
192 descriptions[0]->Focussed(resolution->focussed());
193 descriptions[1]->Focussed(mus_vol->focussed());
194 descriptions[2]->Focussed(sound_vol->focussed());
195 descriptions[3]->Focussed(language->focussed());
196 descriptions[4]->Focussed(detail_level->focussed());
197
198 FT.SetColor(colWhite);
199 FT.DrawString(columnAnchor, area.top + dd + 3, res_names[resolution->GetValue()]);
200 FT.DrawString(columnAnchor, area.top + dd * 2 + 3, Int_StrN(mus_vol->GetValue()));
201 FT.DrawString(columnAnchor, area.top + dd * 3 + 3, Int_StrN(sound_vol->GetValue()));
202 FT.DrawString(columnAnchor, area.top + dd * 4 + 3, Trans.languages[language->GetValue()].language);
203 FT.DrawString(columnAnchor, area.top + dd * 5 + 3, Int_StrN(detail_level->GetValue()));
204
205 FT.SetColor(colLGrey);
206 FT.AutoSizeN(3);
207 FT.DrawString(CENTER, AutoYPosN(68), Trans.Text(41));
208 FT.DrawString(CENTER, AutoYPosN(72), Trans.Text(42));
209
210 DrawGUI();
211
212 Winsys.SwapBuffers();
213 }
214