From: Ingo Ruhnke Date: Fri, 16 Apr 2004 16:52:18 +0000 (+0000) Subject: - some more savegame stuff X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=997685a947eaa0b269873e5956908b734cde1d76;p=supertux.git - some more savegame stuff - added status on worldmap - made worldmap start on 'start game' - moved lives, distros, score into PlayerStatus SVN-Revision: 533 --- diff --git a/src/gameloop.cpp b/src/gameloop.cpp index 68d986924..bf0f9039c 100644 --- a/src/gameloop.cpp +++ b/src/gameloop.cpp @@ -117,9 +117,6 @@ GameSession::GameSession(const std::string& subset_, int levelnb_, int mode) time_left.init(true); start_timers(); - - if(st_gl_mode == ST_GL_LOAD_GAME) - loadgame(levelnb); } GameSession::~GameSession() @@ -130,8 +127,6 @@ GameSession::~GameSession() void GameSession::levelintro(void) { - Player& tux = *world->get_tux(); - char str[60]; /* Level Intro: */ clearscreen(0, 0, 0); @@ -145,7 +140,7 @@ GameSession::levelintro(void) sprintf(str, "by %s", world->get_level()->author.c_str()); red_text->drawf(str, 0, 256, A_HMIDDLE, A_TOP, 1); - sprintf(str, "TUX x %d", tux.lives); + sprintf(str, "TUX x %d", player_status.lives); white_text->drawf(str, 0, 288, A_HMIDDLE, A_TOP, 1); flipscreen(); @@ -267,7 +262,7 @@ GameSession::process_events() break; case SDLK_l: if(debug_mode) - --tux.lives; + --player_status.lives; break; case SDLK_s: if(debug_mode) @@ -375,7 +370,7 @@ GameSession::action(double frame_ratio) /* No more lives!? */ - if (tux.lives < 0) + if (player_status.lives < 0) { if(st_gl_mode != ST_GL_TEST) drawendscreen(); @@ -538,10 +533,6 @@ GameSession::run() { process_options_menu(); } - else if(current_menu == save_game_menu ) - { - process_save_game_menu(); - } else if(current_menu == load_game_menu ) { process_load_game_menu(); @@ -658,7 +649,6 @@ void bumpbrick(float x, float y) void GameSession::drawstatus() { - Player& tux = *world->get_tux(); char str[60]; sprintf(str, "%d", player_status.score); @@ -696,7 +686,7 @@ GameSession::drawstatus() gold_text->draw(str, screen->h + 60, 40, 1); } - for(int i= 0; i < tux.lives; ++i) + for(int i= 0; i < player_status.lives; ++i) { tux_life->draw(565+(18*i),20); } @@ -714,7 +704,7 @@ GameSession::drawendscreen() sprintf(str, "SCORE: %d", player_status.score); gold_text->drawf(str, 0, 224, A_HMIDDLE, A_TOP, 1); - sprintf(str, "DISTROS: %d", player_status.distros); + sprintf(str, "COINS: %d", player_status.distros); gold_text->drawf(str, 0, 256, A_HMIDDLE, A_TOP, 1); flipscreen(); @@ -744,100 +734,6 @@ GameSession::drawresultscreen(void) wait_for_event(event,2000,5000,true); } -void -GameSession::savegame(int) -{ -#if 0 - char savefile[1024]; - FILE* fi; - unsigned int ui; - - sprintf(savefile,"%s/slot%d.save",st_save_dir,slot); - - fi = fopen(savefile, "wb"); - - if (fi == NULL) - { - fprintf(stderr, "Warning: I could not open the slot file "); - } - else - { - fputs(level_subset, fi); - fputs("\n", fi); - fwrite(&level,sizeof(int),1,fi); - fwrite(&score,sizeof(int),1,fi); - fwrite(&distros,sizeof(int),1,fi); - fwrite(&scroll_x,sizeof(float),1,fi); - //FIXME:fwrite(&tux,sizeof(Player),1,fi); - //FIXME:timer_fwrite(&tux.invincible_timer,fi); - //FIXME:timer_fwrite(&tux.skidding_timer,fi); - //FIXME:timer_fwrite(&tux.safe_timer,fi); - //FIXME:timer_fwrite(&tux.frame_timer,fi); - timer_fwrite(&time_left,fi); - ui = st_get_ticks(); - fwrite(&ui,sizeof(int),1,fi); - } - fclose(fi); -#endif -} - -void -GameSession::loadgame(int) -{ -#if 0 - char savefile[1024]; - char str[100]; - FILE* fi; - unsigned int ui; - - sprintf(savefile,"%s/slot%d.save",st_save_dir,slot); - - fi = fopen(savefile, "rb"); - - if (fi == NULL) - { - fprintf(stderr, "Warning: I could not open the slot file "); - - } - else - { - fgets(str, 100, fi); - strcpy(level_subset, str); - level_subset[strlen(level_subset)-1] = '\0'; - fread(&level,sizeof(int),1,fi); - - world->set_defaults(); - world->get_level()->cleanup(); - world->arrays_free(); - world->get_level()->free_gfx(); - world->get_level()->free_song(); - - if(world->get_level()->load(level_subset,level) != 0) - exit(1); - - world->activate_bad_guys(); - world->activate_particle_systems(); - world->get_level()->load_gfx(); - world->get_level()->load_song(); - - levelintro(); - update_time = st_get_ticks(); - - fread(&score, sizeof(int),1,fi); - fread(&distros, sizeof(int),1,fi); - fread(&scroll_x,sizeof(float),1,fi); - //FIXME:fread(&tux, sizeof(Player), 1, fi); - //FIXME:timer_fread(&tux.invincible_timer,fi); - //FIXME:timer_fread(&tux.skidding_timer,fi); - //FIXME:timer_fread(&tux.safe_timer,fi); - //FIXME:timer_fread(&tux.frame_timer,fi); - timer_fread(&time_left,fi); - fread(&ui,sizeof(int),1,fi); - fclose(fi); - } -#endif -} - std::string slotinfo(int slot) { FILE* fi; diff --git a/src/gameloop.h b/src/gameloop.h index 27f6ce1bb..59cf3a4fc 100644 --- a/src/gameloop.h +++ b/src/gameloop.h @@ -69,9 +69,6 @@ class GameSession Level* get_level() { return world->get_level(); } World* get_world() { return world; } - void savegame(int slot); - void loadgame(int slot); - static GameSession* current() { return current_; } private: static GameSession* current_; diff --git a/src/player.cpp b/src/player.cpp index 0cf24388c..0a5de95d8 100644 --- a/src/player.cpp +++ b/src/player.cpp @@ -83,10 +83,7 @@ Player::init() frame_main = 0; frame_ = 0; - lives = 3; - score = 0; - distros = 0; - + player_input_init(&input); keymap.jump = SDLK_UP; @@ -504,11 +501,11 @@ Player::grabdistros() } /* Enough distros for a One-up? */ - if (distros >= DISTROS_LIFEUP) + if (player_status.distros >= DISTROS_LIFEUP) { - distros = distros - DISTROS_LIFEUP; - if(lives < MAX_LIVES) - lives++; + player_status.distros = player_status.distros - DISTROS_LIFEUP; + if(player_status.lives < MAX_LIVES) + ++player_status.lives; /*We want to hear the sound even, if MAX_LIVES is reached*/ play_sound(sounds[SND_LIFEUP], SOUND_CENTER_SPEAKER); } @@ -723,9 +720,6 @@ Player::draw() } } - if(dying) - gold_text->drawf("Penguins can fly !:",0,0,A_HMIDDLE,A_VMIDDLE,1); - if (debug_mode) fillrect(base.x - scroll_x, base.y, 32, 32, 75,75,75, 150); } @@ -830,7 +824,7 @@ Player::is_dying() { /* He died :^( */ - --lives; + --player_status.lives; remove_powerups(); dying = DYING_NOT; } diff --git a/src/player.h b/src/player.h index 62237d273..97e6df17d 100644 --- a/src/player.h +++ b/src/player.h @@ -94,9 +94,6 @@ class Player { public: player_keymap_type keymap; - int lives; - int score; - int distros; player_input_type input; bool got_coffee; diff --git a/src/scene.cpp b/src/scene.cpp index 2ed15f417..681500f4d 100644 --- a/src/scene.cpp +++ b/src/scene.cpp @@ -15,6 +15,13 @@ PlayerStatus player_status; +PlayerStatus::PlayerStatus() + : score(0), + distros(0), + lives(3) +{ +} + // FIXME: Move this into a view class float scroll_x; diff --git a/src/scene.h b/src/scene.h index a517b8e2d..76c5c8929 100644 --- a/src/scene.h +++ b/src/scene.h @@ -23,8 +23,12 @@ struct PlayerStatus { int score; int distros; + int lives; + int next_level; int score_multiplier; + + PlayerStatus(); }; extern PlayerStatus player_status; diff --git a/src/setup.cpp b/src/setup.cpp index ffd9e3f18..62781e332 100644 --- a/src/setup.cpp +++ b/src/setup.cpp @@ -40,6 +40,7 @@ #include "gameloop.h" #include "configfile.h" #include "scene.h" +#include "worldmap.h" #ifdef WIN32 #define mkdir(dir, mode) mkdir(dir) @@ -455,13 +456,6 @@ void update_load_save_game_menu(Menu* pmenu, int load) } } -void process_save_game_menu() -{ - int slot = save_game_menu->check(); - if (slot != -1) - GameSession::current()->savegame(slot - 1); -} - bool process_load_game_menu() { int slot = load_game_menu->check(); @@ -474,30 +468,14 @@ bool process_load_game_menu() if (tmp.length() == strlen("Slot X - Free")) { // Slot is free, so start a new game - GameSession session("default", 1, ST_GL_PLAY); - session.run(); - + worldmap_run(); + show_menu = true; Menu::set_current(main_menu); } else { puts("Warning: Loading games isn't supported at the moment"); -#if 0 - // Slot contains a level, so load it - if (game_started) - { - GameSession session("default",slot - 1,ST_GL_LOAD_GAME); - session.run(); - - show_menu = true; - Menu::set_current(main_menu); - } - else - { - //loadgame(slot - 1); - } -#endif } st_pause_ticks_stop(); return true; diff --git a/src/setup.h b/src/setup.h index 6e1b165a3..0f32be63e 100644 --- a/src/setup.h +++ b/src/setup.h @@ -36,8 +36,6 @@ void st_menu(void); void st_abort(const std::string& reason, const std::string& details); void process_options_menu(void); -void process_save_game_menu(); - /** Return true if the gameloop() was entered, false otherwise */ bool process_load_game_menu(); diff --git a/src/special.cpp b/src/special.cpp index e3f80615a..25ffd17ad 100644 --- a/src/special.cpp +++ b/src/special.cpp @@ -307,8 +307,8 @@ Upgrade::collision(void* p_c_object, int c_object) } else if (kind == UPGRADE_1UP) { - if(pplayer->lives < MAX_LIVES) { - pplayer->lives++; + if(player_status.lives < MAX_LIVES) { + player_status.lives++; play_sound(sounds[SND_LIFEUP], SOUND_CENTER_SPEAKER); } } diff --git a/src/text.h b/src/text.h index fd4fde87b..ebe414ed3 100644 --- a/src/text.h +++ b/src/text.h @@ -47,10 +47,10 @@ class Text Text(const std::string& file, int kind, int w, int h); ~Text(); - void draw(const char* text, int x, int y, int shadowsize, int update = NO_UPDATE); + void draw(const char* text, int x, int y, int shadowsize = 1, int update = NO_UPDATE); void draw_chars(Surface* pchars, const char* text, int x, int y, int update = NO_UPDATE); void drawf(const char* text, int x, int y, TextHAlign halign, TextVAlign valign, int shadowsize, int update = NO_UPDATE); - void draw_align(const char* text, int x, int y, TextHAlign halign, TextVAlign valign, int shadowsize, int update = NO_UPDATE); + void draw_align(const char* text, int x, int y, TextHAlign halign, TextVAlign valign, int shadowsize = 1, int update = NO_UPDATE); void erasetext(const char * text, int x, int y, Surface* surf, int update, int shadowsize); void erasecenteredtext(const char * text, int y, Surface* surf, int update, int shadowsize); }; diff --git a/src/worldmap.cpp b/src/worldmap.cpp index 0e6bd3d76..f6098b5ec 100644 --- a/src/worldmap.cpp +++ b/src/worldmap.cpp @@ -18,6 +18,7 @@ // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include +#include #include #include #include "globals.h" @@ -270,6 +271,7 @@ WorldMap::load_map() { Level level; LispReader reader(lisp_cdr(element)); + level.solved = false; reader.read_string("name", &level.name); reader.read_int("x", &level.x); reader.read_int("y", &level.y); @@ -438,10 +440,17 @@ WorldMap::update() { std::cout << "Enter the current level: " << i->name << std::endl;; halt_music(); - GameSession session(datadir + "levels/default/" + i->name, + GameSession session(datadir + "levels/" + i->name, 1, ST_GL_LOAD_LEVEL_FILE); session.run(); + + if (1) // FIXME: insert exit status checker here + i->solved = true; + play_music(song, 1); + show_menu = 0; + menu_reset(); + savegame(std::string(st_save_dir) + "/slot1.stsg"); return; } } @@ -499,11 +508,54 @@ WorldMap::draw(const Point& offset) } tux->draw(offset); + draw_status(); +} + +void +WorldMap::draw_status() +{ + char str[80]; + sprintf(str, "%d", player_status.score); + white_text->draw("SCORE", 0, 0); + gold_text->draw(str, 96, 0); + + sprintf(str, "%d", player_status.distros); + white_text->draw_align("COINS", 320-64, 0, A_LEFT, A_TOP); + gold_text->draw_align(str, 320+64, 0, A_RIGHT, A_TOP); + + white_text->draw("LIVES", 480, 0); + if (player_status.lives >= 5) + { + sprintf(str, "%dx", player_status.lives); + gold_text->draw(str, 585, 0); + tux_life->draw(565+(18*3), 0); + } + else + { + for(int i= 0; i < player_status.lives; ++i) + tux_life->draw(565+(18*i),0); + } + + if (!tux->is_moving()) + { + for(Levels::iterator i = levels.begin(); i != levels.end(); ++i) + { + if (i->x == tux->get_tile_pos().x && + i->y == tux->get_tile_pos().y) + { + white_text->draw_align(i->name.c_str(), screen->w/2, screen->h, A_HMIDDLE, A_BOTTOM); + break; + } + } + } } void WorldMap::display() { + show_menu = 0; + menu_reset(); + quit = false; song = load_song(datadir + "/music/" + music); @@ -536,6 +588,32 @@ WorldMap::display() free_music(song); } +void +WorldMap::savegame(const std::string& filename) +{ + std::ofstream out(filename.c_str()); + + out << "(supertux-savegame\n" + << " (version 1)\n" + << " (lives " << player_status.lives << ")\n" + << " (score " << player_status.score << ")\n" + << " (distros " << player_status.distros << ")\n" + << " (tux (x " << tux->get_tile_pos().x << ") (y " << tux->get_tile_pos().y << ")\n" + << " (levels\n"; + + for(Levels::iterator i = levels.begin(); i != levels.end(); ++i) + { + if (i->solved) + { + out << " (level (name \"" << i->name << "\")\n" + << " (solved #t))\n"; + } + } + + out << " )\n" + << " )\n\n;; EOF ;;" << std::endl; +} + } // namespace WorldMapNS void worldmap_run() diff --git a/src/worldmap.h b/src/worldmap.h index 87745ae89..a96c4ff27 100644 --- a/src/worldmap.h +++ b/src/worldmap.h @@ -132,6 +132,7 @@ private: int x; int y; std::string name; + bool solved; }; typedef std::vector Levels; @@ -143,6 +144,8 @@ private: bool enter_level; Point offset; + + void draw_status(); public: WorldMap(); ~WorldMap(); @@ -166,6 +169,8 @@ public: /** Check if it is possible to walk from \a pos into \a direction, if possible, write the new position to \a new_pos */ bool path_ok(Direction direction, Point pos, Point* new_pos); + + void savegame(const std::string& filename); }; } // namespace WorldMapNS