[Project64] Get Speed limiter to work on non win32 system

This commit is contained in:
zilmar 2016-08-11 20:50:15 +10:00
parent a3fd417e27
commit 2cf4102c10
2 changed files with 15 additions and 37 deletions

View File

@ -17,31 +17,16 @@
#pragma comment(lib, "winmm.lib") #pragma comment(lib, "winmm.lib")
#endif #endif
CSpeedLimiter::CSpeedLimiter() CSpeedLimiter::CSpeedLimiter() :
m_Frames(0),
m_Speed(60),
m_BaseSpeed(60),
m_Ratio(1000.0F / (float)m_Speed)
{ {
m_Frames = 0;
m_LastTime = 0;
m_Speed = 60;
m_BaseSpeed = 60;
m_Ratio = 1000.0F / (float)m_Speed;
#ifdef _WIN32
TIMECAPS Caps;
timeGetDevCaps(&Caps, sizeof(Caps));
if (timeBeginPeriod(Caps.wPeriodMin) == TIMERR_NOCANDO)
{
g_Notify->DisplayError("Error during timer begin");
}
#endif
} }
CSpeedLimiter::~CSpeedLimiter() CSpeedLimiter::~CSpeedLimiter()
{ {
#ifdef _WIN32
TIMECAPS Caps;
timeGetDevCaps(&Caps, sizeof(Caps));
timeEndPeriod(Caps.wPeriodMin);
#endif
} }
void CSpeedLimiter::SetHertz(uint32_t Hertz) void CSpeedLimiter::SetHertz(uint32_t Hertz)
@ -55,47 +40,38 @@ void CSpeedLimiter::FixSpeedRatio()
{ {
m_Ratio = 1000.0f / static_cast<double>(m_Speed); m_Ratio = 1000.0f / static_cast<double>(m_Speed);
m_Frames = 0; m_Frames = 0;
#ifdef _WIN32
m_LastTime = timeGetTime();
#endif
} }
bool CSpeedLimiter::Timer_Process(uint32_t * FrameRate) bool CSpeedLimiter::Timer_Process(uint32_t * FrameRate)
{ {
#ifdef _WIN32
m_Frames += 1; m_Frames += 1;
uint32_t CurrentTime = timeGetTime(); CDateTime CurrentTime;
CurrentTime.SetToNow();
/* Calculate time that should of elapsed for this frame */ /* Calculate time that should of elapsed for this frame */
double CalculatedTime = (double)m_LastTime + (m_Ratio * (double)m_Frames); uint64_t CalculatedTime = (m_LastTime.Value()) + (m_Ratio * (double)m_Frames);
if ((double)CurrentTime < CalculatedTime) if (CurrentTime.Value() < CalculatedTime)
{ {
int32_t time = (int)(CalculatedTime - (double)CurrentTime); int32_t time = (int)(CalculatedTime - CurrentTime.Value());
if (time > 0) if (time > 0)
{ {
pjutil::Sleep(time); pjutil::Sleep(time);
} }
/* Refresh current time */ /* Refresh current time */
CurrentTime = timeGetTime(); CurrentTime.SetToNow();
} }
if (CurrentTime.Value() - m_LastTime.Value() >= 1000)
if (CurrentTime - m_LastTime >= 1000)
{ {
/* Output FPS */ /* Output FPS */
if (FrameRate != NULL) { *FrameRate = m_Frames; } if (FrameRate != NULL) { *FrameRate = m_Frames; }
m_Frames = 0; m_Frames = 0;
m_LastTime = CurrentTime; m_LastTime = CurrentTime;
return true; return true;
} }
else else
{ {
return false; return false;
} }
#else
return false;
#endif
} }
void CSpeedLimiter::IncreaseSpeed() void CSpeedLimiter::IncreaseSpeed()

View File

@ -11,6 +11,7 @@
#pragma once #pragma once
#include <Project64-core/Settings/GameSettings.h> #include <Project64-core/Settings/GameSettings.h>
#include <Common/DateTimeClass.h>
class CSpeedLimiter : class CSpeedLimiter :
private CGameSettings private CGameSettings
@ -30,6 +31,7 @@ private:
void FixSpeedRatio(); void FixSpeedRatio();
uint32_t m_Speed, m_BaseSpeed, m_Frames, m_LastTime; uint32_t m_Speed, m_BaseSpeed, m_Frames;
CDateTime m_LastTime;
double m_Ratio; double m_Ratio;
}; };