summaryrefslogtreecommitdiff
path: root/noncore/games/sfcave-sdl/sfcave.cpp
authorandyq <andyq>2003-01-21 20:37:00 (UTC)
committer andyq <andyq>2003-01-21 20:37:00 (UTC)
commit0a6563fcc2f49857c581d9def24407a3a4ef526c (patch) (unidiff)
treef1b82a4bd7582ef2cb722cffb87eecff1e1f96e6 /noncore/games/sfcave-sdl/sfcave.cpp
parent50b5915b48fc5cbacf23e4d2b75d7a266f141a4a (diff)
downloadopie-0a6563fcc2f49857c581d9def24407a3a4ef526c.zip
opie-0a6563fcc2f49857c581d9def24407a3a4ef526c.tar.gz
opie-0a6563fcc2f49857c581d9def24407a3a4ef526c.tar.bz2
Clean up of code - fixed memory leaks (most of them) and added new custom config menu
Diffstat (limited to 'noncore/games/sfcave-sdl/sfcave.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/games/sfcave-sdl/sfcave.cpp527
1 files changed, 307 insertions, 220 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
@@ -26,9 +26,9 @@
26 26
27void start( int argc, char *argv[] ) 27void 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
@@ -43,35 +43,51 @@ int main(int argc, char *argv[])
43 43
44SFCave :: SFCave( int argc, char *argv[] ) 44SFCave :: 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(); 89
72 90 setupOK = true;
73 SoundHandler :: cleanUp();
74 SDL_Quit();
75} 91}
76 92
77SFCave :: ~SFCave() 93SFCave :: ~SFCave()
@@ -82,53 +98,57 @@ SFCave :: ~SFCave()
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
89void 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
95void SFCave :: initSDL( int argc, char *argv[] ) 112void 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]);
@@ -136,219 +156,174 @@ void SFCave :: initSDL( int argc, char *argv[] )
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
152void SFCave :: mainEventLoop() 173void 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 );
188 switch( state )
189 {
190 case STATE_MENU:
191 SDL_FillRect( screen, 0, 0 );
192 menu->draw( screen );
193 break;
194 case STATE_HELP:
195 SDL_FillRect( screen, 0, 0 );
196 help->update();
197 help->draw( screen );
198 break;
199 case STATE_NEWGAME:
200 printf( "STATE_NEWGAME\n" );
201 currentGame->setReplay( false );
202 currentGame->init();
203 changeState( STATE_PLAYING );
204 break;
205 195
206 case STATE_REPLAY: 196 handleGameState( );
207 printf( "STATE_NEWGAME\n" );
208 currentGame->setReplay( true );
209 currentGame->init();
210 changeState( STATE_PLAYING );
211 break;
212 197
213 case STATE_PLAYING: 198 SDL_Flip( screen );
214 case STATE_CRASHING:
215 currentGame->update( state );
216 currentGame->draw( screen );
217 break;
218
219 case STATE_CRASHED:
220 currentGame->update( state );
221 currentGame->draw( screen );
222
223 // Display Game Over message
224 break;
225
226 case STATE_QUIT:
227 done = 1;
228 break;
229 }
230
231 /* Show */
232 // if ( state != STATE_CRASHED )
233 SDL_Flip( screen );
234 // SDL_UpdateRect(screen, 0, 0, 0, 0);
235 199
236 if ( limitFPS ) 200 if ( limitFPS )
237 { 201 FPSDelay();
238 /* Slow down polling - limit to x FPS*/
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 202 else
255 SDL_Delay( 5 ); 203 SDL_Delay( 5 );
256 204
257 /* Check for events */ 205 handleEvents();
258 while ( SDL_PollEvent(&event) ) 206 }
207}
208
209
210void SFCave :: handleGameState()
211{
212 switch( state )
213 {
214 case STATE_MENU:
215 SDL_FillRect( screen, 0, 0 );
216 menu->draw( screen );
217 break;
218 case STATE_HELP:
219 SDL_FillRect( screen, 0, 0 );
220 help->update();
221 help->draw( screen );
222 break;
223 case STATE_NEWGAME:
224 currentGame->setReplay( false );
225 currentGame->init();
226 changeState( STATE_PLAYING );
227 break;
228
229 case STATE_REPLAY:
230 currentGame->setReplay( true );
231 currentGame->init();
232 changeState( STATE_PLAYING );
233 break;
234
235 case STATE_PLAYING:
236 case STATE_CRASHING:
237 case STATE_CRASHED:
238 currentGame->update( state );
239 currentGame->draw( screen );
240 break;
241
242 case STATE_QUIT:
243 finish = true;
244 break;
245 }
246}
247
248void SFCave :: handleEvents()
249{
250 SDL_Event event;
251
252 // Check for events
253 while ( SDL_PollEvent(&event) )
254 {
255 switch (event.type)
259 { 256 {
260 switch (event.type) 257 case SDL_KEYDOWN:
258 case SDL_KEYUP:
261 { 259 {
262 case SDL_KEYDOWN: 260 // Escape keypress quits the app
263 case SDL_KEYUP: 261 if ( event.key.keysym.sym == SDLK_ESCAPE )
264 // Escape keypress quits the app 262 {
265 if ( event.key.keysym.sym != SDLK_ESCAPE ) 263 finish = true;
264 break;
265 }
266
267 if ( state == STATE_MENU )
268 {
269 int rc = menu->handleKeys( event.key );
270 if ( rc != -1 )
271 handleMenuSelect( rc );
272 }
273 else if ( state == STATE_HELP )
274 {
275 help->handleKeys( event.key );
276 }
277 else if ( state == STATE_CRASHED )
278 {
279 if ( event.type == SDL_KEYDOWN )
266 { 280 {
267 // printf( "Key Pressed was %d %s\n", event.key.keysym.sym, SDL_GetKeyName( event.key.keysym.sym ) ); 281 if ( event.key.keysym.sym == SDLK_UP ||
268 282 event.key.keysym.sym == SDLK_DOWN ||
269 if ( state == STATE_MENU ) 283 event.key.keysym.sym == SDLK_SPACE )
284 changeState( STATE_NEWGAME );
285 else if ( event.key.keysym.sym == SDLK_RETURN || event.key.keysym.sym == 0 )
270 { 286 {
271 int rc = menu->handleKeys( event.key ); 287 changeState( STATE_MENU );
272 if ( rc != -1 ) 288 menu->resetToTopMenu();
273 handleMenuSelect( rc );
274 } 289 }
275 else if ( state == STATE_HELP ) 290 else if ( event.key.keysym.sym == SDLK_r )
276 { 291 {
277 help->handleKeys( event.key ); 292 changeState( STATE_REPLAY );
278 } 293 }
279 else if ( state == STATE_CRASHED ) 294 else if ( event.key.keysym.sym == SDLK_s )
280 { 295 {
281 if ( event.type == SDL_KEYDOWN ) 296 SoundHandler :: playSound( SND_EXPLOSION );
282 {
283 if ( event.key.keysym.sym == SDLK_UP ||
284 event.key.keysym.sym == SDLK_DOWN ||
285 event.key.keysym.sym == SDLK_SPACE )
286 changeState( STATE_NEWGAME );
287 else if ( event.key.keysym.sym == SDLK_RETURN || event.key.keysym.sym == 0 )
288 {
289 changeState( STATE_MENU );
290 menu->resetToTopMenu();
291 }
292 else if ( event.key.keysym.sym == SDLK_r )
293 {
294 changeState( STATE_REPLAY );
295 }
296 else if ( event.key.keysym.sym == SDLK_s )
297 {
298 SoundHandler :: playSound( SND_EXPLOSION );
299 }
300 }
301 } 297 }
302 else
303 {
304 switch ( event.key.keysym.sym )
305 {
306 case SDLK_f:
307 printf( "showFPS - %d\n", showFps );
308 if ( event.type == SDL_KEYDOWN )
309 showFps = !showFps;
310 break;
311 case SDLK_l:
312 if ( event.type == SDL_KEYDOWN )
313 limitFPS = !limitFPS;
314 break;
315
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:
337 currentGame->handleKeys( event.key );
338 break;
339 }
340 }
341
342 break;
343 } 298 }
299 }
300 else
301 {
302 switch ( event.key.keysym.sym )
303 {
304 case SDLK_f:
305 if ( event.type == SDL_KEYDOWN )
306 showFps = !showFps;
307 break;
308 case SDLK_l:
309 if ( event.type == SDL_KEYDOWN )
310 limitFPS = !limitFPS;
311 break;
344 312
313 default:
314 currentGame->handleKeys( event.key );
315 break;
316 }
317 }
345 318
346 case SDL_QUIT: 319 break;
347 done = 1;
348 break;
349 default:
350 break;
351 } 320 }
321
322 case SDL_QUIT:
323 finish = true;
324 break;
325 default:
326 break;
352 } 327 }
353 } 328 }
354} 329}
@@ -365,7 +340,6 @@ void SFCave :: changeState( int s )
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 }
@@ -374,7 +348,7 @@ void SFCave :: changeState( int s )
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 }
@@ -408,12 +382,7 @@ void SFCave :: handleMenuSelect( int menuId )
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
@@ -425,12 +394,7 @@ void SFCave :: handleMenuSelect( int menuId )
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 }
@@ -494,22 +458,71 @@ void SFCave :: handleMenuSelect( int menuId )
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" );
511 break; 484 break;
512 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 );
524 break;
525
513 default: 526 default:
514 break; 527 break;
515 } 528 }
@@ -533,6 +546,18 @@ void SFCave :: saveSetting( string key, int val )
533 cfg.writeSetting( key, val ); 546 cfg.writeSetting( key, val );
534} 547}
535 548
549void SFCave :: saveSetting( string key, long val )
550{
551 Settings cfg( "sfcave-sdl" );
552 cfg.writeSetting( key, val );
553}
554
555void SFCave :: saveSetting( string key, double val )
556{
557 Settings cfg( "sfcave-sdl" );
558 cfg.writeSetting( key, val );
559}
560
536string SFCave :: loadSetting( string key, string defaultVal ) 561string SFCave :: loadSetting( string key, string defaultVal )
537{ 562{
538 string val; 563 string val;
@@ -544,3 +569,65 @@ string SFCave :: loadSetting( string key, string defaultVal )
544 569
545 return val; 570 return val;
546} 571}
572
573bool 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
582int 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
591double 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
601void 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
616void 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}