X-Git-Url: http://gitweb.pgornicz.com/gitweb.cgi?a=blobdiff_plain;f=src%2Fmain.cpp;h=ddf2dbdeb40bcbda5a39c931d2353ecb4d364202;hb=88085362ef3590bdbae3fb4bf25796c84582cc73;hp=130982453976105d220b09aa89178bdfb883749f;hpb=8baa4b2f0b559c9c48fba41b8dc4a4d5f92f728b;p=physics.git diff --git a/src/main.cpp b/src/main.cpp index 1309824..ddf2dbd 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -15,15 +15,21 @@ * along with this program. If not, see . */ +#include +#include +using namespace bear; + +#include +using std::cout; +using std::endl; + #include #include #include -#include "debug.h" #include "handleSignal.h" #include "game.h" -#include "ticks.h" #include "graphics/graphics.h" #include "input/inputManager.h" @@ -31,64 +37,94 @@ /// ***** Private Method Headers ***** -void init(); +static void mainInit(); +static void mainClean(); + +static void updatesInit(); +static void updatesClean(); -void sighandler( int sig ); +static void drawInit(); +static void drawClean(); -void run(); -void clean(); +static void run(); -void updateFPSCounters(); +static void updateFPSCounters(); -void handleInput(); -void update(float); -void draw(); +static void handleInput(); +static void update(float); +static void draw(); + +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_iTargetFPS = 100; +static float s_fAccDrawWait = 0; -const int min_wait_milli = 20; -const int min_wait_micro = min_wait_milli * 1000; -const float game_step = 10; +static int s_iUPS, s_iFPS; +static int s_iUpdateCount, s_iDrawCount; -int startUpdateThread(void*); -int startDrawThread(void*); +static Ticks s_micLastSecond; +static Timer s_timer; + +static const float s_fGameStep = 10; + +static bool s_bUpdateInitialized = false; +static bool s_bDrawInitialized = false; /// ***** MAIN Method ***** -int main(int argc, char** args) +int main(int /*argc*/, char** /*args*/) { - init(); + mainInit(); run(); - clean(); + mainClean(); + return 0; } /// ***** Initializers/Cleaners ***** -void init() +void mainInit() { installSignal(); - graphics::init(); - DPF(0, "Graphics initialized"); + debug::init(); + + s_timer.init(); +} +void mainClean() +{ + s_timer.fini(); + + debug::fini(); +} + +void updatesInit() +{ + while(!s_bDrawInitialized) + SDL_Delay(100); game::init(); DPF(0, "Game initialized"); @@ -100,17 +136,33 @@ void init() DPF(0, "Configs initialized"); DPF(0, "Initialization Complete"); -} -void clean() + s_bUpdateInitialized = true; +} +void updatesClean() { - DPF(0, "Cleaning up"); + DPF(0, "Update Thread Cleaning"); cfg::clean(); input::clean(); game::clean(); +} + +void drawInit() +{ + graphics::init(); + DPF(0, "Graphics initialized"); + + s_bDrawInitialized = true; + + while(!s_bUpdateInitialized) + SDL_Delay(100); +} +void drawClean() +{ + DPF(0, "Draw Thread Cleaning"); graphics::clean(); } @@ -119,34 +171,37 @@ void clean() void run() { - is_Running = true; - - SDL_Thread* updates = SDL_CreateThread(startUpdateThread, NULL); + s_bIsRunning = true; - // for some reason this needs to be on the main thread ...? - startDrawThread(NULL); + SDL_Thread* s_pUpdatesThread = SDL_CreateThread(startUpdateThread, NULL); + SDL_Thread* s_pDrawThread = SDL_CreateThread(startDrawThread, NULL); - SDL_WaitThread(updates, 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 (s_timer.query() - 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 = s_timer.query(); -#ifdef FPSUPS - cout << "ups:\t" << ups << endl; - cout << "fps:\t" << fps << endl; -#endif + if(cfg::showFPS()) + { + cout << "fps:\t" << s_iFPS << endl; + } + if(cfg::showUPS()) + { + cout << "ups:\t" << s_iUPS << endl; + } } } @@ -154,61 +209,89 @@ void handleInput() { input::update(); + cfg::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() { game::draw(); + SDL_PumpEvents(); // has to be on the Draw thread for the Windows API + SDL_GL_SwapBuffers(); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - draw_Count++; + s_iDrawCount++; } int startUpdateThread(void*) { - last_Second = tickCountMicro(); + updatesInit(); + + s_micLastSecond = s_timer.query(); - while(is_Running) + while(s_bIsRunning) { - MICRO time = tickCountMicro(); + Timer timer; + timer.init(); handleInput(); - update(game_step); + update(s_fGameStep); updateFPSCounters(); - time = tickCountMicro() - time; + const Ticks ticks = timer.query(); + timer.fini(); - float wait = (1000000.0 / target_UPS - time); - update_Wait += 0 < wait ? wait : 0; + float wait = (1000000.0 / s_iTargetUPS - ticks.microseconds()); + 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 = (int)(s_fAccUpdateWait / s_iMinWaitMicro); + s_fAccUpdateWait -= iWaits * s_iMinWaitMicro; + SDL_Delay(iWaits * s_iMinWaitMilli); } } + updatesClean(); + return 0; } int startDrawThread(void*) { - while(is_Running) + drawInit(); + + while(s_bIsRunning) { - draw(); + Timer timer; + timer.init(); + draw(); + const Ticks ticks = timer.query(); + timer.fini(); + + float wait = (1000000.0 / s_iTargetFPS - ticks.microseconds()); + s_fAccDrawWait += 0 < wait ? wait : 0; + + if(s_iMinWaitMicro < s_fAccDrawWait) + { + int iWaits = (int)(s_fAccDrawWait / s_iMinWaitMicro); + s_fAccDrawWait -= iWaits * s_iMinWaitMicro; + SDL_Delay(iWaits * s_iMinWaitMilli); + } } + drawClean(); + return 0; }