Skip to content

Commit 202aa24

Browse files
authored
FPS timing with chrono (#7867)
#changelog #core #framerate
1 parent f7522b9 commit 202aa24

9 files changed

Lines changed: 88 additions & 5 deletions

File tree

libs/openFrameworks/events/ofEvents.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,11 @@ void ofCoreEvents::setFrameRate(int _targetRate) {
211211
} else {
212212
bFrameRateSet = true;
213213
targetRate = _targetRate;
214-
uint64_t nanosPerFrame = 1000000000.0 / (double)targetRate;
215-
timer.setPeriodicEvent(nanosPerFrame);
214+
215+
// uint64_t nanosPerFrame = 1000000000.0 / (double)targetRate;
216+
// timer.setPeriodicEvent(nanosPerFrame);
217+
218+
timerFps.setFps(targetRate);
216219
}
217220
}
218221

@@ -301,7 +304,8 @@ bool ofCoreEvents::notifyDraw() {
301304
auto attended = ofNotifyEvent(draw, voidEventArgs);
302305

303306
if (bFrameRateSet) {
304-
timer.waitNext();
307+
// timer.waitNext();
308+
timerFps.waitNext();
305309
}
306310

307311
if (fps.getNumFrames() == 0) {

libs/openFrameworks/events/ofEvents.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
#include "ofEventUtils.h"
44
#include "ofFpsCounter.h"
5-
#include "ofTimer.h"
5+
//#include "ofTimer.h"
6+
#include "ofTimerFps.h"
67

78
#define GLM_FORCE_CTOR_INIT
89
#define GLM_ENABLE_EXPERIMENTAL
@@ -406,7 +407,8 @@ class ofCoreEvents {
406407
private:
407408
float targetRate;
408409
bool bFrameRateSet;
409-
ofTimer timer;
410+
ofTimerFps timerFps;
411+
// ofTimer timer;
410412
ofFpsCounter fps;
411413

412414
int currentMouseX, currentMouseY;

libs/openFrameworks/utils/ofTimer.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
#include "ofUtils.h"
44

5+
6+
57
class ofTimer {
68
public:
79

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include "ofTimerFps.h"
2+
3+
void ofTimerFps::reset() {
4+
wakeTime = steady_clock::now();
5+
}
6+
7+
ofTimerFps::ofTimerFps(){
8+
reset();
9+
};
10+
11+
void ofTimerFps::setFps(int fps) {
12+
interval = duration_cast<microseconds>(1s) / fps;
13+
}
14+
15+
void ofTimerFps::waitNext() {
16+
// Lazy wakeup
17+
std::this_thread::sleep_until(wakeTime - 36ms); //4ms
18+
19+
// Processor Coffee
20+
while(steady_clock::now() < (wakeTime)) { // 0.05ms 0.5us // - 0.5us - 1ns
21+
std::this_thread::yield();
22+
}
23+
24+
lastWakeTime = wakeTime;
25+
wakeTime += interval;
26+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Written by Dimitre Lima in 2024
2+
3+
#pragma once
4+
5+
#include <chrono>
6+
#include <ctime>
7+
#include <iostream>
8+
#include <thread>
9+
10+
using namespace std::chrono;
11+
using namespace std::chrono_literals;
12+
13+
class ofTimerFps {
14+
public:
15+
ofTimerFps();
16+
void setFps(int fps);
17+
void reset();
18+
void waitNext();
19+
20+
using space = std::chrono::duration<long long, std::nano>;
21+
space interval;
22+
time_point<steady_clock> wakeTime;
23+
time_point<steady_clock> lastWakeTime;
24+
25+
};

libs/openFrameworksCompiled/project/ios/iOS+OFLib.xcodeproj/project.pbxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
035324612BEFEF5D00B50A35 /* ofTimerFps.h in Headers */ = {isa = PBXBuildFile; fileRef = 0353245F2BEFEF5D00B50A35 /* ofTimerFps.h */; };
11+
035324622BEFEF5D00B50A35 /* ofTimerFps.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 035324602BEFEF5D00B50A35 /* ofTimerFps.cpp */; };
1012
15594F0C15C55AC900727FF2 /* EAGLView.m in Sources */ = {isa = PBXBuildFile; fileRef = 15594F0515C55AC900727FF2 /* EAGLView.m */; };
1113
15594F0D15C55AC900727FF2 /* ES1Renderer.m in Sources */ = {isa = PBXBuildFile; fileRef = 15594F0615C55AC900727FF2 /* ES1Renderer.m */; };
1214
15594F0E15C55AC900727FF2 /* ES2Renderer.m in Sources */ = {isa = PBXBuildFile; fileRef = 15594F0715C55AC900727FF2 /* ES2Renderer.m */; };
@@ -236,6 +238,8 @@
236238
/* End PBXBuildFile section */
237239

238240
/* Begin PBXFileReference section */
241+
0353245F2BEFEF5D00B50A35 /* ofTimerFps.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ofTimerFps.h; sourceTree = "<group>"; };
242+
035324602BEFEF5D00B50A35 /* ofTimerFps.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ofTimerFps.cpp; sourceTree = "<group>"; };
239243
15594F0515C55AC900727FF2 /* EAGLView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = EAGLView.m; sourceTree = "<group>"; };
240244
15594F0615C55AC900727FF2 /* ES1Renderer.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ES1Renderer.m; sourceTree = "<group>"; };
241245
15594F0715C55AC900727FF2 /* ES2Renderer.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ES2Renderer.m; sourceTree = "<group>"; };
@@ -935,6 +939,8 @@
935939
67833F8019F8990D00DBE7AA /* ofThreadChannel.h */,
936940
67833F8119F8990D00DBE7AA /* ofTimer.cpp */,
937941
67833F8219F8990D00DBE7AA /* ofTimer.h */,
942+
035324602BEFEF5D00B50A35 /* ofTimerFps.cpp */,
943+
0353245F2BEFEF5D00B50A35 /* ofTimerFps.h */,
938944
E4F76DFC176CB27200798745 /* ofURLFileLoader.cpp */,
939945
E4F76DFD176CB27200798745 /* ofURLFileLoader.h */,
940946
E4F76DFE176CB27200798745 /* ofUtils.cpp */,
@@ -1057,6 +1063,7 @@
10571063
15594F9415C56A8A00727FF2 /* ofxiOSEAGLView.h in Headers */,
10581064
15594F9515C56A8A00727FF2 /* ofxiOSAppDelegate.h in Headers */,
10591065
15594F9615C56A8A00727FF2 /* ofxiOSViewController.h in Headers */,
1066+
035324612BEFEF5D00B50A35 /* ofTimerFps.h in Headers */,
10601067
6678E97719FEB2DF00C00581 /* ofSoundBuffer.h in Headers */,
10611068
2E6E258328F73C2C00EC8E22 /* ofShadow.h in Headers */,
10621069
15594FA515C56BB700727FF2 /* AVFoundationVideoGrabber.h in Headers */,
@@ -1216,6 +1223,7 @@
12161223
15594FC115C56D1E00727FF2 /* ofxiOSImagePicker.mm in Sources */,
12171224
15594FC215C56D1E00727FF2 /* ofxiOSKeyboard.mm in Sources */,
12181225
15594FC315C56D1E00727FF2 /* ofxiOSMapKit.mm in Sources */,
1226+
035324622BEFEF5D00B50A35 /* ofTimerFps.cpp in Sources */,
12191227
15594FC415C56D1E00727FF2 /* ofxiOSMapKitDelegate.mm in Sources */,
12201228
69433CC31FE45BAC004D5B73 /* ofBaseApp.cpp in Sources */,
12211229
67833F8A19F8996300DBE7AA /* ofBufferObject.cpp in Sources */,

libs/openFrameworksCompiled/project/osx/openFrameworksLib.xcodeproj/project.pbxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
0353245D2BEFEC7B00B50A35 /* ofTimerFps.h in Headers */ = {isa = PBXBuildFile; fileRef = 0353245B2BEFEC7A00B50A35 /* ofTimerFps.h */; };
11+
0353245E2BEFEC7B00B50A35 /* ofTimerFps.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 0353245C2BEFEC7B00B50A35 /* ofTimerFps.cpp */; };
1012
19662F2E2834A44400B622ED /* ofGraphicsCairo.h in Headers */ = {isa = PBXBuildFile; fileRef = 19662F2C2834A44400B622ED /* ofGraphicsCairo.h */; };
1113
19662F2F2834A44400B622ED /* ofGraphicsCairo.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 19662F2D2834A44400B622ED /* ofGraphicsCairo.cpp */; };
1214
22246D93176C9987008A8AF4 /* ofGLProgrammableRenderer.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 22246D91176C9987008A8AF4 /* ofGLProgrammableRenderer.cpp */; };
@@ -162,6 +164,8 @@
162164
/* End PBXBuildFile section */
163165

164166
/* Begin PBXFileReference section */
167+
0353245B2BEFEC7A00B50A35 /* ofTimerFps.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ofTimerFps.h; sourceTree = "<group>"; };
168+
0353245C2BEFEC7B00B50A35 /* ofTimerFps.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ofTimerFps.cpp; sourceTree = "<group>"; };
165169
19662F2C2834A44400B622ED /* ofGraphicsCairo.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ofGraphicsCairo.h; sourceTree = "<group>"; };
166170
19662F2D2834A44400B622ED /* ofGraphicsCairo.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ofGraphicsCairo.cpp; sourceTree = "<group>"; };
167171
22246D91176C9987008A8AF4 /* ofGLProgrammableRenderer.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = ofGLProgrammableRenderer.cpp; path = gl/ofGLProgrammableRenderer.cpp; sourceTree = "<group>"; };
@@ -571,6 +575,8 @@
571575
692C298819DC5C5500C27C5D /* ofFpsCounter.h */,
572576
692C298919DC5C5500C27C5D /* ofTimer.cpp */,
573577
692C298A19DC5C5500C27C5D /* ofTimer.h */,
578+
0353245C2BEFEC7B00B50A35 /* ofTimerFps.cpp */,
579+
0353245B2BEFEC7A00B50A35 /* ofTimerFps.h */,
574580
27DEA30F1796F578000A9E90 /* ofXml.cpp */,
575581
27DEA3101796F578000A9E90 /* ofXml.h */,
576582
2276958F170D9DD200604FC3 /* ofMatrixStack.cpp */,
@@ -641,6 +647,7 @@
641647
30CC5385207A36FD008234AF /* ofMathConstants.h in Headers */,
642648
E4F3BA6A12F4C4BF002D19BB /* ofCamera.h in Headers */,
643649
E4F3BA6C12F4C4BF002D19BB /* ofEasyCam.h in Headers */,
650+
0353245D2BEFEC7B00B50A35 /* ofTimerFps.h in Headers */,
644651
E4F3BA7412F4C4BF002D19BB /* ofNode.h in Headers */,
645652
E4F3BA8B12F4C4C9002D19BB /* ofFmodSoundPlayer.h in Headers */,
646653
E4F3BA8F12F4C4C9002D19BB /* ofSoundPlayer.h in Headers */,
@@ -812,6 +819,7 @@
812819
E4F3BB1812F4C752002D19BB /* ofBitmapFont.cpp in Sources */,
813820
E4F3BB1C12F4C752002D19BB /* ofGraphics.cpp in Sources */,
814821
2E6EA7041603AA7A00B7ADF3 /* of3dGraphics.cpp in Sources */,
822+
0353245E2BEFEC7B00B50A35 /* ofTimerFps.cpp in Sources */,
815823
E4F3BB1E12F4C752002D19BB /* ofImage.cpp in Sources */,
816824
E4F3BB2012F4C752002D19BB /* ofPixels.cpp in Sources */,
817825
E4F3BB2A12F4C752002D19BB /* ofTessellator.cpp in Sources */,

libs/openFrameworksCompiled/project/vs/openframeworksLib.vcxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@
210210
<ClInclude Include="..\..\..\openFrameworks\utils\ofThread.h" />
211211
<ClInclude Include="..\..\..\openFrameworks\utils\ofThreadChannel.h" />
212212
<ClInclude Include="..\..\..\openFrameworks\utils\ofTimer.h" />
213+
<ClInclude Include="..\..\..\openFrameworks\utils\ofTimerFps.h" />
213214
<ClInclude Include="..\..\..\openFrameworks\utils\ofURLFileLoader.h" />
214215
<ClInclude Include="..\..\..\openFrameworks\utils\ofUtils.h" />
215216
<ClInclude Include="..\..\..\openFrameworks\utils\ofXml.h" />
@@ -288,6 +289,7 @@
288289
<ClCompile Include="..\..\..\openFrameworks\utils\ofSystemUtils.cpp" />
289290
<ClCompile Include="..\..\..\openFrameworks\utils\ofThread.cpp" />
290291
<ClCompile Include="..\..\..\openFrameworks\utils\ofTimer.cpp" />
292+
<ClCompile Include="..\..\..\openFrameworks\utils\ofTimerFps.cpp" />
291293
<ClCompile Include="..\..\..\openFrameworks\utils\ofURLFileLoader.cpp" />
292294
<ClCompile Include="..\..\..\openFrameworks\utils\ofUtils.cpp" />
293295
<ClCompile Include="..\..\..\openFrameworks\utils\ofXml.cpp" />

libs/openFrameworksCompiled/project/vs/openframeworksLib.vcxproj.filters

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,9 @@
267267
<ClInclude Include="..\..\..\openFrameworks\utils\ofTimer.h">
268268
<Filter>libs\openFrameworks\utils</Filter>
269269
</ClInclude>
270+
<ClInclude Include="..\..\..\openFrameworks\utils\ofTimerFps.h">
271+
<Filter>libs\openFrameworks\utils</Filter>
272+
</ClInclude>
270273
<ClInclude Include="..\..\..\openFrameworks\video\ofDirectShowPlayer.h">
271274
<Filter>libs\openFrameworks\video</Filter>
272275
</ClInclude>
@@ -499,6 +502,9 @@
499502
<ClCompile Include="..\..\..\openFrameworks\utils\ofTimer.cpp">
500503
<Filter>libs\openFrameworks\utils</Filter>
501504
</ClCompile>
505+
<ClCompile Include="..\..\..\openFrameworks\utils\ofTimerFps.cpp">
506+
<Filter>libs\openFrameworks\utils</Filter>
507+
</ClCompile>
502508
<ClCompile Include="..\..\..\openFrameworks\gl\ofBufferObject.cpp">
503509
<Filter>libs\openFrameworks\gl</Filter>
504510
</ClCompile>

0 commit comments

Comments
 (0)