/// ***** Private Method Headers *****
-void init();
+void mainInit();
+void mainClean();
+
+void updatesInit();
+void updatesClean();
+
+void drawInit();
+void drawClean();
void sighandler( int sig );
void update(float);
void draw();
+int startUpdateThread(void*);
+int startDrawThread(void*);
+
/// ***** Private Variables *****
// variable used to determine if it is time to shutdown
* 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");
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()
{
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);
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;
}