summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorThorbjørn Lindeijer <thorbjorn@lindeijer.nl>2010-02-07 17:15:12 +0100
committerThorbjørn Lindeijer <thorbjorn@lindeijer.nl>2010-02-07 17:15:12 +0100
commit057b17859cb9fadf5ce633d31eade2dd75ddbb29 (patch)
treee756dea8ad0abd213f49140d632032c8074bf297 /src
parent3b6c67de81b54d2a6503bd0e9c377b260e4356ac (diff)
downloadmana-client-057b17859cb9fadf5ce633d31eade2dd75ddbb29.tar.gz
mana-client-057b17859cb9fadf5ce633d31eade2dd75ddbb29.tar.bz2
mana-client-057b17859cb9fadf5ce633d31eade2dd75ddbb29.tar.xz
mana-client-057b17859cb9fadf5ce633d31eade2dd75ddbb29.zip
Use SDL_gfx to limit the framerate
Makes sense if we depend on SDL_gfx anyway, and it seems to do a better job at it than the code we had.
Diffstat (limited to 'src')
-rw-r--r--src/game.cpp40
-rw-r--r--src/game.h13
2 files changed, 18 insertions, 35 deletions
diff --git a/src/game.cpp b/src/game.cpp
index 1df03d55..ea1979be 100644
--- a/src/game.cpp
+++ b/src/game.cpp
@@ -288,6 +288,7 @@ Game *Game::mInstance = 0;
Game::Game():
mLastTarget(Being::UNKNOWN),
mLogicCounterId(0), mSecondsCounterId(0),
+ mLimitFps(false),
mCurrentMap(0), mMapName("")
{
assert(!mInstance);
@@ -329,6 +330,7 @@ Game::Game():
Net::getGameHandler()->ping(tick_time);
// Initialize frame limiting
+ SDL_initFramerate(&mFpsManager);
config.addListener("fpslimit", this);
optionChanged("fpslimit");
@@ -431,20 +433,15 @@ static bool saveScreenshot()
void Game::optionChanged(const std::string &name)
{
- // Calculate the new minimum time per frame based on the FPS limit
const int fpsLimit = (int) config.getValue("fpslimit", 60);
- mMinFrameTime = fpsLimit ? 1000 / fpsLimit : 0;
-
- // Reset draw time to current time
- mDrawTime = tick_time * MILLISECONDS_IN_A_TICK;
+ mLimitFps = fpsLimit > 0;
+ if (mLimitFps)
+ SDL_setFramerate(&mFpsManager, fpsLimit);
}
void Game::exec()
{
- // mDrawTime has a higher granularity than gameTime in order to be able to
- // work with minimum frame durations in milliseconds.
int gameTime = tick_time;
- mDrawTime = tick_time * MILLISECONDS_IN_A_TICK;
while (state == STATE_GAME)
{
@@ -468,31 +465,18 @@ void Game::exec()
// 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++;
- gui->draw();
- graphics->updateScreen();
- mDrawTime += mMinFrameTime;
-
- // Make sure to wrap mDrawTime, since tick_time will wrap.
- if (mDrawTime > MAX_TICK_VALUE * MILLISECONDS_IN_A_TICK)
- mDrawTime -= MAX_TICK_VALUE * MILLISECONDS_IN_A_TICK;
- }
- else
- {
- SDL_Delay(MILLISECONDS_IN_A_TICK);
- }
+ frame++;
+ gui->draw();
+ graphics->updateScreen();
}
else
{
- SDL_Delay(MILLISECONDS_IN_A_TICK);
- mDrawTime = tick_time * MILLISECONDS_IN_A_TICK;
+ SDL_Delay(10);
}
+ if (mLimitFps)
+ SDL_framerateDelay(&mFpsManager);
+
// Handle network stuff
Net::getGeneralHandler()->flushNetwork();
if (!Net::getGameHandler()->isConnected())
diff --git a/src/game.h b/src/game.h
index 0ae6f167..31e4f369 100644
--- a/src/game.h
+++ b/src/game.h
@@ -23,7 +23,9 @@
#define GAME_H
#include "configlistener.h"
-#include "SDL.h"
+
+#include <SDL.h>
+#include <SDL_framerate.h>
extern std::string map_path;
extern volatile int fps;
@@ -74,17 +76,14 @@ class Game : public ConfigListener
const std::string &getCurrentMapName() { return mMapName; }
private:
- /** Used to determine whether to draw the next frame. */
- int mDrawTime;
-
- /** The minimum frame time in ms (used for frame limiting). */
- int mMinFrameTime;
-
int mLastTarget;
SDL_TimerID mLogicCounterId;
SDL_TimerID mSecondsCounterId;
+ bool mLimitFps;
+ FPSmanager mFpsManager;
+
WindowMenu *mWindowMenu;
Map *mCurrentMap;