summaryrefslogtreecommitdiff
Side-by-side diff
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
@@ -5,5 +5,5 @@
#include "animatedimage.h"
-AnimatedImage :: AnimatedImage( QString file, int nFrames )
+AnimatedImage :: AnimatedImage( string file, int nFrames )
{
nrFrames = nFrames;
@@ -20,7 +20,4 @@ AnimatedImage :: AnimatedImage( QString file, int nFrames )
SDL_SetColorKey(image, SDL_SRCCOLORKEY | SDL_RLEACCEL, SDL_MapRGB( image->format, 0, 0, 0 ) );
-// image = SDL_DisplayFormat( tmp );
-
-// SDL_FreeSurface( tmp );
frameWidth = image->w/nrFrames;
frameHeight = image->h;
@@ -48,4 +45,7 @@ bool AnimatedImage :: nextFrame()
void AnimatedImage :: draw( SDL_Surface *screen, int x, int y )
{
+ if ( !image )
+ return;
+
SDL_Rect dst;
dst.x = currentFrame * frameWidth;
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
@@ -7,5 +7,5 @@ class AnimatedImage
{
public:
- AnimatedImage( QString file, int nFrames );
+ AnimatedImage( string file, int nFrames );
~AnimatedImage();
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
@@ -74,8 +74,4 @@ void BFont::LoadFont (const char *filename)
}
}
- else
- {
- cerr << "Error! The font has not been loaded!" << endl;
- }
}
@@ -114,5 +110,4 @@ BFont * BFont :: SetFontColor(Uint8 r, Uint8 g, Uint8 b)
color_key = xGetPixel(Surface, 0, Surface->h-1);
- printf("looking...\n");
for( x=0; x < Surface->w; x++) {
for( y=0; y < Surface->h; y++) {
@@ -132,5 +127,5 @@ BFont * BFont :: SetFontColor(Uint8 r, Uint8 g, Uint8 b)
}
}
- printf("unlooking...\n");
+
if (SDL_MUSTLOCK(surface)) SDL_UnlockSurface(surface);
if (SDL_MUSTLOCK(Surface)) SDL_UnlockSurface(Surface);
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
@@ -4,5 +4,4 @@
#include <string>
using namespace std;
-#define QString string
#ifdef QWS
@@ -56,12 +55,30 @@ using namespace std;
#define MENU_DIFFICULTY_NORMAL 17
#define MENU_DIFFICULTY_HARD 18
-#define MENU_SOUNDS 19
-#define MENU_SOUND_ON 20
-#define MENU_SOUND_OFF 21
-#define MENU_MUSIC_ON 22
-#define MENU_MUSIC_OFF 23
+#define MENU_DIFFICULTY_HARD 18
+#define MENU_DIFFICULTY_CUSTOM 19
+#define MENU_SOUNDS 20
+#define MENU_SOUND_ON 21
+#define MENU_SOUND_OFF 22
+#define MENU_MUSIC_ON 23
+#define MENU_MUSIC_OFF 24
+#define MENU_CUSTOM_THRUST 25
+#define MENU_CUSTOM_GRAVITY 26
+#define MENU_CUSTOM_MAXSPEEDUP 27
+#define MENU_CUSTOM_MAXSPEEDDOWN 28
+#define MENU_CUSTOM_INCREASE 29
+#define MENU_CUSTOM_DECREASE 30
+#define MENU_CUSTOM_SAVE 31
+#define MENU_CUSTOM_CANCEL 32
// Sounds
#define SND_EXPLOSION 0
#define SND_THRUST 1
+#define INGAME_MUSIC SOUND_PATH "ingame.mod"
+
+// Constants for player values
+#define PLAYER_THRUST 0
+#define PLAYER_GRAVITY 1
+#define PLAYER_MAX_SPEED_UP 2
+#define PLAYER_MAX_SPEED_DOWN 3
+
#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
@@ -18,4 +18,5 @@ FlyGame :: FlyGame( SFCave *p, int w, int h, int diff )
FlyGame :: ~FlyGame()
{
+ // terrain and player get deleted by parent class
}
@@ -35,4 +36,13 @@ void FlyGame :: init()
player->setMovementInfo( 0.4, 0.6, 4, 5 );
break;
+ case MENU_DIFFICULTY_CUSTOM:
+ {
+ double thrust = parent->loadDoubleSetting( "Fly_custom_player_thrust", 0.3 );
+ double gravity = parent->loadDoubleSetting( "Fly_custom_player_gravity", 0.2 );
+ double maxUp = parent->loadDoubleSetting( "Fly_custom_player_maxupspeed", 1.5 );
+ double maxDown = parent->loadDoubleSetting( "Fly_custom_player_maxdownspeed", 1.5 );
+ player->setMovementInfo( thrust, gravity, maxUp, maxDown );
+ break;
+ }
}
@@ -51,5 +61,5 @@ void FlyGame :: update( int state )
int diff = terrain->getMapBottom( 10 ) - player->getY();
int tmpScore = ((FlyTerrain *)terrain)->getScore( 1, diff );
-// printf( "diff - %d score - %d\n", diff, tmpScore );
+
if ( !startScoring )
{
@@ -70,5 +80,4 @@ void FlyGame :: update( int state )
if ( checkCollisions() )
{
-// printf( "Crashed!\n" );
parent->changeState( STATE_CRASHING );
return;
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
@@ -9,5 +9,5 @@ BFont *FontHandler :: colouredFont;
BFont *FontHandler :: helpFont;
-void FontHandler :: init()
+bool FontHandler :: init()
{
// Load font images
@@ -18,13 +18,25 @@ void FontHandler :: init()
helpFont = new BFont( IMAGES_PATH "help_font.bmp" );
colouredFont = 0;
+
+ // Check if we are installed correctly (we need fonts to function)
+ if ( menuSelFont == 0 || menuUnSelFont == 0 || whiteFont == 0 || helpFont == 0 )
+ {
+ printf( "One or more fonts are not installed correctly\n" );
+ return false;
+ }
+
+ return true;
}
void FontHandler :: cleanUp()
{
- delete menuSelFont;
- delete menuUnSelFont;
- delete whiteFont;
- delete helpFont;
-
+ if ( menuSelFont )
+ delete menuSelFont;
+ if ( menuUnSelFont )
+ delete menuUnSelFont;
+ if ( whiteFont )
+ delete whiteFont;
+ if ( helpFont )
+ delete helpFont;
if ( colouredFont )
delete colouredFont;
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
@@ -14,5 +14,5 @@ class FontHandler
{
public:
- static void init();
+ static bool init();
static void cleanUp();
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
@@ -55,5 +55,5 @@ void Game :: init()
replayList.clear();
}
-
+
score = 0;
nrFrames = 0;
@@ -77,7 +77,4 @@ void Game :: handleKeys( SDL_KeyboardEvent &key )
replayList.push_back( nrFrames );
press = true;
-
-// if ( thrustChannel == -1 && parent->getState() == STATE_PLAYING )
-// thrustChannel = SoundHandler :: playSound( SND_THRUST, -1, -1, false );
}
else
@@ -87,9 +84,4 @@ void Game :: handleKeys( SDL_KeyboardEvent &key )
press = false;
- if ( thrustChannel != -1 )
- {
-// SoundHandler :: stopSound( thrustChannel, true, 300 );
-// thrustChannel = -1;
- }
}
}
@@ -98,7 +90,7 @@ void Game :: handleKeys( SDL_KeyboardEvent &key )
-QString Game :: getGameDifficultyText()
+string Game :: getGameDifficultyText()
{
- QString ret;
+ string ret;
if ( difficulty == MENU_DIFFICULTY_EASY )
@@ -108,4 +100,6 @@ QString Game :: getGameDifficultyText()
else if ( difficulty == MENU_DIFFICULTY_HARD )
ret = "Hard";
+ else if ( difficulty == MENU_DIFFICULTY_CUSTOM )
+ ret = "Custom";
return ret;
@@ -120,4 +114,14 @@ void Game :: setDifficulty( string diff )
else if ( diff == "Hard" )
difficulty = MENU_DIFFICULTY_HARD;
+ else if ( diff == "Custom" )
+ difficulty = MENU_DIFFICULTY_CUSTOM;
+
+ init();
+}
+
+void Game :: setDifficulty( int diff )
+{
+ difficulty = diff;
+ init();
}
@@ -132,5 +136,5 @@ void Game :: update( int state )
if ( state == STATE_PLAYING )
{
- if ( replay )
+ if ( replay )
{
while( replayIt != replayList.end() && (*replayIt) == nrFrames-1 )
@@ -167,12 +171,11 @@ void Game :: draw( SDL_Surface *screen )
{
char tmp[100];
- QString scoreText;
+ string scoreText;
sprintf( tmp, "Score: %06ld High Score: %06ld", score, highScore );
-// printf( "%s\n", (const char *)scoreText );
FontHandler::draw( screen, FONT_WHITE_TEXT, tmp, 3, 10 );
if ( parent->getState() == STATE_CRASHED )
{
- QString crashText;
+ string crashText;
crashText = "Game Over";
int x = (240 - FontHandler::TextWidth( FONT_WHITE_TEXT, (const char *)crashText.c_str() )) / 2;
@@ -224,5 +227,5 @@ void Game :: setSeed( int seed )
}
-void Game :: saveReplay( QString file )
+void Game :: saveReplay( string file )
{
FILE *out;
@@ -237,5 +240,5 @@ void Game :: saveReplay( QString file )
// Build up string of values
// Format is:: <landscape seed> <game type> <difficulty> <framenr> <framenr>.......
- QString val;
+ string val;
char tmp[20];
sprintf( tmp, "%d %d ", currentSeed, difficulty );
@@ -252,5 +255,5 @@ void Game :: saveReplay( QString file )
val += "\n";
- QString line;
+ string line;
sprintf( tmp, "%d\n", val.length() );
line = tmp;
@@ -260,10 +263,7 @@ void Game :: saveReplay( QString file )
fclose( out );
-
- printf( "Replay saved to %s\n", (const char *)file.c_str() );
-
}
-void Game :: loadReplay( QString file )
+void Game :: loadReplay( string file )
{
@@ -286,7 +286,6 @@ void Game :: loadReplay( QString file )
fread( data, 1, length, in );
-// printf( "data - %s", data );
- QString sep = " ";
+ string sep = " ";
StringTokenizer st( data, sep );
@@ -309,7 +308,4 @@ void Game :: loadReplay( QString file )
fclose( in );
-
- printf( "Replay loaded from %s\n", (const char *)file.c_str() );
-
}
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
@@ -26,8 +26,8 @@ public:
void handleKeys( SDL_KeyboardEvent &key );
- QString getGameName() { return gameName; }
+ string getGameName() { return gameName; }
int getDifficulty() { return difficulty; }
- QString getGameDifficultyText();
- void setDifficulty( int diff ) { difficulty = diff; }
+ string getGameDifficultyText();
+ void setDifficulty( int diff );
void setDifficulty( string diff );
@@ -40,13 +40,14 @@ public:
Terrain *getTerrain() { return terrain; }
+ Player *getPlayer() { return player; }
void setSeed( int seed );
- void loadReplay( QString file );
- void saveReplay( QString file );
+ void loadReplay( string file );
+ void saveReplay( string file );
static Game *createGame( SFCave *p, int w, int h, string game, string difficulty );
protected:
- QString gameName;
+ string gameName;
int thrustChannel;
@@ -70,10 +71,8 @@ protected:
int currentSeed;
-// QListIterator<int> *replayIt;
list<int> replayList;
list<int>::iterator replayIt;
-// QList<int> replayList;
bool replay;
- QString replayFile;
+ string replayFile;
private:
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
@@ -19,4 +19,5 @@ GatesGame :: GatesGame( SFCave *p, int w, int h, int diff )
GatesGame :: ~GatesGame()
{
+ // terrain and player get deleted by parent class
}
@@ -47,4 +48,17 @@ void GatesGame :: init()
player->setMovementInfo( 0.6, 0.8, 6, 7 );
break;
+ case MENU_DIFFICULTY_CUSTOM:
+ {
+ // Read custom difficulty settings for this game
+ gapHeight = parent->loadIntSetting( "Gates_custom_gapHeight", 75 );
+
+ double thrust = parent->loadDoubleSetting( "Gates_custom_player_thrust", 0.4 );
+ double gravity = parent->loadDoubleSetting( "Gates_custom_player_gravity", 0.6 );
+ double maxUp = parent->loadDoubleSetting( "Gates_custom_player_maxupspeed", 4.0 );
+ double maxDown = parent->loadDoubleSetting( "Gates_custom_player_maxdownspeed", 5.0 );
+ player->setMovementInfo( thrust, gravity, maxUp, maxDown );
+
+ break;
+ }
}
@@ -78,5 +92,4 @@ void GatesGame :: update( int state )
if ( checkCollisions() )
{
-// printf( "Crashed!\n" );
parent->changeState( STATE_CRASHING );
return;
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
@@ -36,5 +36,5 @@ void Help :: handleKeys( SDL_KeyboardEvent &key )
else textSpeed = 1;
}
-
+
}
else if ( key.type == SDL_KEYUP )
@@ -49,5 +49,5 @@ void Help :: init()
currLine = 0;
textSpeed = 1;
-
+
// Create our coloured font
FontHandler :: changeColor( FONT_HELP_FONT, 0, 0, 255 );
@@ -58,5 +58,4 @@ void Help :: draw( SDL_Surface *screen )
stars->draw( screen );
-
list<string>::iterator it = textList.begin();
@@ -76,5 +75,5 @@ void Help :: draw( SDL_Surface *screen )
it ++;
}
-
+
}
@@ -82,5 +81,5 @@ void Help :: update()
{
stars->move();
-
+
startPos -= textSpeed;
if ( startPos <= -FontHandler::FontHeight( FONT_COLOURED_TEXT ) )
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
@@ -9,5 +9,5 @@
#include "starfield.h"
-MenuOption :: MenuOption( QString text, int id )
+MenuOption :: MenuOption( string text, int id )
{
menuText = text;
@@ -116,4 +116,5 @@ Menu :: Menu( SFCave *p )
// Game Difficulty menu
+ MenuOption *customMenu = 0;
Menu *gameDifficulty = new Menu( options );
item = gameDifficulty->addMenuOption( "Easy", MENU_DIFFICULTY_EASY );
@@ -123,4 +124,5 @@ Menu :: Menu( SFCave *p )
item = gameDifficulty->addMenuOption( "Hard", MENU_DIFFICULTY_HARD );
item->setNextMenu( options, false );
+ customMenu = gameDifficulty->addMenuOption( "Custom", MENU_DIFFICULTY_CUSTOM );
item = gameDifficulty->addMenuOption( "Back", MENU_BACK );
item->setNextMenu( options, false );
@@ -136,4 +138,27 @@ Menu :: Menu( SFCave *p )
item->setNextMenu( options, false );
soundsMenu->setNextMenu( sounds );
+
+ // Custom Menu
+ Menu *custom = new Menu( gameDifficulty );
+ Menu *updown = new Menu( custom );
+ item = custom->addMenuOption( "Thrust", MENU_CUSTOM_THRUST );
+ item->setNextMenu( updown );
+ item = custom->addMenuOption( "Gravity", MENU_CUSTOM_GRAVITY );
+ item->setNextMenu( updown );
+ item = custom->addMenuOption( "Max Speed Up", MENU_CUSTOM_MAXSPEEDUP );
+ item->setNextMenu( updown );
+ item = custom->addMenuOption( "Max Speed Down", MENU_CUSTOM_MAXSPEEDDOWN );
+ item->setNextMenu( updown );
+ item = custom->addMenuOption( "Back", MENU_BACK );
+ item->setNextMenu( gameDifficulty, false );
+ customMenu->setNextMenu( custom );
+
+ // Up down menu
+ item = updown->addMenuOption( "Increase", MENU_CUSTOM_INCREASE );
+ item = updown->addMenuOption( "Decrease", MENU_CUSTOM_DECREASE );
+ item = updown->addMenuOption( "Save", MENU_CUSTOM_SAVE );
+ item->setNextMenu( custom, false );
+ item = updown->addMenuOption( "Cancel", MENU_CUSTOM_CANCEL );
+ item->setNextMenu( custom, false );
// Set static variables for menu selection up
@@ -216,5 +241,4 @@ int Menu :: handleKeys( SDL_KeyboardEvent &key )
return -1;
- statusText = "";
switch( key.keysym.sym )
{
@@ -277,6 +301,6 @@ int Menu :: handleKeys( SDL_KeyboardEvent &key )
if ( currentMenu->parentMenu != 0 )
{
+ statusText = "";
currentMenu = currentMenu->parentMenu;
- printf( "HERE\n" );
return -1;
@@ -287,7 +311,9 @@ int Menu :: handleKeys( SDL_KeyboardEvent &key )
case SDLK_SPACE:
{
+ statusText = "";
// select menu item
int id = currentMenu->currentMenuOption->getMenuId();
-// // if the current item has a child menu then move to that menu
+
+ // if the current item has a child menu then move to that menu
Menu *next = currentMenu->currentMenuOption->getNextMenu();
if ( next != 0 )
@@ -297,11 +323,8 @@ int Menu :: handleKeys( SDL_KeyboardEvent &key )
if ( down )
initCurrentMenu();
-// return -1;
- }
-// else
- {
- return id;
}
+ return id;
+
break;
}
@@ -314,5 +337,5 @@ int Menu :: handleKeys( SDL_KeyboardEvent &key )
}
-MenuOption *Menu :: addMenuOption( QString text, int id )
+MenuOption *Menu :: addMenuOption( string text, int id )
{
MenuOption *item = new MenuOption( text, id );
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
@@ -14,5 +14,5 @@ class MenuOption
{
public:
- MenuOption( QString text, int id );
+ MenuOption( string text, int id );
~MenuOption();
@@ -26,5 +26,5 @@ public:
private:
int menuId;
- QString menuText;
+ string menuText;
bool highlighted;
bool downMenuTree;
@@ -41,9 +41,9 @@ public:
void draw( SDL_Surface *screen );
int handleKeys( SDL_KeyboardEvent & );
- MenuOption *addMenuOption( QString text, int id );
+ MenuOption *addMenuOption( string text, int id );
void resetToTopMenu();
void initCurrentMenu();
- void setStatusText( QString text ) { statusText = text; }
+ void setStatusText( string text ) { statusText = text; }
protected:
@@ -59,5 +59,5 @@ private:
StarField *stars;
- QString statusText;
+ string statusText;
SFCave *parent;
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
@@ -12,6 +12,6 @@ Player :: Player( int w, int h )
sHeight = h;
- thrustUp = 0.4;
- thrustDown = 0.6;
+ thrust = 0.4;
+ gravity = 0.6;
maxUpSpeed = 4.0;
maxDownSpeed = 5.0;
@@ -81,8 +81,5 @@ void Player :: drawTrails( SDL_Surface *screen )
if ( trail[i].x() >= 0 )
{
-// int r = (int) ((255.0/pos.x()) * (trail[i].x));
-// int g = (int) ((150.0/pos.x()) * (trail[i].x));
int c = (int)((150.0/50) * (50.0 - (pos.x() - trail[i].x() ) ));
-// SDL_FillRect( screen, &trail[i], SDL_MapRGBA( screen->format, r, g, 0, 0 ) ); //(int)(1.5*c), 0, 255 ) );
boxRGBA( screen, trail[i].x(), trail[i].y(), trail[i].x() + 2, trail[i].y() + 2, 255, (int)(1.5*c), 0, c );
}
@@ -96,14 +93,14 @@ void Player :: move( bool up )
if ( up )
- thrust -= thrustUp;
+ currentThrust -= thrust;
else
- thrust += thrustDown;
+ currentThrust += gravity;
- if ( thrust > maxDownSpeed )
- thrust = maxDownSpeed;
- else if ( thrust < -maxUpSpeed )
- thrust = -maxUpSpeed;
+ if ( currentThrust > maxDownSpeed )
+ currentThrust = maxDownSpeed;
+ else if ( currentThrust < -maxUpSpeed )
+ currentThrust = -maxUpSpeed;
- pos.moveBy( 0, (int)(thrust) );
+ pos.moveBy( 0, (int)(currentThrust) );
}
@@ -153,10 +150,135 @@ bool Player :: updateCrashing()
}
-void Player :: setMovementInfo( double up, double down, double maxUp, double maxDown )
+void Player :: setMovementInfo( double up, double grav, double maxUp, double maxDown )
{
- thrustUp = up;
- thrustDown = down;
+ thrust = up;
+ gravity = grav;
maxUpSpeed = maxUp;
maxDownSpeed = maxDown;
}
+
+void Player :: incValue( int valueType )
+{
+ switch( valueType )
+ {
+ case PLAYER_THRUST:
+ thrust += 0.1;
+ break;
+ case PLAYER_GRAVITY:
+ gravity += 0.1;
+ break;
+ case PLAYER_MAX_SPEED_UP:
+ maxUpSpeed += 0.1;
+ break;
+ case PLAYER_MAX_SPEED_DOWN:
+ maxDownSpeed += 0.1;
+ break;
+ }
+}
+
+void Player :: decValue( int valueType )
+{
+ switch( valueType )
+ {
+ case PLAYER_THRUST:
+ thrust -= 0.1;
+ break;
+ case PLAYER_GRAVITY:
+ gravity -= 0.1;
+ break;
+ case PLAYER_MAX_SPEED_UP:
+ maxUpSpeed -= 0.1;
+ break;
+ case PLAYER_MAX_SPEED_DOWN:
+ maxDownSpeed -= 0.1;
+ break;
+ }
+}
+
+void Player :: setValue( int valueType, double val )
+{
+ switch( valueType )
+ {
+ case PLAYER_THRUST:
+ thrust = val;
+ break;
+ case PLAYER_GRAVITY:
+ gravity = val;
+ break;
+ case PLAYER_MAX_SPEED_UP:
+ maxUpSpeed = val;
+ break;
+ case PLAYER_MAX_SPEED_DOWN:
+ maxDownSpeed = val;
+ break;
+ }
+}
+
+double Player :: getValue( int valueType )
+{
+ double val;
+ switch( valueType )
+ {
+ case PLAYER_THRUST:
+ val = thrust;
+ break;
+ case PLAYER_GRAVITY:
+ val = gravity;
+ break;
+ case PLAYER_MAX_SPEED_UP:
+ val = maxUpSpeed;
+ break;
+ case PLAYER_MAX_SPEED_DOWN:
+ val = maxDownSpeed;
+ break;
+ }
+
+ return val;
+}
+
+string Player :: getValueTypeString( int valueType )
+{
+ string val;
+ switch( valueType )
+ {
+ case PLAYER_THRUST:
+ val = "thrust";
+ break;
+ case PLAYER_GRAVITY:
+ val = "gravity";
+ break;
+ case PLAYER_MAX_SPEED_UP:
+ val = "maxupspeed";
+ break;
+ case PLAYER_MAX_SPEED_DOWN:
+ val = "maxdownspeed";
+ break;
+ }
+
+ return val;
+}
+
+string Player :: getValueString( int valueType )
+{
+ char val[50];
+ switch( valueType )
+ {
+ case PLAYER_THRUST:
+ sprintf( val, "Thrust - %lf", thrust );
+ break;
+ case PLAYER_GRAVITY:
+ sprintf( val, "Gravity - %lf", gravity );
+ break;
+ case PLAYER_MAX_SPEED_UP:
+ sprintf( val, "Max Speed Up - %lf", maxUpSpeed );
+ break;
+ case PLAYER_MAX_SPEED_DOWN:
+ sprintf( val, "Max Speed Down - %lf", maxDownSpeed );
+ break;
+ }
+
+ string ret = val;
+ return ret;
+}
+
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
@@ -23,5 +23,11 @@ public:
int getHeight() { return pos.h(); }
bool updateCrashing();
- void setMovementInfo( double up, double down, double maxUp, double maxDown );
+ void setMovementInfo( double up, double grav, double maxUp, double maxDown );
+ void incValue( int valType );
+ void decValue( int valType );
+ double getValue( int valueType );
+ string getValueString( int valueType );
+ string getValueTypeString( int valueType );
+ void setValue( int valueType, double val );
private:
@@ -37,8 +43,8 @@ private:
int crashLineLength;
Rect pos;
- double thrust;
+ double currentThrust;
- double thrustUp;
- double thrustDown;
+ double thrust;
+ double gravity;
double maxUpSpeed;
double maxDownSpeed;
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
@@ -6,4 +6,6 @@
#include "settings.h"
+// Defined in util.h
+string getHomeDir();
#define DEFAULT_DIR "."
@@ -17,13 +19,6 @@ Settings::Settings( char * env_file, char * env_dir )
if (env_dir == NULL)
{
- char * homeDir = getenv( "HOME" );;
-
- if ( homeDir )
- {
- envFile.append(homeDir);
- envFile.append("/");
- }
- else
- printf( "Environment var HOME not set!\n" );
+ envFile = getHomeDir();
+ envFile.append("/");
envFile.append(DEFAULT_DIR);
@@ -43,13 +38,6 @@ Settings::Settings( char * env_file, char * env_dir )
Settings::Settings()
{
- char * homeDir = getenv("HOME");
-
- if ( homeDir)
- {
- envFile.append(homeDir);
- envFile.append("/");
- }
- else
- printf( "Environment var HOME not set!\n" );
+ envFile = getHomeDir();
+ envFile.append("/");
envFile.append(DEFAULT_DIR);
@@ -111,4 +99,16 @@ bool Settings::readSetting(const string key_str,unsigned long& result)
}
+bool Settings::readSetting(const string key_str,double& result)
+{
+ string Buffer;
+ if (readSetting(key_str,Buffer))
+ {
+ result = atof( Buffer.c_str() );
+ return true;
+ }
+ else
+ return false;
+}
+
bool Settings::readSetting(const string key_str,bool& result)
{
@@ -160,4 +160,12 @@ void Settings::writeSetting(const string key_str,const bool value)
}
+void Settings::writeSetting(const string key_str,const double value)
+{
+ char Buffer[30];
+
+ sprintf(Buffer,"%lf",value);
+ writeSetting(key_str,string(Buffer));
+}
+
void Settings::writeSetting(const string key_str,const int value)
{
@@ -196,39 +204,32 @@ void Settings::writeSetting(const string key_str,const string value)
// This function will write a value for the key key_str. If the key_str
// already exists then it will be overwritten.
-
- std::vector<string> FileEntries;
FILE *fd=NULL,*ftemp=NULL;
- char * dir_str;
- char * dir_ptr;
char buf[MAX_LINE_SIZE];
- char tempname[12];
- dir_str = strdup(envFile.c_str());
- printf( "dir = %s, file - %s\n", dir_str, envFile.c_str() );
- if (dir_str)
+ string tmp = getHomeDir() + "/tmpsfcave.dat";
+
+ // if file exists we need to save contents
+ fd = fopen( envFile.c_str(), "r" );
+ ftemp = fopen( tmp.c_str(), "w" );
+ if ( fd != NULL && ftemp != NULL )
{
- // remove file from the directory string
- dir_ptr = strrchr(dir_str, (int)'/');
- if (dir_ptr)
+ while (fgets(buf, MAX_LINE_SIZE-1, fd))
{
- *dir_ptr = 0;
-
- // make the directory path if it does not exist
-// mkdir(dir_str, 777 );
+ if ( strncmp( buf, key_str.c_str(), key_str.size() ) != 0 )
+ fprintf( ftemp, "%s", buf );
+ }
+ fclose(fd);
+ }
- // if file exists we need to save contents
- if ((fd = fopen(envFile.c_str(), "r")) != NULL)
- {
- while (fgets(buf, MAX_LINE_SIZE-1, fd))
- FileEntries.push_back(string(buf));
- fclose(fd);
- }
+ if ( ftemp != NULL )
+ {
+ fprintf(ftemp, "%s\t%s\n", key_str.c_str(),value.c_str());
+ fclose( ftemp );
- char *home = getenv( "HOME" );
- string tmp;
- if ( home )
- tmp = home + string( "/" ) + "tmpsfcave.dat";
- else
- tmp = "./tmpsfcave.dat";
+ remove(envFile.c_str());
+ rename( tmp.c_str(), envFile.c_str() );
+ }
+/*
+ string tmp = getHomeDir() + "/tmpsfcave.dat";
strcpy(tempname,tmp.c_str() );
printf( "tmp - %s\n", tempname );
@@ -255,8 +256,4 @@ void Settings::writeSetting(const string key_str,const string value)
fflush(ftemp);
fclose(ftemp);
-
- remove(envFile.c_str());
-
- rename( tempname, envFile.c_str() );
}
else
@@ -266,4 +263,5 @@ void Settings::writeSetting(const string key_str,const string value)
delete dir_str;
}
+*/
}
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
@@ -33,4 +33,5 @@ public:
bool readSetting(const string key_str,long int& result);
bool readSetting(const string key_str,unsigned long& result);
+ bool readSetting(const string key_str,double & result);
bool readSetting(const string key_str,bool& result);
@@ -40,8 +41,9 @@ public:
void writeSetting(const string key_str,const long int result);
void writeSetting(const string key_str,const unsigned long result);
+ void writeSetting(const string key_str,const double value);
void writeSetting(const string key_str,const bool value);
void deleteFile(void);
-
+
private:
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
@@ -27,7 +27,7 @@
void start( int argc, char *argv[] )
{
- FontHandler::init();
- SFCave app( argc, argv );
- FontHandler::cleanUp();
+ SFCave *app = new SFCave( argc, argv );
+ app->mainEventLoop();
+ delete app;
}
@@ -44,33 +44,49 @@ int main(int argc, char *argv[])
SFCave :: SFCave( int argc, char *argv[] )
{
+ setupOK = false;
+
+ // Load settings
string diff = loadSetting( "GameDifficulty", "Easy" );
string game = loadSetting( "GameType", "SFCave" );
musicPath = loadSetting( "MusicPath", SOUND_PATH );
- printf( "musicPath %s\n", musicPath.c_str() );
musicType = loadSetting( "MusicType", "mod,ogg" );
+ bool soundOn = loadBoolSetting( "SoundOn", true );
+ bool musicOn = loadBoolSetting( "MusicOn", true );
if ( musicPath[musicPath.size()-1] != '/' )
musicPath += "/";
+ printf( "musicPath %s\n", musicPath.c_str() );
// Init main SDL Library
initSDL( argc, argv );
+ // Init font handler
+ if ( !FontHandler::init() )
+ {
+ printf( "Unable to initialise fonts!\n" );
+ return;
+ }
+
// Init SoundHandler
if ( !SoundHandler :: init() )
printf("Unable to open audio!\n");
+ SoundHandler :: setSoundsOn( soundOn );
+ SoundHandler :: setMusicOn( musicOn );
+
currentGame = Game::createGame( this, WIDTH, HEIGHT, game, diff );
if ( !currentGame )
currentGame = new SFCaveGame( this, WIDTH, HEIGHT, 0 );
currentGame->setSeed(-1);
+
+ // Create menu
menu = new Menu( this );
-
+
+ // Create help screen
help = new Help( this );
maxFPS = 50;
showFps = false;
- mainEventLoop();
-
- SoundHandler :: cleanUp();
- SDL_Quit();
+
+ setupOK = true;
}
@@ -83,14 +99,15 @@ SFCave :: ~SFCave()
delete menu;
- SDL_FreeSurface( screen );
-}
-
+ if ( help )
+ delete help;
-void SFCave :: drawGameScreen( )
-{
- //ClearScreen(screen, "Titletext");
+ SDL_FreeSurface( screen );
+ FontHandler::cleanUp();
+ SoundHandler :: cleanUp();
+ SDL_Quit();
}
+
void SFCave :: initSDL( int argc, char *argv[] )
{
@@ -99,35 +116,38 @@ void SFCave :: initSDL( int argc, char *argv[] )
Uint32 videoflags;
-
-
- /* Initialize SDL */
+ // Initialize SDL
if ( SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0 ) {
fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
exit(1);
}
- atexit(SDL_Quit);
- /* Alpha blending doesn't work well at 8-bit color */
video_bpp = 16;
- if ( !SDL_VideoModeOK(WIDTH, HEIGHT, 16, SDL_DOUBLEBUF) )
+ if ( !SDL_VideoModeOK(WIDTH, HEIGHT, video_bpp, SDL_DOUBLEBUF) )
printf( "No double buffering\n" );
- videoflags = SDL_HWSURFACE | SDL_SRCALPHA;//|| SDL_DOUBLEBUF;// | SDL_SRCALPHA | SDL_RESIZABLE;
- while ( argc > 1 ) {
+ videoflags = SDL_HWSURFACE | SDL_SRCALPHA;
+ while ( argc > 1 )
+ {
--argc;
- if ( strcmp(argv[argc-1], "-bpp") == 0 ) {
+ if ( strcmp(argv[argc-1], "-bpp") == 0 )
+ {
video_bpp = atoi(argv[argc]);
--argc;
- } else
- if ( strcmp(argv[argc], "-hw") == 0 ) {
+ }
+ else if ( strcmp(argv[argc], "-hw") == 0 )
+ {
videoflags |= SDL_HWSURFACE;
- } else
- if ( strcmp(argv[argc], "-warp") == 0 ) {
+ }
+ else if ( strcmp(argv[argc], "-warp") == 0 )
+ {
videoflags |= SDL_HWPALETTE;
- } else
- if ( strcmp(argv[argc], "-fullscreen") == 0 ) {
+ }
+ else if ( strcmp(argv[argc], "-fullscreen") == 0 )
+ {
videoflags |= SDL_FULLSCREEN;
- } else {
+ }
+ else if ( strcmp(argv[argc], "-h") == 0 )
+ {
fprintf(stderr,
"Usage: %s [-bpp N] [-warp] [-hw] [-fullscreen]\n",
@@ -137,14 +157,15 @@ void SFCave :: initSDL( int argc, char *argv[] )
}
- /* Set 240x320 video mode */
- if ( (screen=SDL_SetVideoMode(WIDTH,HEIGHT,video_bpp,videoflags)) == NULL ) {
- fprintf(stderr, "Couldn't set %ix%i video mode: %s\n",WIDTH,HEIGHT,SDL_GetError());
+ // Set 240x320 video mode
+ if ( (screen = SDL_SetVideoMode( WIDTH,HEIGHT,video_bpp,videoflags )) == NULL )
+ {
+ printf( "Couldn't set %ix%i video mode: %s\n",WIDTH,HEIGHT,SDL_GetError() );
exit(2);
}
- /* Use alpha blending */
- SDL_SetAlpha(screen, SDL_RLEACCEL, 0);
+ // Use alpha blending
+ //SDL_SetAlpha(screen, SDL_RLEACCEL, 0);
- /* Set title for window */
+ // Set title for window
SDL_WM_SetCaption("SFCave","SFCave");
}
@@ -152,202 +173,156 @@ void SFCave :: initSDL( int argc, char *argv[] )
void SFCave :: mainEventLoop()
{
- SDL_Event event;
- int done;
-
- /* Wait for a keystroke */
- done = 0;
+ if ( !setupOK )
+ return;
+
+ // Wait for a keystroke
+ finish = false;
state = 0;
state = STATE_CRASHED;
changeState( STATE_MENU );
- int FPS = 0;
- bool limitFPS = true;
+ FPS = 0;
actualFPS = 0;
- long time1 = 0;
- long start;
- long end;
-// long nrTimes = 0;
- struct timeb tp;
- while ( !done )
+ time1 = 0;
+
+ limitFPS = true;
+ while ( !finish )
{
// calc FPS
- ftime( &tp );
- start =(tp.time%10000)*10000 + tp.millitm;
-// printf( "start = %ld, time1 - %d, st-tm - %d, tp.time - %ld\n", start, time1, start-time1, (tp.time%1000)*1000 );
- if ( start - time1 >= 1000 )
- {
- actualFPS = FPS;
-// printf( "%d FPS = %d\n", nrTimes++, actualFPS );
- FPS = 0;
- time1 = start;
- }
- else
- FPS ++;
+ calcFPS();
SDL_FillRect( screen, 0, 0 );
- switch( state )
- {
- case STATE_MENU:
- SDL_FillRect( screen, 0, 0 );
- menu->draw( screen );
- break;
- case STATE_HELP:
- SDL_FillRect( screen, 0, 0 );
- help->update();
- help->draw( screen );
- break;
- case STATE_NEWGAME:
- printf( "STATE_NEWGAME\n" );
- currentGame->setReplay( false );
- currentGame->init();
- changeState( STATE_PLAYING );
- break;
- case STATE_REPLAY:
- printf( "STATE_NEWGAME\n" );
- currentGame->setReplay( true );
- currentGame->init();
- changeState( STATE_PLAYING );
- break;
+ handleGameState( );
- case STATE_PLAYING:
- case STATE_CRASHING:
- currentGame->update( state );
- currentGame->draw( screen );
- break;
-
- case STATE_CRASHED:
- currentGame->update( state );
- currentGame->draw( screen );
-
- // Display Game Over message
- break;
-
- case STATE_QUIT:
- done = 1;
- break;
- }
-
- /* Show */
-// if ( state != STATE_CRASHED )
- SDL_Flip( screen );
-// SDL_UpdateRect(screen, 0, 0, 0, 0);
+ SDL_Flip( screen );
if ( limitFPS )
- {
- /* Slow down polling - limit to x FPS*/
- ftime( &tp );
- end = abs((tp.time%10000)*10000 + tp.millitm);
- if ( end-start < (1000/maxFPS) )
- {
-// printf( "end - %ld, timetaken for frame = %ld, sleeping for %ld %d\n", end, end-start, (1000/maxFPS)-(end-start), actualFPS );
- if ( (1000/maxFPS)-(end-start) > 500 )
- {
- // Should never happen but in case it does sleep for 5 seconds
- printf( "WARNING WILL ROBINSON! delay = %ld - start %ld, end %ld\n", (1000/maxFPS)-(end-start), start, end );
- SDL_Delay( 5 );
- }
- else
- SDL_Delay((1000/maxFPS)-(end-start) );
- }
- }
+ FPSDelay();
else
SDL_Delay( 5 );
- /* Check for events */
- while ( SDL_PollEvent(&event) )
+ handleEvents();
+ }
+}
+
+
+void SFCave :: handleGameState()
+{
+ switch( state )
+ {
+ case STATE_MENU:
+ SDL_FillRect( screen, 0, 0 );
+ menu->draw( screen );
+ break;
+ case STATE_HELP:
+ SDL_FillRect( screen, 0, 0 );
+ help->update();
+ help->draw( screen );
+ break;
+ case STATE_NEWGAME:
+ currentGame->setReplay( false );
+ currentGame->init();
+ changeState( STATE_PLAYING );
+ break;
+
+ case STATE_REPLAY:
+ currentGame->setReplay( true );
+ currentGame->init();
+ changeState( STATE_PLAYING );
+ break;
+
+ case STATE_PLAYING:
+ case STATE_CRASHING:
+ case STATE_CRASHED:
+ currentGame->update( state );
+ currentGame->draw( screen );
+ break;
+
+ case STATE_QUIT:
+ finish = true;
+ break;
+ }
+}
+
+void SFCave :: handleEvents()
+{
+ SDL_Event event;
+
+ // Check for events
+ while ( SDL_PollEvent(&event) )
+ {
+ switch (event.type)
{
- switch (event.type)
+ case SDL_KEYDOWN:
+ case SDL_KEYUP:
{
- case SDL_KEYDOWN:
- case SDL_KEYUP:
- // Escape keypress quits the app
- if ( event.key.keysym.sym != SDLK_ESCAPE )
+ // Escape keypress quits the app
+ if ( event.key.keysym.sym == SDLK_ESCAPE )
+ {
+ finish = true;
+ break;
+ }
+
+ if ( state == STATE_MENU )
+ {
+ int rc = menu->handleKeys( event.key );
+ if ( rc != -1 )
+ handleMenuSelect( rc );
+ }
+ else if ( state == STATE_HELP )
+ {
+ help->handleKeys( event.key );
+ }
+ else if ( state == STATE_CRASHED )
+ {
+ if ( event.type == SDL_KEYDOWN )
{
-// printf( "Key Pressed was %d %s\n", event.key.keysym.sym, SDL_GetKeyName( event.key.keysym.sym ) );
-
- if ( state == STATE_MENU )
+ if ( event.key.keysym.sym == SDLK_UP ||
+ event.key.keysym.sym == SDLK_DOWN ||
+ event.key.keysym.sym == SDLK_SPACE )
+ changeState( STATE_NEWGAME );
+ else if ( event.key.keysym.sym == SDLK_RETURN || event.key.keysym.sym == 0 )
{
- int rc = menu->handleKeys( event.key );
- if ( rc != -1 )
- handleMenuSelect( rc );
+ changeState( STATE_MENU );
+ menu->resetToTopMenu();
}
- else if ( state == STATE_HELP )
+ else if ( event.key.keysym.sym == SDLK_r )
{
- help->handleKeys( event.key );
+ changeState( STATE_REPLAY );
}
- else if ( state == STATE_CRASHED )
+ else if ( event.key.keysym.sym == SDLK_s )
{
- if ( event.type == SDL_KEYDOWN )
- {
- if ( event.key.keysym.sym == SDLK_UP ||
- event.key.keysym.sym == SDLK_DOWN ||
- event.key.keysym.sym == SDLK_SPACE )
- changeState( STATE_NEWGAME );
- else if ( event.key.keysym.sym == SDLK_RETURN || event.key.keysym.sym == 0 )
- {
- changeState( STATE_MENU );
- menu->resetToTopMenu();
- }
- else if ( event.key.keysym.sym == SDLK_r )
- {
- changeState( STATE_REPLAY );
- }
- else if ( event.key.keysym.sym == SDLK_s )
- {
- SoundHandler :: playSound( SND_EXPLOSION );
- }
- }
+ SoundHandler :: playSound( SND_EXPLOSION );
}
- else
- {
- switch ( event.key.keysym.sym )
- {
- case SDLK_f:
- printf( "showFPS - %d\n", showFps );
- if ( event.type == SDL_KEYDOWN )
- showFps = !showFps;
- break;
- case SDLK_l:
- if ( event.type == SDL_KEYDOWN )
- limitFPS = !limitFPS;
- break;
-
- case SDLK_p:
- if ( event.type == SDL_KEYDOWN )
- {
- maxFPS ++;
- printf( "maxFPS - %d\n", maxFPS );
- }
- break;
-
- case SDLK_o:
- if ( event.type == SDL_KEYDOWN )
- {
- maxFPS --;
- printf( "maxFPS - %d\n", maxFPS );
- }
- break;
-
- case SDLK_n:
- currentGame->getTerrain()->offset++;
- break;
-
- default:
- currentGame->handleKeys( event.key );
- break;
- }
- }
-
- break;
}
+ }
+ else
+ {
+ switch ( event.key.keysym.sym )
+ {
+ case SDLK_f:
+ if ( event.type == SDL_KEYDOWN )
+ showFps = !showFps;
+ break;
+ case SDLK_l:
+ if ( event.type == SDL_KEYDOWN )
+ limitFPS = !limitFPS;
+ break;
+ default:
+ currentGame->handleKeys( event.key );
+ break;
+ }
+ }
- case SDL_QUIT:
- done = 1;
- break;
- default:
- break;
+ break;
}
+
+ case SDL_QUIT:
+ finish = true;
+ break;
+ default:
+ break;
}
}
@@ -366,5 +341,4 @@ void SFCave :: changeState( int s )
string musicFile = chooseRandomFile( musicPath, musicType );
- printf("playing music %s\n", musicFile.c_str() );
SoundHandler :: setMusicVolume( 128 );
SoundHandler :: playMusic( musicFile );
@@ -375,5 +349,5 @@ void SFCave :: changeState( int s )
// Start the in game music
- string musicFile = SOUND_PATH "ingame.mod";
+ string musicFile = INGAME_MUSIC;
SoundHandler :: playMusic( musicFile );
SoundHandler :: setMusicVolume( 25 );
@@ -409,10 +383,5 @@ void SFCave :: handleMenuSelect( int menuId )
case MENU_LOAD_REPLAY:
{
-#ifdef QWS
- QString replayFile = getenv( "HOME" );
-#else
- QString replayFile = ".";
-#endif
- replayFile += string( "/" ) + currentGame->getGameName() + ".replay";
+ string replayFile = getHomeDir() + "/" + currentGame->getGameName() + ".replay";
currentGame->loadReplay( replayFile );
@@ -426,10 +395,5 @@ void SFCave :: handleMenuSelect( int menuId )
if ( currentGame->isReplayAvailable() )
{
-#ifdef QWS
- QString replayFile = getenv( "HOME" );
-#else
- QString replayFile = ".";
-#endif
- replayFile += string( "/" ) + currentGame->getGameName() + ".replay";
+ string replayFile = getHomeDir() + "/" + currentGame->getGameName() + ".replay";
currentGame->saveReplay( replayFile );
@@ -495,20 +459,69 @@ void SFCave :: handleMenuSelect( int menuId )
break;
+ case MENU_DIFFICULTY_CUSTOM:
+ currentGame->setDifficulty( MENU_DIFFICULTY_CUSTOM );
+ saveSetting( "GameDifficulty", "Custom" );
+ break;
+
case MENU_SOUND_ON:
SoundHandler :: setSoundsOn( true );
+ saveSetting( "SoundOn", "true" );
break;
case MENU_SOUND_OFF:
SoundHandler :: setSoundsOn( false );
+ saveSetting( "SoundOn", "false" );
break;
case MENU_MUSIC_ON:
SoundHandler :: setMusicOn( true );
+ saveSetting( "MusicOn", "true" );
break;
case MENU_MUSIC_OFF:
SoundHandler :: setMusicOn( false );
+ saveSetting( "MusicOn", "false" );
break;
+ case MENU_CUSTOM_THRUST:
+ customPlayerMenuVal = PLAYER_THRUST;
+ origValue = currentGame->getPlayer()->getValue( customPlayerMenuVal );
+ setMenuStatusText( currentGame->getPlayer()->getValueString( customPlayerMenuVal ) );
+ break;
+ case MENU_CUSTOM_GRAVITY:
+ customPlayerMenuVal = PLAYER_GRAVITY;
+ origValue = currentGame->getPlayer()->getValue( customPlayerMenuVal );
+ setMenuStatusText( currentGame->getPlayer()->getValueString( customPlayerMenuVal ) );
+ break;
+ case MENU_CUSTOM_MAXSPEEDUP:
+ customPlayerMenuVal = PLAYER_MAX_SPEED_UP;
+ origValue = currentGame->getPlayer()->getValue( customPlayerMenuVal );
+ setMenuStatusText( currentGame->getPlayer()->getValueString( customPlayerMenuVal ) );
+ break;
+ case MENU_CUSTOM_MAXSPEEDDOWN:
+ customPlayerMenuVal = PLAYER_MAX_SPEED_DOWN;
+ origValue = currentGame->getPlayer()->getValue( customPlayerMenuVal );
+ setMenuStatusText( currentGame->getPlayer()->getValueString( customPlayerMenuVal ) );
+ break;
+ case MENU_CUSTOM_INCREASE:
+ currentGame->getPlayer()->incValue( customPlayerMenuVal );
+ setMenuStatusText( currentGame->getPlayer()->getValueString( customPlayerMenuVal ) );
+ break;
+ case MENU_CUSTOM_DECREASE:
+ currentGame->getPlayer()->decValue( customPlayerMenuVal );
+ setMenuStatusText( currentGame->getPlayer()->getValueString( customPlayerMenuVal ) );
+ break;
+ case MENU_CUSTOM_SAVE:
+ {
+ // save settings
+ string key = currentGame->getGameName() + "_custom_player_" + currentGame->getPlayer()->getValueTypeString( customPlayerMenuVal );
+ saveSetting( key, currentGame->getPlayer()->getValue( customPlayerMenuVal ) );
+
+ break;
+ }
+ case MENU_CUSTOM_CANCEL:
+ currentGame->getPlayer()->setValue( customPlayerMenuVal, origValue );
+ break;
+
default:
break;
@@ -534,4 +547,16 @@ void SFCave :: saveSetting( string key, int val )
}
+void SFCave :: saveSetting( string key, long val )
+{
+ Settings cfg( "sfcave-sdl" );
+ cfg.writeSetting( key, val );
+}
+
+void SFCave :: saveSetting( string key, double val )
+{
+ Settings cfg( "sfcave-sdl" );
+ cfg.writeSetting( key, val );
+}
+
string SFCave :: loadSetting( string key, string defaultVal )
{
@@ -545,2 +570,64 @@ string SFCave :: loadSetting( string key, string defaultVal )
return val;
}
+
+bool SFCave :: loadBoolSetting( string key, bool defaultVal )
+{
+ bool val = defaultVal;
+ Settings cfg( "sfcave-sdl" );
+ cfg.readSetting( key, val );
+
+ return val;
+}
+
+int SFCave :: loadIntSetting( string key, int defaultVal )
+{
+ int val = defaultVal;
+ Settings cfg( "sfcave-sdl" );
+ cfg.readSetting( key, val );
+
+ return val;
+}
+
+double SFCave :: loadDoubleSetting( string key, double defaultVal )
+{
+ double val = defaultVal;
+ Settings cfg( "sfcave-sdl" );
+ cfg.readSetting( key, val );
+
+ return val;
+}
+
+
+void SFCave :: calcFPS()
+{
+ struct timeb tp;
+ ftime( &tp );
+ start =(tp.time%10000)*10000 + tp.millitm;
+ if ( start - time1 >= 1000 )
+ {
+ actualFPS = FPS;
+ FPS = 0;
+ time1 = start;
+ }
+ else
+ FPS ++;
+}
+
+void SFCave :: FPSDelay()
+{
+ struct timeb tp;
+ // Slow down polling - limit to x FPS
+ ftime( &tp );
+ end = abs((tp.time%10000)*10000 + tp.millitm);
+ if ( end-start < (1000/maxFPS) )
+ {
+ if ( (1000/maxFPS)-(end-start) > 500 )
+ {
+ // Should never happen but in case it does sleep for 5 seconds
+ printf( "WARNING WILL ROBINSON! delay = %ld - start %ld, end %ld\n", (1000/maxFPS)-(end-start), start, end );
+ SDL_Delay( 5 );
+ }
+ else
+ SDL_Delay((1000/maxFPS)-(end-start) );
+ }
+}
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
@@ -16,5 +16,4 @@ public:
~SFCave();
- void drawGameScreen();
void initSDL( int argc, char *argv[] );
void mainEventLoop();
@@ -31,7 +30,14 @@ public:
void saveSetting( string key, string val );
void saveSetting( string key, int val );
+ void saveSetting( string key, long val );
+ void saveSetting( string key, double val );
string loadSetting( string key, string defaultVal = "" );
+ bool loadBoolSetting( string key, bool defaultVal);
+ int loadIntSetting( string key, int defaultVal );
+ double loadDoubleSetting( string key, double defaultVal );
+
private:
SDL_Surface *screen;
+ bool setupOK;
Game *currentGame;
@@ -39,11 +45,27 @@ private:
Help *help;
int state;
- int maxFPS;
- int actualFPS;
bool showFps;
string musicPath;
string musicType;
+ bool finish;
+
+ bool limitFPS;
+ int maxFPS;
+ int actualFPS;
+ int FPS;
+ long time1;
+ long start;
+ long end;
+
+ // This is used when the user is setting the custom
+ // values in the menu
+ int customPlayerMenuVal;
+ double origValue;
void handleMenuSelect( int menuId );
+ void handleGameState();
+ void handleEvents();
+ void calcFPS();
+ void FPSDelay();
};
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
@@ -40,4 +40,17 @@ void SFCaveGame :: init()
blockDistance = 30;
break;
+ case MENU_DIFFICULTY_CUSTOM:
+ {
+ // Read custom difficulty settings for this game
+ blockDistance = parent->loadIntSetting( "SFCave_custom_blockdistance", 50 );
+
+ double thrust = parent->loadDoubleSetting( "SFCave_custom_player_thrust", 0.4 );
+ double gravity = parent->loadDoubleSetting( "SFCave_custom_player_gravity", 0.6 );
+ double maxUp = parent->loadDoubleSetting( "SFCave_custom_player_maxupspeed", 4.0 );
+ double maxDown = parent->loadDoubleSetting( "SFCave_custom_player_maxdownspeed", 5.0 );
+ player->setMovementInfo( thrust, gravity, maxUp, maxDown );
+
+ break;
+ }
}
@@ -69,5 +82,4 @@ void SFCaveGame :: update( int state )
if ( checkCollisions() )
{
-// printf( "Crashed!\n" );
parent->changeState( STATE_CRASHING );
return;
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
@@ -32,4 +32,5 @@ bool SoundHandler :: init( )
soundOn = true;
+ musicOn = true;
return true;
@@ -39,6 +40,8 @@ void SoundHandler :: cleanUp()
{
// Free audio sounds
- Mix_FreeChunk( sounds[SND_EXPLOSION] );
- Mix_FreeChunk( sounds[SND_THRUST] );
+ if ( sounds[SND_EXPLOSION] )
+ Mix_FreeChunk( sounds[SND_EXPLOSION] );
+ if ( sounds[SND_THRUST] )
+ Mix_FreeChunk( sounds[SND_THRUST] );
if ( music )
@@ -53,5 +56,5 @@ int SoundHandler :: playSound( int soundNr, int channel, int nrLoops, int playBe
return -1;
- if ( soundNr >= NR_SOUNDS )
+ if ( soundNr >= NR_SOUNDS || !sounds[soundNr] )
return -1;
@@ -102,5 +105,5 @@ void SoundHandler :: playMusic( string musicFile )
void SoundHandler :: playMusic( bool fade )
{
- if ( !soundOn )
+ if ( !musicOn )
return;
@@ -109,5 +112,5 @@ void SoundHandler :: playMusic( bool fade )
Mix_VolumeMusic( MIX_MAX_VOLUME );
Mix_RewindMusic();
-
+
if ( fade )
Mix_FadeInMusic( music, -1, 1000 );
@@ -147,5 +150,5 @@ void SoundHandler :: setMusicOn( bool val )
{
musicOn = val;
-
+
if ( !musicOn )
stopMusic();
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
@@ -162,5 +162,5 @@ void StarField :: move( )
pos_x[i] += vel_x[i];
pos_y[i] += vel_y[i];
-
+
if (pos_x[i] < 0)
pos_x[i] = pos_x[i] + 240;
@@ -222,2 +222,74 @@ void StarField :: draw( SDL_Surface *screen, int w, int h )
}
}
+
+
+
+// Test
+#ifdef DEBUG_STARS
+SDL_Surface *screen;
+StarField *stars;
+
+void go()
+{
+ /* Initialize SDL */
+ if ( SDL_Init(SDL_INIT_VIDEO) < 0 )
+ {
+ fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
+ exit(1);
+ }
+ atexit(SDL_Quit);
+
+ int videoflags = SDL_SWSURFACE ;
+
+ if ( (screen=SDL_SetVideoMode(240, 320,32,videoflags)) == NULL )
+ {
+ fprintf(stderr, "Couldn't set %ix%i video mode: %s\n",240,320,SDL_GetError());
+ exit(2);
+ }
+
+ stars = new StarField( false, 200 );
+
+ bool done = false;
+ while ( !done )
+ {
+ SDL_FillRect( screen, 0, 0 );
+ stars->draw( screen );
+ stars->move( );
+
+ SDL_Flip( screen );
+
+ SDL_Delay( 10 );
+
+ SDL_Event event;
+ while ( SDL_PollEvent(&event) )
+ {
+ switch (event.type)
+ {
+ case SDL_KEYDOWN:
+ // Escape keypress quits the app
+ if ( event.key.keysym.sym != SDLK_ESCAPE )
+ {
+ break;
+ }
+ case SDL_QUIT:
+ done = 1;
+ break;
+ default:
+ break;
+ }
+ }
+ }
+ }
+
+
+
+
+#ifdef __cplusplus
+extern "C"
+#endif
+int main( int argc, char *argv[] )
+{
+ go();
+}
+
+#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
@@ -48,4 +48,5 @@ void Terrain :: initTerrain()
SDL_FillRect( terrainSurface, 0, 0 );
+
// Draw Terrain into surface
Sint16 px[5];
@@ -92,5 +93,4 @@ void Terrain :: moveTerrain( int amountToMove )
speed = offset/segSize;
-// printf( "offset - %d, speed - %d\n", offset, speed );
if ( offset >= segSize )
{
@@ -196,5 +196,4 @@ void Terrain :: drawTerrain( SDL_Surface *screen )
dst.w = sWidth;
dst.h = sHeight;
-// dst.h = maxHeight;
SDL_Rect dst2;
@@ -205,20 +204,4 @@ void Terrain :: drawTerrain( SDL_Surface *screen )
stars->draw( screen );
-
-// dst.y = sHeight - maxHeight;
-// dst2.y = sHeight - maxHeight;
-// SDL_BlitSurface(terrainSurface, &dst, screen, &dst2 );
-
-/*
- for ( int i = 0 ; i < MAPSIZE ; ++i )
- {
- int x1 = (i*segSize) - (offset*speed);
- int x2 = ((i+1)*segSize)-(offset*speed);
- if ( x2 >= sWidth )
- x2 = sWidth-1;
- aalineRGBA( screen, x1, mapTop[i], x2, mapTop[i+1], 0, 220, 0, 255 );
- aalineRGBA( screen, x1, mapBottom[i], x2, mapBottom[i+1], 0, 220, 0, 255 );
- }
-*/
}
@@ -227,4 +210,5 @@ bool Terrain :: checkCollision( int x, int y, int h )
if ( y < 0 || y > sHeight )
return true;
+
// First get segment that matches x
SDL_LockSurface( terrainSurface );
@@ -248,5 +232,5 @@ Terrain *terrain;
void go()
{
- /* Initialize SDL */
+ // Initialize SDL
if ( SDL_Init(SDL_INIT_VIDEO) < 0 )
{
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
@@ -36,5 +36,5 @@ Uint32 getpixel(SDL_Surface *surface, int x, int y)
}
-const char *chooseRandomFile( string path, string fileType )
+string chooseRandomFile( string path, string fileType )
{
vector<string> files;
@@ -51,8 +51,6 @@ const char *chooseRandomFile( string path, string fileType )
int pos = file.find( ".", 1 ) + 1;
string tmp = file.substr( pos );
- printf( "pos = %d, tmp =%s\n", pos, tmp.c_str() );
if ( tmp.size() > 0 && fileType.find( tmp ) != -1 )
{
- printf( "Matching <%s> - %s with <%s>\n", file.substr( pos ).c_str(), file.c_str(), fileType.c_str() );
files.push_back( file );
}
@@ -61,4 +59,17 @@ const char *chooseRandomFile( string path, string fileType )
closedir( d );
- return files[nextInt( files.size() )].c_str();
+ return files[nextInt( files.size() )];
+}
+
+
+string getHomeDir()
+{
+ string home;
+#ifdef QWS
+ home = getenv( "HOME" );
+#else
+ home = ".";
+#endif
+
+ return home;
}
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
@@ -6,5 +6,5 @@ using namespace std;
Uint32 getpixel(SDL_Surface *surface, int x, int y);
-const char *chooseRandomFile( string path, string fileType );
-
+string chooseRandomFile( string path, string fileType );
+string getHomeDir();
#endif