X-Git-Url: http://gitweb.pgornicz.com/gitweb.cgi?a=blobdiff_plain;f=src%2Fmain.cpp;h=0efd7485d9486ad58dd76ff10f2143eb08dafff3;hb=c46074c1fc66612c5ea0bfa1f4441491e296703d;hp=d34f42082c5749eaa5ebf389bae36ce25b9d5b69;hpb=ce53f20f9a78b01966c91838ee3b80b4c674bc35;p=physics.git diff --git a/src/main.cpp b/src/main.cpp index d34f420..0efd748 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -31,7 +31,14 @@ /// ***** Private Method Headers ***** -void init(); +void mainInit(); +void mainClean(); + +void updatesInit(); +void updatesClean(); + +void drawInit(); +void drawClean(); void sighandler( int sig ); @@ -44,6 +51,9 @@ void handleInput(); void update(float); void draw(); +int startUpdateThread(void*); +int startDrawThread(void*); + /// ***** Private Variables ***** // variable used to determine if it is time to shutdown @@ -59,31 +69,46 @@ bool is_Running; * last_Second := stores the time of the last second, used for ups and fps */ int target_UPS = 100; +float update_Wait = 0; int ups, fps; int update_Count, draw_Count; -long int last_Second; +MICRO last_Second; -int startUpdateThread(void*); -int startDrawThread(void*); +const int min_wait_milli = 20; +const int min_wait_micro = min_wait_milli * 1000; +const float game_step = 10; + +bool updateInitialized = false; +bool drawInitialized = 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(); - DPF(0, "Graphics initialized"); + debug::init(); +} + +void mainClean() +{ + debug::clean(); +} + +void updatesInit() +{ + while(!drawInitialized) + SDL_Delay(100); game::init(); DPF(0, "Game initialized"); @@ -95,32 +120,52 @@ void init() DPF(0, "Configs initialized"); DPF(0, "Initialization Complete"); + + updateInitialized = true; } -void clean() +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"); + + drawInitialized = true; + + while(!updateInitialized) + SDL_Delay(100); +} + +void drawClean() +{ + DPF(0, "Draw Thread Cleaning"); graphics::clean(); } /// ***** Private Methods ***** +SDL_Thread* updatesThread = NULL; +SDL_Thread* drawThread = NULL; void run() { is_Running = true; - SDL_Thread* updates = SDL_CreateThread(startUpdateThread, NULL); + updatesThread = SDL_CreateThread(startUpdateThread, NULL); + drawThread = SDL_CreateThread(startDrawThread, NULL); - startDrawThread(NULL); - - SDL_WaitThread(updates, NULL); + SDL_WaitThread(updatesThread, NULL); + SDL_WaitThread(drawThread, NULL); } void updateFPSCounters() @@ -137,10 +182,14 @@ void updateFPSCounters() last_Second = tickCountMicro(); -#ifdef FPSUPS - cout << "ups:\t" << ups << endl; - cout << "fps:\t" << fps << endl; -#endif + if(cfg::showFPS()) + { + cout << "fps:\t" << fps << endl; + } + if(cfg::showUPS()) + { + cout << "ups:\t" << ups << endl; + } } } @@ -164,6 +213,8 @@ 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); @@ -173,25 +224,44 @@ void draw() int startUpdateThread(void*) { + updatesInit(); + + last_Second = tickCountMicro(); + while(is_Running) { - handleInput(); - update(10); + MICRO time = tickCountMicro(); + handleInput(); + update(game_step); - updateFPSCounters(); + updateFPSCounters(); + time = tickCountMicro() - time; - SDL_Delay(10); + 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); + } } + updatesClean(); + return 0; } int startDrawThread(void*) { + drawInit(); + while(is_Running) { draw(); } + drawClean(); + return 0; }