X-Git-Url: http://gitweb.pgornicz.com/gitweb.cgi?a=blobdiff_plain;f=src%2Fmain.cpp;h=2744e86bdd370f3e1df4f7b3adf885bb0282b255;hb=e2b1ddcdc1007c0099a77eda28c377ff61d454be;hp=73133064f1ab57e2b11c34a9ed25bf53764c30f6;hpb=7cbd45053add8c0ee042a69a51f7c8e4dc40bc06;p=physics.git diff --git a/src/main.cpp b/src/main.cpp index 7313306..2744e86 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,3 +1,20 @@ +/* + * Copyright (C) 2008 Patrik Gornicz, Gornicz_P (at) hotmail (dot) com. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + #include #include #include @@ -10,169 +27,240 @@ #include "graphics/graphics.h" #include "input/inputManager.h" - - +#include "config/config.h" /// ***** Private Method Headers ***** -void init(); -void sighandler( int sig ); +static void mainInit(); +static void mainClean(); -void run(); -void cleanUp(); +static void updatesInit(); +static void updatesClean(); -void blockUpdate(); -void updateFPSCounters(); +static void drawInit(); +static void drawClean(); -void input(); -void update(float); -void draw(); +static void run(); +static void updateFPSCounters(); -/// ***** MAIN Method ***** -int main() -{ - init(); - run(); - cleanUp(); - return 0; -} +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 = 250; -long int last_Block_Update; -float update_Sum = 0; +static int s_iTargetUPS = 100; +static float s_fAccUpdateWait = 0; -int ups, fps; -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; -/// ***** Private Methods ***** -void init() +static bool s_bUpdateInitialized = false; +static bool s_bDrawInitialized = false; + +/// ***** MAIN Method ***** +int main(int argc, char** args) { - installSignal(); + mainInit(); + run(); + mainClean(); - graphicsInit(); + return 0; +} + +/// ***** Initializers/Cleaners ***** - gameInit(); +void mainInit() +{ + installSignal(); -#ifdef DEBUGGING - cout << "Initialization Complete" << endl; -#endif + debug::init(); +} +void mainClean() +{ + debug::clean(); } -void cleanUp() +void updatesInit() { -#ifdef DEBUGGING - cout << "Cleaning up" << endl; -#endif + while(!s_bDrawInitialized) + SDL_Delay(100); - gameClean(); + game::init(); + DPF(0, "Game initialized"); - graphicsCleanUp(); -} + input::init(); + DPF(0, "Input initialized"); -void run() + cfg::init(); + DPF(0, "Configs initialized"); + + DPF(0, "Initialization Complete"); + + s_bUpdateInitialized = true; +} +void updatesClean() { - is_Running = true; - last_Block_Update = tickCountMicro(); - last_Second = tickCountMicro(); + DPF(0, "Update Thread Cleaning"); - while(is_Running) - { - blockUpdate(); - updateFPSCounters(); - draw(); - } + cfg::clean(); + + input::clean(); + + game::clean(); } -void blockUpdate() +void drawInit() { - long int start = tickCountMicro(); + graphics::init(); + DPF(0, "Graphics initialized"); - // Calculate the updates that should be run for the next draw - update_Sum += (start - last_Block_Update) / (1000000 / (float)target_UPS); + s_bDrawInitialized = true; - // insures the float to int cast is done once. - int iupdate_sum = (int)update_Sum; + while(!s_bUpdateInitialized) + SDL_Delay(100); +} +void drawClean() +{ + DPF(0, "Draw Thread Cleaning"); - // 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; + graphics::clean(); +} - // run the updates - for (int i = 1; i <= iupdate_sum; i++) - { - input(); - update(time_step*i / 1000); - } - // remove the updates that where run from the sum - update_Sum -= iupdate_sum; - last_Block_Update = tickCountMicro(); - } +/// ***** Private Methods ***** + +void run() +{ + s_bIsRunning = true; + + SDL_Thread* s_pUpdatesThread = SDL_CreateThread(startUpdateThread, NULL); + SDL_Thread* s_pDrawThread = SDL_CreateThread(startDrawThread, NULL); + + 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; - update_Count = 0; - draw_Count = 0; + s_iUPS = s_iUpdateCount; + s_iFPS = s_iDrawCount; + + // NOT thread safe, but they're estimates anyways + s_iUpdateCount -= s_iUPS; + s_iDrawCount -= s_iFPS; - last_Second = tickCountMicro(); + s_micLastSecond = tickCountMicro(); - //cout << "ups:\t" << ups << endl; - //cout << "fps:\t" << fps << endl; + if(cfg::showFPS()) + { + cout << "fps:\t" << s_iFPS << endl; + } + if(cfg::showUPS()) + { + cout << "ups:\t" << s_iUPS << endl; + } } } -void input() +void handleInput() { - inputUpdate(); + input::update(); - gameInput(); + cfg::handleInput(); + game::handleInput(); - if(wasReleased(SDLK_ESCAPE)) - is_Running = false; + if(cfg::endGame()) + s_bIsRunning = false; } -void update(float time_step) +void update(float fTimeStep) { - update_Count++; - - gameUpdate(time_step); + game::update(fTimeStep); + s_iUpdateCount++; } void draw() { - draw_Count++; + game::draw(); - gameDraw(); + 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); + + s_iDrawCount++; +} + +int startUpdateThread(void*) +{ + updatesInit(); + + s_micLastSecond = tickCountMicro(); + + while(s_bIsRunning) + { + MICRO time = tickCountMicro(); + handleInput(); + update(s_fGameStep); + + updateFPSCounters(); + time = tickCountMicro() - time; + + float wait = (1000000.0 / s_iTargetUPS - time); + s_fAccUpdateWait += 0 < wait ? wait : 0; + + if(s_iMinWaitMicro < s_fAccUpdateWait) + { + int iWaits = s_fAccUpdateWait / s_iMinWaitMicro; + s_fAccUpdateWait -= iWaits * s_iMinWaitMicro; + SDL_Delay(iWaits * s_iMinWaitMilli); + } + } + + updatesClean(); + + return 0; +} + +int startDrawThread(void*) +{ + drawInit(); + + while(s_bIsRunning) + { + draw(); + } + + drawClean(); + + return 0; }