diff options
author | Bjørn Lindeijer <bjorn@lindeijer.nl> | 2006-09-11 23:04:05 +0000 |
---|---|---|
committer | Bjørn Lindeijer <bjorn@lindeijer.nl> | 2006-09-11 23:04:05 +0000 |
commit | e496e082e38a8c962f23bfac7ce1fa7400ed3bf6 (patch) | |
tree | 978e4fe35382f5096d17e64a0ddc3caddf31f918 /src/game.cpp | |
parent | abeaf0ce93dba357abf8130d62ae10a9f1a35452 (diff) | |
download | mana-e496e082e38a8c962f23bfac7ce1fa7400ed3bf6.tar.gz mana-e496e082e38a8c962f23bfac7ce1fa7400ed3bf6.tar.bz2 mana-e496e082e38a8c962f23bfac7ce1fa7400ed3bf6.tar.xz mana-e496e082e38a8c962f23bfac7ce1fa7400ed3bf6.zip |
Simplified fps limiting a bit and added some comments. Now based on
ConfigListener so that the fpslimit option doesn't need to be queried the whole
time.
Diffstat (limited to 'src/game.cpp')
-rw-r--r-- | src/game.cpp | 49 |
1 files changed, 30 insertions, 19 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 |