1 /***************************************************************************
4 begin : Sun Oct 28 2001
5 copyright : (C) 2001 by Pete Bernert
6 email : BlackDove@addcom.de
7 ***************************************************************************/
8 /***************************************************************************
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. See also the license.txt file for *
14 * additional informations. *
16 ***************************************************************************/
22 #include "externals.h"
28 DWORD dwFrameRateTicks=16;
34 // FPS skipping / limit
41 void CheckFrameRate(void)
43 if(UseFrameSkip) // skipping mode?
45 if(!(dwActFixes&0x80)) // not old skipping mode?
47 dwLaceCnt++; // -> store cnt of vsync between frames
48 if(dwLaceCnt>=MAXLACE && UseFrameLimit) // -> if there are many laces without screen toggling,
49 { // do std frame limitation
50 if(dwLaceCnt==MAXLACE) bInitCap=TRUE;
54 else if(UseFrameLimit) FrameCap();
55 calcfps(); // -> calc fps display in skipping mode
57 else // non-skipping mode:
59 if(UseFrameLimit) FrameCap(); // -> do it
60 if(ulKeybits&KEY_SHOWFPS) calcfps(); // -> and calc fps display
64 #define TIMEBASE 100000
66 unsigned long timeGetTime()
69 gettimeofday(&tv, 0); // well, maybe there are better ways
70 return tv.tv_sec * 100000 + tv.tv_usec/10; // to do that, but at least it works
75 static unsigned long curticks, lastticks, _ticks_since_last_update;
76 static unsigned int TicksToWait = 0;
77 int overslept=0, tickstogo=0;
81 curticks = timeGetTime();
82 _ticks_since_last_update = curticks - lastticks;
84 if((_ticks_since_last_update > TicksToWait) ||
85 (curticks <lastticks))
88 overslept = _ticks_since_last_update - TicksToWait;
89 if((_ticks_since_last_update-TicksToWait) > dwFrameRateTicks)
92 TicksToWait=dwFrameRateTicks - overslept;
98 curticks = timeGetTime();
99 _ticks_since_last_update = curticks - lastticks;
100 tickstogo = TicksToWait - _ticks_since_last_update;
101 if ((_ticks_since_last_update > TicksToWait) ||
102 (curticks < lastticks) || tickstogo < overslept)
105 lastticks = curticks;
106 overslept = _ticks_since_last_update - TicksToWait;
107 TicksToWait = dwFrameRateTicks - overslept;
110 if (tickstogo >= 200 && !(dwActFixes&16))
111 usleep(tickstogo*10 - 200);
121 static int iNumSkips=0,iAdditionalSkip=0; // number of additional frames to skip
122 static DWORD dwLastLace=0; // helper var for frame limitation
123 static DWORD curticks, lastticks, _ticks_since_last_update;
125 static int overslept=0;
127 if(!dwLaceCnt) return; // important: if no updatelace happened, we ignore it completely
129 if(iNumSkips) // we are in skipping mode?
131 dwLastLace+=dwLaceCnt; // -> calc frame limit helper (number of laces)
132 bSkipNextFrame = TRUE; // -> we skip next frame
133 iNumSkips--; // -> ok, one done
135 else // ok, no additional skipping has to be done...
136 { // we check now, if some limitation is needed, or a new skipping has to get started
139 if(bInitCap || bSkipNextFrame) // first time or we skipped before?
141 if(UseFrameLimit && !bInitCap) // frame limit wanted and not first time called?
143 DWORD dwT=_ticks_since_last_update; // -> that's the time of the last drawn frame
144 dwLastLace+=dwLaceCnt; // -> and that's the number of updatelace since the start of the last drawn frame
146 curticks = timeGetTime(); // -> now we calc the time of the last drawn frame + the time we spent skipping
147 _ticks_since_last_update= dwT+curticks - lastticks;
149 dwWaitTime=dwLastLace*dwFrameRateTicks; // -> and now we calc the time the real psx would have needed
151 if(_ticks_since_last_update<dwWaitTime) // -> we were too fast?
153 if((dwWaitTime-_ticks_since_last_update)> // -> some more security, to prevent
154 (60*dwFrameRateTicks)) // wrong waiting times
155 _ticks_since_last_update=dwWaitTime;
157 while(_ticks_since_last_update<dwWaitTime) // -> loop until we have reached the real psx time
158 { // (that's the additional limitation, yup)
159 curticks = timeGetTime();
160 _ticks_since_last_update = dwT+curticks - lastticks;
163 else // we were still too slow ?!!?
165 if(iAdditionalSkip<MAXSKIP) // -> well, somewhen we really have to stop skipping on very slow systems
167 iAdditionalSkip++; // -> inc our watchdog var
168 dwLaceCnt=0; // -> reset lace count
169 lastticks = timeGetTime();
170 return; // -> done, we will skip next frame to get more speed
175 bInitCap=FALSE; // -> ok, we have inited the frameskip func
176 iAdditionalSkip=0; // -> init additional skip
177 bSkipNextFrame=FALSE; // -> we don't skip the next frame
178 lastticks = timeGetTime(); // -> we store the start time of the next frame
179 dwLaceCnt=0; // -> and we start to count the laces
181 _ticks_since_last_update=0;
182 return; // -> done, the next frame will get drawn
185 bSkipNextFrame=FALSE; // init the frame skip signal to 'no skipping' first
187 curticks = timeGetTime(); // get the current time (we are now at the end of one drawn frame)
188 _ticks_since_last_update = curticks - lastticks;
190 dwLastLace=dwLaceCnt; // store curr count (frame limitation helper)
191 dwWaitTime=dwLaceCnt*dwFrameRateTicks; // calc the 'real psx lace time'
192 if (dwWaitTime >= overslept)
193 dwWaitTime-=overslept;
195 if(_ticks_since_last_update>dwWaitTime) // hey, we needed way too long for that frame...
197 if(UseFrameLimit) // if limitation, we skip just next frame,
198 { // and decide after, if we need to do more
203 iNumSkips=_ticks_since_last_update/dwWaitTime; // -> calc number of frames to skip to catch up
204 iNumSkips--; // -> since we already skip next frame, one down
205 if(iNumSkips>MAXSKIP) iNumSkips=MAXSKIP; // -> well, somewhere we have to draw a line
207 bSkipNextFrame = TRUE; // -> signal for skipping the next frame
209 else // we were faster than real psx? fine :)
210 if(UseFrameLimit) // frame limit used? so we wait til the 'real psx time' has been reached
212 if(dwLaceCnt>MAXLACE) // -> security check
213 _ticks_since_last_update=dwWaitTime;
215 while(_ticks_since_last_update<dwWaitTime) // -> just do a waiting loop...
217 curticks = timeGetTime();
218 _ticks_since_last_update = curticks - lastticks;
220 tickstogo = dwWaitTime - _ticks_since_last_update;
221 if (tickstogo-overslept >= 200 && !(dwActFixes&16))
222 usleep(tickstogo*10 - 200);
225 overslept = _ticks_since_last_update - dwWaitTime;
228 lastticks = timeGetTime(); // ok, start time of the next frame
231 dwLaceCnt=0; // init lace counter
236 static unsigned long curticks,_ticks_since_last_update,lastticks;
237 static long fps_cnt = 0;
238 static unsigned long fps_tck = 1;
239 static long fpsskip_cnt = 0;
240 static unsigned long fpsskip_tck = 1;
243 curticks = timeGetTime();
244 _ticks_since_last_update=curticks-lastticks;
246 if(UseFrameSkip && !UseFrameLimit && _ticks_since_last_update)
247 fps_skip=min(fps_skip,((float)TIMEBASE/(float)_ticks_since_last_update+1.0f));
249 lastticks = curticks;
252 if(UseFrameSkip && UseFrameLimit)
254 fpsskip_tck += _ticks_since_last_update;
258 fps_skip = (float)2000/(float)fpsskip_tck;
265 fps_tck += _ticks_since_last_update;
269 fps_cur = (float)(TIMEBASE*20)/(float)fps_tck;
274 //if(UseFrameLimit && fps_cur>fFrameRateHz) // optical adjust ;) avoids flickering fps display
275 //fps_cur=fFrameRateHz;
280 void PCFrameCap (void)
282 static unsigned long curticks, lastticks, _ticks_since_last_update;
283 static unsigned long TicksToWait = 0;
288 curticks = timeGetTime();
289 _ticks_since_last_update = curticks - lastticks;
290 if ((_ticks_since_last_update > TicksToWait) ||
291 (curticks < lastticks))
294 lastticks = curticks;
295 TicksToWait = (TIMEBASE/ (unsigned long)fFrameRateHz);
302 static unsigned long curticks,_ticks_since_last_update,lastticks;
303 static long fps_cnt = 0;
304 static float fps_acc = 0;
307 curticks = timeGetTime();
308 _ticks_since_last_update=curticks-lastticks;
309 if(_ticks_since_last_update)
310 CurrentFPS=(float)TIMEBASE/(float)_ticks_since_last_update;
312 lastticks = curticks;
314 fps_acc += CurrentFPS;
318 fps_cur = fps_acc / 10;
323 fps_skip=CurrentFPS+1.0f;
326 void SetAutoFrameCap(void)
330 fFrameRateHz = fFrameRate;
331 dwFrameRateTicks=(TIMEBASE*100 / (unsigned long)(fFrameRateHz*100));
337 if (PSXDisplay.Interlaced)
338 fFrameRateHz = PSXDisplay.PAL?50.0f:60.0f;
339 else fFrameRateHz = PSXDisplay.PAL?25.0f:30.0f;
343 fFrameRateHz = PSXDisplay.PAL?50.0f:59.94f;
344 dwFrameRateTicks=(TIMEBASE*100 / (unsigned long)(fFrameRateHz*100));
348 void SetFPSHandler(void)
354 if(!fFrameRate) fFrameRate=200.0f;
355 if(fFrameRateHz==0) fFrameRateHz=fFrameRate; // set user framerate
356 dwFrameRateTicks=(TIMEBASE / (unsigned long)fFrameRateHz);