summaryrefslogtreecommitdiff
path: root/noncore/games/sfcave-sdl/sfcave.cpp
blob: 5d1cdd51fc38196f83186e690d6eb98fa03ffb21 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
#include <stdio.h>
#include <stdlib.h>

#include <time.h>
#include <sys/timeb.h>

#include <SDL/SDL.h>
#include <SDL/SDL_gfxPrimitives.h>

#include "constants.h"

#include "sound.h"
#include "menu.h"
#include "help.h"
#include "game.h"
#include "terrain.h"
#include "random.h"
#include "sfcave.h"
#include "font.h"
#include "settings.h"
#include "util.h"

#include "sfcave_game.h"
#include "gates_game.h"
#include "fly_game.h"

void start( int argc, char *argv[] )
{
	SFCave *app = new SFCave( argc, argv );
	app->mainEventLoop();
	delete app;
}

#ifdef __cplusplus
extern "C"
#endif
int main(int argc, char *argv[])
{
	start( argc, argv );
	return 0;
}


SFCave :: SFCave( int argc, char *argv[] )
{
    setupOK = false;

	// Load settings
	string diff = loadSetting( "GameDifficulty", "Easy" );
	string game = loadSetting( "GameType", "SFCave" );
	musicPath = loadSetting( "MusicPath", SOUND_PATH );
	musicType = loadSetting( "MusicType", "mod,ogg" );
	bool soundOn = loadBoolSetting( "SoundOn", true );
	bool musicOn = loadBoolSetting( "MusicOn", true );
	if ( musicPath[musicPath.size()-1] != '/' )
	   musicPath += "/";
	printf( "musicPath %s\n", musicPath.c_str() );

    // Init main SDL Library
	initSDL( argc, argv );

	// Init font handler
	if ( !FontHandler::init() )
	{
	   printf( "Unable to initialise fonts!\n" );
	   return;
	}

	// Init SoundHandler
	if ( !SoundHandler :: init() )
		printf("Unable to open audio!\n");

	SoundHandler :: setSoundsOn( soundOn );
	SoundHandler :: setMusicOn( musicOn );

	currentGame = Game::createGame( this, WIDTH, HEIGHT, game, diff );
	if ( !currentGame )
		currentGame = new SFCaveGame( this, WIDTH, HEIGHT, 0 );
	currentGame->setSeed(-1);

	// Create menu
	menu = new Menu( this );

	// Create help screen
	help = new Help( this );

	maxFPS = 50;
	showFps = false;
	
	setupOK = true;
}

SFCave :: ~SFCave()
{
	if ( currentGame )
		delete currentGame;

	if ( menu )
		delete menu;

	if ( help )
		delete help;

	SDL_FreeSurface( screen );
	FontHandler::cleanUp();
	SoundHandler :: cleanUp();

	SDL_Quit();
}


void SFCave :: initSDL( int argc, char *argv[] )
{
	const SDL_VideoInfo *info;
	Uint8  video_bpp;
	Uint32 videoflags;

	// Initialize SDL
	if ( SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0 ) {
		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
		exit(1);
	}

	video_bpp = 16;

	if ( !SDL_VideoModeOK(WIDTH, HEIGHT, video_bpp, SDL_DOUBLEBUF) )
		printf( "No double buffering\n" );

	videoflags = SDL_HWSURFACE | SDL_SRCALPHA;
	while ( argc > 1 )
	{
		--argc;
		if ( strcmp(argv[argc-1], "-bpp") == 0 )
		{
			video_bpp = atoi(argv[argc]);
			--argc;
		}
		else if ( strcmp(argv[argc], "-hw") == 0 )
		{
			videoflags |= SDL_HWSURFACE;
		}
		else if ( strcmp(argv[argc], "-warp") == 0 )
		{
			videoflags |= SDL_HWPALETTE;
		}
		else if ( strcmp(argv[argc], "-fullscreen") == 0 )
		{
			videoflags |= SDL_FULLSCREEN;
		}
		else if ( strcmp(argv[argc], "-h") == 0 )
		{
			fprintf(stderr,
			"Usage: %s [-bpp N] [-warp] [-hw] [-fullscreen]\n",
								argv[0]);
			exit(1);
		}
	}

	// Set 240x320 video mode
	if ( (screen = SDL_SetVideoMode( WIDTH,HEIGHT,video_bpp,videoflags )) == NULL )
	{
		printf( "Couldn't set %ix%i video mode: %s\n",WIDTH,HEIGHT,SDL_GetError() );
		exit(2);
	}

	// Use alpha blending
	//SDL_SetAlpha(screen, SDL_RLEACCEL, 0);

	// Set title for window
	SDL_WM_SetCaption("SFCave","SFCave");
}

