summaryrefslogtreecommitdiff
path: root/src/client.cpp
diff options
context:
space:
mode:
authorThorbjørn Lindeijer <bjorn@lindeijer.nl>2024-03-08 21:39:31 +0100
committerThorbjørn Lindeijer <bjorn@lindeijer.nl>2024-03-08 21:39:34 +0100
commit144bdaee8c52f3ba4934b64b94e05676d96b8939 (patch)
treeec9f29983c8b2955e5da762fea4208817a41435b /src/client.cpp
parentb119923b4bb90d9ed3bb3a7550401b274d3ef0ad (diff)
downloadmana-144bdaee8c52f3ba4934b64b94e05676d96b8939.tar.gz
mana-144bdaee8c52f3ba4934b64b94e05676d96b8939.tar.bz2
mana-144bdaee8c52f3ba4934b64b94e05676d96b8939.tar.xz
mana-144bdaee8c52f3ba4934b64b94e05676d96b8939.zip
Removed SDL2_gfx dependency
Since the upgrade to SDL2 it was only used for framerate limiting, which I've replicated in a small helper class. Also reduced the framerate limit while minimized from 100 to 10 FPS.
Diffstat (limited to 'src/client.cpp')
-rw-r--r--src/client.cpp55
1 files changed, 35 insertions, 20 deletions
diff --git a/src/client.cpp b/src/client.cpp
index df360d17..18d95ac8 100644
--- a/src/client.cpp
+++ b/src/client.cpp
@@ -29,12 +29,8 @@
#include "game.h"
#include "itemshortcut.h"
#include "keyboardconfig.h"
-#ifdef USE_OPENGL
-#include "openglgraphics.h"
-#endif
#include "playerrelations.h"
#include "sound.h"
-#include "units.h"
#include "gui/changeemaildialog.h"
#include "gui/changepassworddialog.h"
@@ -86,8 +82,6 @@
#ifdef _WIN32
#include <SDL_syswm.h>
#include "utils/specialfolder.h"
-#else
-#include <cerrno>
#endif
#include <sys/stat.h>
@@ -152,9 +146,8 @@ int get_elapsed_time(int startTime)
{
if (startTime <= tick_time)
return (tick_time - startTime) * MILLISECONDS_IN_A_TICK;
- else
- return (tick_time + (MAX_TICK_VALUE - startTime))
- * MILLISECONDS_IN_A_TICK;
+
+ return (tick_time + (MAX_TICK_VALUE - startTime)) * MILLISECONDS_IN_A_TICK;
}
bool isDoubleClick(int selected)
@@ -174,6 +167,36 @@ bool isDoubleClick(int selected)
return false;
}
+/**
+ * A simple but effective frame rate limiter.
+ * Based on FPSmanager in SDL2_gfx.
+ */
+void FpsManager::limitFps(int fpsLimit)
+{
+ if (fpsLimit <= 0)
+ return;
+
+ const auto ticks = SDL_GetTicks();
+
+ // Need to reset if the FPS limit changes
+ if (mBaseTicks > 0 && mFpsLimit == fpsLimit)
+ {
+ ++mFrameCount;
+ const auto targetTicks = mBaseTicks + mFrameCount * 1000 / mFpsLimit;
+
+ // Make sure ticks hasn't wrapped
+ if (ticks > mBaseTicks && targetTicks > ticks)
+ {
+ SDL_Delay(targetTicks - ticks);
+ return;
+ }
+ }
+
+ mFpsLimit = fpsLimit;
+ mFrameCount = 0;
+ mBaseTicks = ticks;
+}
+
Client *Client::mInstance = nullptr;
@@ -411,9 +434,6 @@ Client::Client(const Options &options):
mLogicCounterId = SDL_AddTimer(MILLISECONDS_IN_A_TICK, nextTick, nullptr);
mSecondsCounterId = SDL_AddTimer(1000, nextSecond, nullptr);
- // Initialize frame limiting
- SDL_initFramerate(&mFpsManager);
-
listen(Event::ConfigChannel);
//TODO: fix having to fake a option changed event
@@ -524,15 +544,13 @@ int Client::exec()
frame_count++;
gui->draw();
graphics->updateScreen();
+ mFpsManager.limitFps(mFpsLimit);
}
else
{
- SDL_Delay(10);
+ mFpsManager.limitFps(10);
}
- if (mLimitFps)
- SDL_framerateDelay(&mFpsManager);
-
// TODO: Add connect timeouts
if (mState == STATE_CONNECT_GAME &&
Net::getGameHandler()->isConnected())
@@ -1012,10 +1030,7 @@ void Client::event(Event::Channel channel, const Event &event)
event.getType() == Event::ConfigOptionChanged &&
event.getString("option") == "fpslimit")
{
- const int fpsLimit = config.getIntValue("fpslimit");
- mLimitFps = fpsLimit > 0;
- if (mLimitFps)
- SDL_setFramerate(&mFpsManager, fpsLimit);
+ mFpsLimit = config.getIntValue("fpslimit");
}
}