summaryrefslogtreecommitdiff
path: root/noncore/games
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
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') (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/games/sfcave-sdl/animatedimage.cpp8
-rw-r--r--noncore/games/sfcave-sdl/animatedimage.h2
-rw-r--r--noncore/games/sfcave-sdl/bfont.cpp7
-rw-r--r--noncore/games/sfcave-sdl/constants.h29
-rw-r--r--noncore/games/sfcave-sdl/fly_game.cpp13
-rw-r--r--noncore/games/sfcave-sdl/font.cpp24
-rw-r--r--noncore/games/sfcave-sdl/font.h2
-rw-r--r--noncore/games/sfcave-sdl/game.cpp50
-rw-r--r--noncore/games/sfcave-sdl/game.h17
-rw-r--r--noncore/games/sfcave-sdl/gates_game.cpp15
-rw-r--r--noncore/games/sfcave-sdl/help.cpp9
-rw-r--r--noncore/games/sfcave-sdl/menu.cpp43
-rw-r--r--noncore/games/sfcave-sdl/menu.h10
-rw-r--r--noncore/games/sfcave-sdl/player.cpp152
-rw-r--r--noncore/games/sfcave-sdl/player.h14
-rw-r--r--noncore/games/sfcave-sdl/settings.cpp98
-rw-r--r--noncore/games/sfcave-sdl/settings.h4
-rw-r--r--noncore/games/sfcave-sdl/sfcave.cpp527
-rw-r--r--noncore/games/sfcave-sdl/sfcave.h28
-rw-r--r--noncore/games/sfcave-sdl/sfcave_game.cpp14
-rw-r--r--noncore/games/sfcave-sdl/sound.cpp15
-rw-r--r--noncore/games/sfcave-sdl/starfield.cpp74
-rw-r--r--noncore/games/sfcave-sdl/terrain.cpp22
-rw-r--r--noncore/games/sfcave-sdl/util.cpp19
-rw-r--r--noncore/games/sfcave-sdl/util.h4
25 files changed, 791 insertions, 409 deletions
diff --git a/noncore/games/sfcave-sdl/animatedimage.cpp b/noncore/games/sfcave-sdl/animatedimage.cpp
index d9d6ff6..441c647 100644
--- a/noncore/games/sfcave-sdl/animatedimage.cpp
+++ b/noncore/games/sfcave-sdl/animatedimage.cpp
@@ -1,13 +1,13 @@
1#include "SDL.h" 1#include "SDL.h"
2#include "SDL_image.h" 2#include "SDL_image.h"
3 3
4#include "constants.h" 4#include "constants.h"
5#include "animatedimage.h" 5#include "animatedimage.h"
6 6
7AnimatedImage :: AnimatedImage( QString file, int nFrames ) 7AnimatedImage :: AnimatedImage( string file, int nFrames )
8{ 8{
9 nrFrames = nFrames; 9 nrFrames = nFrames;
10 currentFrame = 0; 10 currentFrame = 0;
11 11
12 // Load image 12 // Load image
13 image = IMG_Load( (const char *)file.c_str() ); 13 image = IMG_Load( (const char *)file.c_str() );
@@ -16,15 +16,12 @@ AnimatedImage :: AnimatedImage( QString file, int nFrames )
16 nrFrames = 0; 16 nrFrames = 0;
17 image = 0; 17 image = 0;
18 return; 18 return;
19 } 19 }
20 20
21 SDL_SetColorKey(image, SDL_SRCCOLORKEY | SDL_RLEACCEL, SDL_MapRGB( image->format, 0, 0, 0 ) ); 21 SDL_SetColorKey(image, SDL_SRCCOLORKEY | SDL_RLEACCEL, SDL_MapRGB( image->format, 0, 0, 0 ) );
22 //image = SDL_DisplayFormat( tmp );
23
24 //SDL_FreeSurface( tmp );
25 frameWidth = image->w/nrFrames; 22 frameWidth = image->w/nrFrames;
26 frameHeight = image->h; 23 frameHeight = image->h;
27} 24}
28 25
29AnimatedImage :: ~AnimatedImage() 26AnimatedImage :: ~AnimatedImage()
30{ 27{
@@ -44,12 +41,15 @@ bool AnimatedImage :: nextFrame()
44 41
45 return rc; 42 return rc;
46} 43}
47 44
48void AnimatedImage :: draw( SDL_Surface *screen, int x, int y ) 45void AnimatedImage :: draw( SDL_Surface *screen, int x, int y )
49{ 46{
47 if ( !image )
48 return;
49
50 SDL_Rect dst; 50 SDL_Rect dst;
51 dst.x = currentFrame * frameWidth; 51 dst.x = currentFrame * frameWidth;
52 dst.y = 0; 52 dst.y = 0;
53 dst.w = frameWidth; 53 dst.w = frameWidth;
54 dst.h = frameHeight; 54 dst.h = frameHeight;
55 55
diff --git a/noncore/games/sfcave-sdl/animatedimage.h b/noncore/games/sfcave-sdl/animatedimage.h
index 1b38e6d..3c03f52 100644
--- a/noncore/games/sfcave-sdl/animatedimage.h
+++ b/noncore/games/sfcave-sdl/animatedimage.h
@@ -3,13 +3,13 @@
3 3
4#include "SDL.h" 4#include "SDL.h"
5 5
6class AnimatedImage 6class AnimatedImage
7{ 7{
8public: 8public:
9 AnimatedImage( QString file, int nFrames ); 9 AnimatedImage( string file, int nFrames );
10 ~AnimatedImage(); 10 ~AnimatedImage();
11 11
12 bool nextFrame(); 12 bool nextFrame();
13 void draw( SDL_Surface *screen, int x, int y ); 13 void draw( SDL_Surface *screen, int x, int y );
14 bool AtEnd(); 14 bool AtEnd();
15 void reset() { currentFrame = 0; } 15 void reset() { currentFrame = 0; }
diff --git a/noncore/games/sfcave-sdl/bfont.cpp b/noncore/games/sfcave-sdl/bfont.cpp
index 0f29104..7dec8f5 100644
--- a/noncore/games/sfcave-sdl/bfont.cpp
+++ b/noncore/games/sfcave-sdl/bfont.cpp
@@ -70,16 +70,12 @@ void BFont::LoadFont (const char *filename)
70 Chars[x].h = 0; 70 Chars[x].h = 0;
71 Chars[x].w = 0; 71 Chars[x].w = 0;
72 } 72 }
73 InitFont(); // Init the font 73 InitFont(); // Init the font
74 } 74 }
75 } 75 }
76 else
77 {
78 cerr << "Error! The font has not been loaded!" << endl;
79 }
80} 76}
81 77
82BFont * BFont :: SetFontColor(Uint8 r, Uint8 g, Uint8 b) 78BFont * BFont :: SetFontColor(Uint8 r, Uint8 g, Uint8 b)
83{ 79{
84 int x,y; 80 int x,y;
85 81
@@ -110,13 +106,12 @@ BFont * BFont :: SetFontColor(Uint8 r, Uint8 g, Uint8 b)
110 106
111 if (SDL_MUSTLOCK(surface)) SDL_LockSurface(surface); 107 if (SDL_MUSTLOCK(surface)) SDL_LockSurface(surface);
112 if (SDL_MUSTLOCK(Surface)) SDL_LockSurface(Surface); 108 if (SDL_MUSTLOCK(Surface)) SDL_LockSurface(Surface);
113 109
114 color_key = xGetPixel(Surface, 0, Surface->h-1); 110 color_key = xGetPixel(Surface, 0, Surface->h-1);
115 111
116 printf("looking...\n");
117 for( x=0; x < Surface->w; x++) { 112 for( x=0; x < Surface->w; x++) {
118 for( y=0; y < Surface->h; y++) { 113 for( y=0; y < Surface->h; y++) {
119 old_r = old_g = old_b = 0; 114 old_r = old_g = old_b = 0;
120 pixel = xGetPixel(Surface,x,y); 115 pixel = xGetPixel(Surface,x,y);
121 116
122 if (pixel != color_key) { 117 if (pixel != color_key) {
@@ -128,13 +123,13 @@ BFont * BFont :: SetFontColor(Uint8 r, Uint8 g, Uint8 b)
128 123
129 pixel = SDL_MapRGB(surface->format,new_r,new_g,new_b); 124 pixel = SDL_MapRGB(surface->format,new_r,new_g,new_b);
130 } 125 }
131 PutPixel(surface,x,y,pixel); 126 PutPixel(surface,x,y,pixel);
132 } 127 }
133 } 128 }
134 printf("unlooking...\n"); 129
135 if (SDL_MUSTLOCK(surface)) SDL_UnlockSurface(surface); 130 if (SDL_MUSTLOCK(surface)) SDL_UnlockSurface(surface);
136 if (SDL_MUSTLOCK(Surface)) SDL_UnlockSurface(Surface); 131 if (SDL_MUSTLOCK(Surface)) SDL_UnlockSurface(Surface);
137 132
138 SDL_SetColorKey(surface, SDL_SRCCOLORKEY, color_key); 133 SDL_SetColorKey(surface, SDL_SRCCOLORKEY, color_key);
139 } 134 }
140 135
diff --git a/noncore/games/sfcave-sdl/constants.h b/noncore/games/sfcave-sdl/constants.h
index 8fadae4..f10764e 100644
--- a/noncore/games/sfcave-sdl/constants.h
+++ b/noncore/games/sfcave-sdl/constants.h
@@ -1,12 +1,11 @@
1#ifndef __CONSTANTS_H 1#ifndef __CONSTANTS_H
2#define __CONSTANTS_H 2#define __CONSTANTS_H
3 3
4#include <string> 4#include <string>
5using namespace std; 5using namespace std;
6#define QString string
7 6
8#ifdef QWS 7#ifdef QWS
9#define IMAGES_PATH "/opt/QtPalmtop/pics/sfcave/data/" 8#define IMAGES_PATH "/opt/QtPalmtop/pics/sfcave/data/"
10#define SOUND_PATH "/opt/QtPalmtop/sounds/sfcave/" 9#define SOUND_PATH "/opt/QtPalmtop/sounds/sfcave/"
11#else 10#else
12#define IMAGES_PATH "./images/" 11#define IMAGES_PATH "./images/"
@@ -52,16 +51,34 @@ using namespace std;
52 #define MENU_GAME_SFCAVE13 51 #define MENU_GAME_SFCAVE13
53 #define MENU_GAME_GATES 14 52 #define MENU_GAME_GATES 14
54 #define MENU_GAME_FLY 15 53 #define MENU_GAME_FLY 15
55 #define MENU_DIFFICULTY_EASY16 54 #define MENU_DIFFICULTY_EASY16
56 #define MENU_DIFFICULTY_NORMAL17 55 #define MENU_DIFFICULTY_NORMAL17
57 #define MENU_DIFFICULTY_HARD18 56 #define MENU_DIFFICULTY_HARD18
58 #define MENU_SOUNDS 19 57 #define MENU_DIFFICULTY_HARD18
59 #define MENU_SOUND_ON 20 58 #define MENU_DIFFICULTY_CUSTOM19
60 #define MENU_SOUND_OFF 21 59 #define MENU_SOUNDS 20
61 #define MENU_MUSIC_ON 22 60 #define MENU_SOUND_ON 21
62 #define MENU_MUSIC_OFF 23 61 #define MENU_SOUND_OFF 22
62 #define MENU_MUSIC_ON 23
63 #define MENU_MUSIC_OFF 24
64#define MENU_CUSTOM_THRUST 25
65#define MENU_CUSTOM_GRAVITY 26
66#define MENU_CUSTOM_MAXSPEEDUP 27
67#define MENU_CUSTOM_MAXSPEEDDOWN 28
68#define MENU_CUSTOM_INCREASE 29
69#define MENU_CUSTOM_DECREASE 30
70#define MENU_CUSTOM_SAVE 31
71#define MENU_CUSTOM_CANCEL 32
63 72
64// Sounds 73// Sounds
65 #define SND_EXPLOSION 0 74 #define SND_EXPLOSION 0
66 #define SND_THRUST 1 75 #define SND_THRUST 1
76 #define INGAME_MUSIC SOUND_PATH "ingame.mod"
77
78// Constants for player values
79#define PLAYER_THRUST 0
80#define PLAYER_GRAVITY 1
81#define PLAYER_MAX_SPEED_UP 2
82#define PLAYER_MAX_SPEED_DOWN 3
83
67#endif 84#endif
diff --git a/noncore/games/sfcave-sdl/fly_game.cpp b/noncore/games/sfcave-sdl/fly_game.cpp
index f5ab401..7605c3f 100644
--- a/noncore/games/sfcave-sdl/fly_game.cpp
+++ b/noncore/games/sfcave-sdl/fly_game.cpp
@@ -14,12 +14,13 @@ FlyGame :: FlyGame( SFCave *p, int w, int h, int diff )
14 player = new Player( w, h ); 14 player = new Player( w, h );
15 highScore = 0; 15 highScore = 0;
16} 16}
17 17
18FlyGame :: ~FlyGame() 18FlyGame :: ~FlyGame()
19{ 19{
20 // terrain and player get deleted by parent class
20} 21}
21 22
22void FlyGame :: init() 23void FlyGame :: init()
23{ 24{
24 Game :: init(); 25 Game :: init();
25 26
@@ -31,12 +32,21 @@ void FlyGame :: init()
31 case MENU_DIFFICULTY_NORMAL: 32 case MENU_DIFFICULTY_NORMAL:
32 player->setMovementInfo( 0.35, 0.4, 2.5, 3 ); 33 player->setMovementInfo( 0.35, 0.4, 2.5, 3 );
33 break; 34 break;
34 case MENU_DIFFICULTY_HARD: 35 case MENU_DIFFICULTY_HARD:
35 player->setMovementInfo( 0.4, 0.6, 4, 5 ); 36 player->setMovementInfo( 0.4, 0.6, 4, 5 );
36 break; 37 break;
38 case MENU_DIFFICULTY_CUSTOM:
39 {
40 double thrust = parent->loadDoubleSetting( "Fly_custom_player_thrust", 0.3 );
41 double gravity = parent->loadDoubleSetting( "Fly_custom_player_gravity", 0.2 );
42 double maxUp = parent->loadDoubleSetting( "Fly_custom_player_maxupspeed", 1.5 );
43 double maxDown = parent->loadDoubleSetting( "Fly_custom_player_maxdownspeed", 1.5 );
44 player->setMovementInfo( thrust, gravity, maxUp, maxDown );
45 break;
46 }
37 } 47 }
38 48
39 startScoring = false; 49 startScoring = false;
40} 50}
41 51
42void FlyGame :: update( int state ) 52void FlyGame :: update( int state )
@@ -47,13 +57,13 @@ void FlyGame :: update( int state )
47 { 57 {
48 58
49 if ( nrFrames % 3 == 0 ) 59 if ( nrFrames % 3 == 0 )
50 { 60 {
51 int diff = terrain->getMapBottom( 10 ) - player->getY(); 61 int diff = terrain->getMapBottom( 10 ) - player->getY();
52 int tmpScore = ((FlyTerrain *)terrain)->getScore( 1, diff ); 62 int tmpScore = ((FlyTerrain *)terrain)->getScore( 1, diff );
53 // printf( "diff - %d score - %d\n", diff, tmpScore ); 63
54 if ( !startScoring ) 64 if ( !startScoring )
55 { 65 {
56 if ( tmpScore > 0 ) 66 if ( tmpScore > 0 )
57 startScoring = true; 67 startScoring = true;
58 } 68 }
59 69
@@ -66,13 +76,12 @@ void FlyGame :: update( int state )
66 score += tmpScore; 76 score += tmpScore;
67 } 77 }
68 } 78 }
69 79
70 if ( checkCollisions() ) 80 if ( checkCollisions() )
71 { 81 {
72 // printf( "Crashed!\n" );
73 parent->changeState( STATE_CRASHING ); 82 parent->changeState( STATE_CRASHING );
74 return; 83 return;
75 } 84 }
76 85
77 // Game logic goes here 86 // Game logic goes here
78 terrain->moveTerrain( 5 ); 87 terrain->moveTerrain( 5 );
diff --git a/noncore/games/sfcave-sdl/font.cpp b/noncore/games/sfcave-sdl/font.cpp
index 2976d48..1988252 100644
--- a/noncore/games/sfcave-sdl/font.cpp
+++ b/noncore/games/sfcave-sdl/font.cpp
@@ -5,30 +5,42 @@
5BFont *FontHandler :: menuSelFont; 5BFont *FontHandler :: menuSelFont;
6BFont *FontHandler :: menuUnSelFont; 6BFont *FontHandler :: menuUnSelFont;
7BFont *FontHandler :: whiteFont; 7BFont *FontHandler :: whiteFont;
8BFont *FontHandler :: colouredFont; 8BFont *FontHandler :: colouredFont;
9BFont *FontHandler :: helpFont; 9BFont *FontHandler :: helpFont;
10 10
11void FontHandler :: init() 11bool FontHandler :: init()
12{ 12{
13 // Load font images 13 // Load font images
14 // Convert to fonts 14 // Convert to fonts
15 menuSelFont = new BFont( IMAGES_PATH "sel_menu_font.bmp" ); 15 menuSelFont = new BFont( IMAGES_PATH "sel_menu_font.bmp" );
16 menuUnSelFont = new BFont( IMAGES_PATH "unsel_menu_font.bmp" ); 16 menuUnSelFont = new BFont( IMAGES_PATH "unsel_menu_font.bmp" );
17 whiteFont = new BFont( IMAGES_PATH "score_font.bmp" ); 17 whiteFont = new BFont( IMAGES_PATH "score_font.bmp" );
18 helpFont = new BFont( IMAGES_PATH "help_font.bmp" ); 18 helpFont = new BFont( IMAGES_PATH "help_font.bmp" );
19 colouredFont = 0; 19 colouredFont = 0;
20
21 // Check if we are installed correctly (we need fonts to function)
22 if ( menuSelFont == 0 || menuUnSelFont == 0 || whiteFont == 0 || helpFont == 0 )
23 {
24 printf( "One or more fonts are not installed correctly\n" );
25 return false;
26 }
27
28 return true;
20} 29}
21 30
22void FontHandler :: cleanUp() 31void FontHandler :: cleanUp()
23{ 32{
24 delete menuSelFont; 33 if ( menuSelFont )
25 delete menuUnSelFont; 34 delete menuSelFont;
26 delete whiteFont; 35 if ( menuUnSelFont )
27 delete helpFont; 36 delete menuUnSelFont;
28 37 if ( whiteFont )
38 delete whiteFont;
39 if ( helpFont )
40 delete helpFont;
29 if ( colouredFont ) 41 if ( colouredFont )
30 delete colouredFont; 42 delete colouredFont;
31} 43}
32 44
33int FontHandler :: TextWidth( int font, const char *text ) 45int FontHandler :: TextWidth( int font, const char *text )
34{ 46{
diff --git a/noncore/games/sfcave-sdl/font.h b/noncore/games/sfcave-sdl/font.h
index e5bb707..5f0674a 100644
--- a/noncore/games/sfcave-sdl/font.h
+++ b/noncore/games/sfcave-sdl/font.h
@@ -10,13 +10,13 @@
10 #define FONT_COLOURED_TEXT 4 10 #define FONT_COLOURED_TEXT 4
11 #define FONT_HELP_FONT 5 11 #define FONT_HELP_FONT 5
12 12
13class FontHandler 13class FontHandler
14{ 14{
15public: 15public:
16 static void init(); 16 static bool init();
17 static void cleanUp(); 17 static void cleanUp();
18 18
19 static int TextWidth( int font, const char *text ); 19 static int TextWidth( int font, const char *text );
20 static int FontHeight( int font ); 20 static int FontHeight( int font );
21 static void draw( SDL_Surface *screen, int font, const char *text, int x, int y ); 21 static void draw( SDL_Surface *screen, int font, const char *text, int x, int y );
22 static void changeColor( int font, int r, int g, int b ); 22 static void changeColor( int font, int r, int g, int b );
diff --git a/noncore/games/sfcave-sdl/game.cpp b/noncore/games/sfcave-sdl/game.cpp
index a644696..e41e510 100644
--- a/noncore/games/sfcave-sdl/game.cpp
+++ b/noncore/games/sfcave-sdl/game.cpp
@@ -51,13 +51,13 @@ void Game :: init()
51 } 51 }
52 else 52 else
53 { 53 {
54 setSeed( -1 ); 54 setSeed( -1 );
55 replayList.clear(); 55 replayList.clear();
56 } 56 }
57 57
58 score = 0; 58 score = 0;
59 nrFrames = 0; 59 nrFrames = 0;
60 press = false; 60 press = false;
61 61
62 // Load highscore 62 // Load highscore
63 string key = getGameName() + "_" + getGameDifficultyText() + "_highscore"; 63 string key = getGameName() + "_" + getGameDifficultyText() + "_highscore";
@@ -73,68 +73,72 @@ void Game :: handleKeys( SDL_KeyboardEvent &key )
73 { 73 {
74 if ( key.type == SDL_KEYDOWN ) 74 if ( key.type == SDL_KEYDOWN )
75 { 75 {
76 if ( !press ) 76 if ( !press )
77 replayList.push_back( nrFrames ); 77 replayList.push_back( nrFrames );
78 press = true; 78 press = true;
79
80 // if ( thrustChannel == -1 && parent->getState() == STATE_PLAYING )
81 // thrustChannel = SoundHandler :: playSound( SND_THRUST, -1, -1, false );
82 } 79 }
83 else 80 else
84 { 81 {
85 if ( press ) 82 if ( press )
86 replayList.push_back( nrFrames ); 83 replayList.push_back( nrFrames );
87 press = false; 84 press = false;
88 85
89 if ( thrustChannel != -1 )
90 {
91 // SoundHandler :: stopSound( thrustChannel, true, 300 );
92 // thrustChannel = -1;
93 }
94 } 86 }
95 } 87 }
96} 88}
97 89
98 90
99 91
100QString Game :: getGameDifficultyText() 92string Game :: getGameDifficultyText()
101{ 93{
102 QString ret; 94 string ret;
103 95
104 if ( difficulty == MENU_DIFFICULTY_EASY ) 96 if ( difficulty == MENU_DIFFICULTY_EASY )
105 ret = "Easy"; 97 ret = "Easy";
106 else if ( difficulty == MENU_DIFFICULTY_NORMAL ) 98 else if ( difficulty == MENU_DIFFICULTY_NORMAL )
107 ret = "Medium"; 99 ret = "Medium";
108 else if ( difficulty == MENU_DIFFICULTY_HARD ) 100 else if ( difficulty == MENU_DIFFICULTY_HARD )
109 ret = "Hard"; 101 ret = "Hard";
102 else if ( difficulty == MENU_DIFFICULTY_CUSTOM )
103 ret = "Custom";
110 104
111 return ret; 105 return ret;
112} 106}
113 107
114void Game :: setDifficulty( string diff ) 108void Game :: setDifficulty( string diff )
115{ 109{
116 if ( diff == "Easy" ) 110 if ( diff == "Easy" )
117 difficulty = MENU_DIFFICULTY_EASY; 111 difficulty = MENU_DIFFICULTY_EASY;
118 else if ( diff == "Medium" ) 112 else if ( diff == "Medium" )
119 difficulty = MENU_DIFFICULTY_NORMAL; 113 difficulty = MENU_DIFFICULTY_NORMAL;
120 else if ( diff == "Hard" ) 114 else if ( diff == "Hard" )
121 difficulty = MENU_DIFFICULTY_HARD; 115 difficulty = MENU_DIFFICULTY_HARD;
116 else if ( diff == "Custom" )
117 difficulty = MENU_DIFFICULTY_CUSTOM;
118
119 init();
120}
121
122void Game :: setDifficulty( int diff )
123{
124 difficulty = diff;
125 init();
122} 126}
123 127
124void Game :: update( int state ) 128void Game :: update( int state )
125{ 129{
126 nrFrames ++; 130 nrFrames ++;
127 131
128 if ( score > highScore ) 132 if ( score > highScore )
129 highScore = score; 133 highScore = score;
130 134
131 135
132 if ( state == STATE_PLAYING ) 136 if ( state == STATE_PLAYING )
133 { 137 {
134 if ( replay ) 138 if ( replay )
135 { 139 {
136 while( replayIt != replayList.end() && (*replayIt) == nrFrames-1 ) 140 while( replayIt != replayList.end() && (*replayIt) == nrFrames-1 )
137 { 141 {
138 press = !press; 142 press = !press;
139 replayIt ++; 143 replayIt ++;
140 } 144 }
@@ -163,20 +167,19 @@ void Game :: preDraw( SDL_Surface *screen )
163{ 167{
164} 168}
165 169
166void Game :: draw( SDL_Surface *screen ) 170void Game :: draw( SDL_Surface *screen )
167{ 171{
168 char tmp[100]; 172 char tmp[100];
169 QString scoreText; 173 string scoreText;
170 sprintf( tmp, "Score: %06ld High Score: %06ld", score, highScore ); 174 sprintf( tmp, "Score: %06ld High Score: %06ld", score, highScore );
171 //printf( "%s\n", (const char *)scoreText );
172 FontHandler::draw( screen, FONT_WHITE_TEXT, tmp, 3, 10 ); 175 FontHandler::draw( screen, FONT_WHITE_TEXT, tmp, 3, 10 );
173 176
174 if ( parent->getState() == STATE_CRASHED ) 177 if ( parent->getState() == STATE_CRASHED )
175 { 178 {
176 QString crashText; 179 string crashText;
177 crashText = "Game Over"; 180 crashText = "Game Over";
178 int x = (240 - FontHandler::TextWidth( FONT_WHITE_TEXT, (const char *)crashText.c_str() )) / 2; 181 int x = (240 - FontHandler::TextWidth( FONT_WHITE_TEXT, (const char *)crashText.c_str() )) / 2;
179 FontHandler::draw( screen, FONT_WHITE_TEXT, (const char *)crashText.c_str(), x, 150 ); 182 FontHandler::draw( screen, FONT_WHITE_TEXT, (const char *)crashText.c_str(), x, 150 );
180 183
181 int fontHeight = FontHandler::FontHeight( FONT_WHITE_TEXT ); 184 int fontHeight = FontHandler::FontHeight( FONT_WHITE_TEXT );
182 crashText = "Press Middle Button to play again"; 185 crashText = "Press Middle Button to play again";
@@ -220,26 +223,26 @@ void Game :: setSeed( int seed )
220 currentSeed = ((unsigned long) time((time_t *) NULL)); 223 currentSeed = ((unsigned long) time((time_t *) NULL));
221 else 224 else
222 currentSeed = seed; 225 currentSeed = seed;
223 PutSeed( currentSeed ); 226 PutSeed( currentSeed );
224} 227}
225 228
226void Game :: saveReplay( QString file ) 229void Game :: saveReplay( string file )
227{ 230{
228 FILE *out; 231 FILE *out;
229 out = fopen( file.c_str(), "w" ); 232 out = fopen( file.c_str(), "w" );
230 if ( !out ) 233 if ( !out )
231 { 234 {
232 printf( "Couldn't write to /home/root/%s\n", file.c_str() ); 235 printf( "Couldn't write to /home/root/%s\n", file.c_str() );
233 parent->setMenuStatusText( "Couldn't save replay file" ); 236 parent->setMenuStatusText( "Couldn't save replay file" );
234 return; 237 return;
235 } 238 }
236 239
237 // Build up string of values 240 // Build up string of values
238 // Format is:: <landscape seed> <game type> <difficulty> <framenr> <framenr>....... 241 // Format is:: <landscape seed> <game type> <difficulty> <framenr> <framenr>.......
239 QString val; 242 string val;
240 char tmp[20]; 243 char tmp[20];
241 sprintf( tmp, "%d %d ", currentSeed, difficulty ); 244 sprintf( tmp, "%d %d ", currentSeed, difficulty );
242 val = tmp; 245 val = tmp;
243 246
244 list<int>::iterator it = replayList.begin(); 247 list<int>::iterator it = replayList.begin();
245 while( it != replayList.end() ) 248 while( it != replayList.end() )
@@ -248,26 +251,23 @@ void Game :: saveReplay( QString file )
248 val += tmp; 251 val += tmp;
249 252
250 it++; 253 it++;
251 } 254 }
252 val += "\n"; 255 val += "\n";
253 256
254 QString line; 257 string line;
255 sprintf( tmp, "%d\n", val.length() ); 258 sprintf( tmp, "%d\n", val.length() );
256 line = tmp; 259 line = tmp;
257 fwrite( line.c_str(), 1, line.length(), out ); 260 fwrite( line.c_str(), 1, line.length(), out );
258 261
259 fwrite( val.c_str(), 1, val.length(), out ); 262 fwrite( val.c_str(), 1, val.length(), out );
260 263
261 fclose( out ); 264 fclose( out );
262
263 printf( "Replay saved to %s\n", (const char *)file.c_str() );
264
265} 265}
266 266
267void Game :: loadReplay( QString file ) 267void Game :: loadReplay( string file )
268{ 268{
269 269
270 FILE *in = fopen( (const char *)file.c_str(), "r" ); 270 FILE *in = fopen( (const char *)file.c_str(), "r" );
271 271
272 if ( in == 0 ) 272 if ( in == 0 )
273 { 273 {
@@ -282,15 +282,14 @@ void Game :: loadReplay( QString file )
282 282
283 int length = -1; 283 int length = -1;
284 sscanf( line, "%d", &length ); 284 sscanf( line, "%d", &length );
285 char *data = new char[length+1]; 285 char *data = new char[length+1];
286 286
287 fread( data, 1, length, in ); 287 fread( data, 1, length, in );
288// printf( "data - %s", data );
289 288
290 QString sep = " "; 289 string sep = " ";
291 290
292 StringTokenizer st( data, sep ); 291 StringTokenizer st( data, sep );
293 292
294 // print it out 293 // print it out
295 vector<string>::iterator it = st.begin(); 294 vector<string>::iterator it = st.begin();
296 currentSeed = atoi( (*it).c_str() ); 295 currentSeed = atoi( (*it).c_str() );
@@ -305,15 +304,12 @@ void Game :: loadReplay( QString file )
305 replayList.push_back( v ); 304 replayList.push_back( v );
306 } 305 }
307 306
308 delete data; 307 delete data;
309 308
310 fclose( in ); 309 fclose( in );
311
312 printf( "Replay loaded from %s\n", (const char *)file.c_str() );
313
314} 310}
315 311
316 312
317Game *Game :: createGame( SFCave *p, int w, int h, string game, string difficulty ) 313Game *Game :: createGame( SFCave *p, int w, int h, string game, string difficulty )
318{ 314{
319 Game *g; 315 Game *g;
diff --git a/noncore/games/sfcave-sdl/game.h b/noncore/games/sfcave-sdl/game.h
index 56fa6a1..087f848 100644
--- a/noncore/games/sfcave-sdl/game.h
+++ b/noncore/games/sfcave-sdl/game.h
@@ -22,35 +22,36 @@ public:
22 22
23 virtual void stateChanged( int from, int to ); 23 virtual void stateChanged( int from, int to );
24 24
25 void setReplay( bool val ) { replay = val; } 25 void setReplay( bool val ) { replay = val; }
26 26
27 void handleKeys( SDL_KeyboardEvent &key ); 27 void handleKeys( SDL_KeyboardEvent &key );
28 QString getGameName() { return gameName; } 28 string getGameName() { return gameName; }
29 int getDifficulty() { return difficulty; } 29 int getDifficulty() { return difficulty; }
30 QString getGameDifficultyText(); 30 string getGameDifficultyText();
31 void setDifficulty( int diff ) { difficulty = diff; } 31 void setDifficulty( int diff );
32 void setDifficulty( string diff ); 32 void setDifficulty( string diff );
33 33
34 long getScore() { return score; } 34 long getScore() { return score; }
35 long getHighScore() { return highScore; } 35 long getHighScore() { return highScore; }
36 void increaseScore( long val ) { score += val; } 36 void increaseScore( long val ) { score += val; }
37 void clearScore() { score = 0; } 37 void clearScore() { score = 0; }
38 bool gotHighScore() { return (score >= highScore); } 38 bool gotHighScore() { return (score >= highScore); }
39 bool isReplayAvailable() { return replayList.size() > 0; } 39 bool isReplayAvailable() { return replayList.size() > 0; }
40 40
41 Terrain *getTerrain() { return terrain; } 41 Terrain *getTerrain() { return terrain; }
42 Player *getPlayer() { return player; }
42 43
43 void setSeed( int seed ); 44 void setSeed( int seed );
44 void loadReplay( QString file ); 45 void loadReplay( string file );
45 void saveReplay( QString file ); 46 void saveReplay( string file );
46 47
47 static Game *createGame( SFCave *p, int w, int h, string game, string difficulty ); 48 static Game *createGame( SFCave *p, int w, int h, string game, string difficulty );
48 49
49protected: 50protected:
50 QString gameName; 51 string gameName;
51 52
52 int thrustChannel; 53 int thrustChannel;
53 54
54 int difficulty; 55 int difficulty;
55 56
56 SFCave *parent; 57 SFCave *parent;
@@ -66,17 +67,15 @@ protected:
66 long score; 67 long score;
67 long highScore; 68 long highScore;
68 69
69 // Stuff for the replays 70 // Stuff for the replays
70 int currentSeed; 71 int currentSeed;
71 72
72// QListIterator<int> *replayIt;
73 list<int> replayList; 73 list<int> replayList;
74 list<int>::iterator replayIt; 74 list<int>::iterator replayIt;
75 //QList<int> replayList;
76 bool replay; 75 bool replay;
77 QString replayFile; 76 string replayFile;
78 77
79private: 78private:
80}; 79};
81 80
82#endif 81#endif
diff --git a/noncore/games/sfcave-sdl/gates_game.cpp b/noncore/games/sfcave-sdl/gates_game.cpp
index 1a9bc89..762801d 100644
--- a/noncore/games/sfcave-sdl/gates_game.cpp
+++ b/noncore/games/sfcave-sdl/gates_game.cpp
@@ -15,12 +15,13 @@ GatesGame :: GatesGame( SFCave *p, int w, int h, int diff )
15 player = new Player( w, h ); 15 player = new Player( w, h );
16 highScore = 0; 16 highScore = 0;
17} 17}
18 18
19GatesGame :: ~GatesGame() 19GatesGame :: ~GatesGame()
20{ 20{
21 // terrain and player get deleted by parent class
21} 22}
22 23
23void GatesGame :: init() 24void GatesGame :: init()
24{ 25{
25 Game :: init(); 26 Game :: init();
26 27
@@ -43,12 +44,25 @@ void GatesGame :: init()
43 player->setMovementInfo( 0.4, 0.6, 4, 5 ); 44 player->setMovementInfo( 0.4, 0.6, 4, 5 );
44 break; 45 break;
45 case MENU_DIFFICULTY_HARD: 46 case MENU_DIFFICULTY_HARD:
46 gapHeight = 25; 47 gapHeight = 25;
47 player->setMovementInfo( 0.6, 0.8, 6, 7 ); 48 player->setMovementInfo( 0.6, 0.8, 6, 7 );
48 break; 49 break;
50 case MENU_DIFFICULTY_CUSTOM:
51 {
52 // Read custom difficulty settings for this game
53 gapHeight = parent->loadIntSetting( "Gates_custom_gapHeight", 75 );
54
55 double thrust = parent->loadDoubleSetting( "Gates_custom_player_thrust", 0.4 );
56 double gravity = parent->loadDoubleSetting( "Gates_custom_player_gravity", 0.6 );
57 double maxUp = parent->loadDoubleSetting( "Gates_custom_player_maxupspeed", 4.0 );
58 double maxDown = parent->loadDoubleSetting( "Gates_custom_player_maxdownspeed", 5.0 );
59 player->setMovementInfo( thrust, gravity, maxUp, maxDown );
60
61 break;
62 }
49 } 63 }
50 64
51 for ( int i = 0 ; i < BLOCKSIZE ; ++i ) 65 for ( int i = 0 ; i < BLOCKSIZE ; ++i )
52 blocks[i].y( -1 ); 66 blocks[i].y( -1 );
53} 67}
54 68
@@ -74,13 +88,12 @@ void GatesGame :: update( int state )
74 nextGate = nrFrames + nextInt( 50 ) + gateDistance; 88 nextGate = nrFrames + nextInt( 50 ) + gateDistance;
75 addGate(); 89 addGate();
76 } 90 }
77 91
78 if ( checkCollisions() ) 92 if ( checkCollisions() )
79 { 93 {
80 // printf( "Crashed!\n" );
81 parent->changeState( STATE_CRASHING ); 94 parent->changeState( STATE_CRASHING );
82 return; 95 return;
83 } 96 }
84 97
85 terrain->moveTerrain( 5 ); 98 terrain->moveTerrain( 5 );
86 moveBlocks( 5 ); 99 moveBlocks( 5 );
diff --git a/noncore/games/sfcave-sdl/help.cpp b/noncore/games/sfcave-sdl/help.cpp
index 91c62da..f1728f6 100644
--- a/noncore/games/sfcave-sdl/help.cpp
+++ b/noncore/games/sfcave-sdl/help.cpp
@@ -32,35 +32,34 @@ void Help :: handleKeys( SDL_KeyboardEvent &key )
32 else if ( key.keysym.sym == SDLK_UP ) 32 else if ( key.keysym.sym == SDLK_UP )
33 { 33 {
34 if ( textSpeed > 0 ) 34 if ( textSpeed > 0 )
35 textSpeed = 0; 35 textSpeed = 0;
36 else textSpeed = 1; 36 else textSpeed = 1;
37 } 37 }
38 38
39 } 39 }
40 else if ( key.type == SDL_KEYUP ) 40 else if ( key.type == SDL_KEYUP )
41 { 41 {
42 if ( key.keysym.sym == SDLK_DOWN ) 42 if ( key.keysym.sym == SDLK_DOWN )
43 textSpeed = 1; 43 textSpeed = 1;
44 } 44 }
45} 45}
46void Help :: init() 46void Help :: init()
47{ 47{
48 startPos = 320; 48 startPos = 320;
49 currLine = 0; 49 currLine = 0;
50 textSpeed = 1; 50 textSpeed = 1;
51 51
52 // Create our coloured font 52 // Create our coloured font
53 FontHandler :: changeColor( FONT_HELP_FONT, 0, 0, 255 ); 53 FontHandler :: changeColor( FONT_HELP_FONT, 0, 0, 255 );
54} 54}
55 55
56void Help :: draw( SDL_Surface *screen ) 56void Help :: draw( SDL_Surface *screen )
57{ 57{
58 stars->draw( screen ); 58 stars->draw( screen );
59 59
60
61 list<string>::iterator it = textList.begin(); 60 list<string>::iterator it = textList.begin();
62 61
63 // Move to start of text 62 // Move to start of text
64 for ( int i = 0 ; i < currLine && it != textList.end() ; ++i ) 63 for ( int i = 0 ; i < currLine && it != textList.end() ; ++i )
65 it++; 64 it++;
66 65
@@ -72,19 +71,19 @@ void Help :: draw( SDL_Surface *screen )
72 71
73 // draw text 72 // draw text
74 FontHandler::draw( screen, FONT_COLOURED_TEXT, text.c_str(), -1, pos ); 73 FontHandler::draw( screen, FONT_COLOURED_TEXT, text.c_str(), -1, pos );
75 pos += FontHandler::FontHeight( FONT_COLOURED_TEXT ); 74 pos += FontHandler::FontHeight( FONT_COLOURED_TEXT );
76 it ++; 75 it ++;
77 } 76 }
78 77
79} 78}
80 79
81void Help :: update() 80void Help :: update()
82{ 81{
83 stars->move(); 82 stars->move();
84 83
85 startPos -= textSpeed; 84 startPos -= textSpeed;
86 if ( startPos <= -FontHandler::FontHeight( FONT_COLOURED_TEXT ) ) 85 if ( startPos <= -FontHandler::FontHeight( FONT_COLOURED_TEXT ) )
87 { 86 {
88 startPos = 0; 87 startPos = 0;
89 currLine ++; 88 currLine ++;
90 89
diff --git a/noncore/games/sfcave-sdl/menu.cpp b/noncore/games/sfcave-sdl/menu.cpp
index 0a7366f..a4a4216 100644
--- a/noncore/games/sfcave-sdl/menu.cpp
+++ b/noncore/games/sfcave-sdl/menu.cpp
@@ -5,13 +5,13 @@
5#include "sfcave.h" 5#include "sfcave.h"
6#include "game.h" 6#include "game.h"
7#include "menu.h" 7#include "menu.h"
8#include "font.h" 8#include "font.h"
9#include "starfield.h" 9#include "starfield.h"
10 10
11MenuOption :: MenuOption( QString text, int id ) 11MenuOption :: MenuOption( string text, int id )
12{ 12{
13 menuText = text; 13 menuText = text;
14 menuId = id; 14 menuId = id;
15 nextMenu = 0; 15 nextMenu = 0;
16 highlighted = false; 16 highlighted = false;
17} 17}
@@ -112,19 +112,21 @@ Menu :: Menu( SFCave *p )
112 item->setNextMenu( options ); 112 item->setNextMenu( options );
113 item = gameType->addMenuOption( "Back", MENU_BACK ); 113 item = gameType->addMenuOption( "Back", MENU_BACK );
114 item->setNextMenu( options ); 114 item->setNextMenu( options );
115 typeMenu->setNextMenu( gameType ); 115 typeMenu->setNextMenu( gameType );
116 116
117 // Game Difficulty menu 117 // Game Difficulty menu
118 MenuOption *customMenu = 0;
118 Menu *gameDifficulty = new Menu( options ); 119 Menu *gameDifficulty = new Menu( options );
119 item = gameDifficulty->addMenuOption( "Easy", MENU_DIFFICULTY_EASY ); 120 item = gameDifficulty->addMenuOption( "Easy", MENU_DIFFICULTY_EASY );
120 item->setNextMenu( options, false ); 121 item->setNextMenu( options, false );
121 item = gameDifficulty->addMenuOption( "Normal", MENU_DIFFICULTY_NORMAL ); 122 item = gameDifficulty->addMenuOption( "Normal", MENU_DIFFICULTY_NORMAL );
122 item->setNextMenu( options, false ); 123 item->setNextMenu( options, false );
123 item = gameDifficulty->addMenuOption( "Hard", MENU_DIFFICULTY_HARD ); 124 item = gameDifficulty->addMenuOption( "Hard", MENU_DIFFICULTY_HARD );
124 item->setNextMenu( options, false ); 125 item->setNextMenu( options, false );
126 customMenu = gameDifficulty->addMenuOption( "Custom", MENU_DIFFICULTY_CUSTOM );
125 item = gameDifficulty->addMenuOption( "Back", MENU_BACK ); 127 item = gameDifficulty->addMenuOption( "Back", MENU_BACK );
126 item->setNextMenu( options, false ); 128 item->setNextMenu( options, false );
127 difficultyMenu->setNextMenu( gameDifficulty ); 129 difficultyMenu->setNextMenu( gameDifficulty );
128 130
129 // Sounds Menu 131 // Sounds Menu
130 Menu *sounds = new Menu( options ); 132 Menu *sounds = new Menu( options );
@@ -132,12 +134,35 @@ Menu :: Menu( SFCave *p )
132 sounds->addMenuOption( "Sound Off", MENU_SOUND_OFF ); 134 sounds->addMenuOption( "Sound Off", MENU_SOUND_OFF );
133 sounds->addMenuOption( "Music On", MENU_MUSIC_ON ); 135 sounds->addMenuOption( "Music On", MENU_MUSIC_ON );
134 sounds->addMenuOption( "Music Off", MENU_MUSIC_OFF ); 136 sounds->addMenuOption( "Music Off", MENU_MUSIC_OFF );
135 item = sounds->addMenuOption( "Back", MENU_BACK ); 137 item = sounds->addMenuOption( "Back", MENU_BACK );
136 item->setNextMenu( options, false ); 138 item->setNextMenu( options, false );
137 soundsMenu->setNextMenu( sounds ); 139 soundsMenu->setNextMenu( sounds );
140
141 // Custom Menu
142 Menu *custom = new Menu( gameDifficulty );
143 Menu *updown = new Menu( custom );
144 item = custom->addMenuOption( "Thrust", MENU_CUSTOM_THRUST );
145 item->setNextMenu( updown );
146 item = custom->addMenuOption( "Gravity", MENU_CUSTOM_GRAVITY );
147 item->setNextMenu( updown );
148 item = custom->addMenuOption( "Max Speed Up", MENU_CUSTOM_MAXSPEEDUP );
149 item->setNextMenu( updown );
150 item = custom->addMenuOption( "Max Speed Down", MENU_CUSTOM_MAXSPEEDDOWN );
151 item->setNextMenu( updown );
152 item = custom->addMenuOption( "Back", MENU_BACK );
153 item->setNextMenu( gameDifficulty, false );
154 customMenu->setNextMenu( custom );
155
156 // Up down menu
157 item = updown->addMenuOption( "Increase", MENU_CUSTOM_INCREASE );
158 item = updown->addMenuOption( "Decrease", MENU_CUSTOM_DECREASE );
159 item = updown->addMenuOption( "Save", MENU_CUSTOM_SAVE );
160 item->setNextMenu( custom, false );
161 item = updown->addMenuOption( "Cancel", MENU_CUSTOM_CANCEL );
162 item->setNextMenu( custom, false );
138 163
139 // Set static variables for menu selection up 164 // Set static variables for menu selection up
140 mainMenu = this; 165 mainMenu = this;
141 currentMenuOption = 0; 166 currentMenuOption = 0;
142 167
143 resetToTopMenu(); 168 resetToTopMenu();
@@ -212,13 +237,12 @@ void Menu :: draw( SDL_Surface *screen )
212 237
213int Menu :: handleKeys( SDL_KeyboardEvent &key ) 238int Menu :: handleKeys( SDL_KeyboardEvent &key )
214{ 239{
215 if ( key.type != SDL_KEYDOWN ) 240 if ( key.type != SDL_KEYDOWN )
216 return -1; 241 return -1;
217 242
218 statusText = "";
219 switch( key.keysym.sym ) 243 switch( key.keysym.sym )
220 { 244 {
221 case SDLK_DOWN: 245 case SDLK_DOWN:
222 { 246 {
223 // Move to next menu item 247 // Move to next menu item
224 currentMenu->currentMenuOption->highlight( false ); 248 currentMenu->currentMenuOption->highlight( false );
@@ -273,50 +297,49 @@ int Menu :: handleKeys( SDL_KeyboardEvent &key )
273 297
274 break; 298 break;
275 } 299 }
276 case SDLK_LEFT: 300 case SDLK_LEFT:
277 if ( currentMenu->parentMenu != 0 ) 301 if ( currentMenu->parentMenu != 0 )
278 { 302 {
303 statusText = "";
279 currentMenu = currentMenu->parentMenu; 304 currentMenu = currentMenu->parentMenu;
280 printf( "HERE\n" );
281 305
282 return -1; 306 return -1;
283 } 307 }
284 break; 308 break;
285 309
286 case SDLK_RETURN: 310 case SDLK_RETURN:
287 case SDLK_SPACE: 311 case SDLK_SPACE:
288 { 312 {
313 statusText = "";
289 // select menu item 314 // select menu item
290 int id = currentMenu->currentMenuOption->getMenuId(); 315 int id = currentMenu->currentMenuOption->getMenuId();
291 // // if the current item has a child menu then move to that menu 316
317 // if the current item has a child menu then move to that menu
292 Menu *next = currentMenu->currentMenuOption->getNextMenu(); 318 Menu *next = currentMenu->currentMenuOption->getNextMenu();
293 if ( next != 0 ) 319 if ( next != 0 )
294 { 320 {
295 bool down = currentMenu->currentMenuOption->isDownMenuTree(); 321 bool down = currentMenu->currentMenuOption->isDownMenuTree();
296 currentMenu = next; 322 currentMenu = next;
297 if ( down ) 323 if ( down )
298 initCurrentMenu(); 324 initCurrentMenu();
299 // return -1;
300 }
301 // else
302 {
303 return id;
304 } 325 }
305 326
327 return id;
328
306 break; 329 break;
307 } 330 }
308 331
309 default: 332 default:
310 break; 333 break;
311 } 334 }
312 335
313 return -1; 336 return -1;
314} 337}
315 338
316MenuOption *Menu :: addMenuOption( QString text, int id ) 339MenuOption *Menu :: addMenuOption( string text, int id )
317{ 340{
318 MenuOption *item = new MenuOption( text, id ); 341 MenuOption *item = new MenuOption( text, id );
319 342
320 listItems.push_back( item ); 343 listItems.push_back( item );
321 344
322 return item; 345 return item;
diff --git a/noncore/games/sfcave-sdl/menu.h b/noncore/games/sfcave-sdl/menu.h
index 08f7528..6a5ef40 100644
--- a/noncore/games/sfcave-sdl/menu.h
+++ b/noncore/games/sfcave-sdl/menu.h
@@ -10,25 +10,25 @@ class SFCave;
10class StarField; 10class StarField;
11class Menu; 11class Menu;
12 12
13class MenuOption 13class MenuOption
14{ 14{
15public: 15public:
16 MenuOption( QString text, int id ); 16 MenuOption( string text, int id );
17 ~MenuOption(); 17 ~MenuOption();
18 18
19 void highlight( bool val ) { highlighted = val; } 19 void highlight( bool val ) { highlighted = val; }
20 int draw( SDL_Surface *screen, int y ); 20 int draw( SDL_Surface *screen, int y );
21 void setNextMenu( Menu *item, bool down = true ); 21 void setNextMenu( Menu *item, bool down = true );
22 Menu *getNextMenu() { return nextMenu; } 22 Menu *getNextMenu() { return nextMenu; }
23 int getMenuId() { return menuId; } 23 int getMenuId() { return menuId; }
24 bool isDownMenuTree() { return downMenuTree; } 24 bool isDownMenuTree() { return downMenuTree; }
25 25
26private: 26private:
27 int menuId; 27 int menuId;
28 QString menuText; 28 string menuText;
29 bool highlighted; 29 bool highlighted;
30 bool downMenuTree; 30 bool downMenuTree;
31 31
32 Menu *nextMenu; 32 Menu *nextMenu;
33}; 33};
34 34
@@ -37,17 +37,17 @@ class Menu
37public: 37public:
38 Menu( SFCave *p ); 38 Menu( SFCave *p );
39 ~Menu(); 39 ~Menu();
40 40
41 void draw( SDL_Surface *screen ); 41 void draw( SDL_Surface *screen );
42 int handleKeys( SDL_KeyboardEvent & ); 42 int handleKeys( SDL_KeyboardEvent & );
43 MenuOption *addMenuOption( QString text, int id ); 43 MenuOption *addMenuOption( string text, int id );
44 void resetToTopMenu(); 44 void resetToTopMenu();
45 void initCurrentMenu(); 45 void initCurrentMenu();
46 46
47 void setStatusText( QString text ) { statusText = text; } 47 void setStatusText( string text ) { statusText = text; }
48 48
49protected: 49protected:
50 50
51private: 51private:
52 static SDL_Surface * sfcaveTextImage; 52 static SDL_Surface * sfcaveTextImage;
53 int angle; 53 int angle;
@@ -55,13 +55,13 @@ private:
55 static Menu *mainMenu; 55 static Menu *mainMenu;
56 static Menu *currentMenu; 56 static Menu *currentMenu;
57 Menu *parentMenu; 57 Menu *parentMenu;
58 58
59 StarField *stars; 59 StarField *stars;
60 60
61 QString statusText; 61 string statusText;
62 62
63 SFCave *parent; 63 SFCave *parent;
64 list<MenuOption *> listItems; 64 list<MenuOption *> listItems;
65 MenuOption *currentMenuOption; 65 MenuOption *currentMenuOption;
66 66
67 Menu( Menu* p ); 67 Menu( Menu* p );
diff --git a/noncore/games/sfcave-sdl/player.cpp b/noncore/games/sfcave-sdl/player.cpp
index 830ee78..2d52ae2 100644
--- a/noncore/games/sfcave-sdl/player.cpp
+++ b/noncore/games/sfcave-sdl/player.cpp
@@ -8,14 +8,14 @@
8 8
9Player :: Player( int w, int h ) 9Player :: Player( int w, int h )
10{ 10{
11 sWidth = w; 11 sWidth = w;
12 sHeight = h; 12 sHeight = h;
13 13
14 thrustUp = 0.4; 14 thrust = 0.4;
15 thrustDown = 0.6; 15 gravity = 0.6;
16 maxUpSpeed = 4.0; 16 maxUpSpeed = 4.0;
17 maxDownSpeed = 5.0; 17 maxDownSpeed = 5.0;
18 18
19 explosion = new AnimatedImage( IMAGES_PATH "explosion.bmp", 15 ); 19 explosion = new AnimatedImage( IMAGES_PATH "explosion.bmp", 15 );
20 init(); 20 init();
21} 21}
@@ -77,37 +77,34 @@ void Player :: drawTrails( SDL_Surface *screen )
77 return; 77 return;
78 78
79 for ( int i = 0 ; i < TRAILSIZE ; ++i ) 79 for ( int i = 0 ; i < TRAILSIZE ; ++i )
80 { 80 {
81 if ( trail[i].x() >= 0 ) 81 if ( trail[i].x() >= 0 )
82 { 82 {
83 // int r = (int) ((255.0/pos.x()) * (trail[i].x));
84 // int g = (int) ((150.0/pos.x()) * (trail[i].x));
85 int c = (int)((150.0/50) * (50.0 - (pos.x() - trail[i].x() ) )); 83 int c = (int)((150.0/50) * (50.0 - (pos.x() - trail[i].x() ) ));
86 // SDL_FillRect( screen, &trail[i], SDL_MapRGBA( screen->format, r, g, 0, 0 ) ); //(int)(1.5*c), 0, 255 ) );
87 boxRGBA( screen, trail[i].x(), trail[i].y(), trail[i].x() + 2, trail[i].y() + 2, 255, (int)(1.5*c), 0, c ); 84 boxRGBA( screen, trail[i].x(), trail[i].y(), trail[i].x() + 2, trail[i].y() + 2, 255, (int)(1.5*c), 0, c );
88 } 85 }
89 } 86 }
90} 87}
91 88
92void Player :: move( bool up ) 89void Player :: move( bool up )
93{ 90{
94 // Find enpty trail and move others 91 // Find enpty trail and move others
95 moveTrails(); 92 moveTrails();
96 93
97 if ( up ) 94 if ( up )
98 thrust -= thrustUp; 95 currentThrust -= thrust;
99 else 96 else
100 thrust += thrustDown; 97 currentThrust += gravity;
101 98
102 if ( thrust > maxDownSpeed ) 99 if ( currentThrust > maxDownSpeed )
103 thrust = maxDownSpeed; 100 currentThrust = maxDownSpeed;
104 else if ( thrust < -maxUpSpeed ) 101 else if ( currentThrust < -maxUpSpeed )
105 thrust = -maxUpSpeed; 102 currentThrust = -maxUpSpeed;
106 103
107 pos.moveBy( 0, (int)(thrust) ); 104 pos.moveBy( 0, (int)(currentThrust) );
108} 105}
109 106
110void Player :: moveTrails() 107void Player :: moveTrails()
111{ 108{
112 bool done = false; 109 bool done = false;
113 bool stillVisible = false; 110 bool stillVisible = false;
@@ -149,14 +146,139 @@ bool Player :: updateCrashing()
149 else 146 else
150 expNextFrame = true; 147 expNextFrame = true;
151 148
152 return crashed; 149 return crashed;
153} 150}
154 151
155void Player :: setMovementInfo( double up, double down, double maxUp, double maxDown ) 152void Player :: setMovementInfo( double up, double grav, double maxUp, double maxDown )
156{ 153{
157 thrustUp = up; 154 thrust = up;
158 thrustDown = down; 155 gravity = grav;
159 maxUpSpeed = maxUp; 156 maxUpSpeed = maxUp;
160 maxDownSpeed = maxDown; 157 maxDownSpeed = maxDown;
161} 158}
162 159
160
161void Player :: incValue( int valueType )
162{
163 switch( valueType )
164 {
165 case PLAYER_THRUST:
166 thrust += 0.1;
167 break;
168 case PLAYER_GRAVITY:
169 gravity += 0.1;
170 break;
171 case PLAYER_MAX_SPEED_UP:
172 maxUpSpeed += 0.1;
173 break;
174 case PLAYER_MAX_SPEED_DOWN:
175 maxDownSpeed += 0.1;
176 break;
177 }
178}
179
180void Player :: decValue( int valueType )
181{
182 switch( valueType )
183 {
184 case PLAYER_THRUST:
185 thrust -= 0.1;
186 break;
187 case PLAYER_GRAVITY:
188 gravity -= 0.1;
189 break;
190 case PLAYER_MAX_SPEED_UP:
191 maxUpSpeed -= 0.1;
192 break;
193 case PLAYER_MAX_SPEED_DOWN:
194 maxDownSpeed -= 0.1;
195 break;
196 }
197}
198
199void Player :: setValue( int valueType, double val )
200{
201 switch( valueType )
202 {
203 case PLAYER_THRUST:
204 thrust = val;
205 break;
206 case PLAYER_GRAVITY:
207 gravity = val;
208 break;
209 case PLAYER_MAX_SPEED_UP:
210 maxUpSpeed = val;
211 break;
212 case PLAYER_MAX_SPEED_DOWN:
213 maxDownSpeed = val;
214 break;
215 }
216}
217
218double Player :: getValue( int valueType )
219{
220 double val;
221 switch( valueType )
222 {
223 case PLAYER_THRUST:
224 val = thrust;
225 break;
226 case PLAYER_GRAVITY:
227 val = gravity;
228 break;
229 case PLAYER_MAX_SPEED_UP:
230 val = maxUpSpeed;
231 break;
232 case PLAYER_MAX_SPEED_DOWN:
233 val = maxDownSpeed;
234 break;
235 }
236
237 return val;
238}
239
240string Player :: getValueTypeString( int valueType )
241{
242 string val;
243 switch( valueType )
244 {
245 case PLAYER_THRUST:
246 val = "thrust";
247 break;
248 case PLAYER_GRAVITY:
249 val = "gravity";
250 break;
251 case PLAYER_MAX_SPEED_UP:
252 val = "maxupspeed";
253 break;
254 case PLAYER_MAX_SPEED_DOWN:
255 val = "maxdownspeed";
256 break;
257 }
258
259 return val;
260}
261
262string Player :: getValueString( int valueType )
263{
264 char val[50];
265 switch( valueType )
266 {
267 case PLAYER_THRUST:
268 sprintf( val, "Thrust - %lf", thrust );
269 break;
270 case PLAYER_GRAVITY:
271 sprintf( val, "Gravity - %lf", gravity );
272 break;
273 case PLAYER_MAX_SPEED_UP:
274 sprintf( val, "Max Speed Up - %lf", maxUpSpeed );
275 break;
276 case PLAYER_MAX_SPEED_DOWN:
277 sprintf( val, "Max Speed Down - %lf", maxDownSpeed );
278 break;
279 }
280
281 string ret = val;
282 return ret;
283}
284
diff --git a/noncore/games/sfcave-sdl/player.h b/noncore/games/sfcave-sdl/player.h
index e4c904a..595c25b 100644
--- a/noncore/games/sfcave-sdl/player.h
+++ b/noncore/games/sfcave-sdl/player.h
@@ -19,13 +19,19 @@ public:
19 void moveTrails(); 19 void moveTrails();
20 Rect getPos() { return pos; } 20 Rect getPos() { return pos; }
21 int getX() { return pos.x(); } 21 int getX() { return pos.x(); }
22 int getY() { return pos.y(); } 22 int getY() { return pos.y(); }
23 int getHeight() { return pos.h(); } 23 int getHeight() { return pos.h(); }
24 bool updateCrashing(); 24 bool updateCrashing();
25 void setMovementInfo( double up, double down, double maxUp, double maxDown ); 25 void setMovementInfo( double up, double grav, double maxUp, double maxDown );
26 void incValue( int valType );
27 void decValue( int valType );
28 double getValue( int valueType );
29 string getValueString( int valueType );
30 string getValueTypeString( int valueType );
31 void setValue( int valueType, double val );
26 32
27private: 33private:
28 AnimatedImage *explosion; 34 AnimatedImage *explosion;
29 35
30 int sWidth; 36 int sWidth;
31 int sHeight; 37 int sHeight;
@@ -33,16 +39,16 @@ private:
33 bool expNextFrame; 39 bool expNextFrame;
34 bool allFaded; 40 bool allFaded;
35 bool crashing; 41 bool crashing;
36 bool crashed; 42 bool crashed;
37 int crashLineLength; 43 int crashLineLength;
38 Rect pos; 44 Rect pos;
39 double thrust; 45 double currentThrust;
40 46
41 double thrustUp; 47 double thrust;
42 double thrustDown; 48 double gravity;
43 double maxUpSpeed; 49 double maxUpSpeed;
44 double maxDownSpeed; 50 double maxDownSpeed;
45 51
46 Rect trail[TRAILSIZE]; 52 Rect trail[TRAILSIZE];
47 53
48}; 54};
diff --git a/noncore/games/sfcave-sdl/settings.cpp b/noncore/games/sfcave-sdl/settings.cpp
index 914c4ec..20cce4f 100644
--- a/noncore/games/sfcave-sdl/settings.cpp
+++ b/noncore/games/sfcave-sdl/settings.cpp
@@ -2,32 +2,27 @@
2#include <stdlib.h> 2#include <stdlib.h>
3#include <sys/stat.h> 3#include <sys/stat.h>
4#include <vector> 4#include <vector>
5 5
6#include "settings.h" 6#include "settings.h"
7 7
8// Defined in util.h
9string getHomeDir();
8 10
9#define DEFAULT_DIR "." 11#define DEFAULT_DIR "."
10#define DEFAULT_FILE "Settings.cfg" 12#define DEFAULT_FILE "Settings.cfg"
11#define MAX_LINE_SIZE 2048 13#define MAX_LINE_SIZE 2048
12 14
13 15
14Settings::Settings( char * env_file, char * env_dir ) 16Settings::Settings( char * env_file, char * env_dir )
15{ 17{
16 // Store the correct environment directory 18 // Store the correct environment directory
17 if (env_dir == NULL) 19 if (env_dir == NULL)
18 { 20 {
19 char * homeDir = getenv( "HOME" );; 21 envFile = getHomeDir();
20 22 envFile.append("/");
21 if ( homeDir )
22 {
23 envFile.append(homeDir);
24 envFile.append("/");
25 }
26 else
27 printf( "Environment var HOME not set!\n" );
28 23
29 envFile.append(DEFAULT_DIR); 24 envFile.append(DEFAULT_DIR);
30 } 25 }
31 else 26 else
32 envFile.append(env_dir); 27 envFile.append(env_dir);
33 28
@@ -39,21 +34,14 @@ Settings::Settings( char * env_file, char * env_dir )
39 else 34 else
40 envFile.append(env_file); 35 envFile.append(env_file);
41} 36}
42 37
43Settings::Settings() 38Settings::Settings()
44{ 39{
45 char * homeDir = getenv("HOME"); 40 envFile = getHomeDir();
46 41 envFile.append("/");
47 if ( homeDir)
48 {
49 envFile.append(homeDir);
50 envFile.append("/");
51 }
52 else
53 printf( "Environment var HOME not set!\n" );
54 42
55 envFile.append(DEFAULT_DIR); 43 envFile.append(DEFAULT_DIR);
56 envFile.append("/"); 44 envFile.append("/");
57 45
58 envFile.append(DEFAULT_FILE); 46 envFile.append(DEFAULT_FILE);
59} 47}
@@ -107,12 +95,24 @@ bool Settings::readSetting(const string key_str,unsigned long& result)
107 return true; 95 return true;
108 } 96 }
109 else 97 else
110 return false; 98 return false;
111} 99}
112 100
101bool Settings::readSetting(const string key_str,double& result)
102{
103 string Buffer;
104 if (readSetting(key_str,Buffer))
105 {
106 result = atof( Buffer.c_str() );
107 return true;
108 }
109 else
110 return false;
111}
112
113bool Settings::readSetting(const string key_str,bool& result) 113bool Settings::readSetting(const string key_str,bool& result)
114{ 114{
115 string Buffer; 115 string Buffer;
116 if (readSetting(key_str,Buffer)) 116 if (readSetting(key_str,Buffer))
117 { 117 {
118 result = (Buffer == "true"); 118 result = (Buffer == "true");
@@ -156,12 +156,20 @@ bool Settings::readSetting(const string key_str,string& results)
156 156
157void Settings::writeSetting(const string key_str,const bool value) 157void Settings::writeSetting(const string key_str,const bool value)
158{ 158{
159 value ?writeSetting(key_str,string("true")) :writeSetting(key_str,string("false")); 159 value ?writeSetting(key_str,string("true")) :writeSetting(key_str,string("false"));
160} 160}
161 161
162void Settings::writeSetting(const string key_str,const double value)
163{
164 char Buffer[30];
165
166 sprintf(Buffer,"%lf",value);
167 writeSetting(key_str,string(Buffer));
168}
169
162void Settings::writeSetting(const string key_str,const int value) 170void Settings::writeSetting(const string key_str,const int value)
163{ 171{
164 char Buffer[30]; 172 char Buffer[30];
165 173
166 sprintf(Buffer,"%i",value); 174 sprintf(Buffer,"%i",value);
167 writeSetting(key_str,string(Buffer)); 175 writeSetting(key_str,string(Buffer));
@@ -192,47 +200,40 @@ void Settings::writeSetting(const string key_str,const unsigned long value)
192} 200}
193 201
194void Settings::writeSetting(const string key_str,const string value) 202void Settings::writeSetting(const string key_str,const string value)
195{ 203{
196 // This function will write a value for the key key_str. If the key_str 204 // This function will write a value for the key key_str. If the key_str
197 // already exists then it will be overwritten. 205 // already exists then it will be overwritten.
198
199 std::vector<string> FileEntries;
200 FILE *fd=NULL,*ftemp=NULL; 206 FILE *fd=NULL,*ftemp=NULL;
201 char * dir_str;
202 char * dir_ptr;
203 char buf[MAX_LINE_SIZE]; 207 char buf[MAX_LINE_SIZE];
204 char tempname[12];
205 208
206 dir_str = strdup(envFile.c_str()); 209 string tmp = getHomeDir() + "/tmpsfcave.dat";
207 printf( "dir = %s, file - %s\n", dir_str, envFile.c_str() ); 210
208 if (dir_str) 211 // if file exists we need to save contents
212 fd = fopen( envFile.c_str(), "r" );
213 ftemp = fopen( tmp.c_str(), "w" );
214 if ( fd != NULL && ftemp != NULL )
209 { 215 {
210 // remove file from the directory string 216 while (fgets(buf, MAX_LINE_SIZE-1, fd))
211 dir_ptr = strrchr(dir_str, (int)'/');
212 if (dir_ptr)
213 { 217 {
214 *dir_ptr = 0; 218 if ( strncmp( buf, key_str.c_str(), key_str.size() ) != 0 )
215 219 fprintf( ftemp, "%s", buf );
216 // make the directory path if it does not exist 220 }
217 // mkdir(dir_str, 777 ); 221 fclose(fd);
222 }
218 223
219 // if file exists we need to save contents 224 if ( ftemp != NULL )
220 if ((fd = fopen(envFile.c_str(), "r")) != NULL) 225 {
221 { 226 fprintf(ftemp, "%s\t%s\n", key_str.c_str(),value.c_str());
222 while (fgets(buf, MAX_LINE_SIZE-1, fd)) 227 fclose( ftemp );
223 FileEntries.push_back(string(buf));
224 fclose(fd);
225 }
226 228
227 char *home = getenv( "HOME" ); 229 remove(envFile.c_str());
228 string tmp; 230 rename( tmp.c_str(), envFile.c_str() );
229 if ( home ) 231 }
230 tmp = home + string( "/" ) + "tmpsfcave.dat"; 232/*
231 else 233 string tmp = getHomeDir() + "/tmpsfcave.dat";
232 tmp = "./tmpsfcave.dat";
233 strcpy(tempname,tmp.c_str() ); 234 strcpy(tempname,tmp.c_str() );
234 printf( "tmp - %s\n", tempname ); 235 printf( "tmp - %s\n", tempname );
235 if ((ftemp = fopen(tempname,"w")) != NULL) 236 if ((ftemp = fopen(tempname,"w")) != NULL)
236 { 237 {
237 char *key1,*key2; 238 char *key1,*key2;
238 char buff1[80],buff2[80]; 239 char buff1[80],buff2[80];
@@ -251,23 +252,20 @@ void Settings::writeSetting(const string key_str,const string value)
251 } 252 }
252 } 253 }
253 254
254 fprintf(ftemp, "%s\t%s\n", key_str.c_str(),value.c_str()); 255 fprintf(ftemp, "%s\t%s\n", key_str.c_str(),value.c_str());
255 fflush(ftemp); 256 fflush(ftemp);
256 fclose(ftemp); 257 fclose(ftemp);
257
258 remove(envFile.c_str());
259
260 rename( tempname, envFile.c_str() );
261 } 258 }
262 else 259 else
263 printf( "Can't open file %s\n", envFile.c_str() ); 260 printf( "Can't open file %s\n", envFile.c_str() );
264 } 261 }
265 262
266 delete dir_str; 263 delete dir_str;
267 } 264 }
265*/
268} 266}
269 267
270void Settings::deleteFile(void) 268void Settings::deleteFile(void)
271{ 269{
272 remove(envFile.c_str()); 270 remove(envFile.c_str());
273} 271}
diff --git a/noncore/games/sfcave-sdl/settings.h b/noncore/games/sfcave-sdl/settings.h
index 5e828ed..a3af999 100644
--- a/noncore/games/sfcave-sdl/settings.h
+++ b/noncore/games/sfcave-sdl/settings.h
@@ -29,23 +29,25 @@ public:
29 29
30 bool readSetting(const string key_str,string& results); 30 bool readSetting(const string key_str,string& results);
31 bool readSetting(const string key_str,int& result); 31 bool readSetting(const string key_str,int& result);
32 bool readSetting(const string key_str,unsigned int& result); 32 bool readSetting(const string key_str,unsigned int& result);
33 bool readSetting(const string key_str,long int& result); 33 bool readSetting(const string key_str,long int& result);
34 bool readSetting(const string key_str,unsigned long& result); 34 bool readSetting(const string key_str,unsigned long& result);
35 bool readSetting(const string key_str,double & result);
35 bool readSetting(const string key_str,bool& result); 36 bool readSetting(const string key_str,bool& result);
36 37
37 void writeSetting(const string key_str,const string value); 38 void writeSetting(const string key_str,const string value);
38 void writeSetting(const string key_str,const int value); 39 void writeSetting(const string key_str,const int value);
39 void writeSetting(const string key_str,const unsigned int result); 40 void writeSetting(const string key_str,const unsigned int result);
40 void writeSetting(const string key_str,const long int result); 41 void writeSetting(const string key_str,const long int result);
41 void writeSetting(const string key_str,const unsigned long result); 42 void writeSetting(const string key_str,const unsigned long result);
43 void writeSetting(const string key_str,const double value);
42 void writeSetting(const string key_str,const bool value); 44 void writeSetting(const string key_str,const bool value);
43 45
44 void deleteFile(void); 46 void deleteFile(void);
45 47
46private: 48private:
47 49
48 string envFile; 50 string envFile;
49}; 51};
50 52
51 53
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
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
35extern "C" 35extern "C"
36#endif 36#endif
37int main(int argc, char *argv[]) 37int main(int argc, char *argv[])
@@ -40,318 +40,293 @@ int main(int argc, char *argv[])
40 return 0; 40 return 0;
41} 41}
42 42
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()
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
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]);
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
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}
355 330
356void SFCave :: changeState( int s ) 331void SFCave :: changeState( int s )
357{ 332{
@@ -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,28 +455,77 @@ 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" );
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 }
516} 529}
517 530
518void SFCave :: setMenuStatusText( string statusText ) 531void SFCave :: setMenuStatusText( string statusText )
@@ -530,17 +543,91 @@ void SFCave :: saveSetting( string key, string val )
530void SFCave :: saveSetting( string key, int val ) 543void 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
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;
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
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}
diff --git a/noncore/games/sfcave-sdl/sfcave.h b/noncore/games/sfcave-sdl/sfcave.h
index 96c2334..c707919 100644
--- a/noncore/games/sfcave-sdl/sfcave.h
+++ b/noncore/games/sfcave-sdl/sfcave.h
@@ -12,13 +12,12 @@ class Help;
12class SFCave 12class SFCave
13{ 13{
14public: 14public:
15 SFCave( int argc, char *argv[] ); 15 SFCave( int argc, char *argv[] );
16 ~SFCave(); 16 ~SFCave();
17 17
18 void drawGameScreen();
19 void initSDL( int argc, char *argv[] ); 18 void initSDL( int argc, char *argv[] );
20 void mainEventLoop(); 19 void mainEventLoop();
21 20
22 void setCrashed( bool val ); 21 void setCrashed( bool val );
23 void changeState( int s ); 22 void changeState( int s );
24 int getState() { return state; } 23 int getState() { return state; }
@@ -27,24 +26,47 @@ public:
27 bool showFPS() { return showFps; } 26 bool showFPS() { return showFps; }
28 27
29 void setMenuStatusText( string statusText ); 28 void setMenuStatusText( string statusText );
30 29
31 void saveSetting( string key, string val ); 30 void saveSetting( string key, string val );
32 void saveSetting( string key, int val ); 31 void saveSetting( string key, int val );
32 void saveSetting( string key, long val );
33 void saveSetting( string key, double val );
33 string loadSetting( string key, string defaultVal = "" ); 34 string loadSetting( string key, string defaultVal = "" );
35 bool loadBoolSetting( string key, bool defaultVal);
36 int loadIntSetting( string key, int defaultVal );
37 double loadDoubleSetting( string key, double defaultVal );
38
34private: 39private:
35 SDL_Surface *screen; 40 SDL_Surface *screen;
41 bool setupOK;
36 42
37 Game *currentGame; 43 Game *currentGame;
38 Menu *menu; 44 Menu *menu;
39 Help *help; 45 Help *help;
40 int state; 46 int state;
41 int maxFPS;
42 int actualFPS;
43 bool showFps; 47 bool showFps;
44 string musicPath; 48 string musicPath;
45 string musicType; 49 string musicType;
50 bool finish;
51
52 bool limitFPS;
53 int maxFPS;
54 int actualFPS;
55 int FPS;
56 long time1;
57 long start;
58 long end;
59
60 // This is used when the user is setting the custom
61 // values in the menu
62 int customPlayerMenuVal;
63 double origValue;
46 64
47 void handleMenuSelect( int menuId ); 65 void handleMenuSelect( int menuId );
66 void handleGameState();
67 void handleEvents();
68 void calcFPS();
69 void FPSDelay();
48}; 70};
49 71
50#endif 72#endif
diff --git a/noncore/games/sfcave-sdl/sfcave_game.cpp b/noncore/games/sfcave-sdl/sfcave_game.cpp
index 72c5ce3..1b00e14 100644
--- a/noncore/games/sfcave-sdl/sfcave_game.cpp
+++ b/noncore/games/sfcave-sdl/sfcave_game.cpp
@@ -36,12 +36,25 @@ void SFCaveGame :: init()
36 case MENU_DIFFICULTY_NORMAL: 36 case MENU_DIFFICULTY_NORMAL:
37 blockDistance = 40; 37 blockDistance = 40;
38 break; 38 break;
39 case MENU_DIFFICULTY_HARD: 39 case MENU_DIFFICULTY_HARD:
40 blockDistance = 30; 40 blockDistance = 30;
41 break; 41 break;
42 case MENU_DIFFICULTY_CUSTOM:
43 {
44 // Read custom difficulty settings for this game
45 blockDistance = parent->loadIntSetting( "SFCave_custom_blockdistance", 50 );
46
47 double thrust = parent->loadDoubleSetting( "SFCave_custom_player_thrust", 0.4 );
48 double gravity = parent->loadDoubleSetting( "SFCave_custom_player_gravity", 0.6 );
49 double maxUp = parent->loadDoubleSetting( "SFCave_custom_player_maxupspeed", 4.0 );
50 double maxDown = parent->loadDoubleSetting( "SFCave_custom_player_maxdownspeed", 5.0 );
51 player->setMovementInfo( thrust, gravity, maxUp, maxDown );
52
53 break;
54 }
42 } 55 }
43 56
44 for ( int i = 0 ; i < BLOCKSIZE ; ++i ) 57 for ( int i = 0 ; i < BLOCKSIZE ; ++i )
45 blocks[i].y( -1 ); 58 blocks[i].y( -1 );
46} 59}
47 60
@@ -65,13 +78,12 @@ void SFCaveGame :: update( int state )
65 blockHeight -= 5; 78 blockHeight -= 5;
66 } 79 }
67 } 80 }
68 81
69 if ( checkCollisions() ) 82 if ( checkCollisions() )
70 { 83 {
71 // printf( "Crashed!\n" );
72 parent->changeState( STATE_CRASHING ); 84 parent->changeState( STATE_CRASHING );
73 return; 85 return;
74 } 86 }
75 87
76 if ( nrFrames % blockDistance == 0 ) 88 if ( nrFrames % blockDistance == 0 )
77 addBlock(); 89 addBlock();
diff --git a/noncore/games/sfcave-sdl/sound.cpp b/noncore/games/sfcave-sdl/sound.cpp
index 5fda859..855f2e6 100644
--- a/noncore/games/sfcave-sdl/sound.cpp
+++ b/noncore/games/sfcave-sdl/sound.cpp
@@ -28,34 +28,37 @@ bool SoundHandler :: init( )
28 sounds[SND_EXPLOSION] = Mix_LoadWAV( SOUND_PATH "explosion.wav"); 28 sounds[SND_EXPLOSION] = Mix_LoadWAV( SOUND_PATH "explosion.wav");
29 sounds[SND_THRUST] = Mix_LoadWAV( SOUND_PATH "thrust.wav"); 29 sounds[SND_THRUST] = Mix_LoadWAV( SOUND_PATH "thrust.wav");
30 30
31 music = 0; 31 music = 0;
32 32
33 soundOn = true; 33 soundOn = true;
34 musicOn = true;
34 35
35 return true; 36 return true;
36} 37}
37 38
38void SoundHandler :: cleanUp() 39void SoundHandler :: cleanUp()
39{ 40{
40 // Free audio sounds 41 // Free audio sounds
41 Mix_FreeChunk( sounds[SND_EXPLOSION] ); 42 if ( sounds[SND_EXPLOSION] )
42 Mix_FreeChunk( sounds[SND_THRUST] ); 43 Mix_FreeChunk( sounds[SND_EXPLOSION] );
44 if ( sounds[SND_THRUST] )
45 Mix_FreeChunk( sounds[SND_THRUST] );
43 46
44 if ( music ) 47 if ( music )
45 Mix_FreeMusic( music ); 48 Mix_FreeMusic( music );
46 49
47 Mix_CloseAudio(); 50 Mix_CloseAudio();
48} 51}
49 52
50int SoundHandler :: playSound( int soundNr, int channel, int nrLoops, int playBeforeFinished ) 53int SoundHandler :: playSound( int soundNr, int channel, int nrLoops, int playBeforeFinished )
51{ 54{
52 if ( !soundOn ) 55 if ( !soundOn )
53 return -1; 56 return -1;
54 57
55 if ( soundNr >= NR_SOUNDS ) 58 if ( soundNr >= NR_SOUNDS || !sounds[soundNr] )
56 return -1; 59 return -1;
57 60
58 Mix_Chunk *chunk = sounds[soundNr]; 61 Mix_Chunk *chunk = sounds[soundNr];
59 if( channel == -1 || !Mix_Playing( channel ) ) 62 if( channel == -1 || !Mix_Playing( channel ) )
60 channel = Mix_PlayChannel(-1, sounds[soundNr], nrLoops); 63 channel = Mix_PlayChannel(-1, sounds[soundNr], nrLoops);
61 64
@@ -98,20 +101,20 @@ void SoundHandler :: playMusic( string musicFile )
98 101
99 playMusic(); 102 playMusic();
100} 103}
101 104
102void SoundHandler :: playMusic( bool fade ) 105void SoundHandler :: playMusic( bool fade )
103{ 106{
104 if ( !soundOn ) 107 if ( !musicOn )
105 return; 108 return;
106 109
107 if ( music ) 110 if ( music )
108 { 111 {
109 Mix_VolumeMusic( MIX_MAX_VOLUME ); 112 Mix_VolumeMusic( MIX_MAX_VOLUME );
110 Mix_RewindMusic(); 113 Mix_RewindMusic();
111 114
112 if ( fade ) 115 if ( fade )
113 Mix_FadeInMusic( music, -1, 1000 ); 116 Mix_FadeInMusic( music, -1, 1000 );
114 else 117 else
115 Mix_PlayMusic( music, -1 ); 118 Mix_PlayMusic( music, -1 );
116 119
117 } 120 }
@@ -143,12 +146,12 @@ void SoundHandler :: setSoundsOn( bool val )
143 soundOn = val; 146 soundOn = val;
144} 147}
145 148
146void SoundHandler :: setMusicOn( bool val ) 149void SoundHandler :: setMusicOn( bool val )
147{ 150{
148 musicOn = val; 151 musicOn = val;
149 152
150 if ( !musicOn ) 153 if ( !musicOn )
151 stopMusic(); 154 stopMusic();
152 else 155 else
153 playMusic( true ); 156 playMusic( true );
154} 157}
diff --git a/noncore/games/sfcave-sdl/starfield.cpp b/noncore/games/sfcave-sdl/starfield.cpp
index c1f2d73..82edfc1 100644
--- a/noncore/games/sfcave-sdl/starfield.cpp
+++ b/noncore/games/sfcave-sdl/starfield.cpp
@@ -158,13 +158,13 @@ void StarField :: move( )
158 158
159 159
160 160
161 //Move Star 161 //Move Star
162 pos_x[i] += vel_x[i]; 162 pos_x[i] += vel_x[i];
163 pos_y[i] += vel_y[i]; 163 pos_y[i] += vel_y[i];
164 164
165 if (pos_x[i] < 0) 165 if (pos_x[i] < 0)
166 pos_x[i] = pos_x[i] + 240; 166 pos_x[i] = pos_x[i] + 240;
167 167
168 if (pos_x[i] > 240) 168 if (pos_x[i] > 240)
169 pos_x[i] = pos_x[i] - 240; 169 pos_x[i] = pos_x[i] - 240;
170 if (pos_y[i] < 0) 170 if (pos_y[i] < 0)
@@ -218,6 +218,78 @@ void StarField :: draw( SDL_Surface *screen, int w, int h )
218 218
219 //*** NOTE : if the velocity of the stars never changes then the values such as 'pos_x[i] + vel_x[i]' could be precalculated for each star *** 219 //*** NOTE : if the velocity of the stars never changes then the values such as 'pos_x[i] + vel_x[i]' could be precalculated for each star ***
220 } 220 }
221 SDL_UnlockSurface( screen ); 221 SDL_UnlockSurface( screen );
222 } 222 }
223} 223}
224
225
226
227// Test
228#ifdef DEBUG_STARS
229SDL_Surface *screen;
230StarField *stars;
231
232void go()
233{
234 /* Initialize SDL */
235 if ( SDL_Init(SDL_INIT_VIDEO) < 0 )
236 {
237 fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
238 exit(1);
239 }
240 atexit(SDL_Quit);
241
242 int videoflags = SDL_SWSURFACE ;
243
244 if ( (screen=SDL_SetVideoMode(240, 320,32,videoflags)) == NULL )
245 {
246 fprintf(stderr, "Couldn't set %ix%i video mode: %s\n",240,320,SDL_GetError());
247 exit(2);
248 }
249
250 stars = new StarField( false, 200 );
251
252 bool done = false;
253 while ( !done )
254 {
255 SDL_FillRect( screen, 0, 0 );
256 stars->draw( screen );
257 stars->move( );
258
259 SDL_Flip( screen );
260
261 SDL_Delay( 10 );
262
263 SDL_Event event;
264 while ( SDL_PollEvent(&event) )
265 {
266 switch (event.type)
267 {
268 case SDL_KEYDOWN:
269 // Escape keypress quits the app
270 if ( event.key.keysym.sym != SDLK_ESCAPE )
271 {
272 break;
273 }
274 case SDL_QUIT:
275 done = 1;
276 break;
277 default:
278 break;
279 }
280 }
281 }
282 }
283
284
285
286
287#ifdef __cplusplus
288extern "C"
289#endif
290int main( int argc, char *argv[] )
291{
292 go();
293}
294
295#endif
diff --git a/noncore/games/sfcave-sdl/terrain.cpp b/noncore/games/sfcave-sdl/terrain.cpp
index c001a56..b243f45 100644
--- a/noncore/games/sfcave-sdl/terrain.cpp
+++ b/noncore/games/sfcave-sdl/terrain.cpp
@@ -44,12 +44,13 @@ void Terrain :: initTerrain()
44 mapTop[0] = (int)(nextInt(50)) + 5; 44 mapTop[0] = (int)(nextInt(50)) + 5;
45 mapBottom[0] = sHeight - (maxHeight - mapTop[0]); 45 mapBottom[0] = sHeight - (maxHeight - mapTop[0]);
46 for ( int i = 1 ; i < MAPSIZE ; ++i ) 46 for ( int i = 1 ; i < MAPSIZE ; ++i )
47 setPoint( i ); 47 setPoint( i );
48 48
49 SDL_FillRect( terrainSurface, 0, 0 ); 49 SDL_FillRect( terrainSurface, 0, 0 );
50
50 // Draw Terrain into surface 51 // Draw Terrain into surface
51 Sint16 px[5]; 52 Sint16 px[5];
52 Sint16 py[5]; 53 Sint16 py[5];
53 54
54 for ( int i = 0 ; i < MAPSIZE ; ++i ) 55 for ( int i = 0 ; i < MAPSIZE ; ++i )
55 { 56 {
@@ -88,13 +89,12 @@ void Terrain :: moveTerrain( int amountToMove )
88{ 89{
89 stars->move(); 90 stars->move();
90 91
91 offset += amountToMove; 92 offset += amountToMove;
92 speed = offset/segSize; 93 speed = offset/segSize;
93 94
94 //printf( "offset - %d, speed - %d\n", offset, speed );
95 if ( offset >= segSize ) 95 if ( offset >= segSize )
96 { 96 {
97 for ( int i = 0 ; i < (MAPSIZE)-speed ; ++i ) 97 for ( int i = 0 ; i < (MAPSIZE)-speed ; ++i )
98 { 98 {
99 mapTop[i] = mapTop[i+speed]; 99 mapTop[i] = mapTop[i+speed];
100 mapBottom[i] = mapBottom[i+speed]; 100 mapBottom[i] = mapBottom[i+speed];
@@ -192,43 +192,27 @@ void Terrain :: drawTerrain( SDL_Surface *screen )
192 192
193 SDL_Rect dst; 193 SDL_Rect dst;
194 dst.x = offset; 194 dst.x = offset;
195 dst.y = 0; 195 dst.y = 0;
196 dst.w = sWidth; 196 dst.w = sWidth;
197 dst.h = sHeight; 197 dst.h = sHeight;
198 //dst.h = maxHeight;
199 198
200 SDL_Rect dst2; 199 SDL_Rect dst2;
201 dst2.x = 0; 200 dst2.x = 0;
202 dst2.y = 0; 201 dst2.y = 0;
203 202
204 SDL_BlitSurface(terrainSurface, &dst, screen, &dst2 ); 203 SDL_BlitSurface(terrainSurface, &dst, screen, &dst2 );
205 204
206 stars->draw( screen ); 205 stars->draw( screen );
207
208 //dst.y = sHeight - maxHeight;
209 //dst2.y = sHeight - maxHeight;
210 //SDL_BlitSurface(terrainSurface, &dst, screen, &dst2 );
211
212/*
213 for ( int i = 0 ; i < MAPSIZE ; ++i )
214 {
215 int x1 = (i*segSize) - (offset*speed);
216 int x2 = ((i+1)*segSize)-(offset*speed);
217 if ( x2 >= sWidth )
218 x2 = sWidth-1;
219 aalineRGBA( screen, x1, mapTop[i], x2, mapTop[i+1], 0, 220, 0, 255 );
220 aalineRGBA( screen, x1, mapBottom[i], x2, mapBottom[i+1], 0, 220, 0, 255 );
221 }
222*/
223} 206}
224 207
225bool Terrain :: checkCollision( int x, int y, int h ) 208bool Terrain :: checkCollision( int x, int y, int h )
226{ 209{
227 if ( y < 0 || y > sHeight ) 210 if ( y < 0 || y > sHeight )
228 return true; 211 return true;
212
229 // First get segment that matches x 213 // First get segment that matches x
230 SDL_LockSurface( terrainSurface ); 214 SDL_LockSurface( terrainSurface );
231 215
232 Uint32 c = getpixel( terrainSurface, x, y ); 216 Uint32 c = getpixel( terrainSurface, x, y );
233 217
234 SDL_UnlockSurface( terrainSurface ); 218 SDL_UnlockSurface( terrainSurface );
@@ -244,13 +228,13 @@ bool Terrain :: checkCollision( int x, int y, int h )
244#ifdef DEBUG_TERRAIN 228#ifdef DEBUG_TERRAIN
245SDL_Surface *screen; 229SDL_Surface *screen;
246Terrain *terrain; 230Terrain *terrain;
247 231
248void go() 232void go()
249{ 233{
250 /* Initialize SDL */ 234 // Initialize SDL
251 if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) 235 if ( SDL_Init(SDL_INIT_VIDEO) < 0 )
252 { 236 {
253 fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError()); 237 fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
254 exit(1); 238 exit(1);
255 } 239 }
256 atexit(SDL_Quit); 240 atexit(SDL_Quit);
diff --git a/noncore/games/sfcave-sdl/util.cpp b/noncore/games/sfcave-sdl/util.cpp
index 86738ad..f73e256 100644
--- a/noncore/games/sfcave-sdl/util.cpp
+++ b/noncore/games/sfcave-sdl/util.cpp
@@ -32,13 +32,13 @@ Uint32 getpixel(SDL_Surface *surface, int x, int y)
32 32
33 default: 33 default:
34 return 0; /* shouldn't happen, but avoids warnings */ 34 return 0; /* shouldn't happen, but avoids warnings */
35 } 35 }
36} 36}
37 37
38const char *chooseRandomFile( string path, string fileType ) 38string chooseRandomFile( string path, string fileType )
39{ 39{
40 vector<string> files; 40 vector<string> files;
41 DIR *d = opendir( path.c_str() ); 41 DIR *d = opendir( path.c_str() );
42 if ( !d ) 42 if ( !d )
43 return ""; 43 return "";
44 44
@@ -47,18 +47,29 @@ const char *chooseRandomFile( string path, string fileType )
47 { 47 {
48 string file = string( path ) + item->d_name; 48 string file = string( path ) + item->d_name;
49 49
50 // Rip extension from file 50 // Rip extension from file
51 int pos = file.find( ".", 1 ) + 1; 51 int pos = file.find( ".", 1 ) + 1;
52 string tmp = file.substr( pos ); 52 string tmp = file.substr( pos );
53 printf( "pos = %d, tmp =%s\n", pos, tmp.c_str() );
54 if ( tmp.size() > 0 && fileType.find( tmp ) != -1 ) 53 if ( tmp.size() > 0 && fileType.find( tmp ) != -1 )
55 { 54 {
56 printf( "Matching <%s> - %s with <%s>\n", file.substr( pos ).c_str(), file.c_str(), fileType.c_str() );
57 files.push_back( file ); 55 files.push_back( file );
58 } 56 }
59 item = readdir( d ); 57 item = readdir( d );
60 } 58 }
61 59
62 closedir( d ); 60 closedir( d );
63 return files[nextInt( files.size() )].c_str(); 61 return files[nextInt( files.size() )];
62}
63
64
65string getHomeDir()
66{
67 string home;
68#ifdef QWS
69 home = getenv( "HOME" );
70#else
71 home = ".";
72#endif
73
74 return home;
64} 75}
diff --git a/noncore/games/sfcave-sdl/util.h b/noncore/games/sfcave-sdl/util.h
index fe3e9c0..e3aa31a 100644
--- a/noncore/games/sfcave-sdl/util.h
+++ b/noncore/games/sfcave-sdl/util.h
@@ -2,9 +2,9 @@
2#define __UTIL_H 2#define __UTIL_H
3 3
4#include <string> 4#include <string>
5using namespace std; 5using namespace std;
6 6
7Uint32 getpixel(SDL_Surface *surface, int x, int y); 7Uint32 getpixel(SDL_Surface *surface, int x, int y);
8const char *chooseRandomFile( string path, string fileType ); 8string chooseRandomFile( string path, string fileType );
9 9string getHomeDir();
10#endif 10#endif