Leviathan  0.8.0.0
Leviathan game engine
RenderingStatistics.cpp
Go to the documentation of this file.
1 // ------------------------------------ //
2 #include "RenderingStatistics.h"
3 
4 #include "../Common/DataStoring/DataStore.h"
5 #include "../TimeIncludes.h"
6 using namespace Leviathan;
7 // ------------------------------------ //
8 Leviathan::RenderingStatistics::RenderingStatistics() : LastMinuteFPS(80), LastMinuteRenderTimes(320){
9 
10  Frames = 0;
11 
12  FPS = 0;
13  RenderMCRSeconds = 0;
14 
15  MinFPS = 9999;
16  MaxFPS = 0;
17  AverageFps = 0;
18 
19  MaxFrameTime = 0;
20  MinFrameTime = 9999;
21  AverageRenderTime = 0;
22 
23  HalfMinuteStartTime = 0;
24  SecondStartTime = 0;
25  RenderingStartTime = 0;
26  RenderingEndTime = 0;
27 
28  DoubtfulCancel = 0;
29 
30  LastMinuteFPSPos = 0;
31  LastMinuteRenderTimesPos = 0;
32 
33 
34 
35  IsFirstFrame = true;
36  EraseOld = true;
37 }
38 
40 
41 }
42 // ------------------------------------ //
44  RenderingStartTime = Time::GetTimeMicro64();
45 
46  Frames++;
47 }
48 
50  RenderingEndTime = Time::GetTimeMicro64();
51 
52  RenderMCRSeconds = (int)(RenderingEndTime-RenderingStartTime);
53 
54 
55  MakeSureHasEnoughRoom(LastMinuteRenderTimes, LastMinuteRenderTimesPos);
56 
57  LastMinuteRenderTimes[LastMinuteRenderTimesPos] = RenderMCRSeconds;
58  ++LastMinuteRenderTimesPos;
59 
60  // half minute check //
61  if(RenderingEndTime > HalfMinuteStartTime+(1000000*30)){
62 
63  HalfMinuteStartTime = RenderingEndTime;
64  HalfMinuteMark();
65  }
66 
67  if(IsFirstFrame)
68  IsFirstFrame = false;
69 
70 }
71 // ------------------------------------ //
73  dstore->SetFPS(FPS);
74  dstore->SetFPSAverage(AverageFps);
75  dstore->SetFPSMax(MaxFPS);
76  dstore->SetFPSMin(MinFPS);
77 
78  dstore->SetFrameTime(RenderMCRSeconds);
79  dstore->SetFrameTimeAverage(AverageRenderTime);
80  dstore->SetFrameTimeMax(MaxFrameTime);
81  dstore->SetFrameTimeMin(MinFrameTime);
82 }
83 
84 void Leviathan::RenderingStatistics::HalfMinuteMark(){
85  EraseOld = true;
86 
87  // Calculate the averages //
88  int fpses = 0;
89 
90  for(size_t i = 0; i < LastMinuteFPSPos+1; i++){
91  fpses += LastMinuteFPS[i];
92  }
93 
94  if(LastMinuteFPSPos == 0){
95 
96  AverageFps = 0;
97 
98  } else {
99 
100  AverageFps = static_cast<int>(fpses/LastMinuteFPSPos+1);
101  }
102 
103  // Frame time averages //
104  int frametimes = 0;
105 
106 
107  for(size_t i = 0; i < LastMinuteRenderTimesPos+1; i++){
108 
109  frametimes += LastMinuteRenderTimes[i];
110  }
111 
112  if(LastMinuteRenderTimesPos == 0){
113 
114  AverageRenderTime = 0;
115  } else {
116 
117  AverageRenderTime = static_cast<int>(frametimes/LastMinuteRenderTimesPos+1);
118  }
119 
120  // Reset the insert positions //
121  LastMinuteRenderTimesPos = 0;
122  LastMinuteFPSPos = 0;
123 }
124 
125 void Leviathan::RenderingStatistics::SecondMark(){
126  FPS = Frames;
127  Frames = 0;
128 
129  // checks on fps //
130  if((FPS > MaxFPS) || (EraseOld)){
131  MaxFPS = FPS;
132  }
133  if((FPS < MinFPS) || (EraseOld)){
134  MinFPS = FPS;
135  }
136  // frame time checks //
137  if((RenderMCRSeconds > MaxFrameTime) || (EraseOld)){
138  MaxFrameTime = RenderMCRSeconds;
139  }
140  if((RenderMCRSeconds < MinFrameTime) || (EraseOld)){
141  MinFrameTime = RenderMCRSeconds;
142  }
143 
144  MakeSureHasEnoughRoom(LastMinuteFPS, LastMinuteFPSPos);
145 
146  LastMinuteFPS[LastMinuteFPSPos] = FPS;
147  ++LastMinuteFPSPos;
148 
149  EraseOld = false;
150 }
151 // ------------------------------------ //
152 bool Leviathan::RenderingStatistics::CanRenderNow(int maxfps, int& TimeSinceLastFrame){
153 
154 
155  // calculate can a frame be rendered now without going over max fps //
156  auto CurrentTime = Time::GetTimeMicro64();
157 
158  // check first frame //
159  if(IsFirstFrame){
160  // set values to current time //
161  HalfMinuteStartTime = CurrentTime;
162  SecondStartTime = CurrentTime;
163  RenderingStartTime = CurrentTime;
164  RenderingEndTime = CurrentTime;
165  // on first frame always can render //
166  return true;
167  }
168 
169  int TimePassed = (int)(CurrentTime-RenderingStartTime);
170  int TimeFromLastSecond = (int)(CurrentTime-SecondStartTime);
171 
172  if(TimeFromLastSecond < -1){
173  // second passed //
174  SecondStartTime = CurrentTime;
175  //DEBUG_OUTPUT(L"second pass\n");
176  TimeFromLastSecond = 0;
177 
178  SecondMark();
179  return true;
180  }
181 
182 
183  // Check has a second passed //
184  bool Set = false;
185  while(TimeFromLastSecond > 1000000){
186  TimeFromLastSecond -= 1000000;
187  Set = true;
188 
189  }
190 
191  if(Set){
192 
193  // second passed //
194  SecondStartTime = CurrentTime;
195  SecondMark();
196  }
197 
198  // send back time before last frame in microseconds //
199  TimeSinceLastFrame = TimePassed;
200 
201 
202  // calculate how many frames should have been rendered //
203  // divide current passed by 1 second and multiply frame rate to see how many should be at this time //
204  float Percentage = (TimeFromLastSecond/1000000.f);
205  int FramesShouldBe = (int)((Percentage*maxfps)+0.5f);
206 
207  // check do we have enough frames, if we don't we can render 1 //
208  // or if a second has passed render then //
209  if(Frames <= FramesShouldBe){
210 
211 
212  return true;
213  }
214 
215 
216  return false;
217 }
218 // ------------------------------------ //
219 void RenderingStatistics::MakeSureHasEnoughRoom(std::vector<int> &tarvec,
220  const size_t &accessspot)
221 {
222 
223  if(tarvec.size() <= accessspot+1){
224  if(tarvec.size() > 5000){
225 
226  tarvec.resize((size_t)(tarvec.size()*1.35f));
227  Logger::Get()->Warning("RenderingStatistics: large frame time tracking buffer is "
228  "getting larger, size: "+Convert::ToString(tarvec.size()));
229 
230  } else {
231 
232  tarvec.resize((size_t)(tarvec.size()*1.8f));
233  }
234  }
235 }
236 
237 
238 
239 
240 
241 
242 
243 
DLLEXPORT void SetFPS(int newval)
Definition: DataStore.cpp:387
DLLEXPORT void SetFPSAverage(int newval)
Definition: DataStore.cpp:537
DLLEXPORT void SetFPSMax(int newval)
Definition: DataStore.cpp:531
DLLEXPORT void SetFrameTimeAverage(int newval)
Definition: DataStore.cpp:519
DLLEXPORT void SetFPSMin(int newval)
Definition: DataStore.cpp:525
DLLEXPORT void SetFrameTimeMax(int newval)
Definition: DataStore.cpp:513
DLLEXPORT void Warning(const std::string &data) override
Definition: Logger.cpp:190
DLLEXPORT void SetFrameTimeMin(int newval)
Definition: DataStore.cpp:507
static DLLEXPORT int64_t GetTimeMicro64()
static std::string ToString(const T &val)
Definition: Convert.h:72
static DLLEXPORT Logger * Get()
Definition: Logger.cpp:106
DLLEXPORT void ReportStats(DataStore *dstore)
DLLEXPORT void SetFrameTime(int newval)
Definition: DataStore.cpp:393
DLLEXPORT bool CanRenderNow(int maxfps, int &TimeSinceLastFrame)
The access mask controls which registered functions and classes a script sees.
Definition: GameModule.h:12