Skip to content

Commit

Permalink
make manual timer an actual option and polish it a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
khang06 committed Mar 31, 2020
1 parent ddff451 commit 678a91c
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 17 deletions.
3 changes: 3 additions & 0 deletions PianoFromAbove/Config.h
Expand Up @@ -195,6 +195,9 @@ class Config : public ISettings
void SetVideoSettings(const VideoSettings &videoSettings) { m_VideoSettings = videoSettings; }
void SetControlsSettings(const ControlsSettings &ControlsSettings) { m_ControlsSettings = ControlsSettings; }

// i really need to start writting getters and setters
bool m_bManualTimer = false;

private:
// Singleton
Config();
Expand Down
2 changes: 2 additions & 0 deletions PianoFromAbove/ConfigProcs.cpp
Expand Up @@ -705,6 +705,8 @@ INT_PTR WINAPI TracksProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
vq_capacity_proc_res = 0; // just in case
}
}

Config::GetConfig().m_bManualTimer = IsDlgButtonChecked(hWnd, IDC_CHECK1);
}
case IDCANCEL:
EndDialog( hWnd, iId );
Expand Down
52 changes: 46 additions & 6 deletions PianoFromAbove/GameState.cpp
Expand Up @@ -11,6 +11,7 @@
#include <algorithm>
#include <tchar.h>
#include <ppl.h>
#include <dwmapi.h>

#include "Globals.h"
#include "GameState.h"
Expand Down Expand Up @@ -164,6 +165,8 @@ void SplashScreen::InitState()
if ( cAudio.iOutDevice >= 0 )
m_OutDevice.Open( cAudio.iOutDevice );
m_OutDevice.SetVolume( 1.0 );

m_Timer.Init(false);
}

GameState::GameError SplashScreen::Init()
Expand Down Expand Up @@ -593,6 +596,9 @@ void MainScreen::InitState()
double dNSpeed = cPlayback.GetNSpeed();
m_llTimeSpan = static_cast< long long >( 3.0 * dNSpeed * 1000000 );

// m_Timer will be initialized *later*
m_RealTimer.Init(false);

