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 @@ -3,9 +3,9 @@ #include "constants.h" #include "animatedimage.h" -AnimatedImage :: AnimatedImage( QString file, int nFrames ) +AnimatedImage :: AnimatedImage( string file, int nFrames ) { nrFrames = nFrames; currentFrame = 0; @@ -18,11 +18,8 @@ AnimatedImage :: AnimatedImage( QString file, int nFrames ) return; } 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; } @@ -46,8 +43,11 @@ 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; dst.w = 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 @@ -5,9 +5,9 @@ class AnimatedImage { public: - AnimatedImage( QString file, int nFrames ); + AnimatedImage( string file, int nFrames ); ~AnimatedImage(); bool nextFrame(); void draw( SDL_Surface *screen, int x, int y ); 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 @@ -72,12 +72,8 @@ 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) { @@ -112,9 +108,8 @@ BFont * BFont :: SetFontColor(Uint8 r, Uint8 g, Uint8 b) if (SDL_MUSTLOCK(Surface)) SDL_LockSurface(Surface); 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; pixel = xGetPixel(Surface,x,y); @@ -130,9 +125,9 @@ 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); SDL_SetColorKey(surface, SDL_SRCCOLORKEY, color_key); 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 @@ -2,9 +2,8 @@ #define __CONSTANTS_H #include <string> using namespace std; -#define QString string #ifdef QWS #define IMAGES_PATH "/opt/QtPalmtop/pics/sfcave/data/" #define SOUND_PATH "/opt/QtPalmtop/sounds/sfcave/" @@ -54,14 +53,32 @@ using namespace std; #define MENU_GAME_FLY 15 #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 @@ -16,8 +16,9 @@ FlyGame :: FlyGame( SFCave *p, int w, int h, int diff ) } FlyGame :: ~FlyGame() { + // terrain and player get deleted by parent class } void FlyGame :: init() { @@ -33,8 +34,17 @@ void FlyGame :: init() break; 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; } @@ -49,9 +59,9 @@ void FlyGame :: update( int state ) if ( nrFrames % 3 == 0 ) { 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 ) startScoring = true; @@ -68,9 +78,8 @@ 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 @@ -7,26 +7,38 @@ BFont *FontHandler :: menuUnSelFont; BFont *FontHandler :: whiteFont; BFont *FontHandler :: colouredFont; BFont *FontHandler :: helpFont; -void FontHandler :: init() +bool FontHandler :: init() { // Load font images // Convert to fonts menuSelFont = new BFont( IMAGES_PATH "sel_menu_font.bmp" ); menuUnSelFont = new BFont( IMAGES_PATH "unsel_menu_font.bmp" ); 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 @@ -12,9 +12,9 @@ class FontHandler { public: - static void init(); + static bool init(); static void cleanUp(); static int TextWidth( int font, const char *text ); static int FontHeight( int font ); 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 @@ -53,9 +53,9 @@ void Game :: init() { setSeed( -1 ); replayList.clear(); } - + score = 0; nrFrames = 0; press = false; @@ -75,39 +75,33 @@ 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 { if ( press ) 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"; else if ( difficulty == MENU_DIFFICULTY_NORMAL ) ret = "Medium"; else if ( difficulty == MENU_DIFFICULTY_HARD ) ret = "Hard"; + else if ( difficulty == MENU_DIFFICULTY_CUSTOM ) + ret = "Custom"; return ret; } @@ -118,8 +112,18 @@ void Game :: setDifficulty( string diff ) else if ( diff == "Medium" ) 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 ) { @@ -130,9 +134,9 @@ void Game :: update( int state ) if ( state == STATE_PLAYING ) { - if ( replay ) + if ( replay ) { while( replayIt != replayList.end() && (*replayIt) == nrFrames-1 ) { press = !press; @@ -165,16 +169,15 @@ 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 ); @@ -222,9 +225,9 @@ void Game :: setSeed( int seed ) currentSeed = seed; PutSeed( currentSeed ); } -void Game :: saveReplay( QString file ) +void Game :: saveReplay( string file ) { FILE *out; out = fopen( file.c_str(), "w" ); if ( !out ) @@ -235,9 +238,9 @@ 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; @@ -250,22 +253,19 @@ void Game :: saveReplay( QString file ) it++; } val += "\n"; - QString line; + string line; sprintf( tmp, "%d\n", val.length() ); line = tmp; fwrite( line.c_str(), 1, line.length(), out ); 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" ); @@ -284,11 +284,10 @@ void Game :: loadReplay( QString file ) sscanf( line, "%d", &length ); char *data = new char[length+1]; fread( data, 1, length, in ); -// printf( "data - %s", data ); - QString sep = " "; + string sep = " "; StringTokenizer st( data, sep ); // print it out @@ -307,11 +306,8 @@ void Game :: loadReplay( QString file ) delete data; fclose( in ); - - printf( "Replay loaded from %s\n", (const char *)file.c_str() ); - } Game *Game :: createGame( SFCave *p, int w, int h, string game, string difficulty ) 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 @@ -24,12 +24,12 @@ 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; } long getHighScore() { return highScore; } @@ -38,17 +38,18 @@ public: bool gotHighScore() { return (score >= highScore); } 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; int difficulty; @@ -68,14 +69,12 @@ 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 @@ -17,8 +17,9 @@ GatesGame :: GatesGame( SFCave *p, int w, int h, int diff ) } GatesGame :: ~GatesGame() { + // terrain and player get deleted by parent class } void GatesGame :: init() { @@ -45,8 +46,21 @@ void GatesGame :: init() case MENU_DIFFICULTY_HARD: 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 ) blocks[i].y( -1 ); @@ -76,9 +90,8 @@ 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 @@ -34,9 +34,9 @@ void Help :: handleKeys( SDL_KeyboardEvent &key ) if ( textSpeed > 0 )
textSpeed = 0;
else textSpeed = 1;
}
-
+
}
else if ( key.type == SDL_KEYUP )
{
if ( key.keysym.sym == SDLK_DOWN )
@@ -47,18 +47,17 @@ void Help :: init() {
startPos = 320;
currLine = 0;
textSpeed = 1;
-
+
// Create our coloured font
FontHandler :: changeColor( FONT_HELP_FONT, 0, 0, 255 );
}
void Help :: draw( SDL_Surface *screen )
{
stars->draw( screen );
-
list<string>::iterator it = textList.begin();
// Move to start of text
for ( int i = 0 ; i < currLine && it != textList.end() ; ++i )
@@ -74,15 +73,15 @@ void Help :: draw( SDL_Surface *screen ) FontHandler::draw( screen, FONT_COLOURED_TEXT, text.c_str(), -1, pos );
pos += FontHandler::FontHeight( FONT_COLOURED_TEXT );
it ++;
}
-
+
}
void Help :: update()
{
stars->move();
-
+
startPos -= textSpeed;
if ( startPos <= -FontHandler::FontHeight( FONT_COLOURED_TEXT ) )
{
startPos = 0;
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 @@ -7,9 +7,9 @@ #include "menu.h" #include "font.h" #include "starfield.h" -MenuOption :: MenuOption( QString text, int id ) +MenuOption :: MenuOption( string text, int id ) { menuText = text; menuId = id; nextMenu = 0; @@ -114,15 +114,17 @@ Menu :: Menu( SFCave *p ) item->setNextMenu( options ); 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 ); item = gameDifficulty->addMenuOption( "Normal", MENU_DIFFICULTY_NORMAL ); 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 ); @@ -134,8 +136,31 @@ Menu :: Menu( SFCave *p ) sounds->addMenuOption( "Music Off", MENU_MUSIC_OFF ); 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; currentMenuOption = 0; @@ -214,9 +239,8 @@ int Menu :: handleKeys( SDL_KeyboardEvent &key ) { if ( key.type != SDL_KEYDOWN ) return -1; - statusText = ""; switch( key.keysym.sym ) { case SDLK_DOWN: { @@ -275,35 +299,34 @@ int Menu :: handleKeys( SDL_KeyboardEvent &key ) } case SDLK_LEFT: if ( currentMenu->parentMenu != 0 ) { + statusText = ""; currentMenu = currentMenu->parentMenu; - printf( "HERE\n" ); return -1; } break; 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 ) { bool down = currentMenu->currentMenuOption->isDownMenuTree(); currentMenu = next; if ( down ) initCurrentMenu(); -// return -1; - } -// else - { - return id; } + return id; + break; } default: @@ -312,9 +335,9 @@ 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 ); listItems.push_back( item ); diff --git a/noncore/games/sfcave-sdl/menu.h b/noncore/games/sfcave-sdl/menu.h index 08f7528..6a5ef40 100644 --- a/noncore/games/sfcave-sdl/menu.h +++ b/noncore/games/sfcave-sdl/menu.h @@ -12,9 +12,9 @@ class Menu; class MenuOption { public: - MenuOption( QString text, int id ); + MenuOption( string text, int id ); ~MenuOption(); void highlight( bool val ) { highlighted = val; } int draw( SDL_Surface *screen, int y ); @@ -24,9 +24,9 @@ public: bool isDownMenuTree() { return downMenuTree; } private: int menuId; - QString menuText; + string menuText; bool highlighted; bool downMenuTree; Menu *nextMenu; @@ -39,13 +39,13 @@ public: ~Menu(); 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: private: @@ -57,9 +57,9 @@ private: Menu *parentMenu; StarField *stars; - QString statusText; + string statusText; SFCave *parent; list<MenuOption *> listItems; MenuOption *currentMenuOption; 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 @@ -10,10 +10,10 @@ 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; explosion = new AnimatedImage( IMAGES_PATH "explosion.bmp", 15 ); @@ -79,12 +79,9 @@ void Player :: drawTrails( SDL_Surface *screen ) for ( int i = 0 ; i < TRAILSIZE ; ++i ) { 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 ); } } } @@ -94,18 +91,18 @@ void Player :: move( bool up ) // Find enpty trail and move others 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() { @@ -151,12 +148,137 @@ 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 @@ -21,9 +21,15 @@ public: int getX() { return pos.x(); } 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; @@ -35,12 +41,12 @@ private: bool crashing; bool crashed; int crashLineLength; Rect pos; - double thrust; + double currentThrust; - double thrustUp; - double thrustDown; + double thrust; + double gravity; double maxUpSpeed; double maxDownSpeed; Rect trail[TRAILSIZE]; 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 @@ -4,8 +4,10 @@ #include <vector> #include "settings.h" +// Defined in util.h +string getHomeDir(); #define DEFAULT_DIR "." #define DEFAULT_FILE "Settings.cfg" #define MAX_LINE_SIZE 2048 @@ -15,17 +17,10 @@ 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); } else @@ -41,17 +36,10 @@ 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("/"); @@ -109,8 +97,20 @@ bool Settings::readSetting(const string key_str,unsigned long& result) else 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; if (readSetting(key_str,Buffer)) @@ -158,8 +158,16 @@ 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]; @@ -194,43 +202,36 @@ void Settings::writeSetting(const string key_str,const unsigned long value) 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) { @@ -253,19 +254,16 @@ 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() ); } 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 @@ -31,19 +31,21 @@ public: bool readSetting(const string key_str,int& result); 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); void writeSetting(const string key_str,const int value); 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 @@ -25,11 +25,11 @@ #include "fly_game.h" 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 extern "C" @@ -42,37 +42,53 @@ 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() { @@ -81,275 +97,234 @@ 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]); exit(1); } } - /* 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; } } } @@ -364,18 +339,17 @@ 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 ); } else if ( state == STATE_MENU && (s == STATE_NEWGAME || s == STATE_REPLAY) ) { SoundHandler :: stopMusic( ); // Start the in game music - string musicFile = SOUND_PATH "ingame.mod"; + string musicFile = INGAME_MUSIC; SoundHandler :: playMusic( musicFile ); SoundHandler :: setMusicVolume( 25 ); } @@ -407,14 +381,9 @@ void SFCave :: handleMenuSelect( int menuId ) break; 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 ); break; @@ -424,14 +393,9 @@ 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 ); } else @@ -493,24 +457,73 @@ void SFCave :: handleMenuSelect( int menuId ) currentGame->setDifficulty( MENU_DIFFICULTY_HARD ); saveSetting( "GameDifficulty", "Hard" ); break; + case MENU_DIFFICULTY_CUSTOM: + currentGame->setDifficulty( MENU_DIFFICULTY_CUSTOM ); + saveSetting( "GameDifficulty", "Custom" ); + break; + case MENU_SOUND_ON: SoundHandler :: setSoundsOn( true ); + saveSetting( "SoundOn", "true" ); break; case MENU_SOUND_OFF: SoundHandler :: setSoundsOn( false ); + saveSetting( "SoundOn", "false" ); break; case MENU_MUSIC_ON: SoundHandler :: setMusicOn( true ); + saveSetting( "MusicOn", "true" ); break; case MENU_MUSIC_OFF: SoundHandler :: setMusicOn( false ); + saveSetting( "MusicOn", "false" ); break; + case MENU_CUSTOM_THRUST: + customPlayerMenuVal = PLAYER_THRUST; + origValue = currentGame->getPlayer()->getValue( customPlayerMenuVal ); + setMenuStatusText( currentGame->getPlayer()->getValueString( customPlayerMenuVal ) ); + break; + case MENU_CUSTOM_GRAVITY: + customPlayerMenuVal = PLAYER_GRAVITY; + origValue = currentGame->getPlayer()->getValue( customPlayerMenuVal ); + setMenuStatusText( currentGame->getPlayer()->getValueString( customPlayerMenuVal ) ); + break; + case MENU_CUSTOM_MAXSPEEDUP: + customPlayerMenuVal = PLAYER_MAX_SPEED_UP; + origValue = currentGame->getPlayer()->getValue( customPlayerMenuVal ); + setMenuStatusText( currentGame->getPlayer()->getValueString( customPlayerMenuVal ) ); + break; + case MENU_CUSTOM_MAXSPEEDDOWN: + customPlayerMenuVal = PLAYER_MAX_SPEED_DOWN; + origValue = currentGame->getPlayer()->getValue( customPlayerMenuVal ); + setMenuStatusText( currentGame->getPlayer()->getValueString( customPlayerMenuVal ) ); + break; + case MENU_CUSTOM_INCREASE: + currentGame->getPlayer()->incValue( customPlayerMenuVal ); + setMenuStatusText( currentGame->getPlayer()->getValueString( customPlayerMenuVal ) ); + break; + case MENU_CUSTOM_DECREASE: + currentGame->getPlayer()->decValue( customPlayerMenuVal ); + setMenuStatusText( currentGame->getPlayer()->getValueString( customPlayerMenuVal ) ); + break; + case MENU_CUSTOM_SAVE: + { + // save settings + string key = currentGame->getGameName() + "_custom_player_" + currentGame->getPlayer()->getValueTypeString( customPlayerMenuVal ); + saveSetting( key, currentGame->getPlayer()->getValue( customPlayerMenuVal ) ); + + break; + } + case MENU_CUSTOM_CANCEL: + currentGame->getPlayer()->setValue( customPlayerMenuVal, origValue ); + break; + default: break; } } @@ -532,8 +545,20 @@ void SFCave :: saveSetting( string key, int val ) Settings cfg( "sfcave-sdl" ); cfg.writeSetting( key, val ); } +void SFCave :: saveSetting( string key, long val ) +{ + Settings cfg( "sfcave-sdl" ); + cfg.writeSetting( key, val ); +} + +void SFCave :: saveSetting( string key, double val ) +{ + Settings cfg( "sfcave-sdl" ); + cfg.writeSetting( key, val ); +} + string SFCave :: loadSetting( string key, string defaultVal ) { string val; Settings cfg( "sfcave-sdl" ); @@ -543,4 +568,66 @@ string SFCave :: loadSetting( string key, string defaultVal ) val = defaultVal; return val; } + +bool SFCave :: loadBoolSetting( string key, bool defaultVal ) +{ + bool val = defaultVal; + Settings cfg( "sfcave-sdl" ); + cfg.readSetting( key, val ); + + return val; +} + +int SFCave :: loadIntSetting( string key, int defaultVal ) +{ + int val = defaultVal; + Settings cfg( "sfcave-sdl" ); + cfg.readSetting( key, val ); + + return val; +} + +double SFCave :: loadDoubleSetting( string key, double defaultVal ) +{ + double val = defaultVal; + Settings cfg( "sfcave-sdl" ); + cfg.readSetting( key, val ); + + return val; +} + + +void SFCave :: calcFPS() +{ + struct timeb tp; + ftime( &tp ); + start =(tp.time%10000)*10000 + tp.millitm; + if ( start - time1 >= 1000 ) + { + actualFPS = FPS; + FPS = 0; + time1 = start; + } + else + FPS ++; +} + +void SFCave :: FPSDelay() +{ + struct timeb tp; + // Slow down polling - limit to x FPS + ftime( &tp ); + end = abs((tp.time%10000)*10000 + tp.millitm); + if ( end-start < (1000/maxFPS) ) + { + if ( (1000/maxFPS)-(end-start) > 500 ) + { + // Should never happen but in case it does sleep for 5 seconds + printf( "WARNING WILL ROBINSON! delay = %ld - start %ld, end %ld\n", (1000/maxFPS)-(end-start), start, end ); + SDL_Delay( 5 ); + } + else + SDL_Delay((1000/maxFPS)-(end-start) ); + } +} 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 @@ -14,9 +14,8 @@ class SFCave public: SFCave( int argc, char *argv[] ); ~SFCave(); - void drawGameScreen(); void initSDL( int argc, char *argv[] ); void mainEventLoop(); void setCrashed( bool val ); @@ -29,22 +28,45 @@ public: void setMenuStatusText( string statusText ); 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 @@ -38,8 +38,21 @@ void SFCaveGame :: init() break; 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 ) blocks[i].y( -1 ); @@ -67,9 +80,8 @@ 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 @@ -30,17 +30,20 @@ bool SoundHandler :: init( ) music = 0;
soundOn = true;
+ musicOn = true;
return true;
}
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 );
@@ -51,9 +54,9 @@ 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];
if( channel == -1 || !Mix_Playing( channel ) )
@@ -100,16 +103,16 @@ 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
Mix_PlayMusic( music, -1 );
@@ -145,9 +148,9 @@ void SoundHandler :: setSoundsOn( bool val ) void SoundHandler :: setMusicOn( bool val )
{
musicOn = val;
-
+
if ( !musicOn )
stopMusic();
else
playMusic( true );
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 @@ -160,9 +160,9 @@ 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;
if (pos_x[i] > 240)
@@ -220,4 +220,76 @@ 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 @@ -46,8 +46,9 @@ void Terrain :: initTerrain() for ( int i = 1 ; i < MAPSIZE ; ++i ) setPoint( i ); SDL_FillRect( terrainSurface, 0, 0 ); + // Draw Terrain into surface Sint16 px[5]; Sint16 py[5]; @@ -90,9 +91,8 @@ 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 ) { @@ -194,39 +194,23 @@ void Terrain :: drawTerrain( SDL_Surface *screen ) dst.x = offset; dst.y = 0; dst.w = sWidth; dst.h = sHeight; -// dst.h = maxHeight; SDL_Rect dst2; dst2.x = 0; dst2.y = 0; 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 ); Uint32 c = getpixel( terrainSurface, x, y ); @@ -246,9 +230,9 @@ SDL_Surface *screen; 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()); exit(1); 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 @@ -34,9 +34,9 @@ Uint32 getpixel(SDL_Surface *surface, int x, int y) return 0; /* shouldn't happen, but avoids warnings */
}
}
-const char *chooseRandomFile( string path, string fileType )
+string chooseRandomFile( string path, string fileType )
{
vector<string> files;
DIR *d = opendir( path.c_str() );
if ( !d )
@@ -49,16 +49,27 @@ 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 @@ -4,7 +4,7 @@ #include <string>
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
|