2012-12-19 09:30:18 +00:00
|
|
|
/****************************************************************************
|
|
|
|
* *
|
|
|
|
* Project 64 - 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 *
|
|
|
|
* *
|
|
|
|
****************************************************************************/
|
2010-06-07 02:23:58 +00:00
|
|
|
#include "stdafx.h"
|
2008-09-18 03:15:49 +00:00
|
|
|
#pragma comment(lib, "winmm.lib")
|
|
|
|
|
2012-12-31 12:46:03 +00:00
|
|
|
CSpeedLimitor::CSpeedLimitor(void)
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
|
|
|
m_Frames = 0;
|
|
|
|
m_LastTime = 0;
|
|
|
|
m_Speed = 60;
|
|
|
|
m_BaseSpeed = 60;
|
|
|
|
m_Ratio = 1000.0F / (float)m_Speed;
|
|
|
|
|
|
|
|
TIMECAPS Caps;
|
|
|
|
timeGetDevCaps(&Caps, sizeof(Caps));
|
|
|
|
if (timeBeginPeriod(Caps.wPeriodMin) == TIMERR_NOCANDO) {
|
2012-11-17 00:58:31 +00:00
|
|
|
g_Notify->DisplayError("Error during timer begin");
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CSpeedLimitor::~CSpeedLimitor(void) {
|
|
|
|
TIMECAPS Caps;
|
|
|
|
timeGetDevCaps(&Caps, sizeof(Caps));
|
|
|
|
timeEndPeriod(Caps.wPeriodMin);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSpeedLimitor::SetHertz (DWORD Hertz ) {
|
|
|
|
m_Speed = Hertz;
|
|
|
|
m_BaseSpeed = Hertz;
|
|
|
|
FixSpeedRatio();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSpeedLimitor::FixSpeedRatio ( void )
|
|
|
|
{
|
|
|
|
m_Ratio = 1000.0f / static_cast<double>(m_Speed);
|
|
|
|
m_Frames = 0;
|
|
|
|
m_LastTime = timeGetTime();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CSpeedLimitor::Timer_Process (DWORD * FrameRate ) {
|
|
|
|
m_Frames += 1;
|
|
|
|
DWORD CurrentTime = timeGetTime();
|
|
|
|
|
|
|
|
/* Calculate time that should of elapsed for this frame */
|
|
|
|
double CalculatedTime = (double)m_LastTime + (m_Ratio * (double)m_Frames);
|
|
|
|
if ((double)CurrentTime < CalculatedTime) {
|
|
|
|
long time = (int)(CalculatedTime - (double)CurrentTime);
|
|
|
|
if (time > 0) {
|
|
|
|
Sleep(time);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Refresh current time */
|
|
|
|
CurrentTime = timeGetTime();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (CurrentTime - m_LastTime >= 1000) {
|
|
|
|
/* Output FPS */
|
|
|
|
if (FrameRate != NULL) { *FrameRate = m_Frames; }
|
|
|
|
m_Frames = 0;
|
|
|
|
m_LastTime = CurrentTime;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-31 12:46:03 +00:00
|
|
|
void CSpeedLimitor::IncreaeSpeed ( void )
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2012-12-31 12:46:03 +00:00
|
|
|
if (m_Speed >= 60)
|
|
|
|
{
|
|
|
|
m_Speed += 10;
|
|
|
|
}
|
|
|
|
else if (m_Speed >= 15)
|
|
|
|
{
|
|
|
|
m_Speed += 5;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_Speed += 1;
|
|
|
|
}
|
|
|
|
SpeedChanged(m_Speed);
|
2008-09-18 03:15:49 +00:00
|
|
|
FixSpeedRatio();
|
|
|
|
}
|
|
|
|
|
2012-12-31 12:46:03 +00:00
|
|
|
void CSpeedLimitor::DecreaeSpeed ( void )
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2012-12-31 12:46:03 +00:00
|
|
|
if (m_Speed > 60)
|
2010-07-23 10:45:35 +00:00
|
|
|
{
|
2012-12-31 12:46:03 +00:00
|
|
|
m_Speed -= 10;
|
2010-07-23 10:45:35 +00:00
|
|
|
}
|
2012-12-31 12:46:03 +00:00
|
|
|
else if (m_Speed > 15)
|
|
|
|
{
|
|
|
|
m_Speed -= 5;
|
|
|
|
}
|
|
|
|
else if (m_Speed > 1)
|
|
|
|
{
|
|
|
|
m_Speed -= 1;
|
|
|
|
}
|
|
|
|
SpeedChanged(m_Speed);
|
|
|
|
FixSpeedRatio();
|
2015-01-31 19:27:27 +00:00
|
|
|
}
|