From: Tobias Gläßer Date: Tue, 27 Jul 2004 13:25:05 +0000 (+0000) Subject: C++ifyied SuperTux ticks. X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=39b41c92197217841cf737496c1c1755a21e7ae5;p=supertux.git C++ifyied SuperTux ticks. SVN-Revision: 1631 --- diff --git a/lib/special/timer.cpp b/lib/special/timer.cpp index ae0763210..ed3608ed6 100644 --- a/lib/special/timer.cpp +++ b/lib/special/timer.cpp @@ -23,40 +23,40 @@ using namespace SuperTux; -unsigned int SuperTux::st_pause_ticks, SuperTux::st_pause_count; +unsigned int Ticks::pause_ticks, Ticks::pause_count; -unsigned int SuperTux::st_get_ticks(void) +unsigned int Ticks::get(void) { - if(st_pause_count != 0) - return /*SDL_GetTicks()*/ - st_pause_ticks /*- SDL_GetTicks()*/ + st_pause_count; + if(pause_count != 0) + return /*SDL_GetTicks()*/ - pause_ticks /*- SDL_GetTicks()*/ + pause_count; else - return SDL_GetTicks() - st_pause_ticks; + return SDL_GetTicks() - pause_ticks; } -void SuperTux::st_pause_ticks_init(void) +void Ticks::pause_init(void) { - st_pause_ticks = 0; - st_pause_count = 0; + pause_ticks = 0; + pause_count = 0; } -void SuperTux::st_pause_ticks_start(void) +void Ticks::pause_start(void) { - if(st_pause_count == 0) - st_pause_count = SDL_GetTicks(); + if(pause_count == 0) + pause_count = SDL_GetTicks(); } -void SuperTux::st_pause_ticks_stop(void) +void Ticks::pause_stop(void) { -if(st_pause_count == 0) +if(pause_count == 0) return; - st_pause_ticks += SDL_GetTicks() - st_pause_count; - st_pause_count = 0; + pause_ticks += SDL_GetTicks() - pause_count; + pause_count = 0; } -bool SuperTux::st_pause_ticks_started(void) +bool Ticks::pause_started(void) { -if(st_pause_count == 0) +if(pause_count == 0) return false; else return true; @@ -68,11 +68,11 @@ Timer::Timer() } void -Timer::init(bool st_ticks) +Timer::init(bool game_ticks) { period = 0; time = 0; - get_ticks = st_ticks ? st_get_ticks : SDL_GetTicks; + get_ticks = game_ticks ? Ticks::get : SDL_GetTicks; } void @@ -85,7 +85,7 @@ Timer::start(unsigned int period_) void Timer::stop() { - if(get_ticks == st_get_ticks) + if(get_ticks == get_ticks) init(true); else init(false); @@ -136,7 +136,7 @@ Timer::fwrite(FILE* fi) ::fwrite(&period,sizeof(unsigned int),1,fi); ::fwrite(&diff_ticks,sizeof(unsigned int),1,fi); - if(get_ticks == st_get_ticks) + if(get_ticks == get_ticks) tick_mode = true; else tick_mode = false; @@ -154,7 +154,7 @@ Timer::fread(FILE* fi) ::fread(&tick_mode,sizeof(unsigned int),1,fi); if (tick_mode) - get_ticks = st_get_ticks; + get_ticks = get_ticks; else get_ticks = SDL_GetTicks; diff --git a/lib/special/timer.h b/lib/special/timer.h index 113ea063a..0c988818f 100644 --- a/lib/special/timer.h +++ b/lib/special/timer.h @@ -24,22 +24,29 @@ namespace SuperTux { - extern unsigned int st_pause_ticks, st_pause_count; + class Ticks + { + public: + /// Time a game is running. (Non-pause mode, etc.) + static unsigned int get(void); - /// Time a game is running. (Non-pause mode, etc.) - unsigned int st_get_ticks(void); + static void pause_init(void); + static void pause_start(void); + static void pause_stop(void); + static bool pause_started(void); - void st_pause_ticks_init(void); - void st_pause_ticks_start(void); - void st_pause_ticks_stop(void); - bool st_pause_ticks_started(void); + private: + static unsigned int pause_ticks; + static unsigned int pause_count; + + }; /// Timer /** This class can be used as stop watch for example. It's also possible to calculate frames per seconds and things like that with it. It's a general timing class, but it - can esspecially be used together with st_get_ticks(). */ + can esspecially be used together with Ticks::get(). */ class Timer { public: @@ -51,8 +58,8 @@ namespace SuperTux Timer(); /// Initialize the timer. - /** @param st_ticks: If true internally st_get_ticks() is used, else SDL_GetTicks() is used. */ - void init(bool st_ticks); + /** @param st_ticks: If true internally Ticks::get() is used, else SDL_GetTicks() is used. */ + void init(bool game_ticks); /// Start the timer with the given period (in ms). void start(unsigned int period); diff --git a/src/gameloop.cpp b/src/gameloop.cpp index 1fa2f9318..4d7c4dd29 100644 --- a/src/gameloop.cpp +++ b/src/gameloop.cpp @@ -189,8 +189,8 @@ void GameSession::start_timers() { time_left.start(level->time_left*1000); - st_pause_ticks_init(); - update_time = st_get_ticks(); + Ticks::pause_init(); + update_time = Ticks::get(); } void @@ -219,7 +219,7 @@ GameSession::on_escape_press() tux.key_event((SDLKey)keymap.fire, UP); Menu::set_current(game_menu); - st_pause_ticks_start(); + Ticks::pause_start(); } } @@ -250,7 +250,7 @@ GameSession::process_events() { Menu::current()->event(event); if(!Menu::current()) - st_pause_ticks_stop(); + Ticks::pause_stop(); } switch(event.type) @@ -283,7 +283,7 @@ GameSession::process_events() else // normal mode { if(!Menu::current() && !game_pause) - st_pause_ticks_stop(); + Ticks::pause_stop(); SDL_Event event; while (SDL_PollEvent(&event)) @@ -293,7 +293,7 @@ GameSession::process_events() { Menu::current()->event(event); if(!Menu::current()) - st_pause_ticks_stop(); + Ticks::pause_stop(); } else { @@ -350,12 +350,12 @@ GameSession::process_events() if(game_pause) { game_pause = false; - st_pause_ticks_stop(); + Ticks::pause_stop(); } else { game_pause = true; - st_pause_ticks_start(); + Ticks::pause_start(); } } break; @@ -573,10 +573,10 @@ GameSession::process_menu() switch (game_menu->check()) { case MNID_CONTINUE: - st_pause_ticks_stop(); + Ticks::pause_stop(); break; case MNID_ABORTLEVEL: - st_pause_ticks_stop(); + Ticks::pause_stop(); exit_status = ES_LEVEL_ABORT; break; } @@ -600,7 +600,7 @@ GameSession::run() int fps_cnt = 0; - update_time = last_update_time = st_get_ticks(); + update_time = last_update_time = Ticks::get(); // Eat unneeded events SDL_Event event; @@ -654,7 +654,7 @@ GameSession::run() /* Set the time of the last update and the time of the current update */ last_update_time = update_time; - update_time = st_get_ticks(); + update_time = Ticks::get(); /* Pause till next frame, if the machine running the game is too fast: */ /* FIXME: Works great for in OpenGl mode, where the CPU doesn't have to do that much. But @@ -662,7 +662,7 @@ GameSession::run() if(last_update_time >= update_time - 12) { SDL_Delay(10); - update_time = st_get_ticks(); + update_time = Ticks::get(); } /* Handle time: */ @@ -866,7 +866,7 @@ bool process_load_game_menu() Menu::set_current(main_menu); - st_pause_ticks_stop(); + Ticks::pause_stop(); return true; } else diff --git a/src/title.cpp b/src/title.cpp index a12c855fd..ddb0c0138 100644 --- a/src/title.cpp +++ b/src/title.cpp @@ -262,7 +262,7 @@ void title(void) walking = true; - st_pause_ticks_init(); + Ticks::pause_init(); titlesession = new GameSession(datadir + "/levels/misc/menu.stl", ST_GL_DEMO_GAME); @@ -280,7 +280,7 @@ void title(void) /* --- Main title loop: --- */ frame = 0; - update_time = st_get_ticks(); + update_time = Ticks::get(); random_timer.start(rand() % 2000 + 2000); Menu::set_current(main_menu); @@ -289,7 +289,7 @@ void title(void) { // if we spent to much time on a menu entry if( (update_time - last_update_time) > 1000) - update_time = last_update_time = st_get_ticks(); + update_time = last_update_time = Ticks::get(); // Calculate the movement-factor double frame_ratio = ((double)(update_time-last_update_time))/((double)FRAME_RATE); @@ -349,7 +349,7 @@ void title(void) leveleditor->run(); delete leveleditor; Menu::set_current(main_menu); - update_time = st_get_ticks(); + update_time = Ticks::get(); break; case MNID_CREDITS: display_text_file("CREDITS", SCROLL_SPEED_CREDITS, white_big_text , white_text, white_small_text, blue_text ); @@ -381,7 +381,7 @@ void title(void) update_load_save_game_menu(load_game_menu); Menu::set_current(main_menu); - update_time = st_get_ticks(); + update_time = Ticks::get(); } else if (process_load_game_menu()) { @@ -389,7 +389,7 @@ void title(void) titlesession->get_current_sector()->activate(); titlesession->set_current(); //titletux.level_begin(); - update_time = st_get_ticks(); + update_time = Ticks::get(); } } else if(menu == contrib_menu) @@ -408,7 +408,7 @@ void title(void) /* Set the time of the last update and the time of the current update */ last_update_time = update_time; - update_time = st_get_ticks(); + update_time = Ticks::get(); /* Pause: */ frame++; diff --git a/src/worldmap.cpp b/src/worldmap.cpp index f67109d4a..23b42b2df 100644 --- a/src/worldmap.cpp +++ b/src/worldmap.cpp @@ -960,7 +960,7 @@ WorldMap::display() unsigned int last_update_time; unsigned int update_time; - last_update_time = update_time = st_get_ticks(); + last_update_time = update_time = Ticks::get(); DrawingContext context; while(!quit) @@ -973,7 +973,7 @@ WorldMap::display() delta = .3f; last_update_time = update_time; - update_time = st_get_ticks(); + update_time = Ticks::get(); Vector tux_pos = tux->get_pos(); if (1)