X-Git-Url: http://gitweb.pgornicz.com/gitweb.cgi?a=blobdiff_plain;f=src%2Fmain.cpp;h=2744e86bdd370f3e1df4f7b3adf885bb0282b255;hb=e2b1ddcdc1007c0099a77eda28c377ff61d454be;hp=1e9d4ad7e0b916915b5bd5cfa247d77f05eb7649;hpb=3c3c195d2c5230cc55a9c3b67af12fb1c6f84b87;p=physics.git diff --git a/src/main.cpp b/src/main.cpp index 1e9d4ad..2744e86 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -31,261 +31,236 @@ /// ***** Private Method Headers ***** -void init(); +static void mainInit(); +static void mainClean(); -void sighandler( int sig ); +static void updatesInit(); +static void updatesClean(); -void run(); -void clean(); +static void drawInit(); +static void drawClean(); -void blockUpdate(); -void updateFPSCounters(); +static void run(); -void handleInput(); -void update(float); -void draw(); +static void updateFPSCounters(); + +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 - * last_Update := stores the time of the last update - * update_Sum := used to determine the updates needs for this run - - * 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; -long int last_Block_Update; -float update_Sum = 0; +static int s_iTargetUPS = 100; +static float s_fAccUpdateWait = 0; -int ups=100, fps=100; -int update_Count, draw_Count; -long int last_Second; +static int s_iUPS, s_iFPS; +static int s_iUpdateCount, s_iDrawCount; +static MICRO s_micLastSecond; +static const float s_fGameStep = 10; -float target_time_steps_per_second = 1000; -float times; -float time_steps_Count; - - -// experiment with rolling averages -float rUpdate = 100; -float rDraw = 100; -float rInput = 100; -float rRun = 100; - -float num = 10; - -float total = 0; +static bool s_bUpdateInitialized = false; +static bool s_bDrawInitialized = false; /// ***** MAIN Method ***** int main(int argc, char** args) { - init(); + mainInit(); run(); - clean(); + mainClean(); + return 0; } /// ***** Initializers/Cleaners ***** -void init() +void mainInit() { installSignal(); - graphics::init(); + debug::init(); +} +void mainClean() +{ + debug::clean(); +} -#ifdef DEBUGGING - cout << "Graphics initialized" << endl; - cout << "Graphics initialized2" << endl; -#endif +void updatesInit() +{ + while(!s_bDrawInitialized) + SDL_Delay(100); game::init(); - -#ifdef DEBUGGING - cout << "Game initialized" << endl; -#endif + DPF(0, "Game initialized"); input::init(); - -#ifdef DEBUGGING - cout << "Input initialized" << endl; -#endif + DPF(0, "Input initialized"); cfg::init(); + DPF(0, "Configs initialized"); -#ifdef DEBUGGING - cout << "Configs initialized" << endl; -#endif + DPF(0, "Initialization Complete"); -#ifdef DEBUGGING - cout << "Initialization Complete" << endl; -#endif + s_bUpdateInitialized = true; } - -void clean() +void updatesClean() { -#ifdef DEBUGGING - cout << "Cleaning up" << endl; -#endif + DPF(0, "Update Thread Cleaning"); cfg::clean(); input::clean(); game::clean(); - - graphics::clean(); } -/// ***** Private Methods ***** - -void run() +void drawInit() { - is_Running = true; - last_Block_Update = tickCountMicro(); - last_Second = tickCountMicro(); - - while(is_Running) - { - long int time; - - time = tickCountMicro(); - blockUpdate(); - updateFPSCounters(); - draw(); - time = tickCountMicro() - time; + graphics::init(); + DPF(0, "Graphics initialized"); - rRun = (rRun * (num-1) + time) / num; + s_bDrawInitialized = true; - //cout << "total:\t" << total << endl; - //cout << "rR:\t" << rRun << endl; - //total = 0; - } + while(!s_bUpdateInitialized) + SDL_Delay(100); } - -void blockUpdate() +void drawClean() { - long int start = tickCountMicro(); - long int diff = start - last_Block_Update; - - //cout << "Block" << endl; - - // Calculate the updates that should be run for the next draw - update_Sum += (float)(diff * target_UPS) / 1000000; + DPF(0, "Draw Thread Cleaning"); - // insures the float to int cast is done once. - int iupdate_sum = (int)update_Sum; + graphics::clean(); +} - // TODO the main run loop needs to be tested and pruned - if (iupdate_sum > 0) - { - // Calculate a time step that spreads the updates out as much as - // possible used because really quick updates are nearly wasted +/// ***** Private Methods ***** - //float time_step = ((float)diff) / iupdate_sum / 1000; - //float time_step = 1000 / (100000 / rUpdate) / iupdate_sum; - //float time_step = 1000 / ups / iupdate_sum; - float time_step = 10; +void run() +{ + s_bIsRunning = true; - // run the updates - for (int i = 0; i < iupdate_sum; i++) - { - handleInput(); - update(time_step); - } + SDL_Thread* s_pUpdatesThread = SDL_CreateThread(startUpdateThread, NULL); + SDL_Thread* s_pDrawThread = SDL_CreateThread(startDrawThread, NULL); - // remove the updates that were run from the sum - update_Sum -= iupdate_sum; - last_Block_Update = tickCountMicro(); - } + 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; - times = time_steps_Count; - update_Count = 0; - draw_Count = 0; - time_steps_Count = 0; - - last_Second = tickCountMicro(); - -#ifdef FPSUPS - cout << "ups:\t" << ups << endl; - cout << "fps:\t" << fps << endl; - cout << "times:\t" << times << endl; -#endif + s_iUPS = s_iUpdateCount; + s_iFPS = s_iDrawCount; + + // NOT thread safe, but they're estimates anyways + s_iUpdateCount -= s_iUPS; + s_iDrawCount -= s_iFPS; + + s_micLastSecond = tickCountMicro(); + + if(cfg::showFPS()) + { + cout << "fps:\t" << s_iFPS << endl; + } + if(cfg::showUPS()) + { + cout << "ups:\t" << s_iUPS << endl; + } } } void handleInput() { - long int time; - - time = tickCountMicro(); - input::update(); + input::update(); - game::handleInput(); + cfg::handleInput(); + game::handleInput(); - if(cfg::endGame()) - is_Running = false; - time = tickCountMicro() - time; - - rInput = (rInput*(num-1) + time) /num; - - total += rInput; + if(cfg::endGame()) + s_bIsRunning = false; } -void update(float time_step) +void update(float fTimeStep) { - long int time; + game::update(fTimeStep); + s_iUpdateCount++; +} - update_Count++; - time_steps_Count += time_step; +void draw() +{ + game::draw(); - time = tickCountMicro(); - game::update(time_step); - time = tickCountMicro() - time; + SDL_PumpEvents(); // has to be on the Draw thread for the Windows API - rUpdate = (rUpdate * (num-1) + time) / num; + SDL_GL_SwapBuffers(); - //cout << "ts:\t" << time_step << endl; - //cout << "ru:\t" << rUpdate << endl; + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - total += rUpdate; + s_iDrawCount++; } -void draw() +int startUpdateThread(void*) { - long int time; + updatesInit(); + + s_micLastSecond = tickCountMicro(); + + while(s_bIsRunning) + { + MICRO time = tickCountMicro(); + handleInput(); + update(s_fGameStep); + + updateFPSCounters(); + time = tickCountMicro() - time; - draw_Count++; + float wait = (1000000.0 / s_iTargetUPS - time); + s_fAccUpdateWait += 0 < wait ? wait : 0; - time = tickCountMicro(); - game::draw(); + if(s_iMinWaitMicro < s_fAccUpdateWait) + { + int iWaits = s_fAccUpdateWait / s_iMinWaitMicro; + s_fAccUpdateWait -= iWaits * s_iMinWaitMicro; + SDL_Delay(iWaits * s_iMinWaitMilli); + } + } - SDL_GL_SwapBuffers(); + updatesClean(); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + return 0; +} - //SDL_Delay(10); - time = tickCountMicro() - time; +int startDrawThread(void*) +{ + drawInit(); - rDraw = (rDraw*(num-1) + time) /num; + while(s_bIsRunning) + { + draw(); + } - //cout << "rd:\t" << rDraw << endl; + drawClean(); - total += rDraw; + return 0; }