Add a better FPS limiter
This commit is contained in:
@@ -12,7 +12,6 @@
|
||||
#include "CameraController.h"
|
||||
#include "DebugComponent.h"
|
||||
#include "ScaleComponent.h"
|
||||
#include "FPSLimiter.h"
|
||||
#include "GameTargets/TargetInspector.h"
|
||||
|
||||
using ConfigFile = TLAC::FileSystem::ConfigFile;
|
||||
@@ -42,7 +41,6 @@ namespace TLAC::Components
|
||||
new FrameRateManager(),
|
||||
new FastLoader(),
|
||||
new ScaleComponent(),
|
||||
new FPSLimiter(),
|
||||
new StageManager(),
|
||||
new CameraController(),
|
||||
new DebugComponent(),
|
||||
|
||||
@@ -1,67 +0,0 @@
|
||||
#include "FPSLimiter.h"
|
||||
#include <Windows.h>
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
#include <cmath>
|
||||
#include <cstdio>
|
||||
#include <tchar.h>
|
||||
|
||||
using dsec = std::chrono::duration<double>;
|
||||
using seconds = std::chrono::seconds;
|
||||
|
||||
// this is pretty much just lybxlpsv's limiter, but removed from GLComponent
|
||||
|
||||
namespace TLAC::Components
|
||||
{
|
||||
using namespace std::chrono;
|
||||
|
||||
static std::chrono::time_point mBeginFrame = system_clock::now();
|
||||
static std::chrono::time_point prevTimeInSeconds = time_point_cast<seconds>(mBeginFrame);
|
||||
|
||||
const LPCTSTR config_file_name = _T(".\\config.ini");
|
||||
int fpsLimit = GetPrivateProfileIntW(L"graphics", L"fps.limit", 0, config_file_name);
|
||||
|
||||
FPSLimiter::FPSLimiter()
|
||||
{
|
||||
}
|
||||
|
||||
FPSLimiter::~FPSLimiter()
|
||||
{
|
||||
}
|
||||
|
||||
const char* FPSLimiter::GetDisplayName()
|
||||
{
|
||||
return "fps_limiter";
|
||||
}
|
||||
|
||||
void FPSLimiter::Initialize(ComponentsManager* manager)
|
||||
{
|
||||
mBeginFrame = std::chrono::system_clock::now();
|
||||
prevTimeInSeconds = std::chrono::time_point_cast<seconds>(mBeginFrame);
|
||||
frameCountPerSecond = 0;
|
||||
if(fpsLimit != 0)
|
||||
printf("FPSLimiter fpsLimit: %d\n", fpsLimit);
|
||||
}
|
||||
|
||||
// I assume this gets called once per frame... if not this won't work
|
||||
void FPSLimiter::Update()
|
||||
{
|
||||
auto invFpsLimit = round<std::chrono::system_clock::duration>(dsec{ 1. / fpsLimit });
|
||||
auto mEndFrame = mBeginFrame + invFpsLimit;
|
||||
auto timeInSeconds = std::chrono::time_point_cast<seconds>(std::chrono::system_clock::now());
|
||||
|
||||
++frameCountPerSecond;
|
||||
if (timeInSeconds > prevTimeInSeconds)
|
||||
{
|
||||
//printf("FPSLimiter::Update(): FPS: %d\n", frameCountPerSecond);
|
||||
frameCountPerSecond = 0;
|
||||
prevTimeInSeconds = timeInSeconds;
|
||||
}
|
||||
|
||||
// This part keeps the frame rate.
|
||||
if (fpsLimit > 19) // not sure why lyb used 19 here
|
||||
std::this_thread::sleep_until(mEndFrame);
|
||||
mBeginFrame = mEndFrame;
|
||||
mEndFrame = mBeginFrame + invFpsLimit;
|
||||
}
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
#pragma once
|
||||
#include "EmulatorComponent.h"
|
||||
#include <chrono>
|
||||
|
||||
using seconds = std::chrono::seconds;
|
||||
|
||||
namespace TLAC::Components
|
||||
{
|
||||
class FPSLimiter : public EmulatorComponent
|
||||
{
|
||||
public:
|
||||
FPSLimiter();
|
||||
~FPSLimiter();
|
||||
|
||||
virtual const char* GetDisplayName() override;
|
||||
|
||||
virtual void Initialize(ComponentsManager*) override;
|
||||
virtual void Update() override;
|
||||
|
||||
private:
|
||||
int frameCountPerSecond;
|
||||
};
|
||||
}
|
||||
@@ -177,7 +177,6 @@
|
||||
<ClInclude Include="Components\DebugComponent.h" />
|
||||
<ClInclude Include="Components\EmulatorComponent.h" />
|
||||
<ClInclude Include="Components\FastLoader.h" />
|
||||
<ClInclude Include="Components\FPSLimiter.h" />
|
||||
<ClInclude Include="Components\FrameRateManager.h" />
|
||||
<ClInclude Include="Components\GameState.h" />
|
||||
<ClInclude Include="Components\GameTargets\HoldState.h" />
|
||||
@@ -238,7 +237,6 @@
|
||||
<ClCompile Include="Components\DebugComponent.cpp" />
|
||||
<ClCompile Include="Components\EmulatorComponent.cpp" />
|
||||
<ClCompile Include="Components\FastLoader.cpp" />
|
||||
<ClCompile Include="Components\FPSLimiter.cpp" />
|
||||
<ClCompile Include="Components\FrameRateManager.cpp" />
|
||||
<ClCompile Include="Components\GameTargets\TargetInspector.cpp" />
|
||||
<ClCompile Include="Components\Input\InputEmulator.cpp" />
|
||||
|
||||
@@ -177,9 +177,6 @@
|
||||
<ClInclude Include="Input\Xinput\XinputState.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Components\FPSLimiter.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Components\GameTargets\HoldState.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
@@ -326,9 +323,6 @@
|
||||
<ClCompile Include="Input\Xinput\XinputState.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Components\FPSLimiter.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Components\GameTargets\TargetInspector.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
|
||||
Reference in New Issue
Block a user