X-Git-Url: http://gitweb.pgornicz.com/gitweb.cgi?a=blobdiff_plain;f=src%2Fmain.cpp;h=130982453976105d220b09aa89178bdfb883749f;hb=8baa4b2f0b559c9c48fba41b8dc4a4d5f92f728b;hp=5381a86a1f15207415183d634bf91a6d7328a4da;hpb=b1d92c2f86c7da20ef22f9e064ff9b1a2d5ede4e;p=physics.git diff --git a/src/main.cpp b/src/main.cpp index 5381a86..1309824 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -38,7 +38,6 @@ void sighandler( int sig ); void run(); void clean(); -void blockUpdate(); void updateFPSCounters(); void handleInput(); @@ -52,26 +51,29 @@ bool is_Running; /* 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 */ -int target_UPS = 250; -long int last_Block_Update; -float update_Sum = 0; +int target_UPS = 100; +float update_Wait = 0; int ups, fps; int update_Count, draw_Count; -long int last_Second; +MICRO last_Second; +const int min_wait_milli = 20; +const int min_wait_micro = min_wait_milli * 1000; +const float game_step = 10; + +int startUpdateThread(void*); +int startDrawThread(void*); /// ***** MAIN Method ***** -int main() +int main(int argc, char** args) { init(); run(); @@ -86,23 +88,23 @@ void init() installSignal(); graphics::init(); + DPF(0, "Graphics initialized"); game::init(); + DPF(0, "Game initialized"); input::init(); + DPF(0, "Input initialized"); cfg::init(); + DPF(0, "Configs initialized"); -#ifdef DEBUGGING - cout << "Initialization Complete" << endl; -#endif + DPF(0, "Initialization Complete"); } void clean() { -#ifdef DEBUGGING - cout << "Cleaning up" << endl; -#endif + DPF(0, "Cleaning up"); cfg::clean(); @@ -118,44 +120,13 @@ void clean() void run() { is_Running = true; - last_Block_Update = tickCountMicro(); - last_Second = tickCountMicro(); - - while(is_Running) - { - blockUpdate(); - updateFPSCounters(); - draw(); - } -} - -void blockUpdate() -{ - long int start = tickCountMicro(); - // Calculate the updates that should be run for the next draw - update_Sum += (start - last_Block_Update) / (1000000 / (float)target_UPS); + SDL_Thread* updates = SDL_CreateThread(startUpdateThread, NULL); - // insures the float to int cast is done once. - int iupdate_sum = (int)update_Sum; + // for some reason this needs to be on the main thread ...? + startDrawThread(NULL); - // 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 - float time_step = ((float)(start - last_Block_Update)) / iupdate_sum; - - // run the updates - for (int i = 1; i <= iupdate_sum; i++) - { - handleInput(); - update(time_step*i / 1000); - } - // remove the updates that where run from the sum - update_Sum -= iupdate_sum; - last_Block_Update = tickCountMicro(); - } + SDL_WaitThread(updates, NULL); } void updateFPSCounters() @@ -165,8 +136,10 @@ void updateFPSCounters() { ups = update_Count; fps = draw_Count; - update_Count = 0; - draw_Count = 0; + + // NOT thread safe, but they're estimates anyways + update_Count -= ups; + draw_Count -= fps; last_Second = tickCountMicro(); @@ -181,7 +154,7 @@ void handleInput() { input::update(); - game::input(); + game::handleInput(); if(cfg::endGame()) is_Running = false; @@ -189,18 +162,53 @@ void handleInput() void update(float time_step) { - update_Count++; - game::update(time_step); + update_Count++; } void draw() { - draw_Count++; - game::draw(); SDL_GL_SwapBuffers(); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + draw_Count++; +} + +int startUpdateThread(void*) +{ + last_Second = tickCountMicro(); + + while(is_Running) + { + MICRO time = tickCountMicro(); + handleInput(); + update(game_step); + + updateFPSCounters(); + time = tickCountMicro() - time; + + float wait = (1000000.0 / target_UPS - time); + update_Wait += 0 < wait ? wait : 0; + + while(min_wait_micro < update_Wait) + { + update_Wait -= min_wait_micro; + SDL_Delay(min_wait_milli); + } + } + + return 0; +} + +int startDrawThread(void*) +{ + while(is_Running) + { + draw(); + } + + return 0; }