2016-01-27 09:11:59 +00:00
|
|
|
/****************************************************************************
|
|
|
|
* *
|
2016-09-17 18:10:22 +00:00
|
|
|
* Project64 - A Nintendo 64 emulator. *
|
2016-01-27 09:11:59 +00:00
|
|
|
* http://www.pj64-emu.com/ *
|
|
|
|
* Copyright (C) 2012 Project64. All rights reserved. *
|
|
|
|
* *
|
|
|
|
* License: *
|
|
|
|
* GNU/GPLv2 http://www.gnu.org/licenses/gpl-2.0.html *
|
|
|
|
* *
|
|
|
|
****************************************************************************/
|
2016-09-17 18:10:22 +00:00
|
|
|
|
2016-01-27 09:11:59 +00:00
|
|
|
#include "stdafx.h"
|
2016-09-17 18:10:22 +00:00
|
|
|
#include "Project64-Core/N64System/SpeedLimiterClass.h"
|
|
|
|
|
2016-01-27 09:11:59 +00:00
|
|
|
#include <Common/Util.h>
|
|
|
|
|
2016-09-17 18:10:22 +00:00
|
|
|
// ---------------------------------------------------
|
|
|
|
|
|
|
|
const uint32_t CSpeedLimiter::m_HighSpeed = 60;
|
|
|
|
const uint32_t CSpeedLimiter::m_MediumSpeed = 15;
|
|
|
|
|
|
|
|
// ---------------------------------------------------
|
|
|
|
|
2016-08-11 10:50:15 +00:00
|
|
|
CSpeedLimiter::CSpeedLimiter() :
|
|
|
|
m_Frames(0),
|
2016-09-17 18:10:22 +00:00
|
|
|
m_Speed(m_HighSpeed),
|
|
|
|
m_BaseSpeed(m_HighSpeed)
|
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()
|
|
|
|
{
|
2016-09-16 21:37:27 +00:00
|
|
|
m_MicroSecondsPerFrame = 1000000 / m_Speed;
|
2016-01-27 09:11:59 +00:00
|
|
|
m_Frames = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CSpeedLimiter::Timer_Process(uint32_t * FrameRate)
|
|
|
|
{
|
|
|
|
m_Frames += 1;
|
2016-09-16 21:37:27 +00:00
|
|
|
HighResTimeStamp CurrentTime;
|
2016-08-11 10:50:15 +00:00
|
|
|
CurrentTime.SetToNow();
|
2016-01-27 09:11:59 +00:00
|
|
|
|
|
|
|
/* Calculate time that should of elapsed for this frame */
|
2016-09-16 21:37:27 +00:00
|
|
|
uint64_t LastTime = m_LastTime.GetMicroSeconds(), CurrentTimeValue = CurrentTime.GetMicroSeconds();
|
|
|
|
if (LastTime == 0)
|
2016-01-27 09:11:59 +00:00
|
|
|
{
|
2016-09-16 21:37:27 +00:00
|
|
|
m_Frames = 0;
|
|
|
|
m_LastTime = CurrentTime;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
uint64_t CalculatedTime = LastTime + (m_MicroSecondsPerFrame * m_Frames);
|
|
|
|
if (CurrentTimeValue < CalculatedTime)
|
|
|
|
{
|
|
|
|
int32_t time = (int)(CalculatedTime - CurrentTimeValue);
|
2016-01-27 09:11:59 +00:00
|
|
|
if (time > 0)
|
|
|
|
{
|
2016-09-16 21:37:27 +00:00
|
|
|
pjutil::Sleep((time / 1000) + 1);
|
2016-01-27 09:11:59 +00:00
|
|
|
}
|
|
|
|
/* Refresh current time */
|
2016-08-11 10:50:15 +00:00
|
|
|
CurrentTime.SetToNow();
|
2016-09-16 21:37:27 +00:00
|
|
|
CurrentTimeValue = CurrentTime.GetMicroSeconds();
|
2016-01-27 09:11:59 +00:00
|
|
|
}
|
2016-09-16 21:37:27 +00:00
|
|
|
if (CurrentTimeValue - LastTime >= 1000000)
|
2016-01-27 09:11:59 +00:00
|
|
|
{
|
|
|
|
/* Output FPS */
|
|
|
|
if (FrameRate != NULL) { *FrameRate = m_Frames; }
|
|
|
|
m_Frames = 0;
|
|
|
|
m_LastTime = CurrentTime;
|
|
|
|
return true;
|
|
|
|
}
|
2016-09-16 21:37:27 +00:00
|
|
|
return false;
|
2016-01-27 09:11:59 +00:00
|
|
|
}
|
|
|
|
|
2016-09-17 18:10:22 +00:00
|
|
|
void CSpeedLimiter::AlterSpeed( const ESpeedChange SpeedChange )
|
2016-01-27 09:11:59 +00:00
|
|
|
{
|
2016-09-17 18:10:22 +00:00
|
|
|
int32_t SpeedFactor = 1;
|
|
|
|
if (SpeedChange == DECREASE_SPEED) { SpeedFactor = -1; }
|
2016-01-27 09:11:59 +00:00
|
|
|
|
2016-09-17 18:10:22 +00:00
|
|
|
if (m_Speed >= m_HighSpeed)
|
|
|
|
{
|
|
|
|
m_Speed += 10 * SpeedFactor;
|
|
|
|
}
|
|
|
|
else if (m_Speed >= m_MediumSpeed)
|
|
|
|
{
|
|
|
|
m_Speed += 5 * SpeedFactor;
|
|
|
|
}
|
2016-09-17 18:15:03 +00:00
|
|
|
else if (m_Speed > 1 && SpeedChange == DECREASE_SPEED || SpeedChange == INCREASE_SPEED)
|
2016-09-17 18:10:22 +00:00
|
|
|
{
|
|
|
|
m_Speed += 1 * SpeedFactor;
|
|
|
|
}
|
|
|
|
|
|
|
|
SpeedChanged(m_Speed);
|
|
|
|
FixSpeedRatio();
|
|
|
|
}
|