Diffstat (limited to 'noncore/games/sfcave-sdl/sfcave.cpp') (more/less context) (show whitespace changes)
-rw-r--r-- | noncore/games/sfcave-sdl/sfcave.cpp | 353 |
1 files changed, 220 insertions, 133 deletions
diff --git a/noncore/games/sfcave-sdl/sfcave.cpp b/noncore/games/sfcave-sdl/sfcave.cpp index 8d376a1..dbd788c 100644 --- a/noncore/games/sfcave-sdl/sfcave.cpp +++ b/noncore/games/sfcave-sdl/sfcave.cpp | |||
@@ -23,15 +23,15 @@ | |||
23 | #include "sfcave_game.h" | 23 | #include "sfcave_game.h" |
24 | #include "gates_game.h" | 24 | #include "gates_game.h" |
25 | #include "fly_game.h" | 25 | #include "fly_game.h" |
26 | 26 | ||
27 | void start( int argc, char *argv[] ) | 27 | void start( int argc, char *argv[] ) |
28 | { | 28 | { |
29 | FontHandler::init(); | 29 | SFCave *app = new SFCave( argc, argv ); |
30 | SFCave app( argc, argv ); | 30 | app->mainEventLoop(); |
31 | FontHandler::cleanUp(); | 31 | delete app; |
32 | } | 32 | } |
33 | 33 | ||
34 | #ifdef __cplusplus | 34 | #ifdef __cplusplus |
35 | extern "C" | 35 | extern "C" |
36 | #endif | 36 | #endif |
37 | int main(int argc, char *argv[]) | 37 | int main(int argc, char *argv[]) |
@@ -40,234 +40,232 @@ int main(int argc, char *argv[]) | |||
40 | return 0; | 40 | return 0; |
41 | } | 41 | } |
42 | 42 | ||
43 | 43 | ||
44 | SFCave :: SFCave( int argc, char *argv[] ) | 44 | SFCave :: SFCave( int argc, char *argv[] ) |
45 | { | 45 | { |
46 | setupOK = false; | ||
47 | |||
48 | // Load settings | ||
46 | string diff = loadSetting( "GameDifficulty", "Easy" ); | 49 | string diff = loadSetting( "GameDifficulty", "Easy" ); |
47 | string game = loadSetting( "GameType", "SFCave" ); | 50 | string game = loadSetting( "GameType", "SFCave" ); |
48 | musicPath = loadSetting( "MusicPath", SOUND_PATH ); | 51 | musicPath = loadSetting( "MusicPath", SOUND_PATH ); |
49 | printf( "musicPath %s\n", musicPath.c_str() ); | ||
50 | musicType = loadSetting( "MusicType", "mod,ogg" ); | 52 | musicType = loadSetting( "MusicType", "mod,ogg" ); |
53 | bool soundOn = loadBoolSetting( "SoundOn", true ); | ||
54 | bool musicOn = loadBoolSetting( "MusicOn", true ); | ||
51 | if ( musicPath[musicPath.size()-1] != '/' ) | 55 | if ( musicPath[musicPath.size()-1] != '/' ) |
52 | musicPath += "/"; | 56 | musicPath += "/"; |
57 | printf( "musicPath %s\n", musicPath.c_str() ); | ||
53 | 58 | ||
54 | // Init main SDL Library | 59 | // Init main SDL Library |
55 | initSDL( argc, argv ); | 60 | initSDL( argc, argv ); |
56 | 61 | ||
62 | // Init font handler | ||
63 | if ( !FontHandler::init() ) | ||
64 | { | ||
65 | printf( "Unable to initialise fonts!\n" ); | ||
66 | return; | ||
67 | } | ||
68 | |||
57 | // Init SoundHandler | 69 | // Init SoundHandler |
58 | if ( !SoundHandler :: init() ) | 70 | if ( !SoundHandler :: init() ) |
59 | printf("Unable to open audio!\n"); | 71 | printf("Unable to open audio!\n"); |
60 | 72 | ||
73 | SoundHandler :: setSoundsOn( soundOn ); | ||
74 | SoundHandler :: setMusicOn( musicOn ); | ||
75 | |||
61 | currentGame = Game::createGame( this, WIDTH, HEIGHT, game, diff ); | 76 | currentGame = Game::createGame( this, WIDTH, HEIGHT, game, diff ); |
62 | if ( !currentGame ) | 77 | if ( !currentGame ) |
63 | currentGame = new SFCaveGame( this, WIDTH, HEIGHT, 0 ); | 78 | currentGame = new SFCaveGame( this, WIDTH, HEIGHT, 0 ); |
64 | currentGame->setSeed(-1); | 79 | currentGame->setSeed(-1); |
80 | |||
81 | // Create menu | ||
65 | menu = new Menu( this ); | 82 | menu = new Menu( this ); |
66 | 83 | ||
84 | // Create help screen | ||
67 | help = new Help( this ); | 85 | help = new Help( this ); |
68 | 86 | ||
69 | maxFPS = 50; | 87 | maxFPS = 50; |
70 | showFps = false; | 88 | showFps = false; |
71 | mainEventLoop(); | ||
72 | 89 | ||
73 | SoundHandler :: cleanUp(); | 90 | setupOK = true; |
74 | SDL_Quit(); | ||
75 | } | 91 | } |
76 | 92 | ||
77 | SFCave :: ~SFCave() | 93 | SFCave :: ~SFCave() |
78 | { | 94 | { |
79 | if ( currentGame ) | 95 | if ( currentGame ) |
80 | delete currentGame; | 96 | delete currentGame; |
81 | 97 | ||
82 | if ( menu ) | 98 | if ( menu ) |
83 | delete menu; | 99 | delete menu; |
84 | 100 | ||
85 | SDL_FreeSurface( screen ); | 101 | if ( help ) |
86 | } | 102 | delete help; |
87 | |||
88 | 103 | ||
89 | void SFCave :: drawGameScreen( ) | 104 | SDL_FreeSurface( screen ); |
90 | { | 105 | FontHandler::cleanUp(); |
91 | //ClearScreen(screen, "Titletext"); | 106 | SoundHandler :: cleanUp(); |
92 | 107 | ||
108 | SDL_Quit(); | ||
93 | } | 109 | } |
94 | 110 | ||
111 | |||
95 | void SFCave :: initSDL( int argc, char *argv[] ) | 112 | void SFCave :: initSDL( int argc, char *argv[] ) |
96 | { | 113 | { |
97 | const SDL_VideoInfo *info; | 114 | const SDL_VideoInfo *info; |
98 | Uint8 video_bpp; | 115 | Uint8 video_bpp; |
99 | Uint32 videoflags; | 116 | Uint32 videoflags; |
100 | 117 | ||
101 | 118 | // Initialize SDL | |
102 | |||
103 | /* Initialize SDL */ | ||
104 | if ( SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0 ) { | 119 | if ( SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0 ) { |
105 | fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError()); | 120 | fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError()); |
106 | exit(1); | 121 | exit(1); |
107 | } | 122 | } |
108 | atexit(SDL_Quit); | ||
109 | 123 | ||
110 | /* Alpha blending doesn't work well at 8-bit color */ | ||
111 | video_bpp = 16; | 124 | video_bpp = 16; |
112 | 125 | ||
113 | if ( !SDL_VideoModeOK(WIDTH, HEIGHT, 16, SDL_DOUBLEBUF) ) | 126 | if ( !SDL_VideoModeOK(WIDTH, HEIGHT, video_bpp, SDL_DOUBLEBUF) ) |
114 | printf( "No double buffering\n" ); | 127 | printf( "No double buffering\n" ); |
115 | 128 | ||
116 | videoflags = SDL_HWSURFACE | SDL_SRCALPHA;//|| SDL_DOUBLEBUF;// | SDL_SRCALPHA | SDL_RESIZABLE; | 129 | videoflags = SDL_HWSURFACE | SDL_SRCALPHA; |
117 | while ( argc > 1 ) { | 130 | while ( argc > 1 ) |
131 | { | ||
118 | --argc; | 132 | --argc; |
119 | if ( strcmp(argv[argc-1], "-bpp") == 0 ) { | 133 | if ( strcmp(argv[argc-1], "-bpp") == 0 ) |
134 | { | ||
120 | video_bpp = atoi(argv[argc]); | 135 | video_bpp = atoi(argv[argc]); |
121 | --argc; | 136 | --argc; |
122 | } else | 137 | } |
123 | if ( strcmp(argv[argc], "-hw") == 0 ) { | 138 | else if ( strcmp(argv[argc], "-hw") == 0 ) |
139 | { | ||
124 | videoflags |= SDL_HWSURFACE; | 140 | videoflags |= SDL_HWSURFACE; |
125 | } else | 141 | } |
126 | if ( strcmp(argv[argc], "-warp") == 0 ) { | 142 | else if ( strcmp(argv[argc], "-warp") == 0 ) |
143 | { | ||
127 | videoflags |= SDL_HWPALETTE; | 144 | videoflags |= SDL_HWPALETTE; |
128 | } else | 145 | } |
129 | if ( strcmp(argv[argc], "-fullscreen") == 0 ) { | 146 | else if ( strcmp(argv[argc], "-fullscreen") == 0 ) |
147 | { | ||
130 | videoflags |= SDL_FULLSCREEN; | 148 | videoflags |= SDL_FULLSCREEN; |
131 | } else { | 149 | } |
150 | else if ( strcmp(argv[argc], "-h") == 0 ) | ||
151 | { | ||
132 | fprintf(stderr, | 152 | fprintf(stderr, |
133 | "Usage: %s [-bpp N] [-warp] [-hw] [-fullscreen]\n", | 153 | "Usage: %s [-bpp N] [-warp] [-hw] [-fullscreen]\n", |
134 | argv[0]); | 154 | argv[0]); |
135 | exit(1); | 155 | exit(1); |
136 | } | 156 | } |
137 | } | 157 | } |
138 | 158 | ||
139 | /* Set 240x320 video mode */ | 159 | // Set 240x320 video mode |
140 | if ( (screen=SDL_SetVideoMode(WIDTH,HEIGHT,video_bpp,videoflags)) == NULL ) { | 160 | if ( (screen = SDL_SetVideoMode( WIDTH,HEIGHT,video_bpp,videoflags )) == NULL ) |
141 | fprintf(stderr, "Couldn't set %ix%i video mode: %s\n",WIDTH,HEIGHT,SDL_GetError()); | 161 | { |
162 | printf( "Couldn't set %ix%i video mode: %s\n",WIDTH,HEIGHT,SDL_GetError() ); | ||
142 | exit(2); | 163 | exit(2); |
143 | } | 164 | } |
144 | 165 | ||
145 | /* Use alpha blending */ | 166 | // Use alpha blending |
146 | SDL_SetAlpha(screen, SDL_RLEACCEL, 0); | 167 | //SDL_SetAlpha(screen, SDL_RLEACCEL, 0); |
147 | 168 | ||
148 | /* Set title for window */ | 169 | // Set title for window |
149 | SDL_WM_SetCaption("SFCave","SFCave"); | 170 | SDL_WM_SetCaption("SFCave","SFCave"); |
150 | } | 171 | } |
151 | 172 | ||
152 | void SFCave :: mainEventLoop() | 173 | void SFCave :: mainEventLoop() |
153 | { | 174 | { |
154 | SDL_Event event; | 175 | if ( !setupOK ) |
155 | int done; | 176 | return; |
156 | 177 | ||
157 | /* Wait for a keystroke */ | 178 | // Wait for a keystroke |
158 | done = 0; | 179 | finish = false; |
159 | state = 0; | 180 | state = 0; |
160 | state = STATE_CRASHED; | 181 | state = STATE_CRASHED; |
161 | changeState( STATE_MENU ); | 182 | changeState( STATE_MENU ); |
162 | 183 | ||
163 | int FPS = 0; | 184 | FPS = 0; |
164 | bool limitFPS = true; | ||
165 | actualFPS = 0; | 185 | actualFPS = 0; |
166 | long time1 = 0; | 186 | time1 = 0; |
167 | long start; | 187 | |
168 | long end; | 188 | limitFPS = true; |
169 | //long nrTimes = 0; | 189 | while ( !finish ) |
170 | struct timeb tp; | ||
171 | while ( !done ) | ||
172 | { | 190 | { |
173 | // calc FPS | 191 | // calc FPS |
174 | ftime( &tp ); | 192 | calcFPS(); |
175 | start =(tp.time%10000)*10000 + tp.millitm; | ||
176 | // printf( "start = %ld, time1 - %d, st-tm - %d, tp.time - %ld\n", start, time1, start-time1, (tp.time%1000)*1000 ); | ||
177 | if ( start - time1 >= 1000 ) | ||
178 | { | ||
179 | actualFPS = FPS; | ||
180 | // printf( "%d FPS = %d\n", nrTimes++, actualFPS ); | ||
181 | FPS = 0; | ||
182 | time1 = start; | ||
183 | } | ||
184 | else | ||
185 | FPS ++; | ||
186 | 193 | ||
187 | SDL_FillRect( screen, 0, 0 ); | 194 | SDL_FillRect( screen, 0, 0 ); |
195 | |||
196 | handleGameState( ); | ||
197 | |||
198 | SDL_Flip( screen ); | ||
199 | |||
200 | if ( limitFPS ) | ||
201 | FPSDelay(); | ||
202 | else | ||
203 | SDL_Delay( 5 ); | ||
204 | |||
205 | handleEvents(); | ||
206 | } | ||
207 | } | ||
208 | |||
209 | |||
210 | void SFCave :: handleGameState() | ||
211 | { | ||
188 | switch( state ) | 212 | switch( state ) |
189 | { | 213 | { |
190 | case STATE_MENU: | 214 | case STATE_MENU: |
191 | SDL_FillRect( screen, 0, 0 ); | 215 | SDL_FillRect( screen, 0, 0 ); |
192 | menu->draw( screen ); | 216 | menu->draw( screen ); |
193 | break; | 217 | break; |
194 | case STATE_HELP: | 218 | case STATE_HELP: |
195 | SDL_FillRect( screen, 0, 0 ); | 219 | SDL_FillRect( screen, 0, 0 ); |
196 | help->update(); | 220 | help->update(); |
197 | help->draw( screen ); | 221 | help->draw( screen ); |
198 | break; | 222 | break; |
199 | case STATE_NEWGAME: | 223 | case STATE_NEWGAME: |
200 | printf( "STATE_NEWGAME\n" ); | ||
201 | currentGame->setReplay( false ); | 224 | currentGame->setReplay( false ); |
202 | currentGame->init(); | 225 | currentGame->init(); |
203 | changeState( STATE_PLAYING ); | 226 | changeState( STATE_PLAYING ); |
204 | break; | 227 | break; |
205 | 228 | ||
206 | case STATE_REPLAY: | 229 | case STATE_REPLAY: |
207 | printf( "STATE_NEWGAME\n" ); | ||
208 | currentGame->setReplay( true ); | 230 | currentGame->setReplay( true ); |
209 | currentGame->init(); | 231 | currentGame->init(); |
210 | changeState( STATE_PLAYING ); | 232 | changeState( STATE_PLAYING ); |
211 | break; | 233 | break; |
212 | 234 | ||
213 | case STATE_PLAYING: | 235 | case STATE_PLAYING: |
214 | case STATE_CRASHING: | 236 | case STATE_CRASHING: |
215 | currentGame->update( state ); | ||
216 | currentGame->draw( screen ); | ||
217 | break; | ||
218 | |||
219 | case STATE_CRASHED: | 237 | case STATE_CRASHED: |
220 | currentGame->update( state ); | 238 | currentGame->update( state ); |
221 | currentGame->draw( screen ); | 239 | currentGame->draw( screen ); |
222 | |||
223 | // Display Game Over message | ||
224 | break; | 240 | break; |
225 | 241 | ||
226 | case STATE_QUIT: | 242 | case STATE_QUIT: |
227 | done = 1; | 243 | finish = true; |
228 | break; | 244 | break; |
229 | } | 245 | } |
246 | } | ||
230 | 247 | ||
231 | /* Show */ | 248 | void SFCave :: handleEvents() |
232 | // if ( state != STATE_CRASHED ) | ||
233 | SDL_Flip( screen ); | ||
234 | // SDL_UpdateRect(screen, 0, 0, 0, 0); | ||
235 | |||
236 | if ( limitFPS ) | ||
237 | { | 249 | { |
238 | /* Slow down polling - limit to x FPS*/ | 250 | SDL_Event event; |
239 | ftime( &tp ); | ||
240 | end = abs((tp.time%10000)*10000 + tp.millitm); | ||
241 | if ( end-start < (1000/maxFPS) ) | ||
242 | { | ||
243 | // printf( "end - %ld, timetaken for frame = %ld, sleeping for %ld %d\n", end, end-start, (1000/maxFPS)-(end-start), actualFPS ); | ||
244 | if ( (1000/maxFPS)-(end-start) > 500 ) | ||
245 | { | ||
246 | // Should never happen but in case it does sleep for 5 seconds | ||
247 | printf( "WARNING WILL ROBINSON! delay = %ld - start %ld, end %ld\n", (1000/maxFPS)-(end-start), start, end ); | ||
248 | SDL_Delay( 5 ); | ||
249 | } | ||
250 | else | ||
251 | SDL_Delay((1000/maxFPS)-(end-start) ); | ||
252 | } | ||
253 | } | ||
254 | else | ||
255 | SDL_Delay( 5 ); | ||
256 | 251 | ||
257 | /* Check for events */ | 252 | // Check for events |
258 | while ( SDL_PollEvent(&event) ) | 253 | while ( SDL_PollEvent(&event) ) |
259 | { | 254 | { |
260 | switch (event.type) | 255 | switch (event.type) |
261 | { | 256 | { |
262 | case SDL_KEYDOWN: | 257 | case SDL_KEYDOWN: |
263 | case SDL_KEYUP: | 258 | case SDL_KEYUP: |
259 | { | ||
264 | // Escape keypress quits the app | 260 | // Escape keypress quits the app |
265 | if ( event.key.keysym.sym != SDLK_ESCAPE ) | 261 | if ( event.key.keysym.sym == SDLK_ESCAPE ) |
266 | { | 262 | { |
267 | // printf( "Key Pressed was %d %s\n", event.key.keysym.sym, SDL_GetKeyName( event.key.keysym.sym ) ); | 263 | finish = true; |
264 | break; | ||
265 | } | ||
268 | 266 | ||
269 | if ( state == STATE_MENU ) | 267 | if ( state == STATE_MENU ) |
270 | { | 268 | { |
271 | int rc = menu->handleKeys( event.key ); | 269 | int rc = menu->handleKeys( event.key ); |
272 | if ( rc != -1 ) | 270 | if ( rc != -1 ) |
273 | handleMenuSelect( rc ); | 271 | handleMenuSelect( rc ); |
@@ -301,60 +299,37 @@ void SFCave :: mainEventLoop() | |||
301 | } | 299 | } |
302 | else | 300 | else |
303 | { | 301 | { |
304 | switch ( event.key.keysym.sym ) | 302 | switch ( event.key.keysym.sym ) |
305 | { | 303 | { |
306 | case SDLK_f: | 304 | case SDLK_f: |
307 | printf( "showFPS - %d\n", showFps ); | ||
308 | if ( event.type == SDL_KEYDOWN ) | 305 | if ( event.type == SDL_KEYDOWN ) |
309 | showFps = !showFps; | 306 | showFps = !showFps; |
310 | break; | 307 | break; |
311 | case SDLK_l: | 308 | case SDLK_l: |
312 | if ( event.type == SDL_KEYDOWN ) | 309 | if ( event.type == SDL_KEYDOWN ) |
313 | limitFPS = !limitFPS; | 310 | limitFPS = !limitFPS; |
314 | break; | 311 | break; |
315 | 312 | ||
316 | case SDLK_p: | ||
317 | if ( event.type == SDL_KEYDOWN ) | ||
318 | { | ||
319 | maxFPS ++; | ||
320 | printf( "maxFPS - %d\n", maxFPS ); | ||
321 | } | ||
322 | break; | ||
323 | |||
324 | case SDLK_o: | ||
325 | if ( event.type == SDL_KEYDOWN ) | ||
326 | { | ||
327 | maxFPS --; | ||
328 | printf( "maxFPS - %d\n", maxFPS ); | ||
329 | } | ||
330 | break; | ||
331 | |||
332 | case SDLK_n: | ||
333 | currentGame->getTerrain()->offset++; | ||
334 | break; | ||
335 | |||
336 | default: | 313 | default: |
337 | currentGame->handleKeys( event.key ); | 314 | currentGame->handleKeys( event.key ); |
338 | break; | 315 | break; |
339 | } | 316 | } |
340 | } | 317 | } |
341 | 318 | ||
342 | break; | 319 | break; |
343 | } | 320 | } |
344 | 321 | ||
345 | |||
346 | case SDL_QUIT: | 322 | case SDL_QUIT: |
347 | done = 1; | 323 | finish = true; |
348 | break; | 324 | break; |
349 | default: | 325 | default: |
350 | break; | 326 | break; |
351 | } | 327 | } |
352 | } | 328 | } |
353 | } | 329 | } |
354 | } | ||
355 | 330 | ||
356 | void SFCave :: changeState( int s ) | 331 | void SFCave :: changeState( int s ) |
357 | { | 332 | { |
358 | if ( state != s ) | 333 | if ( state != s ) |
359 | currentGame->stateChanged( state, s ); | 334 | currentGame->stateChanged( state, s ); |
360 | 335 | ||
@@ -362,22 +337,21 @@ void SFCave :: changeState( int s ) | |||
362 | help->init(); | 337 | help->init(); |
363 | if ( state == STATE_CRASHED && s == STATE_MENU ) | 338 | if ( state == STATE_CRASHED && s == STATE_MENU ) |
364 | { | 339 | { |
365 | SoundHandler :: stopMusic( true ); | 340 | SoundHandler :: stopMusic( true ); |
366 | 341 | ||
367 | string musicFile = chooseRandomFile( musicPath, musicType ); | 342 | string musicFile = chooseRandomFile( musicPath, musicType ); |
368 | printf("playing music %s\n", musicFile.c_str() ); | ||
369 | SoundHandler :: setMusicVolume( 128 ); | 343 | SoundHandler :: setMusicVolume( 128 ); |
370 | SoundHandler :: playMusic( musicFile ); | 344 | SoundHandler :: playMusic( musicFile ); |
371 | } | 345 | } |
372 | else if ( state == STATE_MENU && (s == STATE_NEWGAME || s == STATE_REPLAY) ) | 346 | else if ( state == STATE_MENU && (s == STATE_NEWGAME || s == STATE_REPLAY) ) |
373 | { | 347 | { |
374 | SoundHandler :: stopMusic( ); | 348 | SoundHandler :: stopMusic( ); |
375 | 349 | ||
376 | // Start the in game music | 350 | // Start the in game music |
377 | string musicFile = SOUND_PATH "ingame.mod"; | 351 | string musicFile = INGAME_MUSIC; |
378 | SoundHandler :: playMusic( musicFile ); | 352 | SoundHandler :: playMusic( musicFile ); |
379 | SoundHandler :: setMusicVolume( 25 ); | 353 | SoundHandler :: setMusicVolume( 25 ); |
380 | } | 354 | } |
381 | 355 | ||
382 | state = s; | 356 | state = s; |
383 | } | 357 | } |
@@ -405,35 +379,25 @@ void SFCave :: handleMenuSelect( int menuId ) | |||
405 | else | 379 | else |
406 | setMenuStatusText( "No replay available yet" ); | 380 | setMenuStatusText( "No replay available yet" ); |
407 | break; | 381 | break; |
408 | 382 | ||
409 | case MENU_LOAD_REPLAY: | 383 | case MENU_LOAD_REPLAY: |
410 | { | 384 | { |
411 | #ifdef QWS | 385 | string replayFile = getHomeDir() + "/" + currentGame->getGameName() + ".replay"; |
412 | QString replayFile = getenv( "HOME" ); | ||
413 | #else | ||
414 | QString replayFile = "."; | ||
415 | #endif | ||
416 | replayFile += string( "/" ) + currentGame->getGameName() + ".replay"; | ||
417 | 386 | ||
418 | currentGame->loadReplay( replayFile ); | 387 | currentGame->loadReplay( replayFile ); |
419 | 388 | ||
420 | break; | 389 | break; |
421 | } | 390 | } |
422 | 391 | ||
423 | case MENU_SAVE_REPLAY: | 392 | case MENU_SAVE_REPLAY: |
424 | { | 393 | { |
425 | 394 | ||
426 | if ( currentGame->isReplayAvailable() ) | 395 | if ( currentGame->isReplayAvailable() ) |
427 | { | 396 | { |
428 | #ifdef QWS | 397 | string replayFile = getHomeDir() + "/" + currentGame->getGameName() + ".replay"; |
429 | QString replayFile = getenv( "HOME" ); | ||
430 | #else | ||
431 | QString replayFile = "."; | ||
432 | #endif | ||
433 | replayFile += string( "/" ) + currentGame->getGameName() + ".replay"; | ||
434 | 398 | ||
435 | currentGame->saveReplay( replayFile ); | 399 | currentGame->saveReplay( replayFile ); |
436 | } | 400 | } |
437 | else | 401 | else |
438 | setMenuStatusText( "No replay available yet" ); | 402 | setMenuStatusText( "No replay available yet" ); |
439 | 403 | ||
@@ -491,26 +455,75 @@ void SFCave :: handleMenuSelect( int menuId ) | |||
491 | 455 | ||
492 | case MENU_DIFFICULTY_HARD: | 456 | case MENU_DIFFICULTY_HARD: |
493 | currentGame->setDifficulty( MENU_DIFFICULTY_HARD ); | 457 | currentGame->setDifficulty( MENU_DIFFICULTY_HARD ); |
494 | saveSetting( "GameDifficulty", "Hard" ); | 458 | saveSetting( "GameDifficulty", "Hard" ); |
495 | break; | 459 | break; |
496 | 460 | ||
461 | case MENU_DIFFICULTY_CUSTOM: | ||
462 | currentGame->setDifficulty( MENU_DIFFICULTY_CUSTOM ); | ||
463 | saveSetting( "GameDifficulty", "Custom" ); | ||
464 | break; | ||
465 | |||
497 | case MENU_SOUND_ON: | 466 | case MENU_SOUND_ON: |
498 | SoundHandler :: setSoundsOn( true ); | 467 | SoundHandler :: setSoundsOn( true ); |
468 | saveSetting( "SoundOn", "true" ); | ||
499 | break; | 469 | break; |
500 | 470 | ||
501 | case MENU_SOUND_OFF: | 471 | case MENU_SOUND_OFF: |
502 | SoundHandler :: setSoundsOn( false ); | 472 | SoundHandler :: setSoundsOn( false ); |
473 | saveSetting( "SoundOn", "false" ); | ||
503 | break; | 474 | break; |
504 | 475 | ||
505 | case MENU_MUSIC_ON: | 476 | case MENU_MUSIC_ON: |
506 | SoundHandler :: setMusicOn( true ); | 477 | SoundHandler :: setMusicOn( true ); |
478 | saveSetting( "MusicOn", "true" ); | ||
507 | break; | 479 | break; |
508 | 480 | ||
509 | case MENU_MUSIC_OFF: | 481 | case MENU_MUSIC_OFF: |
510 | SoundHandler :: setMusicOn( false ); | 482 | SoundHandler :: setMusicOn( false ); |
483 | saveSetting( "MusicOn", "false" ); | ||
484 | break; | ||
485 | |||
486 | case MENU_CUSTOM_THRUST: | ||
487 | customPlayerMenuVal = PLAYER_THRUST; | ||
488 | origValue = currentGame->getPlayer()->getValue( customPlayerMenuVal ); | ||
489 | setMenuStatusText( currentGame->getPlayer()->getValueString( customPlayerMenuVal ) ); | ||
490 | break; | ||
491 | case MENU_CUSTOM_GRAVITY: | ||
492 | customPlayerMenuVal = PLAYER_GRAVITY; | ||
493 | origValue = currentGame->getPlayer()->getValue( customPlayerMenuVal ); | ||
494 | setMenuStatusText( currentGame->getPlayer()->getValueString( customPlayerMenuVal ) ); | ||
495 | break; | ||
496 | case MENU_CUSTOM_MAXSPEEDUP: | ||
497 | customPlayerMenuVal = PLAYER_MAX_SPEED_UP; | ||
498 | origValue = currentGame->getPlayer()->getValue( customPlayerMenuVal ); | ||
499 | setMenuStatusText( currentGame->getPlayer()->getValueString( customPlayerMenuVal ) ); | ||
500 | break; | ||
501 | case MENU_CUSTOM_MAXSPEEDDOWN: | ||
502 | customPlayerMenuVal = PLAYER_MAX_SPEED_DOWN; | ||
503 | origValue = currentGame->getPlayer()->getValue( customPlayerMenuVal ); | ||
504 | setMenuStatusText( currentGame->getPlayer()->getValueString( customPlayerMenuVal ) ); | ||
505 | break; | ||
506 | case MENU_CUSTOM_INCREASE: | ||
507 | currentGame->getPlayer()->incValue( customPlayerMenuVal ); | ||
508 | setMenuStatusText( currentGame->getPlayer()->getValueString( customPlayerMenuVal ) ); | ||
509 | break; | ||
510 | case MENU_CUSTOM_DECREASE: | ||
511 | currentGame->getPlayer()->decValue( customPlayerMenuVal ); | ||
512 | setMenuStatusText( currentGame->getPlayer()->getValueString( customPlayerMenuVal ) ); | ||
513 | break; | ||
514 | case MENU_CUSTOM_SAVE: | ||
515 | { | ||
516 | // save settings | ||
517 | string key = currentGame->getGameName() + "_custom_player_" + currentGame->getPlayer()->getValueTypeString( customPlayerMenuVal ); | ||
518 | saveSetting( key, currentGame->getPlayer()->getValue( customPlayerMenuVal ) ); | ||
519 | |||
520 | break; | ||
521 | } | ||
522 | case MENU_CUSTOM_CANCEL: | ||
523 | currentGame->getPlayer()->setValue( customPlayerMenuVal, origValue ); | ||
511 | break; | 524 | break; |
512 | 525 | ||
513 | default: | 526 | default: |
514 | break; | 527 | break; |
515 | } | 528 | } |
516 | } | 529 | } |
@@ -530,17 +543,91 @@ void SFCave :: saveSetting( string key, string val ) | |||
530 | void SFCave :: saveSetting( string key, int val ) | 543 | void SFCave :: saveSetting( string key, int val ) |
531 | { | 544 | { |
532 | Settings cfg( "sfcave-sdl" ); | 545 | Settings cfg( "sfcave-sdl" ); |
533 | cfg.writeSetting( key, val ); | 546 | cfg.writeSetting( key, val ); |
534 | } | 547 | } |
535 | 548 | ||
549 | void SFCave :: saveSetting( string key, long val ) | ||
550 | { | ||
551 | Settings cfg( "sfcave-sdl" ); | ||
552 | cfg.writeSetting( key, val ); | ||
553 | } | ||
554 | |||
555 | void SFCave :: saveSetting( string key, double val ) | ||
556 | { | ||
557 | Settings cfg( "sfcave-sdl" ); | ||
558 | cfg.writeSetting( key, val ); | ||
559 | } | ||
560 | |||
536 | string SFCave :: loadSetting( string key, string defaultVal ) | 561 | string SFCave :: loadSetting( string key, string defaultVal ) |
537 | { | 562 | { |
538 | string val; | 563 | string val; |
539 | Settings cfg( "sfcave-sdl" ); | 564 | Settings cfg( "sfcave-sdl" ); |
540 | cfg.readSetting( key, val ); | 565 | cfg.readSetting( key, val ); |
541 | 566 | ||
542 | if ( val == "" ) | 567 | if ( val == "" ) |
543 | val = defaultVal; | 568 | val = defaultVal; |
544 | 569 | ||
545 | return val; | 570 | return val; |
546 | } | 571 | } |
572 | |||
573 | bool SFCave :: loadBoolSetting( string key, bool defaultVal ) | ||
574 | { | ||
575 | bool val = defaultVal; | ||
576 | Settings cfg( "sfcave-sdl" ); | ||
577 | cfg.readSetting( key, val ); | ||
578 | |||
579 | return val; | ||
580 | } | ||
581 | |||
582 | int SFCave :: loadIntSetting( string key, int defaultVal ) | ||
583 | { | ||
584 | int val = defaultVal; | ||
585 | Settings cfg( "sfcave-sdl" ); | ||
586 | cfg.readSetting( key, val ); | ||
587 | |||
588 | return val; | ||
589 | } | ||
590 | |||
591 | double SFCave :: loadDoubleSetting( string key, double defaultVal ) | ||
592 | { | ||
593 | double val = defaultVal; | ||
594 | Settings cfg( "sfcave-sdl" ); | ||
595 | cfg.readSetting( key, val ); | ||
596 | |||
597 | return val; | ||
598 | } | ||
599 | |||
600 | |||
601 | void SFCave :: calcFPS() | ||
602 | { | ||
603 | struct timeb tp; | ||
604 | ftime( &tp ); | ||
605 | start =(tp.time%10000)*10000 + tp.millitm; | ||
606 | if ( start - time1 >= 1000 ) | ||
607 | { | ||
608 | actualFPS = FPS; | ||
609 | FPS = 0; | ||
610 | time1 = start; | ||
611 | } | ||
612 | else | ||
613 | FPS ++; | ||
614 | } | ||
615 | |||
616 | void SFCave :: FPSDelay() | ||
617 | { | ||
618 | struct timeb tp; | ||
619 | // Slow down polling - limit to x FPS | ||
620 | ftime( &tp ); | ||
621 | end = abs((tp.time%10000)*10000 + tp.millitm); | ||
622 | if ( end-start < (1000/maxFPS) ) | ||
623 | { | ||
624 | if ( (1000/maxFPS)-(end-start) > 500 ) | ||
625 | { | ||
626 | // Should never happen but in case it does sleep for 5 seconds | ||
627 | printf( "WARNING WILL ROBINSON! delay = %ld - start %ld, end %ld\n", (1000/maxFPS)-(end-start), start, end ); | ||
628 | SDL_Delay( 5 ); | ||
629 | } | ||
630 | else | ||
631 | SDL_Delay((1000/maxFPS)-(end-start) ); | ||
632 | } | ||
633 | } | ||