diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/game.cpp | 49 | ||||
-rw-r--r-- | src/game.h | 14 |
2 files changed, 42 insertions, 21 deletions
diff --git a/src/game.cpp b/src/game.cpp index 3008e9cf..a6057d07 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -278,6 +278,10 @@ Game::Game(Network *network): SDL_AddTimer(10, nextTick, NULL); // Logic counter SDL_AddTimer(1000, nextSecond, NULL); // Seconds counter + // Initialize frame limiting + config.addListener("fpslimit", this); + optionChanged("fpslimit"); + // Initialize beings beingManager->setPlayer(player_node); player_node->setNetwork(network); @@ -342,12 +346,23 @@ bool saveScreenshot(SDL_Surface *screenshot) return ImageWriter::writePNG(screenshot, filename.str()); } +void Game::optionChanged(const std::string &name) +{ + int fpsLimit = (int) config.getValue("fpslimit", 0); + + // Calculate new minimum frame time + mMinFrameTime = fpsLimit ? 1000 / fpsLimit : 0; + + // Reset draw time to current time + mDrawTime = tick_time * 10; +} + void Game::logic() { + // mDrawTime has a higher granularity than gameTime in order to be able to + // work with minimum frame durations in milliseconds. int gameTime = tick_time; - int drawTime = tick_time * 10; - int delta = 0; - int fpsLimit = 0; + mDrawTime = tick_time * 10; while (!done) { @@ -359,35 +374,31 @@ void Game::logic() gameTime++; } + // This is done because at some point tick_time will wrap. gameTime = tick_time; - fpsLimit = (int)config.getValue("fpslimit", 60); - delta = fpsLimit ? 1000 / fpsLimit : 0; - - // Update the screen when application is active, delay otherwise - if (SDL_GetAppState() & SDL_APPACTIVE) { - if (fpsLimit == 0) { + // Update the screen when application is active, delay otherwise. + if (SDL_GetAppState() & SDL_APPACTIVE) + { + // Draw a frame if either frames are not limited or enough time has + // passed since the last frame. + if (!mMinFrameTime || + get_elapsed_time(mDrawTime / 10) > mMinFrameTime) + { frame++; engine->draw(graphics); graphics->updateScreen(); - drawTime = tick_time * 10; + mDrawTime += mMinFrameTime; } else { - if (abs(tick_time * 10 - drawTime) <= delta) { - SDL_Delay(10); - } else { - frame++; - engine->draw(graphics); - graphics->updateScreen(); - drawTime += delta; - } + SDL_Delay(10); } } else { SDL_Delay(10); - drawTime = tick_time * 10; + mDrawTime = tick_time * 10; } // Handle network stuff @@ -27,6 +27,8 @@ #include <iosfwd> #include <memory> +#include "configlistener.h" + #define SPEECH_TIME 80 #define SPEECH_MAX_TIME 100 @@ -37,7 +39,7 @@ extern std::string map_path; extern volatile int fps; extern volatile int tick_time; -class Game +class Game : public ConfigListener { public: Game(Network *network); @@ -47,9 +49,17 @@ class Game void handleInput(); - protected: + void optionChanged(const std::string &name); + + private: Network *mNetwork; + /** Used to determine whether to draw the next frame. */ + int mDrawTime; + + /** The minimum frame time (used for frame limiting). */ + int mMinFrameTime; + typedef std::auto_ptr<MessageHandler> MessageHandlerPtr; MessageHandlerPtr mBeingHandler; MessageHandlerPtr mBuySellHandler; |