void SFCave :: mainEventLoop()
{
    if ( !setupOK )
        return;
        
	// Wait for a keystroke
	finish = false;
	state = 0;
	state = STATE_CRASHED;
	changeState( STATE_MENU );

	FPS = 0;
	actualFPS = 0;
	time1 = 0;

	limitFPS = true;
	while ( !finish )
	{
		// calc FPS
		calcFPS();

    	SDL_FillRect( screen, 0, 0 );

		handleGameState( );

   		SDL_Flip( screen );

		if ( limitFPS )
			FPSDelay();
		else
			SDL_Delay( 5 );

		handleEvents();
	}
}


void SFCave :: handleGameState()
{
	switch( state )
	{
		case STATE_MENU:
			SDL_FillRect( screen, 0, 0 );
			menu->draw( screen );
			break;
		case STATE_HELP:
			SDL_FillRect( screen, 0, 0 );
			help->update();
			help->draw( screen );
			break;
		case STATE_NEWGAME:
			currentGame->setReplay( false );
			currentGame->init();
			changeState( STATE_PLAYING );
			break;

		case STATE_REPLAY:
			currentGame->setReplay( true );
			currentGame->init();
			changeState( STATE_PLAYING );
			break;

		case STATE_PLAYING:
		case STATE_CRASHING:
		case STATE_CRASHED:
			currentGame->update( state );
			currentGame->draw( screen );
			break;

		case STATE_QUIT:
			finish = true;
			break;
	}
}

void SFCave :: handleEvents()
{
	SDL_Event event;

	// Check for events
	while ( SDL_PollEvent(&event) )
	{
		switch (event.type)
		{
			case SDL_KEYDOWN:
			case SDL_KEYUP:
			{
				// Escape keypress quits the app
				if ( event.key.keysym.sym == SDLK_ESCAPE )
				{
					finish = true;
					break;
				}

				if ( state == STATE_MENU )
				{
					int rc = menu->handleKeys( event.key );
					if ( rc != -1 )
						handleMenuSelect( rc );
				}
				else if ( state == STATE_HELP )
				{
					help->handleKeys( event.key );
				}
				else if ( state == STATE_CRASHED )
				{
					if ( event.type == SDL_KEYDOWN )
					{
						if ( event.key.keysym.sym == SDLK_UP ||
							event.key.keysym.sym == SDLK_DOWN ||
							event.key.keysym.sym == SDLK_SPACE )
							changeState( STATE_NEWGAME );
						else if ( event.key.keysym.sym == SDLK_RETURN || event.key.keysym.sym == 0 )
						{
							changeState( STATE_MENU );
							menu->resetToTopMenu();
						}
						else if ( event.key.keysym.sym == SDLK_r )
						{
							changeState( STATE_REPLAY );
						}
						else if ( event.key.keysym.sym == SDLK_s )
						{
							SoundHandler :: playSound( SND_EXPLOSION );
						}
					}
				}
				else
				{
					switch ( event.key.keysym.sym )
					{
						case SDLK_f:
							if ( event.type == SDL_KEYDOWN )
								showFps = !showFps;
							break;
						case SDLK_l:
							if ( event.type == SDL_KEYDOWN )
								limitFPS = !limitFPS;
							break;

						default:
							currentGame->handleKeys( event.key );
							break;
					}
				}

				break;
			}

			case SDL_QUIT:
				finish = true;
				break;
			default:
				break;
		}
	}
}

void SFCave :: changeState( int s )
{
	if ( state != s )
		currentGame->stateChanged( state, s );

    if ( state == STATE_MENU && s == STATE_HELP )
        help->init();
    if ( state == STATE_CRASHED && s == STATE_MENU )
	{
		SoundHandler :: stopMusic( true );

		string musicFile = chooseRandomFile( musicPath, musicType );
    	SoundHandler :: setMusicVolume( 128 );
		SoundHandler :: playMusic( musicFile );
	}
    else if ( state == STATE_MENU && (s == STATE_NEWGAME || s == STATE_REPLAY) )
    {
		SoundHandler :: stopMusic( );

        // Start the in game music
        string musicFile = INGAME_MUSIC;
    	SoundHandler :: playMusic( musicFile );
    	SoundHandler :: setMusicVolume( 25 );
    }

	state = s;
}


