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