X-Git-Url: http://gitweb.pgornicz.com/gitweb.cgi?p=physics.git;a=blobdiff_plain;f=src%2Fmain.cpp;h=f94fac6213f7c71db316a3e557148946a942bcbb;hp=130982453976105d220b09aa89178bdfb883749f;hb=2a02c4bb2ab611952ac783eea9b93a1d44790dd7;hpb=4e77f48b331eeab55f2dd58436fe47b734314bdc diff --git a/src/main.cpp b/src/main.cpp index 1309824..f94fac6 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 @@ -69,26 +79,36 @@ 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*); +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"); @@ -100,33 +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); - // for some reason this needs to be on the main thread ...? - startDrawThread(NULL); - - SDL_WaitThread(updates, NULL); + SDL_WaitThread(updatesThread, NULL); + SDL_WaitThread(drawThread, NULL); } void updateFPSCounters() @@ -179,6 +218,8 @@ void draw() int startUpdateThread(void*) { + updatesInit(); + last_Second = tickCountMicro(); while(is_Running) @@ -200,15 +241,21 @@ int startUpdateThread(void*) } } + updatesClean(); + return 0; } int startDrawThread(void*) { + drawInit(); + while(is_Running) { draw(); } + drawClean(); + return 0; }