project64/Source/Project64-core/N64System/SpeedLimiterClass.cpp

136 lines
3.2 KiB
C++
Raw Normal View History

2012-12-19 09:30:18 +00:00
/****************************************************************************
* *
2015-11-10 05:21:49 +00:00
* Project64 - A Nintendo 64 emulator. *
2012-12-19 09:30:18 +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 *
* *
****************************************************************************/
#include "stdafx.h"
#include "SpeedLimiterClass.h"
2015-12-19 23:57:54 +00:00
#include <Common/Util.h>
#ifdef _WIN32
#include <Windows.h>
#include <Mmsystem.h>
#pragma comment(lib, "winmm.lib")
#endif
2015-12-14 22:09:12 +00:00
CSpeedLimiter::CSpeedLimiter()
{
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
}
2015-12-14 22:09:12 +00:00
CSpeedLimiter::~CSpeedLimiter()
{
#ifdef _WIN32
TIMECAPS Caps;
timeGetDevCaps(&Caps, sizeof(Caps));
timeEndPeriod(Caps.wPeriodMin);
#endif
}
2015-12-14 22:09:12 +00:00
void CSpeedLimiter::SetHertz(uint32_t Hertz)
{
m_Speed = Hertz;
m_BaseSpeed = Hertz;
FixSpeedRatio();
}
2015-12-14 22:09:12 +00:00
void CSpeedLimiter::FixSpeedRatio()
{
m_Ratio = 1000.0f / static_cast<double>(m_Speed);
m_Frames = 0;
#ifdef _WIN32
m_LastTime = timeGetTime();
#endif
}
2015-12-14 22:09:12 +00:00
bool CSpeedLimiter::Timer_Process(uint32_t * FrameRate)
{
#ifdef _WIN32
m_Frames += 1;
uint32_t 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)
{
int32_t time = (int)(CalculatedTime - (double)CurrentTime);
if (time > 0)
{
pjutil::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;
}
#else
return false;
#endif
}
2015-12-14 22:09:12 +00:00
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();
}
2015-12-14 22:09:12 +00:00
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();
2015-01-31 19:27:27 +00:00
}