Skip to content

Commit

Permalink
improve frame counter
Browse files Browse the repository at this point in the history
  • Loading branch information
mehah committed Nov 27, 2023
1 parent 28f206e commit c117a8b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/framework/core/adaptativeframecounter.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class AdaptativeFrameCounter
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 setTargetFps(const uint16_t target) { if (m_targetFps != target) m_targetFps = target; }

void resetTargetFps() { m_targetFps = 0; }

Expand Down
20 changes: 6 additions & 14 deletions src/framework/core/graphicalapplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ void GraphicalApplication::init(std::vector<std::string>& args, uint8_t asyncDis
g_sounds.init();
#endif

m_frameCounter.init();
m_mapProcessFrameCounter.init();
m_graphicFrameCounter.init();
}

void GraphicalApplication::deinit()
Expand Down Expand Up @@ -136,18 +137,9 @@ void GraphicalApplication::run()

std::condition_variable foregroundUICondition, foregroundMapCondition;

AdaptativeFrameCounter frameCounter2;
frameCounter2.setTargetFps(500u); // The secondary thread is limited to 500 fps.

const auto& realFPS = [&] {
if (g_window.vsyncEnabled() || getMaxFps() || getTargetFps()) {
// get min fps between the two threads
return std::min<int>(m_frameCounter.getFps(), frameCounter2.getFps());
}

return getFps() < frameCounter2.getFps() ? getFps() :
// adjusts the main FPS according to the secondary FPS percentage.
std::max<int>(10, getFps() - m_frameCounter.getFpsPercent(frameCounter2.getPercent()));
m_mapProcessFrameCounter.setTargetFps(g_window.vsyncEnabled() || getMaxFps() || getTargetFps() ? 500u : 999u);
return std::min<int>(m_graphicFrameCounter.getFps(), m_mapProcessFrameCounter.getFps());
};

const auto& drawForeground = [&] {
Expand Down Expand Up @@ -206,7 +198,7 @@ void GraphicalApplication::run()
g_ui.m_mapWidget->drawSelf(DrawPoolType::MAP);
} else g_ui.m_mapWidget = nullptr;

frameCounter2.update();
m_mapProcessFrameCounter.update();
}

foregroundUICondition.notify_one();
Expand All @@ -227,7 +219,7 @@ void GraphicalApplication::run()
// update screen pixels
g_window.swapBuffers();

if (m_frameCounter.update()) {
if (m_graphicFrameCounter.update()) {
g_dispatcher.addEvent([this, fps = realFPS()] {
g_lua.callGlobalField("g_app", "onFps", fps);
});
Expand Down
15 changes: 8 additions & 7 deletions src/framework/core/graphicalapplication.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ class GraphicalApplication : public Application
void mainPoll();
void close() override;

void setMaxFps(uint16_t maxFps) { m_frameCounter.setMaxFps(maxFps); }
void setTargetFps(uint16_t targetFps) { m_frameCounter.setTargetFps(targetFps); }
void setMaxFps(uint16_t maxFps) { m_graphicFrameCounter.setMaxFps(maxFps); }
void setTargetFps(uint16_t targetFps) { m_graphicFrameCounter.setTargetFps(targetFps); }

uint16_t getFps() { return m_frameCounter.getFps(); }
uint8_t getMaxFps() { return m_frameCounter.getMaxFps(); }
uint8_t getTargetFps() { return m_frameCounter.getTargetFps(); }
uint16_t getFps() { return m_graphicFrameCounter.getFps(); }
uint8_t getMaxFps() { return m_graphicFrameCounter.getMaxFps(); }
uint8_t getTargetFps() { return m_graphicFrameCounter.getTargetFps(); }

void resetTargetFps() { m_frameCounter.resetTargetFps(); }
void resetTargetFps() { m_graphicFrameCounter.resetTargetFps(); }

bool isOnInputEvent() { return m_onInputEvent; }
bool mustOptimize() {
Expand Down Expand Up @@ -112,7 +112,8 @@ class GraphicalApplication : public Application
float m_animatedTextScale{ PlatformWindow::DEFAULT_DISPLAY_DENSITY };
float m_staticTextScale{ PlatformWindow::DEFAULT_DISPLAY_DENSITY };

AdaptativeFrameCounter m_frameCounter;
AdaptativeFrameCounter m_mapProcessFrameCounter;
AdaptativeFrameCounter m_graphicFrameCounter;
};

extern GraphicalApplication g_app;

0 comments on commit c117a8b

Please sign in to comment.