author | andyq <andyq> | 2003-01-21 20:37:00 (UTC) |
---|---|---|
committer | andyq <andyq> | 2003-01-21 20:37:00 (UTC) |
commit | 0a6563fcc2f49857c581d9def24407a3a4ef526c (patch) (side-by-side diff) | |
tree | f1b82a4bd7582ef2cb722cffb87eecff1e1f96e6 /noncore | |
parent | 50b5915b48fc5cbacf23e4d2b75d7a266f141a4a (diff) | |
download | opie-0a6563fcc2f49857c581d9def24407a3a4ef526c.zip opie-0a6563fcc2f49857c581d9def24407a3a4ef526c.tar.gz opie-0a6563fcc2f49857c581d9def24407a3a4ef526c.tar.bz2 |
Clean up of code - fixed memory leaks (most of them) and added new custom config menu
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 @@ #include "constants.h" #include "animatedimage.h" -AnimatedImage :: AnimatedImage( QString file, int nFrames ) +AnimatedImage :: AnimatedImage( string file, int nFrames ) { nrFrames = nFrames; currentFrame = 0; @@ -19,9 +19,6 @@ 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; } @@ -47,6 +44,9 @@ bool AnimatedImage :: nextFrame() void AnimatedImage :: draw( SDL_Surface *screen, int x, int y ) { + if ( !image ) + return; + SDL_Rect dst; dst.x = currentFrame * frameWidth; 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 @@ class AnimatedImage { public: - AnimatedImage( QString file, int nFrames ); + AnimatedImage( string file, int nFrames ); ~AnimatedImage(); 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) InitFont(); // Init the font } } - else - { - cerr << "Error! The font has not been loaded!" << endl; - } } BFont * BFont :: SetFontColor(Uint8 r, Uint8 g, Uint8 b) @@ -113,7 +109,6 @@ 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++) { old_r = old_g = old_b = 0; @@ -131,7 +126,7 @@ BFont * BFont :: SetFontColor(Uint8 r, Uint8 g, Uint8 b) PutPixel(surface,x,y,pixel); } } - 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 @@ -3,7 +3,6 @@ #include <string> using namespace std; -#define QString string #ifdef QWS #define IMAGES_PATH "/opt/QtPalmtop/pics/sfcave/data/" @@ -55,13 +54,31 @@ using namespace std; #define MENU_DIFFICULTY_EASY 16 #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 @@ -17,6 +17,7 @@ FlyGame :: FlyGame( SFCave *p, int w, int h, int diff ) FlyGame :: ~FlyGame() { + // terrain and player get deleted by parent class } void FlyGame :: init() @@ -34,6 +35,15 @@ void FlyGame :: init() case MENU_DIFFICULTY_HARD: 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; + } } startScoring = false; @@ -50,7 +60,7 @@ 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 ) { if ( tmpScore > 0 ) @@ -69,7 +79,6 @@ 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 @@ -8,7 +8,7 @@ BFont *FontHandler :: whiteFont; BFont *FontHandler :: colouredFont; BFont *FontHandler :: helpFont; -void FontHandler :: init() +bool FontHandler :: init() { // Load font images // Convert to fonts @@ -17,15 +17,27 @@ void FontHandler :: init() whiteFont = new BFont( IMAGES_PATH "score_font.bmp" ); 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 @@ -13,7 +13,7 @@ class FontHandler { public: - static void init(); + static bool init(); static void cleanUp(); 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() setSeed( -1 ); replayList.clear(); } - + score = 0; nrFrames = 0; press = false; @@ -76,9 +76,6 @@ void Game :: handleKeys( SDL_KeyboardEvent &key ) if ( !press ) replayList.push_back( nrFrames ); press = true; - -// if ( thrustChannel == -1 && parent->getState() == STATE_PLAYING ) -// thrustChannel = SoundHandler :: playSound( SND_THRUST, -1, -1, false ); } else { @@ -86,20 +83,15 @@ void Game :: handleKeys( SDL_KeyboardEvent &key ) replayList.push_back( nrFrames ); press = false; - if ( thrustChannel != -1 ) - { -// SoundHandler :: stopSound( thrustChannel, true, 300 ); -// thrustChannel = -1; - } } } } -QString Game :: getGameDifficultyText() +string Game :: getGameDifficultyText() { - QString ret; + string ret; if ( difficulty == MENU_DIFFICULTY_EASY ) ret = "Easy"; @@ -107,6 +99,8 @@ QString Game :: getGameDifficultyText() ret = "Medium"; else if ( difficulty == MENU_DIFFICULTY_HARD ) ret = "Hard"; + else if ( difficulty == MENU_DIFFICULTY_CUSTOM ) + ret = "Custom"; return ret; } @@ -119,6 +113,16 @@ void Game :: setDifficulty( string diff ) difficulty = MENU_DIFFICULTY_NORMAL; 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(); } void Game :: update( int state ) @@ -131,7 +135,7 @@ void Game :: update( int state ) if ( state == STATE_PLAYING ) { - if ( replay ) + if ( replay ) { while( replayIt != replayList.end() && (*replayIt) == nrFrames-1 ) { @@ -166,14 +170,13 @@ void Game :: preDraw( SDL_Surface *screen ) 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; FontHandler::draw( screen, FONT_WHITE_TEXT, (const char *)crashText.c_str(), x, 150 ); @@ -223,7 +226,7 @@ void Game :: setSeed( int seed ) PutSeed( currentSeed ); } -void Game :: saveReplay( QString file ) +void Game :: saveReplay( string file ) { FILE *out; out = fopen( file.c_str(), "w" ); @@ -236,7 +239,7 @@ 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 ); val = tmp; @@ -251,7 +254,7 @@ void Game :: saveReplay( QString file ) } val += "\n"; - QString line; + string line; sprintf( tmp, "%d\n", val.length() ); line = tmp; fwrite( line.c_str(), 1, line.length(), out ); @@ -259,12 +262,9 @@ void Game :: saveReplay( QString file ) fwrite( val.c_str(), 1, val.length(), out ); fclose( out ); - - printf( "Replay saved to %s\n", (const char *)file.c_str() ); - } -void Game :: loadReplay( QString file ) +void Game :: loadReplay( string file ) { FILE *in = fopen( (const char *)file.c_str(), "r" ); @@ -285,9 +285,8 @@ void Game :: loadReplay( QString file ) char *data = new char[length+1]; fread( data, 1, length, in ); -// printf( "data - %s", data ); - QString sep = " "; + string sep = " "; StringTokenizer st( data, sep ); @@ -308,9 +307,6 @@ void Game :: loadReplay( QString file ) delete data; 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 @@ -25,10 +25,10 @@ public: void setReplay( bool val ) { replay = val; } 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 ); long getScore() { return score; } @@ -39,15 +39,16 @@ public: bool isReplayAvailable() { return replayList.size() > 0; } 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; @@ -69,12 +70,10 @@ protected: // Stuff for the replays 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 @@ -18,6 +18,7 @@ GatesGame :: GatesGame( SFCave *p, int w, int h, int diff ) GatesGame :: ~GatesGame() { + // terrain and player get deleted by parent class } void GatesGame :: init() @@ -46,6 +47,19 @@ void GatesGame :: init() gapHeight = 25; 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; + } } for ( int i = 0 ; i < BLOCKSIZE ; ++i ) @@ -77,7 +91,6 @@ 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 @@ -35,7 +35,7 @@ void Help :: handleKeys( SDL_KeyboardEvent &key ) textSpeed = 0;
else textSpeed = 1;
}
-
+
}
else if ( key.type == SDL_KEYUP )
{
@@ -48,7 +48,7 @@ void Help :: init() startPos = 320;
currLine = 0;
textSpeed = 1;
-
+
// Create our coloured font
FontHandler :: changeColor( FONT_HELP_FONT, 0, 0, 255 );
}
@@ -57,7 +57,6 @@ void Help :: draw( SDL_Surface *screen ) {
stars->draw( screen );
-
list<string>::iterator it = textList.begin();
// Move to start of text
@@ -75,13 +74,13 @@ void Help :: draw( SDL_Surface *screen ) pos += FontHandler::FontHeight( FONT_COLOURED_TEXT );
it ++;
}
-
+
}
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 @@ -8,7 +8,7 @@ #include "font.h" #include "starfield.h" -MenuOption :: MenuOption( QString text, int id ) +MenuOption :: MenuOption( string text, int id ) { menuText = text; menuId = id; @@ -115,6 +115,7 @@ Menu :: Menu( SFCave *p ) typeMenu->setNextMenu( gameType ); // Game Difficulty menu + MenuOption *customMenu = 0; Menu *gameDifficulty = new Menu( options ); item = gameDifficulty->addMenuOption( "Easy", MENU_DIFFICULTY_EASY ); item->setNextMenu( options, false ); @@ -122,6 +123,7 @@ Menu :: Menu( SFCave *p ) item->setNextMenu( options, false ); 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 ); difficultyMenu->setNextMenu( gameDifficulty ); @@ -135,6 +137,29 @@ Menu :: Menu( SFCave *p ) item = sounds->addMenuOption( "Back", MENU_BACK ); 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 mainMenu = this; @@ -215,7 +240,6 @@ int Menu :: handleKeys( SDL_KeyboardEvent &key ) if ( key.type != SDL_KEYDOWN ) return -1; - statusText = ""; switch( key.keysym.sym ) { case SDLK_DOWN: @@ -276,8 +300,8 @@ int Menu :: handleKeys( SDL_KeyboardEvent &key ) case SDLK_LEFT: if ( currentMenu->parentMenu != 0 ) { + statusText = ""; currentMenu = currentMenu->parentMenu; - printf( "HERE\n" ); return -1; } @@ -286,9 +310,11 @@ int Menu :: handleKeys( SDL_KeyboardEvent &key ) case SDLK_RETURN: 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 ) { @@ -296,13 +322,10 @@ int Menu :: handleKeys( SDL_KeyboardEvent &key ) currentMenu = next; if ( down ) initCurrentMenu(); -// return -1; - } -// else - { - return id; } + return id; + break; } @@ -313,7 +336,7 @@ int Menu :: handleKeys( SDL_KeyboardEvent &key ) return -1; } -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 @@ -13,7 +13,7 @@ class Menu; class MenuOption { public: - MenuOption( QString text, int id ); + MenuOption( string text, int id ); ~MenuOption(); void highlight( bool val ) { highlighted = val; } @@ -25,7 +25,7 @@ public: private: int menuId; - QString menuText; + string menuText; bool highlighted; bool downMenuTree; @@ -40,11 +40,11 @@ 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: @@ -58,7 +58,7 @@ private: StarField *stars; - QString statusText; + string statusText; SFCave *parent; 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 ) sWidth = w; sHeight = h; - thrustUp = 0.4; - thrustDown = 0.6; + thrust = 0.4; + gravity = 0.6; maxUpSpeed = 4.0; maxDownSpeed = 5.0; @@ -80,10 +80,7 @@ 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 ); } } @@ -95,16 +92,16 @@ void Player :: move( bool up ) moveTrails(); 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) ); } void Player :: moveTrails() @@ -152,11 +149,136 @@ bool Player :: updateCrashing() return crashed; } -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 @@ -22,7 +22,13 @@ public: int getY() { return pos.y(); } 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: AnimatedImage *explosion; @@ -36,10 +42,10 @@ private: bool crashed; 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 @@ -5,6 +5,8 @@ #include "settings.h" +// Defined in util.h +string getHomeDir(); #define DEFAULT_DIR "." #define DEFAULT_FILE "Settings.cfg" @@ -16,15 +18,8 @@ Settings::Settings( char * env_file, char * env_dir ) // Store the correct environment directory 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); } @@ -42,15 +37,8 @@ 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); envFile.append("/"); @@ -110,6 +98,18 @@ bool Settings::readSetting(const string key_str,unsigned long& result) return false; } +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) { string Buffer; @@ -159,6 +159,14 @@ void Settings::writeSetting(const string key_str,const bool value) value ? writeSetting(key_str,string("true")) :writeSetting(key_str,string("false")); } +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) { char Buffer[30]; @@ -195,41 +203,34 @@ 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 ); if ((ftemp = fopen(tempname,"w")) != NULL) @@ -254,10 +255,6 @@ void Settings::writeSetting(const string key_str,const string value) fprintf(ftemp, "%s\t%s\n", key_str.c_str(),value.c_str()); fflush(ftemp); fclose(ftemp); - - remove(envFile.c_str()); - - rename( tempname, envFile.c_str() ); } else printf( "Can't open file %s\n", envFile.c_str() ); @@ -265,6 +262,7 @@ void Settings::writeSetting(const string key_str,const string value) delete dir_str; } +*/ } void 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: bool readSetting(const string key_str,unsigned int& result); 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); void writeSetting(const string key_str,const string value); @@ -39,10 +40,11 @@ public: void writeSetting(const string key_str,const unsigned int result); 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: 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 @@ void start( int argc, char *argv[] ) { - FontHandler::init(); - SFCave app( argc, argv ); - FontHandler::cleanUp(); + SFCave *app = new SFCave( argc, argv ); + app->mainEventLoop(); + delete app; } #ifdef __cplusplus @@ -43,35 +43,51 @@ 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; } SFCave :: ~SFCave() @@ -82,53 +98,57 @@ SFCave :: ~SFCave() if ( menu ) 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[] ) { const SDL_VideoInfo *info; Uint8 video_bpp; 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", argv[0]); @@ -136,219 +156,174 @@ 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"); } 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; } } } @@ -365,7 +340,6 @@ void SFCave :: changeState( int s ) SoundHandler :: stopMusic( true ); string musicFile = chooseRandomFile( musicPath, musicType ); - printf("playing music %s\n", musicFile.c_str() ); SoundHandler :: setMusicVolume( 128 ); SoundHandler :: playMusic( musicFile ); } @@ -374,7 +348,7 @@ void SFCave :: changeState( int s ) SoundHandler :: stopMusic( ); // Start the in game music - string musicFile = SOUND_PATH "ingame.mod"; + string musicFile = INGAME_MUSIC; SoundHandler :: playMusic( musicFile ); SoundHandler :: setMusicVolume( 25 ); } @@ -408,12 +382,7 @@ 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 ); @@ -425,12 +394,7 @@ 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 ); } @@ -494,22 +458,71 @@ void SFCave :: handleMenuSelect( int menuId ) saveSetting( "GameDifficulty", "Hard" ); break; + case MENU_DIFFICULTY_CUSTOM: + currentGame->setDifficulty( MENU_DIFFICULTY_CUSTOM ); + saveSetting( "GameDifficulty", "Custom" ); + break; + case MENU_SOUND_ON: SoundHandler :: setSoundsOn( true ); + saveSetting( "SoundOn", "true" ); break; case MENU_SOUND_OFF: SoundHandler :: setSoundsOn( false ); + saveSetting( "SoundOn", "false" ); break; case MENU_MUSIC_ON: SoundHandler :: setMusicOn( true ); + saveSetting( "MusicOn", "true" ); break; case MENU_MUSIC_OFF: SoundHandler :: setMusicOn( false ); + saveSetting( "MusicOn", "false" ); break; + case MENU_CUSTOM_THRUST: + customPlayerMenuVal = PLAYER_THRUST; + origValue = currentGame->getPlayer()->getValue( customPlayerMenuVal ); + setMenuStatusText( currentGame->getPlayer()->getValueString( customPlayerMenuVal ) ); + break; + case MENU_CUSTOM_GRAVITY: + customPlayerMenuVal = PLAYER_GRAVITY; + origValue = currentGame->getPlayer()->getValue( customPlayerMenuVal ); + setMenuStatusText( currentGame->getPlayer()->getValueString( customPlayerMenuVal ) ); + break; + case MENU_CUSTOM_MAXSPEEDUP: + customPlayerMenuVal = PLAYER_MAX_SPEED_UP; + origValue = currentGame->getPlayer()->getValue( customPlayerMenuVal ); + setMenuStatusText( currentGame->getPlayer()->getValueString( customPlayerMenuVal ) ); + break; + case MENU_CUSTOM_MAXSPEEDDOWN: + customPlayerMenuVal = PLAYER_MAX_SPEED_DOWN; + origValue = currentGame->getPlayer()->getValue( customPlayerMenuVal ); + setMenuStatusText( currentGame->getPlayer()->getValueString( customPlayerMenuVal ) ); + break; + case MENU_CUSTOM_INCREASE: + currentGame->getPlayer()->incValue( customPlayerMenuVal ); + setMenuStatusText( currentGame->getPlayer()->getValueString( customPlayerMenuVal ) ); + break; + case MENU_CUSTOM_DECREASE: + currentGame->getPlayer()->decValue( customPlayerMenuVal ); + setMenuStatusText( currentGame->getPlayer()->getValueString( customPlayerMenuVal ) ); + break; + case MENU_CUSTOM_SAVE: + { + // save settings + string key = currentGame->getGameName() + "_custom_player_" + currentGame->getPlayer()->getValueTypeString( customPlayerMenuVal ); + saveSetting( key, currentGame->getPlayer()->getValue( customPlayerMenuVal ) ); + + break; + } + case MENU_CUSTOM_CANCEL: + currentGame->getPlayer()->setValue( customPlayerMenuVal, origValue ); + break; + default: break; } @@ -533,6 +546,18 @@ void SFCave :: saveSetting( string key, int val ) cfg.writeSetting( key, val ); } +void SFCave :: saveSetting( string key, long val ) +{ + Settings cfg( "sfcave-sdl" ); + cfg.writeSetting( key, val ); +} + +void SFCave :: saveSetting( string key, double val ) +{ + Settings cfg( "sfcave-sdl" ); + cfg.writeSetting( key, val ); +} + string SFCave :: loadSetting( string key, string defaultVal ) { string val; @@ -544,3 +569,65 @@ 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 @@ -15,7 +15,6 @@ public: SFCave( int argc, char *argv[] ); ~SFCave(); - void drawGameScreen(); void initSDL( int argc, char *argv[] ); void mainEventLoop(); @@ -30,21 +29,44 @@ 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; Menu *menu; 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(); }; #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() case MENU_DIFFICULTY_HARD: 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; + } } for ( int i = 0 ; i < BLOCKSIZE ; ++i ) @@ -68,7 +81,6 @@ 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 @@ -31,6 +31,7 @@ bool SoundHandler :: init( ) music = 0;
soundOn = true;
+ musicOn = true;
return true;
}
@@ -38,8 +39,10 @@ bool SoundHandler :: init( ) 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 )
Mix_FreeMusic( music );
@@ -52,7 +55,7 @@ int SoundHandler :: playSound( int soundNr, int channel, int nrLoops, int playBe if ( !soundOn )
return -1;
- if ( soundNr >= NR_SOUNDS )
+ if ( soundNr >= NR_SOUNDS || !sounds[soundNr] )
return -1;
Mix_Chunk *chunk = sounds[soundNr];
@@ -101,14 +104,14 @@ void SoundHandler :: playMusic( string musicFile ) void SoundHandler :: playMusic( bool fade )
{
- if ( !soundOn )
+ if ( !musicOn )
return;
if ( music )
{
Mix_VolumeMusic( MIX_MAX_VOLUME );
Mix_RewindMusic();
-
+
if ( fade )
Mix_FadeInMusic( music, -1, 1000 );
else
@@ -146,7 +149,7 @@ void SoundHandler :: setSoundsOn( bool val ) void SoundHandler :: setMusicOn( bool val )
{
musicOn = val;
-
+
if ( !musicOn )
stopMusic();
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( ) //Move Star
pos_x[i] += vel_x[i];
pos_y[i] += vel_y[i];
-
+
if (pos_x[i] < 0)
pos_x[i] = pos_x[i] + 240;
@@ -221,3 +221,75 @@ void StarField :: draw( SDL_Surface *screen, int w, int h ) SDL_UnlockSurface( screen );
}
}
+
+
+
+// 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 @@ -47,6 +47,7 @@ void Terrain :: initTerrain() setPoint( i ); SDL_FillRect( terrainSurface, 0, 0 ); + // Draw Terrain into surface Sint16 px[5]; Sint16 py[5]; @@ -91,7 +92,6 @@ void Terrain :: moveTerrain( int amountToMove ) offset += amountToMove; speed = offset/segSize; -// printf( "offset - %d, speed - %d\n", offset, speed ); if ( offset >= segSize ) { for ( int i = 0 ; i < (MAPSIZE)-speed ; ++i ) @@ -195,7 +195,6 @@ void Terrain :: drawTerrain( SDL_Surface *screen ) dst.y = 0; dst.w = sWidth; dst.h = sHeight; -// dst.h = maxHeight; SDL_Rect dst2; dst2.x = 0; @@ -204,28 +203,13 @@ void Terrain :: drawTerrain( SDL_Surface *screen ) SDL_BlitSurface(terrainSurface, &dst, screen, &dst2 ); 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 ); - } -*/ } 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 ); @@ -247,7 +231,7 @@ Terrain *terrain; void go() { - /* Initialize SDL */ + // Initialize SDL if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) { 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) }
}
-const char *chooseRandomFile( string path, string fileType )
+string chooseRandomFile( string path, string fileType )
{
vector<string> files;
DIR *d = opendir( path.c_str() );
@@ -50,15 +50,26 @@ const char *chooseRandomFile( string path, string fileType ) // Rip extension from file
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 );
}
item = readdir( d );
}
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 @@ -5,6 +5,6 @@ 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
|