Skip to content

Commit

Permalink
fix fps
Browse files Browse the repository at this point in the history
  • Loading branch information
mehah committed Nov 25, 2023
1 parent 4ca45f2 commit 49db957
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/framework/core/adaptativeframecounter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

bool AdaptativeFrameCounter::update()
{
const uint8_t maxFps = m_targetFps == 0 ? m_maxFps : std::clamp<uint8_t>(m_targetFps, 1, std::max<uint8_t>(m_maxFps, m_targetFps));
const auto maxFps = m_targetFps == 0 ? m_maxFps : std::clamp<uint16_t>(m_targetFps, 1, std::max<uint16_t>(m_maxFps, m_targetFps));
if (maxFps > 0) {
const int32_t sleepPeriod = (getMaxPeriod(maxFps) - 1000) - m_timer.elapsed_micros();
if (sleepPeriod > 0) stdext::microsleep(sleepPeriod);
Expand Down
8 changes: 4 additions & 4 deletions src/framework/core/adaptativeframecounter.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,17 @@ class AdaptativeFrameCounter
bool update();

uint16_t getFps() const { return m_fps; }
uint8_t getMaxFps() const { return m_maxFps; }
uint8_t getTargetFps() const { return m_targetFps; }
uint16_t getMaxFps() const { return m_maxFps; }
uint16_t getTargetFps() const { return m_targetFps; }

void setMaxFps(const uint16_t max) { m_maxFps = max; }
void setTargetFps(const uint16_t target) { m_targetFps = target; }

void resetTargetFps() { m_targetFps = 0; }

float getPercent() const {
const auto maxFps = std::clamp<uint8_t>(m_targetFps, 1, std::max<uint8_t>(m_maxFps, m_targetFps));
return ((maxFps - m_fps) * 100) / static_cast<float>(m_fps);
const float maxFps = std::clamp<uint16_t>(m_targetFps, 1, std::max<uint16_t>(m_maxFps, m_targetFps));
return ((maxFps - m_fps) / maxFps) * 100.f;
}

float getFpsPercent(float percent) const {
Expand Down
4 changes: 2 additions & 2 deletions src/framework/core/graphicalapplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,9 @@ void GraphicalApplication::run()
if (g_window.vsyncEnabled() || getMaxFps() || getTargetFps()) {
// get min fps between the two threads
return std::min<int>(m_frameCounter.getFps(), frameCounter2.getFps());
} else {
return std::max<int>(10, getFps() - m_frameCounter.getFpsPercent(frameCounter2.getPercent()));
}

return std::max<int>(10, getFps() - m_frameCounter.getFpsPercent(frameCounter2.getPercent()));
};

g_asyncDispatcher.dispatch([&] {
Expand Down
9 changes: 6 additions & 3 deletions src/framework/platform/win32window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -936,8 +936,11 @@ void WIN32Window::setFullscreen(bool fullscreen)

void WIN32Window::setVerticalSync(bool enable)
{
g_mainDispatcher.addEvent([&, enable] {
m_vsync = enable;
if (m_vsync == enable)
return;

m_vsync = enable;
g_mainDispatcher.addEvent([this, enable] {
#ifdef OPENGL_ES
eglSwapInterval(m_eglDisplay, enable);
#else
Expand All @@ -950,7 +953,7 @@ void WIN32Window::setVerticalSync(bool enable)

wglSwapInterval(enable);
#endif
});
});
}

void WIN32Window::setIcon(const std::string& file)
Expand Down

0 comments on commit 49db957

Please sign in to comment.