From c1a449ec1b6c381dbda54c47e0f984cbdba09ab4 Mon Sep 17 00:00:00 2001 From: Ingo Ruhnke Date: Sat, 10 Apr 2004 20:26:13 +0000 Subject: [PATCH] - turned gameloop into a class, in the hope to reduce some global variables in the long run SVN-Revision: 456 --- src/gameloop.cpp | 19 +++++++++++-------- src/gameloop.h | 11 ++++++++--- src/leveleditor.cpp | 5 ++++- src/setup.cpp | 8 ++++++-- src/supertux.cpp | 3 ++- src/worldmap.cpp | 5 +++-- 6 files changed, 34 insertions(+), 17 deletions(-) diff --git a/src/gameloop.cpp b/src/gameloop.cpp index d602d9d0e..3b8ceb2dd 100644 --- a/src/gameloop.cpp +++ b/src/gameloop.cpp @@ -579,10 +579,8 @@ void game_draw(void) /* --- GAME LOOP! --- */ -int gameloop(const char * subset, int levelnb, int mode) +GameSession::GameSession(const char * subset, int levelnb, int mode) { - int fps_cnt, jump, done; - timer_type fps_timer, frame_timer; timer_init(&fps_timer, true); timer_init(&frame_timer, true); @@ -627,11 +625,18 @@ int gameloop(const char * subset, int levelnb, int mode) if(st_gl_mode == ST_GL_LOAD_GAME) loadgame(levelnb); +} - /* --- MAIN GAME LOOP!!! --- */ +int +GameSession::run() +{ + int fps_cnt; + bool jump; + bool done; + /* --- MAIN GAME LOOP!!! --- */ jump = false; - done = 0; + done = false; quit = 0; global_frame_counter = 0; game_pause = 0; @@ -641,14 +646,12 @@ int gameloop(const char * subset, int levelnb, int mode) fps_cnt = 0; /* Clear screen: */ - clearscreen(0, 0, 0); updatescreen(); /* Play music: */ play_current_music(); - while (SDL_PollEvent(&event)) {} @@ -691,7 +694,7 @@ int gameloop(const char * subset, int levelnb, int mode) break; case 7: st_pause_ticks_stop(); - done = 1; + done = true; break; } } diff --git a/src/gameloop.h b/src/gameloop.h index 6e940c44c..542b7b174 100644 --- a/src/gameloop.h +++ b/src/gameloop.h @@ -29,11 +29,16 @@ extern Level current_level; extern int game_started; -/* Function prototypes: */ -class Tile; +class GameSession +{ + private: + timer_type fps_timer, frame_timer; + public: + GameSession(const char * subset, int levelnb, int mode); + int run(); +}; void activate_bad_guys(Level* plevel); -int gameloop(const char * subset, int levelnb, int mode); void savegame(int slot); void loadgame(int slot); std::string slotinfo(int slot); diff --git a/src/leveleditor.cpp b/src/leveleditor.cpp index f95e9b44b..5f0fc8326 100644 --- a/src/leveleditor.cpp +++ b/src/leveleditor.cpp @@ -1186,7 +1186,10 @@ void le_change(float x, float y, int tm, unsigned int c) void le_testlevel() { le_current_level->save("test", le_level); - gameloop("test",le_level, ST_GL_TEST); + + GameSession session("test",le_level, ST_GL_TEST); + session.run(); + Menu::set_current(leveleditor_menu); arrays_free(); le_current_level->load_gfx(); diff --git a/src/setup.cpp b/src/setup.cpp index 1e87b4380..1e1b22b63 100644 --- a/src/setup.cpp +++ b/src/setup.cpp @@ -468,7 +468,9 @@ bool process_load_game_menu() if (tmp.length() == strlen("Slot X - Free")) { // Slot is free, so start a new game - gameloop("default", 1, ST_GL_PLAY); + GameSession session("default", 1, ST_GL_PLAY); + session.run(); + show_menu = true; Menu::set_current(main_menu); } @@ -476,7 +478,9 @@ bool process_load_game_menu() { // Slot contains a level, so load it if (game_started) { - gameloop("default",slot - 1,ST_GL_LOAD_GAME); + GameSession session("default",slot - 1,ST_GL_LOAD_GAME); + session.run(); + show_menu = true; Menu::set_current(main_menu); } diff --git a/src/supertux.cpp b/src/supertux.cpp index d43ca9eb2..5d90b61ff 100644 --- a/src/supertux.cpp +++ b/src/supertux.cpp @@ -34,7 +34,8 @@ int main(int argc, char * argv[]) } else if (level_startup_file) { - gameloop(level_startup_file, 1, ST_GL_LOAD_LEVEL_FILE); + GameSession session(level_startup_file, 1, ST_GL_LOAD_LEVEL_FILE); + session.run(); } else { diff --git a/src/worldmap.cpp b/src/worldmap.cpp index 6c8d91812..19db528a6 100644 --- a/src/worldmap.cpp +++ b/src/worldmap.cpp @@ -417,8 +417,9 @@ WorldMap::update() { std::cout << "Enter the current level: " << i->name << std::endl;; halt_music(); - gameloop(const_cast((datadir + "levels/default/" + i->name).c_str()), - 1, ST_GL_LOAD_LEVEL_FILE); + GameSession session(const_cast((datadir + "levels/default/" + i->name).c_str()), + 1, ST_GL_LOAD_LEVEL_FILE); + session.run(); play_music(song, 1); return; } -- 2.11.0