void SFCave :: handleMenuSelect( int menuId )
{
	switch( menuId )
	{
		case MENU_STARTGAME:
			changeState( STATE_NEWGAME );
			break;

		case MENU_HELP:
            changeState( STATE_HELP );
			break;

		case MENU_QUIT:
			changeState( STATE_QUIT );
			break;

		case MENU_PLAY_REPLAY:
			if ( currentGame->isReplayAvailable() )
				changeState( STATE_REPLAY );
			else
				setMenuStatusText( "No replay available yet" );
			break;

		case MENU_LOAD_REPLAY:
		{
    		string replayFile = getHomeDir() + "/" + currentGame->getGameName() + ".replay";

			currentGame->loadReplay( replayFile );

			break;
		}

		case MENU_SAVE_REPLAY:
		{

			if ( currentGame->isReplayAvailable() )
			{
				string replayFile = getHomeDir() + "/" + currentGame->getGameName() + ".replay";

				currentGame->saveReplay( replayFile );
			}
			else
				setMenuStatusText( "No replay available yet" );

			break;
		}
		case MENU_CLEAR_SCORES:
			break;

		case MENU_GAME_SFCAVE:
			if ( currentGame->getGameName() != "SFCave" )
			{
				int diff = currentGame->getDifficulty();
				delete currentGame;
				currentGame = new SFCaveGame( this, WIDTH, HEIGHT, 0 );
				currentGame->setDifficulty( diff );

				saveSetting( "GameType", "SFCave" );
			}
			break;

		case MENU_GAME_GATES:
			if ( currentGame->getGameName() != "Gates" )
			{
				int diff = currentGame->getDifficulty();
				delete currentGame;
				currentGame = new GatesGame( this, WIDTH, HEIGHT, 0 );
				currentGame->setDifficulty( diff );

				saveSetting( "GameType", "Gates" );
			}
			break;
			break;

		case MENU_GAME_FLY:
			if ( currentGame->getGameName() != "Fly" )
			{
				int diff = currentGame->getDifficulty();
				delete currentGame;
				currentGame = new FlyGame( this, WIDTH, HEIGHT, 0 );
				currentGame->setDifficulty( diff );

				saveSetting( "GameType", "Fly" );
			}
			break;

		case MENU_DIFFICULTY_EASY:
			currentGame->setDifficulty( MENU_DIFFICULTY_EASY );
			saveSetting( "GameDifficulty", "Easy" );
			break;

		case MENU_DIFFICULTY_NORMAL:
			currentGame->setDifficulty( MENU_DIFFICULTY_NORMAL );
			saveSetting( "GameDifficulty", "Medium" );
			break;

		case MENU_DIFFICULTY_HARD:
			currentGame->setDifficulty( MENU_DIFFICULTY_HARD );
			saveSetting( "GameDifficulty", "Hard" );
			break;

		case MENU_DIFFICULTY_CUSTOM:
			currentGame->setDifficulty( MENU_DIFFICULTY_CUSTOM );
			saveSetting( "GameDifficulty", "Custom" );
			break;

		case MENU_SOUND_ON:
			SoundHandler :: setSoundsOn( true );
			saveSetting( "SoundOn", "true" );
			break;

		case MENU_SOUND_OFF:
			SoundHandler :: setSoundsOn( false );
			saveSetting( "SoundOn", "false" );
			break;

		case MENU_MUSIC_ON:
			SoundHandler :: setMusicOn( true );
			saveSetting( "MusicOn", "true" );
			break;

		case MENU_MUSIC_OFF:
			SoundHandler :: setMusicOn( false );
			saveSetting( "MusicOn", "false" );
			break;

        case MENU_CUSTOM_THRUST:
            customPlayerMenuVal = PLAYER_THRUST;
            origValue = currentGame->getPlayer()->getValue( customPlayerMenuVal );
			setMenuStatusText( currentGame->getPlayer()->getValueString( customPlayerMenuVal ) );
            break;
        case MENU_CUSTOM_GRAVITY:
            customPlayerMenuVal = PLAYER_GRAVITY;
            origValue = currentGame->getPlayer()->getValue( customPlayerMenuVal );
			setMenuStatusText( currentGame->getPlayer()->getValueString( customPlayerMenuVal ) );
            break;
        case MENU_CUSTOM_MAXSPEEDUP:
            customPlayerMenuVal = PLAYER_MAX_SPEED_UP;
            origValue = currentGame->getPlayer()->getValue( customPlayerMenuVal );
			setMenuStatusText( currentGame->getPlayer()->getValueString( customPlayerMenuVal ) );
            break;
        case MENU_CUSTOM_MAXSPEEDDOWN:
            customPlayerMenuVal = PLAYER_MAX_SPEED_DOWN;
            origValue = currentGame->getPlayer()->getValue( customPlayerMenuVal );
			setMenuStatusText( currentGame->getPlayer()->getValueString( customPlayerMenuVal ) );
            break;
        case MENU_CUSTOM_INCREASE:
            currentGame->getPlayer()->incValue( customPlayerMenuVal );
			setMenuStatusText( currentGame->getPlayer()->getValueString( customPlayerMenuVal ) );
            break;
        case MENU_CUSTOM_DECREASE:
            currentGame->getPlayer()->decValue( customPlayerMenuVal );
			setMenuStatusText( currentGame->getPlayer()->getValueString( customPlayerMenuVal ) );
            break;
        case MENU_CUSTOM_SAVE:
        {
            // save settings
            string key = currentGame->getGameName() + "_custom_player_" + currentGame->getPlayer()->getValueTypeString( customPlayerMenuVal );
   			saveSetting( key, currentGame->getPlayer()->getValue( customPlayerMenuVal ) );

            break;
        }
        case MENU_CUSTOM_CANCEL:
            currentGame->getPlayer()->setValue( customPlayerMenuVal, origValue );
            break;

		default:
			break;
	}
}

