1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-11-24 08:32:34 +02:00

Implemented framerate limit option (not available in UI)

This commit is contained in:
Ivan Savenko 2023-03-05 21:06:52 +02:00
parent 37b2fb7ff3
commit 2e69a4769d
5 changed files with 50 additions and 32 deletions

View File

@ -1046,7 +1046,8 @@ static void mainLoop()
resChanged([](const JsonNode &newState){ CGuiHandler::pushUserEvent(EUserEvent::FULLSCREEN_TOGGLED); });
inGuiThread.reset(new bool(true));
GH.mainFPSmng->init();
assert(GH.mainFPSmng);
GH.mainFPSmng->init(settings["video"]["targetfps"].Integer());
while(1) //main SDL events loop
{

View File

@ -93,7 +93,8 @@ void CGuiHandler::processLists(const ui16 activityFlag, std::function<void (std:
void CGuiHandler::init()
{
mainFPSmng->init();
mainFPSmng = new CFramerateManager();
mainFPSmng->init(settings["video"]["targetfps"].Integer());
isPointerRelativeMode = settings["general"]["userRelativePointer"].Bool();
pointerSpeedMultiplier = settings["general"]["relativePointerSpeedMultiplier"].Float();
}
@ -663,7 +664,7 @@ void CGuiHandler::renderFrame()
if(nullptr != curInt)
curInt->update();
if(settings["general"]["showfps"].Bool())
if(settings["video"]["showfps"].Bool())
drawFPSCounter();
SDL_UpdateTexture(screenTexture, nullptr, screen->pixels, screen->pitch);
@ -691,12 +692,9 @@ CGuiHandler::CGuiHandler()
, mouseButtonsMask(0)
, continueEventHandling(true)
, curInt(nullptr)
, mainFPSmng(nullptr)
, statusbar(nullptr)
{
// Creates the FPS manager and sets the framerate to 48 which is doubled the value of the original Heroes 3 FPS rate
mainFPSmng = new CFramerateManager(60);
//do not init CFramerateManager here --AVS
terminate_cond = new CondSh<bool>(false);
}
@ -764,7 +762,7 @@ void CGuiHandler::drawFPSCounter()
static SDL_Rect overlay = { 0, 0, 64, 32};
uint32_t black = SDL_MapRGB(screen->format, 10, 10, 10);
SDL_FillRect(screen, &overlay, black);
std::string fps = boost::lexical_cast<std::string>(mainFPSmng->fps);
std::string fps = boost::lexical_cast<std::string>(mainFPSmng->getFramerate());
graphics->fonts[FONT_BIG]->renderTextLeft(screen, fps, Colors::YELLOW, Point(10, 10));
}
@ -850,19 +848,20 @@ void CGuiHandler::pushUserEvent(EUserEvent usercode, void * userdata)
SDL_PushEvent(&event);
}
CFramerateManager::CFramerateManager(int rate)
{
this->rate = rate;
this->rateticks = (1000.0 / rate);
this->fps = 0;
this->accumulatedFrames = 0;
this->accumulatedTime = 0;
this->lastticks = 0;
this->timeElapsed = 0;
}
CFramerateManager::CFramerateManager()
: rate(0)
, rateticks(0)
, fps(0)
, accumulatedFrames(0)
, accumulatedTime(0)
, lastticks(0)
, timeElapsed(0)
{}
void CFramerateManager::init()
void CFramerateManager::init(int newRate)
{
rate = newRate;
rateticks = 1000.0 / rate;
this->lastticks = SDL_GetTicks();
}

View File

@ -49,17 +49,20 @@ class CFramerateManager
{
private:
double rateticks;
ui32 lastticks, timeElapsed;
ui32 lastticks;
ui32 timeElapsed;
int rate;
ui32 accumulatedTime,accumulatedFrames;
public:
int fps; // the actual fps value
ui32 accumulatedTime;
ui32 accumulatedFrames;
CFramerateManager(int rate); // initializes the manager with a given fps rate
void init(); // needs to be called directly before the main game loop to reset the internal timer
public:
CFramerateManager(); // initializes the manager with a given fps rate
void init(int newRate); // needs to be called directly before the main game loop to reset the internal timer
void framerateDelay(); // needs to be called every game update cycle
ui32 getElapsedMilliseconds() const {return this->timeElapsed;}
ui32 getFrameNumber() const { return accumulatedFrames; }
ui32 getFramerate() const { return fps; };
};
// Handles GUI logic and drawing

View File

@ -88,7 +88,7 @@ GeneralOptionsTab::GeneralOptionsTab()
});
addCallback("framerateChanged", [](bool value)
{
setBoolSetting("general", "showfps", value);
setBoolSetting("video", "showfps", value);
});
//moved from "other" tab that is disabled for now to avoid excessible tabs with barely any content
@ -119,7 +119,7 @@ GeneralOptionsTab::GeneralOptionsTab()
});
std::shared_ptr<CToggleButton> framerateCheckbox = widget<CToggleButton>("framerateCheckbox");
framerateCheckbox->setSelected(settings["general"]["showfps"].Bool());
framerateCheckbox->setSelected(settings["video"]["showfps"].Bool());
std::shared_ptr<CSlider> musicSlider = widget<CSlider>("musicSlider");
musicSlider->moveTo(CCS->musich->getVolume());

View File

@ -19,7 +19,6 @@
"additionalProperties" : false,
"required" : [
"playerName",
"showfps",
"music",
"sound",
"encoding",
@ -39,10 +38,6 @@
"type":"string",
"default" : "Player"
},
"showfps" : {
"type" : "boolean",
"default" : false
},
"music" : {
"type" : "number",
"default" : 88
@ -114,7 +109,19 @@
"type" : "object",
"additionalProperties" : false,
"default": {},
"required" : [ "screenRes", "bitsPerPixel", "fullscreen", "realFullscreen", "cursor", "spellbookAnimation", "driver", "showIntro", "displayIndex" ],
"required" : [
"screenRes",
"bitsPerPixel",
"fullscreen",
"realFullscreen",
"cursor",
"spellbookAnimation",
"driver",
"showIntro",
"displayIndex",
"showfps",
"targetfps"
],
"properties" : {
"screenRes" : {
"type" : "object",
@ -159,6 +166,14 @@
"displayIndex" : {
"type" : "number",
"default" : 0
},
"showfps" : {
"type" : "boolean",
"default" : false
},
"targetfps" : {
"type" : "number",
"default" : 60
}
}
},