Otclient  14/8/2020
adaptativeframecounter.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010-2020 OTClient <https://github.com/edubart/otclient>
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20  * THE SOFTWARE.
21  */
22 
23 #include "adaptativeframecounter.h"
24 #include "clock.h"
25 
27 {
28  m_frames = 0;
29  m_partialFrames = 0;
30  m_partialFps = 0;
31  m_maxPartialFps = 0;
32  m_frameDelaySum = 0;
33  m_mediumFrameDelay = 0;
34  m_lastFps = 0;
35  m_lastFrame = 0;
36  m_maxFps = 0;
37  m_sleepMicros = 0;
38  m_mediumFrameDelay = 0;
39  m_lastFpsUpdate = g_clock.micros();
40  m_lastPartialFpsUpdate = g_clock.micros();
41 }
42 
44 {
45  if(m_maxFps == 0)
46  return true;
47 
48  ticks_t now = g_clock.micros();
49  if(now - m_lastFrame < m_bestFrameDelay)
50  return false;
51  return true;
52 }
53 
55 {
56  ticks_t now = g_clock.micros();
57  m_frames++;
58  m_partialFrames++;
59  m_frameDelaySum += now - m_lastFrame;
60  m_lastFrame = now ;
61 }
62 
64 {
65  ticks_t now = g_clock.micros();
66  ticks_t delta = now - m_lastPartialFpsUpdate;
67  if(delta > 41000 && m_partialFrames > 0) {
68  m_partialFps = m_partialFrames / (delta / 1000000.0f);
69  m_lastPartialFpsUpdate = now;
70  m_partialFrames = 0;
71  }
72 
73  delta = now - m_lastFpsUpdate;
74  if(delta >= 1000000) {
75  m_lastFps = m_frames;
76  if(m_frames > 0)
77  m_mediumFrameDelay = m_frameDelaySum / m_frames;
78  else
79  m_mediumFrameDelay = 0;
80  m_lastFpsUpdate = now;
81  m_frames = 0;
82  m_frameDelaySum = 0;
83 
84  //dump << stdext::format("FPS => %d Partial FPS => %d Frame Delay Hit => %.2f%%", m_lastFps, (int)m_partialFps, getFrameDelayHit());
85  return true;
86  }
87  return false;
88 }
89 
91 {
92  maxFps = stdext::clamp<int>(maxFps, 0, 1000);
93 
94  if(maxFps != 0) {
95  m_bestFrameDelay = 1000000 / maxFps;
96  } else {
97  m_maxPartialFps = 0;
98  m_bestFrameDelay = 0;
99  }
100  m_maxFps = maxFps;
101 }
102 
104 {
105  if(m_maxFps == 0)
106  return 0;
107  return m_lastFrame + m_bestFrameDelay - g_clock.micros();
108 }
109 
111 {
112  if(m_bestFrameDelay > 0)
113  return ((m_bestFrameDelay - std::abs(m_bestFrameDelay - m_mediumFrameDelay)) * 100.0f) / (float)m_bestFrameDelay;
114  else
115  return 100.0f;
116 }
AdaptativeFrameCounter::getFrameDelayHit
float getFrameDelayHit()
Definition: adaptativeframecounter.cpp:110
AdaptativeFrameCounter::processNextFrame
void processNextFrame()
Definition: adaptativeframecounter.cpp:54
AdaptativeFrameCounter::update
bool update()
Definition: adaptativeframecounter.cpp:63
AdaptativeFrameCounter::AdaptativeFrameCounter
AdaptativeFrameCounter()
Definition: adaptativeframecounter.cpp:26
ticks_t
int64 ticks_t
Definition: types.h:43
clock.h
adaptativeframecounter.h
AdaptativeFrameCounter::setMaxFps
void setMaxFps(int maxFps)
Definition: adaptativeframecounter.cpp:90
AdaptativeFrameCounter::shouldProcessNextFrame
bool shouldProcessNextFrame()
Definition: adaptativeframecounter.cpp:43
Clock::micros
ticks_t micros()
Definition: clock.h:36
AdaptativeFrameCounter::getMaximumSleepMicros
int getMaximumSleepMicros()
Definition: adaptativeframecounter.cpp:103
g_clock
Clock g_clock
Definition: clock.cpp:25