From: Patrik Gornicz Date: Thu, 22 Jan 2009 02:07:27 +0000 (-0500) Subject: refactored main X-Git-Tag: physics-premerge~47 X-Git-Url: http://gitweb.pgornicz.com/gitweb.cgi?a=commitdiff_plain;h=e2b1ddcdc1007c0099a77eda28c377ff61d454be;p=libbear.git refactored main --- diff --git a/TODO b/TODO index 8a2c497..899d0f9 100644 --- a/TODO +++ b/TODO @@ -27,7 +27,9 @@ and a * entry is something to remember when working in this area of the project. - replace the set in effectManager - create my own list - create my own queue - - repleace queue in entityCreator + - replace queue in entityCreator + +- figure out a better way to sync the initializers of the threads ******************************************************************************* diff --git a/src/main.cpp b/src/main.cpp index 050aff7..2744e86 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -31,56 +31,57 @@ /// ***** Private Method Headers ***** -void mainInit(); -void mainClean(); +static void mainInit(); +static void mainClean(); -void updatesInit(); -void updatesClean(); +static void updatesInit(); +static void updatesClean(); -void drawInit(); -void drawClean(); +static void drawInit(); +static void drawClean(); -void sighandler( int sig ); +static void run(); -void run(); -void clean(); +static void updateFPSCounters(); -void updateFPSCounters(); +static void handleInput(); +static void update(float); +static void draw(); -void handleInput(); -void update(float); -void draw(); - -int startUpdateThread(void*); -int startDrawThread(void*); +static int startUpdateThread(void*); +static int startDrawThread(void*); /// ***** Private Variables ***** // variable used to determine if it is time to shutdown -bool is_Running; +static bool s_bIsRunning; + +// Minimum possible wait times by used library +static const int s_iMinWaitMilli = 20; +static const int s_iMinWaitMicro = s_iMinWaitMilli * 1000; /* Values for the main game loop - * target_UPS := the amount of updates that is wanted in one second * - * ups := updates per second for the last second - * fps := frames per second for the last second - * update_Count := counts this seconds updates - * draw_Count := counts this seconds draws - * last_Second := stores the time of the last second, used for ups and fps + * s_iTargetUPS := the amount of updates that is wanted in one second + * s_fAccUpdateWait := the accumulated wait time for the update sleeps + * + * s_iUPS := updates per second for the last second + * s_iFPS := frames per second for the last second + * s_iUpdateCount := counts this seconds updates + * s_iDrawCount := counts this seconds draws + * s_micLastSecond := stores the time of the last second, used for ups and fps */ -int target_UPS = 100; -float update_Wait = 0; +static int s_iTargetUPS = 100; +static float s_fAccUpdateWait = 0; -int ups, fps; -int update_Count, draw_Count; -MICRO last_Second; +static int s_iUPS, s_iFPS; +static int s_iUpdateCount, s_iDrawCount; +static MICRO s_micLastSecond; -const int min_wait_milli = 20; -const int min_wait_micro = min_wait_milli * 1000; -const float game_step = 10; +static const float s_fGameStep = 10; -bool updateInitialized = false; -bool drawInitialized = false; +static bool s_bUpdateInitialized = false; +static bool s_bDrawInitialized = false; /// ***** MAIN Method ***** int main(int argc, char** args) @@ -88,6 +89,7 @@ int main(int argc, char** args) mainInit(); run(); mainClean(); + return 0; } @@ -99,7 +101,6 @@ void mainInit() debug::init(); } - void mainClean() { debug::clean(); @@ -107,7 +108,7 @@ void mainClean() void updatesInit() { - while(!drawInitialized) + while(!s_bDrawInitialized) SDL_Delay(100); game::init(); @@ -121,9 +122,8 @@ void updatesInit() DPF(0, "Initialization Complete"); - updateInitialized = true; + s_bUpdateInitialized = true; } - void updatesClean() { DPF(0, "Update Thread Cleaning"); @@ -140,12 +140,11 @@ void drawInit() graphics::init(); DPF(0, "Graphics initialized"); - drawInitialized = true; + s_bDrawInitialized = true; - while(!updateInitialized) + while(!s_bUpdateInitialized) SDL_Delay(100); } - void drawClean() { DPF(0, "Draw Thread Cleaning"); @@ -154,41 +153,39 @@ void drawClean() } /// ***** Private Methods ***** -SDL_Thread* updatesThread = NULL; -SDL_Thread* drawThread = NULL; void run() { - is_Running = true; + s_bIsRunning = true; - updatesThread = SDL_CreateThread(startUpdateThread, NULL); - drawThread = SDL_CreateThread(startDrawThread, NULL); + SDL_Thread* s_pUpdatesThread = SDL_CreateThread(startUpdateThread, NULL); + SDL_Thread* s_pDrawThread = SDL_CreateThread(startDrawThread, NULL); - SDL_WaitThread(updatesThread, NULL); - SDL_WaitThread(drawThread, NULL); + SDL_WaitThread(s_pUpdatesThread, NULL); + SDL_WaitThread(s_pDrawThread, NULL); } void updateFPSCounters() { // Check if a second has passed to recalculate UPS and FPS - if (tickCountMicro() - last_Second >= 1000000) + if (tickCountMicro() - s_micLastSecond >= 1000000) { - ups = update_Count; - fps = draw_Count; + s_iUPS = s_iUpdateCount; + s_iFPS = s_iDrawCount; // NOT thread safe, but they're estimates anyways - update_Count -= ups; - draw_Count -= fps; + s_iUpdateCount -= s_iUPS; + s_iDrawCount -= s_iFPS; - last_Second = tickCountMicro(); + s_micLastSecond = tickCountMicro(); if(cfg::showFPS()) { - cout << "fps:\t" << fps << endl; + cout << "fps:\t" << s_iFPS << endl; } if(cfg::showUPS()) { - cout << "ups:\t" << ups << endl; + cout << "ups:\t" << s_iUPS << endl; } } } @@ -201,13 +198,13 @@ void handleInput() game::handleInput(); if(cfg::endGame()) - is_Running = false; + s_bIsRunning = false; } -void update(float time_step) +void update(float fTimeStep) { - game::update(time_step); - update_Count++; + game::update(fTimeStep); + s_iUpdateCount++; } void draw() @@ -220,31 +217,32 @@ void draw() glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - draw_Count++; + s_iDrawCount++; } int startUpdateThread(void*) { updatesInit(); - last_Second = tickCountMicro(); + s_micLastSecond = tickCountMicro(); - while(is_Running) + while(s_bIsRunning) { MICRO time = tickCountMicro(); handleInput(); - update(game_step); + update(s_fGameStep); updateFPSCounters(); time = tickCountMicro() - time; - float wait = (1000000.0 / target_UPS - time); - update_Wait += 0 < wait ? wait : 0; + float wait = (1000000.0 / s_iTargetUPS - time); + s_fAccUpdateWait += 0 < wait ? wait : 0; - while(min_wait_micro < update_Wait) + if(s_iMinWaitMicro < s_fAccUpdateWait) { - update_Wait -= min_wait_micro; - SDL_Delay(min_wait_milli); + int iWaits = s_fAccUpdateWait / s_iMinWaitMicro; + s_fAccUpdateWait -= iWaits * s_iMinWaitMicro; + SDL_Delay(iWaits * s_iMinWaitMilli); } } @@ -257,7 +255,7 @@ int startDrawThread(void*) { drawInit(); - while(is_Running) + while(s_bIsRunning) { draw(); }