void SFCave :: setMenuStatusText( string statusText )
{
	menu->setStatusText( statusText );
}


void SFCave :: saveSetting( string key, string val )
{
	Settings cfg( "sfcave-sdl" );
	cfg.writeSetting( key, val );
}

void SFCave :: saveSetting( string key, int val )
{
	Settings cfg( "sfcave-sdl" );
	cfg.writeSetting( key, val );
}

void SFCave :: saveSetting( string key, long val )
{
	Settings cfg( "sfcave-sdl" );
	cfg.writeSetting( key, val );
}

void SFCave :: saveSetting( string key, double val )
{
	Settings cfg( "sfcave-sdl" );
	cfg.writeSetting( key, val );
}

string SFCave :: loadSetting( string key, string defaultVal )
{
	string val;
	Settings cfg( "sfcave-sdl" );
	cfg.readSetting( key, val );

	if ( val == "" )
		val = defaultVal;

	return val;
}

bool SFCave :: loadBoolSetting( string key, bool defaultVal )
{
	bool val = defaultVal;
	Settings cfg( "sfcave-sdl" );
	cfg.readSetting( key, val );

	return val;
}

int SFCave :: loadIntSetting( string key, int defaultVal )
{
	int val = defaultVal;
	Settings cfg( "sfcave-sdl" );
	cfg.readSetting( key, val );

	return val;
}

double SFCave :: loadDoubleSetting( string key, double defaultVal )
{
	double val = defaultVal;
	Settings cfg( "sfcave-sdl" );
	cfg.readSetting( key, val );

	return val;
}


void SFCave :: calcFPS()
{
	struct timeb tp;
	ftime( &tp );
	start =(tp.time%10000)*10000 + tp.millitm;
	if ( start - time1 >= 1000 )
	{
		actualFPS = FPS;
		FPS = 0;
		time1 = start;
	}
	else
		FPS ++;
}

void SFCave :: FPSDelay()
{
	struct timeb tp;
	// Slow down polling - limit to x FPS
	ftime( &tp );
	end = abs((tp.time%10000)*10000 + tp.millitm);
	if ( end-start < (1000/maxFPS) )
	{
		if ( (1000/maxFPS)-(end-start) > 500 )
		{
			// Should never happen but in case it does sleep for 5 seconds
			printf( "WARNING WILL ROBINSON! delay = %ld - start %ld, end %ld\n", (1000/maxFPS)-(end-start), start, end );
			SDL_Delay( 5 );
		}
		else
			SDL_Delay((1000/maxFPS)-(end-start) );
	}
}