forked from vcc6809/VCC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
throttle.c
96 lines (78 loc) · 2.61 KB
/
throttle.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
Copyright 2015 by Joseph Forgione
This file is part of VCC (Virtual Color Computer).
VCC (Virtual Color Computer) is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
VCC (Virtual Color Computer) is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with VCC (Virtual Color Computer). If not, see <http://www.gnu.org/licenses/>.
*/
#include <windows.h>
#include "throttle.h"
#include "audio.h"
#include "defines.h"
#include "vcc.h"
static _LARGE_INTEGER StartTime,EndTime,OneFrame,CurrentTime,SleepRes,TargetTime,OneMs;
static _LARGE_INTEGER MasterClock,Now;
static unsigned char FrameSkip=0;
static float fMasterClock=0;
void CalibrateThrottle(void)
{
timeBeginPeriod(1); //Needed to get max resolution from the timer normally its 10Ms
QueryPerformanceFrequency(&MasterClock);
OneFrame.QuadPart=MasterClock.QuadPart/(TARGETFRAMERATE);
OneMs.QuadPart=MasterClock.QuadPart/1000;
fMasterClock=(float)MasterClock.QuadPart;
}
void StartRender(void)
{
QueryPerformanceCounter(&StartTime);
return;
}
void EndRender(unsigned char Skip)
{
FrameSkip=Skip;
TargetTime.QuadPart=( StartTime.QuadPart+(OneFrame.QuadPart*FrameSkip));
return;
}
void FrameWait(void)
{
QueryPerformanceCounter(&CurrentTime);
while ( (TargetTime.QuadPart-CurrentTime.QuadPart)> (OneMs.QuadPart*2)) //If we have more that 2Ms till the end of the frame
{
Sleep(1); //Give about 1Ms back to the system
QueryPerformanceCounter(&CurrentTime); //And check again
}
if (GetSoundStatus()) //Lean on the sound card a bit for timing
{
PurgeAuxBuffer();
if (FrameSkip==1)
{
if (GetFreeBlockCount()>AUDIOBUFFERS/2) //Dont let the buffer get lest that half full
return;
while (GetFreeBlockCount() < 1); // Dont let it fill up either
}
}
while ( CurrentTime.QuadPart< TargetTime.QuadPart) //Poll Untill frame end.
QueryPerformanceCounter(&CurrentTime);
return;
}
float CalculateFPS(void) //Done at end of render;
{
static unsigned short FrameCount=0;
static float fps=0,fNow=0,fLast=0;
if (++FrameCount!=FRAMEINTERVAL)
return(fps);
QueryPerformanceCounter(&Now);
fNow=(float)Now.QuadPart;
fps=(fNow-fLast)/fMasterClock;
fLast=fNow;
FrameCount=0;
fps= FRAMEINTERVAL/fps;
return(fps);
}