mirror of
https://github.com/vcmi/vcmi.git
synced 2025-11-23 22:37:55 +02:00
optimized fps draw
This commit is contained in:
@@ -35,6 +35,7 @@
|
||||
|
||||
#include "../lib/AsyncRunner.h"
|
||||
#include "../lib/CConfigHandler.h"
|
||||
#include "../lib/texts/TextOperations.h"
|
||||
|
||||
#include <SDL_render.h>
|
||||
|
||||
@@ -125,8 +126,8 @@ void GameEngine::updateFrame()
|
||||
handleEvents();
|
||||
windows().simpleRedraw();
|
||||
|
||||
if (settings["video"]["showfps"].Bool())
|
||||
drawFPSCounter();
|
||||
if (settings["video"]["infobox"]["show"].Bool())
|
||||
drawInfoBox();
|
||||
|
||||
screenHandlerInstance->updateScreenTexture();
|
||||
|
||||
@@ -184,14 +185,47 @@ Point GameEngine::screenDimensions() const
|
||||
return screenHandlerInstance->getLogicalResolution();
|
||||
}
|
||||
|
||||
void GameEngine::drawFPSCounter()
|
||||
void GameEngine::drawInfoBox()
|
||||
{
|
||||
Canvas target = screenHandler().getScreenCanvas();
|
||||
Rect targetArea(0, screenDimensions().y - 20, 48, 11);
|
||||
std::string fps = std::to_string(framerate().getFramerate())+" FPS";
|
||||
auto font = EFonts::FONT_SMALL;
|
||||
const auto & fontPtr = ENGINE->renderHandler().loadFont(font);
|
||||
|
||||
target.drawColor(targetArea, ColorRGBA(10, 10, 10));
|
||||
target.drawText(targetArea.center(), EFonts::FONT_SMALL, Colors::WHITE, ETextAlignment::CENTER, fps);
|
||||
Canvas target = screenHandler().getScreenCanvas();
|
||||
|
||||
auto powerState = ENGINE->input().getPowerState();
|
||||
std::string powerSymbol = ""; // add symbol if emoji are supported (e.g. VCMI extras)
|
||||
if(fontPtr->canRepresentCharacter("🔋") && powerState.powerState == PowerStateMode::ON_BATTERY)
|
||||
powerSymbol = "🔋 ";
|
||||
else if(fontPtr->canRepresentCharacter("🔌") && powerState.powerState == PowerStateMode::CHARGING)
|
||||
powerSymbol = "🔌 ";
|
||||
|
||||
std::string fps = std::to_string(framerate().getFramerate())+" FPS";
|
||||
std::string time = TextOperations::getFormattedTimeLocal(std::time(nullptr));
|
||||
std::string power = powerState.powerState == PowerStateMode::UNKNOWN ? "" : powerSymbol + std::to_string(powerState.percent) + "%";
|
||||
|
||||
std::string textToDisplay = time + (power.empty() ? "" : " | " + power) + " | " + fps;
|
||||
|
||||
maxInfoBoxTextWidth = std::max(maxInfoBoxTextWidth, static_cast<int>(fontPtr->getStringWidth(textToDisplay))); // do not get smaller (can cause graphical glitches)
|
||||
|
||||
Rect targetArea;
|
||||
std::string edge = settings["video"]["infobox"]["edge"].String();
|
||||
int pos1 = settings["video"]["infobox"]["pos1"].Integer();
|
||||
int pos2 = settings["video"]["infobox"]["pos2"].Integer();
|
||||
|
||||
Point boxSize(maxInfoBoxTextWidth + 4, fontPtr->getLineHeight() + 2);
|
||||
|
||||
if (edge == "topleft")
|
||||
targetArea = Rect(pos2, pos1, boxSize.x, boxSize.y);
|
||||
else if (edge == "topright")
|
||||
targetArea = Rect(screenDimensions().x - pos2 - boxSize.x, pos1, boxSize.x, boxSize.y);
|
||||
else if (edge == "bottomleft")
|
||||
targetArea = Rect(pos2, screenDimensions().y - pos1 - boxSize.y, boxSize.x, boxSize.y);
|
||||
else if (edge == "bottomright")
|
||||
targetArea = Rect(screenDimensions().x - pos2 - boxSize.x, screenDimensions().y - pos1 - boxSize.y, boxSize.x, boxSize.y);
|
||||
|
||||
target.drawColor(targetArea.resize(1), Colors::BRIGHT_YELLOW);
|
||||
target.drawColor(targetArea, ColorRGBA(0, 0, 0));
|
||||
target.drawText(targetArea.center(), font, Colors::WHITE, ETextAlignment::CENTER, textToDisplay);
|
||||
}
|
||||
|
||||
bool GameEngine::amIGuiThread()
|
||||
|
||||
@@ -57,9 +57,11 @@ private:
|
||||
|
||||
IGameEngineUser *engineUser = nullptr;
|
||||
|
||||
int maxInfoBoxTextWidth = 0;
|
||||
|
||||
void updateFrame();
|
||||
void handleEvents(); //takes events from queue and calls interested objects
|
||||
void drawFPSCounter(); // draws the FPS to the upper left corner of the screen
|
||||
void drawInfoBox(); // draws box with additional infos (e.g. fps)
|
||||
|
||||
public:
|
||||
std::mutex interfaceMutex;
|
||||
|
||||
@@ -33,6 +33,7 @@
|
||||
#include <SDL_events.h>
|
||||
#include <SDL_timer.h>
|
||||
#include <SDL_clipboard.h>
|
||||
#include <SDL_power.h>
|
||||
|
||||
InputHandler::InputHandler()
|
||||
: enableMouse(settings["input"]["enableMouse"].Bool())
|
||||
@@ -148,6 +149,21 @@ void InputHandler::copyToClipBoard(const std::string & text)
|
||||
SDL_SetClipboardText(text.c_str());
|
||||
}
|
||||
|
||||
PowerState InputHandler::getPowerState()
|
||||
{
|
||||
int seconds;
|
||||
int percent;
|
||||
auto sdlPowerState = SDL_GetPowerInfo(&seconds, &percent);
|
||||
|
||||
PowerStateMode powerState = PowerStateMode::UNKNOWN;
|
||||
if(sdlPowerState == SDL_POWERSTATE_ON_BATTERY)
|
||||
powerState = PowerStateMode::ON_BATTERY;
|
||||
else if(sdlPowerState == SDL_POWERSTATE_CHARGING || sdlPowerState == SDL_POWERSTATE_CHARGED)
|
||||
powerState = PowerStateMode::CHARGING;
|
||||
|
||||
return PowerState{powerState, seconds, percent};
|
||||
}
|
||||
|
||||
std::vector<SDL_Event> InputHandler::acquireEvents()
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(eventsMutex);
|
||||
|
||||
@@ -32,6 +32,19 @@ enum class InputMode
|
||||
CONTROLLER
|
||||
};
|
||||
|
||||
enum class PowerStateMode
|
||||
{
|
||||
UNKNOWN,
|
||||
CHARGING,
|
||||
ON_BATTERY
|
||||
};
|
||||
|
||||
struct PowerState {
|
||||
PowerStateMode powerState;
|
||||
int seconds;
|
||||
int percent;
|
||||
};
|
||||
|
||||
class InputHandler
|
||||
{
|
||||
std::vector<SDL_Event> eventsQueue;
|
||||
@@ -111,4 +124,5 @@ public:
|
||||
InputMode getCurrentInputMode();
|
||||
|
||||
void copyToClipBoard(const std::string & text);
|
||||
PowerState getPowerState();
|
||||
};
|
||||
|
||||
@@ -152,9 +152,10 @@ GeneralOptionsTab::GeneralOptionsTab()
|
||||
{
|
||||
selectLongTouchDuration();
|
||||
});
|
||||
addCallback("framerateChanged", [](bool value)
|
||||
addCallback("infoboxChanged", [](bool value)
|
||||
{
|
||||
setBoolSetting("video", "showfps", value);
|
||||
Settings gameRes = settings.write["video"]["infobox"];
|
||||
gameRes["show"].Bool() = value;
|
||||
});
|
||||
addCallback("hapticFeedbackChanged", [](bool value)
|
||||
{
|
||||
@@ -225,8 +226,8 @@ GeneralOptionsTab::GeneralOptionsTab()
|
||||
if (fullscreenExclusiveCheckbox)
|
||||
fullscreenExclusiveCheckbox->setSelected(settings["video"]["fullscreen"].Bool() && settings["video"]["realFullscreen"].Bool());
|
||||
|
||||
std::shared_ptr<CToggleButton> framerateCheckbox = widget<CToggleButton>("framerateCheckbox");
|
||||
framerateCheckbox->setSelected(settings["video"]["showfps"].Bool());
|
||||
std::shared_ptr<CToggleButton> infoboxCheckbox = widget<CToggleButton>("infoboxCheckbox");
|
||||
infoboxCheckbox->setSelected(settings["video"]["infobox"]["show"].Bool());
|
||||
|
||||
std::shared_ptr<CToggleButton> hapticFeedbackCheckbox = widget<CToggleButton>("hapticFeedbackCheckbox");
|
||||
if (hapticFeedbackCheckbox)
|
||||
|
||||
Reference in New Issue
Block a user