1 
2 /**
3  *
4  * @file setup.cpp
5  *
6  * Part of the OpenJazz project
7  *
8  * @par History:
9  * - 23rd of August 2005: Created main.c and menu.c
10  * - 3rd of February 2009: Renamed main.c to main.cpp and menu.c to menu.cpp
11  * - 18th July 2009: Created menusetup.cpp from parts of menu.cpp
12  * - 26th July 2009: Renamed menusetup.cpp to setupmenu.cpp
13  * - 21st July 2013: Created setup.cpp from parts of main.cpp and setupmenu.cpp
14  *
15  * @par Licence:
16  * Copyright (c) 2005-2017 Alister Thomson
17  *
18  * OpenJazz is distributed under the terms of
19  * the GNU General Public License, version 2.0
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
24  *
25  * @par Description:
26  * Deals with the running of setup menus.
27  *
28  */
29 
30 
31 #include "io/controls.h"
32 #include "io/file.h"
33 #include "io/gfx/video.h"
34 #include "io/sound.h"
35 #include "player/player.h"
36 #include "setup.h"
37 #include "util.h"
38 
39 
40 #ifdef __SYMBIAN32__
41     #ifdef UIQ3
42         #define CONFIG_FILE "c:\\shared\\openjazz\\openjazz.cfg"
43     #else
44         #define CONFIG_FILE "c:\\data\\openjazz\\openjazz.cfg"
45     #endif
46 #else
47     #define CONFIG_FILE "openjazz.cfg"
48 #endif
49 
50 
51 /**
52  * Create default setup
53  */
Setup()54 Setup::Setup () {
55 
56 	// Create the player's name
57 	characterName = createEditableString(CHAR_NAME);
58 
59 	// Assign the player's colour
60 	characterCols[0] = CHAR_FUR;
61 	characterCols[1] = CHAR_BAND;
62 	characterCols[2] = CHAR_GUN;
63 	characterCols[3] = CHAR_WBAND;
64 
65 	return;
66 
67 }
68 
69 
70 /**
71  * Delete the setup data
72  */
~Setup()73 Setup::~Setup () {
74 
75 	delete[] characterName;
76 
77 }
78 
79 
80 /**
81  * Load settings from config file.
82  */
load(int * videoW,int * videoH,bool * fullscreen,int * videoScale)83 void Setup::load (int* videoW, int* videoH, bool* fullscreen, int* videoScale) {
84 
85 	File* file;
86 	int count;
87 
88 	// Open config file
89 
90 	try {
91 
92 		file = new File(CONFIG_FILE, false);
93 
94 	} catch (int e) {
95 
96 		log("Configuration file not found.");
97 
98 		return;
99 
100 	}
101 
102 	// Check that the config file has the correct version
103 	if (file->loadChar() != 3) {
104 
105 		log("Valid configuration file not found.");
106 
107 		return;
108 
109 	}
110 
111 
112 	// Read video settings
113 	*videoW = file->loadShort(MAX_SCREEN_WIDTH);
114 	*videoH = file->loadShort(MAX_SCREEN_HEIGHT);
115 	count = file->loadChar();
116 #ifndef FULLSCREEN_ONLY
117 	*fullscreen = count & 1;
118 #endif
119 #ifdef SCALE
120 	if (count >= 10) count = 2;
121 	*videoScale = count >> 1;
122 #endif
123 
124 
125 	// Read controls
126 	for (count = 0; count < CONTROLS - 4; count++)
127 		controls.setKey(count, (SDLKey)(file->loadInt()));
128 
129 	for (count = 0; count < CONTROLS; count++)
130 		controls.setButton(count, file->loadInt());
131 
132 	for (count = 0; count < CONTROLS; count++) {
133 
134 		int a, d;
135 
136 		a = file->loadInt();
137 		d = file->loadInt();
138 		controls.setAxis(count, a, d);
139 
140 	}
141 
142 	// Read the player's name
143 	for (count = 0; count < STRING_LENGTH; count++)
144 		setup.characterName[count] = file->loadChar();
145 
146 	setup.characterName[STRING_LENGTH] = 0;
147 
148 	// Read the player's colours
149 	setup.characterCols[0] = file->loadChar();
150 	setup.characterCols[1] = file->loadChar();
151 	setup.characterCols[2] = file->loadChar();
152 	setup.characterCols[3] = file->loadChar();
153 
154 	// Read the music and sound effect volume
155 	setMusicVolume(file->loadChar());
156 	setSoundVolume(file->loadChar());
157 
158 	// Read gameplay options
159 	count = file->loadChar();
160 	setup.slowMotion = ((count & 4) != 0);
161 	setup.manyBirds = ((count & 1) != 0);
162 	setup.leaveUnneeded = ((count & 2) != 0);
163 
164 
165 	delete file;
166 
167 
168 	return;
169 
170 }
171 
172 
173 /**
174  * Save settings to config file.
175  */
save()176 void Setup::save () {
177 
178 	File *file;
179 	int count;
180 	int videoScale;
181 
182 	// Open config file
183 	try {
184 
185 		file = new File(CONFIG_FILE, true);
186 
187 	} catch (int e) {
188 
189 		file = NULL;
190 
191 	}
192 
193 	// Check that the config file was opened
194 	if (!file) {
195 
196 		logError("Could not write configuration file",
197 			"File could not be opened.");
198 
199 		return;
200 
201 	}
202 
203 
204 	// Write the version number
205 	file->storeChar(3);
206 
207 	// Write video settings
208 	file->storeShort(video.getWidth());
209 	file->storeShort(video.getHeight());
210 #ifdef SCALE
211 	videoScale = video.getScaleFactor();
212 #else
213 	videoScale = 1;
214 #endif
215 	videoScale <<= 1;
216 #ifndef FULLSCREEN_ONLY
217 	videoScale |= video.isFullscreen()? 1: 0;
218 #endif
219 	file->storeChar(videoScale);
220 
221 
222 	// Write controls
223 	for (count = 0; count < CONTROLS - 4; count++)
224 		file->storeInt(controls.getKey(count));
225 
226 	for (count = 0; count < CONTROLS; count++)
227 		file->storeInt(controls.getButton(count));
228 
229 	for (count = 0; count < CONTROLS; count++) {
230 
231 		file->storeInt(controls.getAxis(count));
232 		file->storeInt(controls.getAxisDirection(count));
233 
234 	}
235 
236 	// Write the player's name
237 	for (count = 0; count < STRING_LENGTH; count++)
238 		file->storeChar(setup.characterName[count]);
239 
240 	// Write the player's colour
241 	file->storeChar(setup.characterCols[0]);
242 	file->storeChar(setup.characterCols[1]);
243 	file->storeChar(setup.characterCols[2]);
244 	file->storeChar(setup.characterCols[3]);
245 
246 	// Write the music and sound effect volume
247 	file->storeChar(getMusicVolume());
248 	file->storeChar(getSoundVolume());
249 
250 	// Write gameplay options
251 
252 	count = 0;
253 
254 	if (setup.slowMotion) count |= 4;
255 	if (setup.manyBirds) count |= 1;
256 	if (setup.leaveUnneeded) count |= 2;
257 
258 	file->storeChar(count);
259 
260 
261 	delete file;
262 
263 
264 	return;
265 
266 }
267 
268