1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 * Copyright (C) 2011 yongzh (freeman.yong@gmail.com) *
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. *
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. *
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 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
20 #include "FrameSkipper.h"
24 FrameSkipper::FrameSkipper()
25 : skipType(AUTO), maxSkips(2), targetFPS(60)
29 void FrameSkipper::start()
37 bool FrameSkipper::hasSkipped()
39 oldSkip = skipCounter;
41 if (skipType == MANUAL) {
42 if (++skipCounter > maxSkips)
44 //printf("frameskipper(Manual): oldSkip=%i, skipCounter=%i, countVI=%i\n", oldSkip, skipCounter, countVI);
49 unsigned int elapsed = ticksGetTicks()/* - initialTicks*/;
50 int realCount = (elapsed-initialTicks - 4) * targetFPS; // 4ms tolerance
51 if ((realCount > countVI*1000) && (skipCounter < maxSkips) && (countVI<10)) {
53 //printf("Skip frame elapsed=%u realCount=%u countVI=%u skipCounter=%i\n", elapsed-initialTicks, realCount, countVI, skipCounter);
55 //printf(" Ok frame elapsed=%u realCount=%u countVI=%u skipCounter=%i\n", elapsed-initialTicks, realCount, countVI, skipCounter);
61 //printf("frameskipper(Auto): oldSkip=%i, skipCounter=%i, countVI=%i, realCount=%u\n", oldSkip, skipCounter, countVI, realCount);
65 void FrameSkipper::newFrame()
67 oldSkip = skipCounter;
69 if (skipType == MANUAL) {
70 if (++skipCounter > maxSkips)
72 //printf("frameskipper(Manual): oldSkip=%i, skipCounter=%i, countVI=%i\n", oldSkip, skipCounter, countVI);
77 unsigned int elapsed = ticksGetTicks()/* - initialTicks*/;
78 int realCount = (elapsed-initialTicks - 4) * targetFPS; // 4ms tolerance
79 if ((realCount > countVI*1000) && (skipCounter < maxSkips) && (countVI<10)) {
81 //printf("Skip frame elapsed=%u realCount=%u countVI=%u skipCounter=%i\n", elapsed-initialTicks, realCount, countVI, skipCounter);
83 //printf(" Ok frame elapsed=%u realCount=%u countVI=%u skipCounter=%i\n", elapsed-initialTicks, realCount, countVI, skipCounter);
89 //printf("frameskipper(Auto): oldSkip=%i, skipCounter=%i, countVI=%i, realCount=%u\n", oldSkip, skipCounter, countVI, realCount);
94 void FrameSkipper::update()
97 if (initialTicks == 0) {
98 initialTicks = ticksGetTicks();
103 skipCounter=0; // failsafe...
106 // for the first frame
107 if (initialTicks == 0) {
108 initialTicks = ticksGetTicks();
114 if (skipType == MANUAL) {
115 if (++skipCounter > maxSkips)
120 unsigned int elapsed = ticksGetTicks()/* - initialTicks*/;
121 unsigned int realCount = (elapsed-initialTicks) * targetFPS;
124 // if (realCount >= virtualCount) {
125 if (realCount > virtualCount+100 &&
126 /*skipType == AUTO &&*/ skipCounter < maxSkips) {
128 //printf("Skip frame elapsed=%u initialTicks=%u realCount=%u virtualCound=%u skipCounter=%i\n", elapsed, initialTicks, realCount, virtualCount, skipCounter);
130 // virtualCount = realCount;
131 // if (skipType == AUTO)
133 initialTicks=elapsed;
138 initialTicks=elapsed;