if (m_bDumpFrames) {
m_hVideoPipe = CreateNamedPipe(TEXT("\\\\.\\pipe\\pfadump"),
PIPE_ACCESS_OUTBOUND,
Expand All @@ -612,14 +618,26 @@ void MainScreen::InitState()
// Called immediately before changing to this state
GameState::GameError MainScreen::Init()
{
static const AudioSettings &cAudio = Config::GetConfig().GetAudioSettings();
static Config& config = Config::GetConfig();
static const AudioSettings &cAudio = config.GetAudioSettings();
if ( cAudio.iOutDevice >= 0 )
m_OutDevice.Open( cAudio.iOutDevice );

m_OutDevice.SetVolume( 1.0 );
if (m_Timer.m_bManualTimer)
m_Timer.SetFrameRate(144);
//batch_vertices.reserve(m_MIDI.GetInfo().iNoteCount * 4);
m_Timer.Init(config.m_bManualTimer);
if (m_Timer.m_bManualTimer) {
// get the screen's refresh rate
DWM_TIMING_INFO timing_info;
memset(&timing_info, 0, sizeof(timing_info));
timing_info.cbSize = sizeof(timing_info);
if (FAILED(DwmGetCompositionTimingInfo(NULL, &timing_info))) {
MessageBox(NULL, L"Failed to get the screen refresh rate! Defaulting to 60hz...", L"", MB_ICONERROR);
m_Timer.SetFrameRate(60);
} else {
m_Timer.SetFrameRate(ceil(static_cast<float>(timing_info.rateRefresh.uiNumerator) / static_cast<float>(timing_info.rateRefresh.uiDenominator)));
}

}

batch_vertices.reserve(vq_capacity_proc_res);
return Success;
Expand Down Expand Up @@ -920,7 +938,10 @@ GameState::GameError MainScreen::Logic( void )
m_iStartNote = min( cVisual.iFirstKey, cVisual.iLastKey );
m_iEndNote = max( cVisual.iFirstKey, cVisual.iLastKey );
m_bShowFPS = cVideo.bShowFPS;
m_pRenderer->SetLimitFPS( cVideo.bLimitFPS );
if (m_Timer.m_bManualTimer)
m_pRenderer->SetLimitFPS(true);
else
m_pRenderer->SetLimitFPS( cVideo.bLimitFPS );
if ( cVisual.iBkgColor != m_csBackground.iOrigBGR ) m_csBackground.SetColor( cVisual.iBkgColor, 0.7f, 1.3f );

double dMaxCorrect = ( mInfo.iMaxVolume > 0 ? 127.0 / mInfo.iMaxVolume : 1.0 );
Expand All @@ -930,10 +951,12 @@ GameState::GameError MainScreen::Logic( void )
// Time stuff
long long llMaxTime = GetMaxTime();
long long llElapsed = m_Timer.GetMicroSecs();
long long llRealElapsed = m_RealTimer.GetMicroSecs();
m_Timer.Start();
m_RealTimer.Start();

// Compute FPS every half a second
m_llFPSTime += llElapsed;
m_llFPSTime += llRealElapsed;
m_iFPSCount++;
if ( m_llFPSTime >= 500000 )
{
Expand Down Expand Up @@ -1874,6 +1897,8 @@ void MainScreen::RenderBorder()
void MainScreen::RenderText()
{
int iLines = 4;
if (m_Timer.m_bManualTimer)
iLines++;

// Screen info
RECT rcStatus = { m_pRenderer->GetBufferWidth() - 156, 0, m_pRenderer->GetBufferWidth(), 6 + 16 * iLines };
Expand Down Expand Up @@ -1930,6 +1955,12 @@ void MainScreen::RenderStatus(LPRECT prcStatus)
TCHAR sStateSize[128];
_stprintf_s(sStateSize, TEXT("%llu"), state_size);

TCHAR sSpeed[128];
if (m_Timer.m_bManualTimer) {
// Build speed text if in manual timer mode
_stprintf_s(sSpeed, TEXT("%.1f%%"), (m_dFPS / m_Timer.m_dFramerate) * 100.0);
}


// Display the text
InflateRect(prcStatus, -6, -3);
Expand Down Expand Up @@ -1961,6 +1992,15 @@ void MainScreen::RenderStatus(LPRECT prcStatus)
OffsetRect(prcStatus, -2, -1);
m_pRenderer->DrawText(TEXT("m_vState:"), Renderer::Small, prcStatus, 0, 0xFFFFFFFF);
m_pRenderer->DrawText(sStateSize, Renderer::Small, prcStatus, DT_RIGHT, 0xFFFFFFFF);

if (m_Timer.m_bManualTimer) {
OffsetRect(prcStatus, 2, 16 + 1);
m_pRenderer->DrawText(TEXT("Speed:"), Renderer::Small, prcStatus, 0, 0xFF404040);
m_pRenderer->DrawText(sSpeed, Renderer::Small, prcStatus, DT_RIGHT, 0xFF404040);
OffsetRect(prcStatus, -2, -1);
m_pRenderer->DrawText(TEXT("Speed:"), Renderer::Small, prcStatus, 0, 0xFFFFFFFF);
m_pRenderer->DrawText(sSpeed, Renderer::Small, prcStatus, DT_RIGHT, 0xFFFFFFFF);
}
}

void MainScreen::RenderMessage(LPRECT prcMsg, TCHAR* sMsg)
Expand Down
1 change: 1 addition & 0 deletions PianoFromAbove/GameState.h
Expand Up @@ -249,6 +249,7 @@ class MainScreen : public GameState
double m_dSpeed; // Speed multiplier
bool m_bPaused; // Paused state
Timer m_Timer; // Frame timers
Timer m_RealTimer;
bool m_bMute;
double m_dVolume;
size_t m_iNotesPlayed = 0;
Expand Down
16 changes: 8 additions & 8 deletions PianoFromAbove/Misc.cpp
Expand Up @@ -20,11 +20,16 @@ using namespace std;
// The Timer class
//-----------------------------------------------------------------------------

Timer::Timer()
Timer::~Timer()
{
if ( !m_bManualTimer)
timeEndPeriod( 1 );
}

void Timer::Init(bool manual) {
// Get the frequency. This should be done in a static constructor. Oh well.
LARGE_INTEGER liFreq = { 0 };
m_bManualTimer = false;
m_bManualTimer = manual;
m_llManualTicks = 0;
if (!m_bManualTimer) {
timeBeginPeriod(1);
Expand All @@ -40,12 +45,6 @@ Timer::Timer()
m_llStartTicks = m_llPausedTicks = 0;
}

Timer::~Timer()
{
if ( !m_bManualTimer)
timeEndPeriod( 1 );
}

// Start/reset the timer
void Timer::Start()
{
Expand Down Expand Up @@ -120,6 +119,7 @@ void Timer::AddManualTime(long long time)
void Timer::SetFrameRate(unsigned rate)
{
m_llTicksPerSec = (long long)rate * 100;
m_dFramerate = rate;
}

void Timer::IncrementFrame()
Expand Down
3 changes: 2 additions & 1 deletion PianoFromAbove/Misc.h
Expand Up @@ -17,8 +17,8 @@ class Timer
{
public:
// Initializes variables
Timer();
~Timer();
void Init(bool manual);

// The various clock actions
void Start();
Expand All @@ -40,6 +40,7 @@ class Timer
void SetFrameRate(unsigned rate);
void IncrementFrame();
bool m_bManualTimer;
double m_dFramerate;

private:
static const long long m_llPrecisionLimit = 1000000000ll;
Expand Down
Binary file modified PianoFromAbove/PianoFromAbove.rc
Binary file not shown.
2 changes: 1 addition & 1 deletion PianoFromAbove/PianoFromAbove.vcxproj
Expand Up @@ -173,7 +173,7 @@
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<AdditionalManifestDependencies>"type='Win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'";%(AdditionalManifestDependencies)</AdditionalManifestDependencies>
<AdditionalDependencies>d3d9.lib;d3dx9.lib;Comctl32.lib;WinMM.lib;UxTheme.lib;Advapi32.lib;Msimg32.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies> Dwmapi.lib;d3d9.lib;d3dx9.lib;Comctl32.lib;WinMM.lib;UxTheme.lib;Advapi32.lib;Msimg32.lib;%(AdditionalDependencies)</AdditionalDependencies>
<Version>
</Version>
</Link>
Expand Down
3 changes: 2 additions & 1 deletion PianoFromAbove/resource.h
Expand Up @@ -84,6 +84,7 @@
#define IDC_LICENSE 1093
#define IDC_LOCK 1094
#define IDC_NSPEED 1095
#define IDC_CHECK1 1096
#define ID_FILE_PLAYFILE 40001
#define ID_FILE_ADDFILE 40002
#define ID_FILE_ADDFOLDER 40003
Expand Down Expand Up @@ -181,7 +182,7 @@
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE 153
#define _APS_NEXT_COMMAND_VALUE 40172
#define _APS_NEXT_CONTROL_VALUE 1096
#define _APS_NEXT_CONTROL_VALUE 1097
#define _APS_NEXT_SYMED_VALUE 102
#endif
#endif

0 comments on commit 678a91c

Please sign in to comment.