Glide Plugin GLES2 port from mupen64plus-ae, but with special FrameSkip code
[mupen64plus-pandora.git] / source / gles2glide64 / src / Glide64 / FrameSkipper.cpp
CommitLineData
98e75f2d 1/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 * Copyright (C) 2011 yongzh (freeman.yong@gmail.com) *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
18 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
19
20#include "FrameSkipper.h"
21#include "ticks.h"
22#include <stdio.h>
23
24FrameSkipper::FrameSkipper()
25 : skipType(AUTO), maxSkips(2), targetFPS(60)
26{
27}
28
29void FrameSkipper::start()
30{
31 initialTicks = 0;
32 virtualCount = 0;
33 skipCounter = 0;
34 oldSkip = 0;
35}
36#if 0
37bool FrameSkipper::hasSkipped()
38{
39 oldSkip = skipCounter;
40
41 if (skipType == MANUAL) {
42 if (++skipCounter > maxSkips)
43 skipCounter = 0;
44//printf("frameskipper(Manual): oldSkip=%i, skipCounter=%i, countVI=%i\n", oldSkip, skipCounter, countVI);
45 countVI = 0;
46 return (oldSkip>0);
47 }
48
49 unsigned int elapsed = ticksGetTicks()/* - initialTicks*/;
50 int realCount = (elapsed-initialTicks - 4) * targetFPS; // 4ms tolerance
51 if ((realCount > countVI*1000) && (skipCounter < maxSkips) && (countVI<10)) {
52 skipCounter++;
53//printf("Skip frame elapsed=%u realCount=%u countVI=%u skipCounter=%i\n", elapsed-initialTicks, realCount, countVI, skipCounter);
54 } else {
55//printf(" Ok frame elapsed=%u realCount=%u countVI=%u skipCounter=%i\n", elapsed-initialTicks, realCount, countVI, skipCounter);
56 skipCounter = 0;
57 initialTicks=elapsed;
58 virtualCount=0;
59 countVI=0;
60 }
61//printf("frameskipper(Auto): oldSkip=%i, skipCounter=%i, countVI=%i, realCount=%u\n", oldSkip, skipCounter, countVI, realCount);
62 return (oldSkip>0);
63}
64#else
65void FrameSkipper::newFrame()
66{
67 oldSkip = skipCounter;
68
69 if (skipType == MANUAL) {
70 if (++skipCounter > maxSkips)
71 skipCounter = 0;
72//printf("frameskipper(Manual): oldSkip=%i, skipCounter=%i, countVI=%i\n", oldSkip, skipCounter, countVI);
73 countVI = 0;
74 return;
75 }
76
77 unsigned int elapsed = ticksGetTicks()/* - initialTicks*/;
78 int realCount = (elapsed-initialTicks - 4) * targetFPS; // 4ms tolerance
79 if ((realCount > countVI*1000) && (skipCounter < maxSkips) && (countVI<10)) {
80 skipCounter++;
81//printf("Skip frame elapsed=%u realCount=%u countVI=%u skipCounter=%i\n", elapsed-initialTicks, realCount, countVI, skipCounter);
82 } else {
83//printf(" Ok frame elapsed=%u realCount=%u countVI=%u skipCounter=%i\n", elapsed-initialTicks, realCount, countVI, skipCounter);
84 skipCounter = 0;
85 initialTicks=elapsed;
86 virtualCount=0;
87 countVI=0;
88 }
89//printf("frameskipper(Auto): oldSkip=%i, skipCounter=%i, countVI=%i, realCount=%u\n", oldSkip, skipCounter, countVI, realCount);
90 return;
91}
92#endif
93
94void FrameSkipper::update()
95{
96#if 1
97 if (initialTicks == 0) {
98 initialTicks = ticksGetTicks();
99 }
100 countVI++;
101
102 if (countVI>20) {
103 skipCounter=0; // failsafe...
104 }
105#else
106 // for the first frame
107 if (initialTicks == 0) {
108 initialTicks = ticksGetTicks();
109 return;
110 }
111
112 oldSkip=skipCounter;
113
114 if (skipType == MANUAL) {
115 if (++skipCounter > maxSkips)
116 skipCounter = 0;
117 return;
118 }
119
120 unsigned int elapsed = ticksGetTicks()/* - initialTicks*/;
121 unsigned int realCount = (elapsed-initialTicks) * targetFPS;
122
123 virtualCount+=1000;
124// if (realCount >= virtualCount) {
125 if (realCount > virtualCount+100 &&
126 /*skipType == AUTO &&*/ skipCounter < maxSkips) {
127 skipCounter++;
128//printf("Skip frame elapsed=%u initialTicks=%u realCount=%u virtualCound=%u skipCounter=%i\n", elapsed, initialTicks, realCount, virtualCount, skipCounter);
129 } else {
130// virtualCount = realCount;
131// if (skipType == AUTO)
132 skipCounter = 0;
133 initialTicks=elapsed;
134 virtualCount=0;
135 }
136/* } else {
137 skipCounter = 0;
138 initialTicks=elapsed;
139 virtualCount=0;
140 }*/
141#endif
142}