aboutsummaryrefslogtreecommitdiff
path: root/src/libparsec/sw_timer.cpp
blob: 78d73ed8d655e171c9bcb00ad9b74dbf3f1fb4fb (plain)
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*
 * PARSEC - Frame Timing
 *
 * Orginally written by:
 *   Copyright (c) Markus Hadwiger     <msh@parsec.org>   1998-2000
 *
 *  This program 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 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */ 
#include "config.h"

#ifdef SYSTEM_TARGET_WINDOWS

// C library includes
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// time functions

// compilation flags/debug support
#include "debug.h"

// general definitions
#include "general.h"
#include "objstruc.h"

// global externals
#include "globals.h"

// subsystem headers
#ifdef PARSEC_CLIENT
#include "aud_defs.h"
#include "inp_defs.h"
#endif //PARSEC_CLIENT
#include "sys_defs.h"

// windows headers
#include <windows.h>

// local module header
#include "sw_timer.h"



// global variables for frame timing ------------------------------------------
//
volatile int		FrameRate;
volatile int		FrameCounter;
volatile int		RefTimeCount;
volatile int		RefFrameCount;


// timer vars -----------------------------------------------------------------
//
MMRESULT		sw_SecTimerID;			// ID of the timer that is called each second
__int64			sw_curTimer;			// current timer value
hprec_t			sw_dTimerFreqFac;		// factor from timer to refframes
__int64			sw_TimerFreq;			// frequency of performance timer


// reference frame count pausing ----------------------------------------------
//
static int			refframe_count_paused	= FALSE;
static refframe_t	refframe_count_pauseval	= REFFRAME_INVALID;
static refframe_t	refframe_count_pauseofs	= 0;


//  callback for Sec.Timer to set the current framerate -----------------------
//
void CALLBACK
SW_SecTimeProc( UINT uID, UINT uMsg, DWORD dwUser, DWORD dw1, DWORD dw2 )
{
	FrameRate = FrameCounter;
	FrameCounter = 0;
}


// init frame timer handler ---------------------------------------------------
//
int SYSs_InitFrameTimer()
{
	int static init_done = FALSE;

	if ( init_done ) {

		// calc refframe count for old frequency
		QueryPerformanceCounter( (LARGE_INTEGER*) &sw_curTimer );
		refframe_t refframecount = (int) ( sw_curTimer * sw_dTimerFreqFac );

		// calc correction factor for new frequency
		sw_dTimerFreqFac = (hprec_t)FRAME_MEASURE_TIMEBASE / sw_TimerFreq;

		// calc offset to ensure monotonically increasing refframecount
		// even though we now use the new frequency
		RefFrameCount = (int) ( sw_curTimer * sw_dTimerFreqFac );
		refframe_count_pauseofs += RefFrameCount - refframecount;

	} else {

		// query the frequency of the performance counter and calculate
		// the factor to adjust to our FRAME_MEASURE_TIMEBASE
		QueryPerformanceFrequency( (LARGE_INTEGER *) &sw_TimerFreq );
		sw_dTimerFreqFac = (hprec_t)FRAME_MEASURE_TIMEBASE / sw_TimerFreq;

		// create the timer which is called every second
		sw_SecTimerID = timeSetEvent( 1000, 0, &SW_SecTimeProc, 0, TIME_PERIODIC );

		// set initial count
		SYSs_InitRefFrameCount();

		init_done = TRUE;
	}

	return 1;
}


// de-init frame timer handler ------------------------------------------------
//
int SYSs_KillFrameTimer()
{
	// terminate the seconds timer
	timeKillEvent( sw_SecTimerID );

	return 1;
}


// init reference frame counting ----------------------------------------------
//
void SYSs_InitRefFrameCount()
{
	SYSs_GetRefFrameCount();

	// init refframe count pausing/offset
	refframe_count_paused	= FALSE;
	refframe_count_pauseval	= REFFRAME_INVALID;
	refframe_count_pauseofs	= 0;
}


// get count of current reference frame ---------------------------------------
//
refframe_t SYSs_GetRefFrameCount()
{
	QueryPerformanceCounter( (LARGE_INTEGER*) &sw_curTimer );
	RefFrameCount = (int) ( sw_curTimer * sw_dTimerFreqFac );

	return RefFrameCount - refframe_count_pauseofs;
}


// pause reference frame counting ---------------------------------------------
//
void SYSs_PauseRefFrameCount()
{
	if ( !refframe_count_paused ) {

		SYSs_GetRefFrameCount();

		refframe_count_pauseval	= RefFrameCount;
		refframe_count_paused	= TRUE;
	}
}


// resume reference frame counting --------------------------------------------
//
void SYSs_ResumeRefFrameCount()
{
	if ( refframe_count_paused ) {

		SYSs_GetRefFrameCount();

		refframe_count_pauseofs	+= RefFrameCount - refframe_count_pauseval;
		refframe_count_pauseval	 = REFFRAME_INVALID;
		refframe_count_paused	 = FALSE;
	}
}


// wait for specified number of reference frames ------------------------------
//
void SYSs_Wait( refframe_t refframes )
{
	refframe_t basecount = SYSs_GetRefFrameCount();

	while ( SYSs_GetRefFrameCount() - basecount < refframes ) {
		SYSs_Yield();
	}
}


// guaranteed to be called once per frame -------------------------------------
//
int SYSs_Yield()
{
#ifdef PARSEC_CLIENT

	// yield to sound driver
	AUDs_MaintainSound();

	// must be called to retrieve key strokes from buffer
	INPs_Collect();

#endif // PARSEC_CLIENT

	return 1;
}

#ifdef PARSEC_SERVER

REGISTER_MODULE( SW_TIMER )
{
	SYSs_InitFrameTimer();
}

#endif // PARSEC_SERVER

#endif // SYSTEM_TARGET_WINDOWS