2016-01-27 09:11:59 +00:00
|
|
|
/****************************************************************************
|
|
|
|
* *
|
|
|
|
* Project64 - A Nintendo 64 emulator. *
|
|
|
|
* http://www.pj64-emu.com/ *
|
|
|
|
* Copyright (C) 2012 Project64. All rights reserved. *
|
|
|
|
* *
|
|
|
|
* License: *
|
|
|
|
* GNU/GPLv2 http://www.gnu.org/licenses/gpl-2.0.html *
|
|
|
|
* *
|
|
|
|
****************************************************************************/
|
|
|
|
#include "stdafx.h"
|
|
|
|
#include "SpeedLimiterClass.h"
|
|
|
|
#include <Common/Util.h>
|
|
|
|
#ifdef _WIN32
|
|
|
|
#include <Windows.h>
|
|
|
|
#include <Mmsystem.h>
|
|
|
|
#pragma comment(lib, "winmm.lib")
|
|
|
|
#endif
|
|
|
|
|
2016-08-11 10:50:15 +00:00
|
|
|
CSpeedLimiter::CSpeedLimiter() :
|
|
|
|
m_Frames(0),
|
|
|
|
m_Speed(60),
|
|
|
|
m_BaseSpeed(60),
|
|
|
|
m_Ratio(1000.0F / (float)m_Speed)
|
2016-01-27 09:11:59 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
CSpeedLimiter::~CSpeedLimiter()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSpeedLimiter::SetHertz(uint32_t Hertz)
|
|
|
|
{
|
|
|
|
m_Speed = Hertz;
|
|
|
|
m_BaseSpeed = Hertz;
|
|
|
|
FixSpeedRatio();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSpeedLimiter::FixSpeedRatio()
|
|
|
|
{
|
|
|
|
m_Ratio = 1000.0f / static_cast<double>(m_Speed);
|
|
|
|
m_Frames = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CSpeedLimiter::Timer_Process(uint32_t * FrameRate)
|
|
|
|
{
|
|
|
|
m_Frames += 1;
|
2016-08-11 10:50:15 +00:00
|
|
|
CDateTime CurrentTime;
|
|
|
|
CurrentTime.SetToNow();
|
2016-01-27 09:11:59 +00:00
|
|
|
|
|
|
|
/* Calculate time that should of elapsed for this frame */
|
2016-08-11 10:50:15 +00:00
|
|
|
uint64_t CalculatedTime = (m_LastTime.Value()) + (m_Ratio * (double)m_Frames);
|
|
|
|
if (CurrentTime.Value() < CalculatedTime)
|
2016-01-27 09:11:59 +00:00
|
|
|
{
|
2016-08-11 10:50:15 +00:00
|
|
|
int32_t time = (int)(CalculatedTime - CurrentTime.Value());
|
2016-01-27 09:11:59 +00:00
|
|
|
if (time > 0)
|
|
|
|
{
|
|
|
|
pjutil::Sleep(time);
|
|
|
|
}
|
|
|
|
/* Refresh current time */
|
2016-08-11 10:50:15 +00:00
|
|
|
CurrentTime.SetToNow();
|
2016-01-27 09:11:59 +00:00
|
|
|
}
|
2016-08-11 10:50:15 +00:00
|
|
|
if (CurrentTime.Value() - m_LastTime.Value() >= 1000)
|
2016-01-27 09:11:59 +00:00
|
|
|
{
|
|
|
|
/* Output FPS */
|
|
|
|
if (FrameRate != NULL) { *FrameRate = m_Frames; }
|
|
|
|
m_Frames = 0;
|
|
|
|
m_LastTime = CurrentTime;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSpeedLimiter::IncreaseSpeed()
|
|
|
|
{
|
|
|
|
if (m_Speed >= 60)
|
|
|
|
{
|
|
|
|
m_Speed += 10;
|
|
|
|
}
|
|
|
|
else if (m_Speed >= 15)
|
|
|
|
{
|
|
|
|
m_Speed += 5;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_Speed += 1;
|
|
|
|
}
|
|
|
|
SpeedChanged(m_Speed);
|
|
|
|
FixSpeedRatio();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSpeedLimiter::DecreaseSpeed()
|
|
|
|
{
|
|
|
|
if (m_Speed > 60)
|
|
|
|
{
|
|
|
|
m_Speed -= 10;
|
|
|
|
}
|
|
|
|
else if (m_Speed > 15)
|
|
|
|
{
|
|
|
|
m_Speed -= 5;
|
|
|
|
}
|
|
|
|
else if (m_Speed > 1)
|
|
|
|
{
|
|
|
|
m_Speed -= 1;
|
|
|
|
}
|
|
|
|
SpeedChanged(m_Speed);
|
|
|
|
FixSpeedRatio();
|
|
|
|
}
|