Skip to content

Commit

Permalink
FPS timing with chrono (#7867)
Browse files Browse the repository at this point in the history
#changelog #core #framerate
  • Loading branch information
dimitre committed May 11, 2024
1 parent f7522b9 commit 202aa24
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 5 deletions.
10 changes: 7 additions & 3 deletions libs/openFrameworks/events/ofEvents.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,11 @@ void ofCoreEvents::setFrameRate(int _targetRate) {
} else {
bFrameRateSet = true;
targetRate = _targetRate;
uint64_t nanosPerFrame = 1000000000.0 / (double)targetRate;
timer.setPeriodicEvent(nanosPerFrame);

// uint64_t nanosPerFrame = 1000000000.0 / (double)targetRate;
// timer.setPeriodicEvent(nanosPerFrame);

timerFps.setFps(targetRate);
}
}

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

if (bFrameRateSet) {
timer.waitNext();
// timer.waitNext();
timerFps.waitNext();
}

if (fps.getNumFrames() == 0) {
Expand Down
6 changes: 4 additions & 2 deletions libs/openFrameworks/events/ofEvents.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

#include "ofEventUtils.h"
#include "ofFpsCounter.h"
#include "ofTimer.h"
//#include "ofTimer.h"
#include "ofTimerFps.h"

#define GLM_FORCE_CTOR_INIT
#define GLM_ENABLE_EXPERIMENTAL
Expand Down Expand Up @@ -406,7 +407,8 @@ class ofCoreEvents {
private:
float targetRate;
bool bFrameRateSet;
ofTimer timer;
ofTimerFps timerFps;
// ofTimer timer;
ofFpsCounter fps;

int currentMouseX, currentMouseY;
Expand Down
2 changes: 2 additions & 0 deletions libs/openFrameworks/utils/ofTimer.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include "ofUtils.h"



class ofTimer {
public:

Expand Down
26 changes: 26 additions & 0 deletions libs/openFrameworks/utils/ofTimerFps.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include "ofTimerFps.h"

void ofTimerFps::reset() {
wakeTime = steady_clock::now();
}

ofTimerFps::ofTimerFps(){
reset();
};

void ofTimerFps::setFps(int fps) {
interval = duration_cast<microseconds>(1s) / fps;
}

void ofTimerFps::waitNext() {
// Lazy wakeup
std::this_thread::sleep_until(wakeTime - 36ms); //4ms

// Processor Coffee
while(steady_clock::now() < (wakeTime)) { // 0.05ms 0.5us // - 0.5us - 1ns
std::this_thread::yield();
}

lastWakeTime = wakeTime;
wakeTime += interval;
}
25 changes: 25 additions & 0 deletions libs/openFrameworks/utils/ofTimerFps.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Written by Dimitre Lima in 2024

#pragma once

#include <chrono>
#include <ctime>
#include <iostream>
#include <thread>

using namespace std::chrono;
using namespace std::chrono_literals;

class ofTimerFps {
public:
ofTimerFps();
void setFps(int fps);
void reset();
void waitNext();

using space = std::chrono::duration<long long, std::nano>;
space interval;
time_point<steady_clock> wakeTime;
time_point<steady_clock> lastWakeTime;

};
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
objects = {

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

/* Begin PBXFileReference section */
0353245F2BEFEF5D00B50A35 /* ofTimerFps.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ofTimerFps.h; sourceTree = "<group>"; };
035324602BEFEF5D00B50A35 /* ofTimerFps.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ofTimerFps.cpp; sourceTree = "<group>"; };
15594F0515C55AC900727FF2 /* EAGLView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = EAGLView.m; sourceTree = "<group>"; };
15594F0615C55AC900727FF2 /* ES1Renderer.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ES1Renderer.m; sourceTree = "<group>"; };
15594F0715C55AC900727FF2 /* ES2Renderer.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ES2Renderer.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -935,6 +939,8 @@
67833F8019F8990D00DBE7AA /* ofThreadChannel.h */,
67833F8119F8990D00DBE7AA /* ofTimer.cpp */,
67833F8219F8990D00DBE7AA /* ofTimer.h */,
035324602BEFEF5D00B50A35 /* ofTimerFps.cpp */,
0353245F2BEFEF5D00B50A35 /* ofTimerFps.h */,
E4F76DFC176CB27200798745 /* ofURLFileLoader.cpp */,
E4F76DFD176CB27200798745 /* ofURLFileLoader.h */,
E4F76DFE176CB27200798745 /* ofUtils.cpp */,
Expand Down Expand Up @@ -1057,6 +1063,7 @@
15594F9415C56A8A00727FF2 /* ofxiOSEAGLView.h in Headers */,
15594F9515C56A8A00727FF2 /* ofxiOSAppDelegate.h in Headers */,
15594F9615C56A8A00727FF2 /* ofxiOSViewController.h in Headers */,
035324612BEFEF5D00B50A35 /* ofTimerFps.h in Headers */,
6678E97719FEB2DF00C00581 /* ofSoundBuffer.h in Headers */,
2E6E258328F73C2C00EC8E22 /* ofShadow.h in Headers */,
15594FA515C56BB700727FF2 /* AVFoundationVideoGrabber.h in Headers */,
Expand Down Expand Up @@ -1216,6 +1223,7 @@
15594FC115C56D1E00727FF2 /* ofxiOSImagePicker.mm in Sources */,
15594FC215C56D1E00727FF2 /* ofxiOSKeyboard.mm in Sources */,
15594FC315C56D1E00727FF2 /* ofxiOSMapKit.mm in Sources */,
035324622BEFEF5D00B50A35 /* ofTimerFps.cpp in Sources */,
15594FC415C56D1E00727FF2 /* ofxiOSMapKitDelegate.mm in Sources */,
69433CC31FE45BAC004D5B73 /* ofBaseApp.cpp in Sources */,
67833F8A19F8996300DBE7AA /* ofBufferObject.cpp in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
objects = {

/* Begin PBXBuildFile section */
0353245D2BEFEC7B00B50A35 /* ofTimerFps.h in Headers */ = {isa = PBXBuildFile; fileRef = 0353245B2BEFEC7A00B50A35 /* ofTimerFps.h */; };
0353245E2BEFEC7B00B50A35 /* ofTimerFps.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 0353245C2BEFEC7B00B50A35 /* ofTimerFps.cpp */; };
19662F2E2834A44400B622ED /* ofGraphicsCairo.h in Headers */ = {isa = PBXBuildFile; fileRef = 19662F2C2834A44400B622ED /* ofGraphicsCairo.h */; };
19662F2F2834A44400B622ED /* ofGraphicsCairo.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 19662F2D2834A44400B622ED /* ofGraphicsCairo.cpp */; };
22246D93176C9987008A8AF4 /* ofGLProgrammableRenderer.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 22246D91176C9987008A8AF4 /* ofGLProgrammableRenderer.cpp */; };
Expand Down Expand Up @@ -162,6 +164,8 @@
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
0353245B2BEFEC7A00B50A35 /* ofTimerFps.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ofTimerFps.h; sourceTree = "<group>"; };
0353245C2BEFEC7B00B50A35 /* ofTimerFps.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ofTimerFps.cpp; sourceTree = "<group>"; };
19662F2C2834A44400B622ED /* ofGraphicsCairo.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ofGraphicsCairo.h; sourceTree = "<group>"; };
19662F2D2834A44400B622ED /* ofGraphicsCairo.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ofGraphicsCairo.cpp; sourceTree = "<group>"; };
22246D91176C9987008A8AF4 /* ofGLProgrammableRenderer.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = ofGLProgrammableRenderer.cpp; path = gl/ofGLProgrammableRenderer.cpp; sourceTree = "<group>"; };
Expand Down Expand Up @@ -571,6 +575,8 @@
692C298819DC5C5500C27C5D /* ofFpsCounter.h */,
692C298919DC5C5500C27C5D /* ofTimer.cpp */,
692C298A19DC5C5500C27C5D /* ofTimer.h */,
0353245C2BEFEC7B00B50A35 /* ofTimerFps.cpp */,
0353245B2BEFEC7A00B50A35 /* ofTimerFps.h */,
27DEA30F1796F578000A9E90 /* ofXml.cpp */,
27DEA3101796F578000A9E90 /* ofXml.h */,
2276958F170D9DD200604FC3 /* ofMatrixStack.cpp */,
Expand Down Expand Up @@ -641,6 +647,7 @@
30CC5385207A36FD008234AF /* ofMathConstants.h in Headers */,
E4F3BA6A12F4C4BF002D19BB /* ofCamera.h in Headers */,
E4F3BA6C12F4C4BF002D19BB /* ofEasyCam.h in Headers */,
0353245D2BEFEC7B00B50A35 /* ofTimerFps.h in Headers */,
E4F3BA7412F4C4BF002D19BB /* ofNode.h in Headers */,
E4F3BA8B12F4C4C9002D19BB /* ofFmodSoundPlayer.h in Headers */,
E4F3BA8F12F4C4C9002D19BB /* ofSoundPlayer.h in Headers */,
Expand Down Expand Up @@ -812,6 +819,7 @@
E4F3BB1812F4C752002D19BB /* ofBitmapFont.cpp in Sources */,
E4F3BB1C12F4C752002D19BB /* ofGraphics.cpp in Sources */,
2E6EA7041603AA7A00B7ADF3 /* of3dGraphics.cpp in Sources */,
0353245E2BEFEC7B00B50A35 /* ofTimerFps.cpp in Sources */,
E4F3BB1E12F4C752002D19BB /* ofImage.cpp in Sources */,
E4F3BB2012F4C752002D19BB /* ofPixels.cpp in Sources */,
E4F3BB2A12F4C752002D19BB /* ofTessellator.cpp in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@
<ClInclude Include="..\..\..\openFrameworks\utils\ofThread.h" />
<ClInclude Include="..\..\..\openFrameworks\utils\ofThreadChannel.h" />
<ClInclude Include="..\..\..\openFrameworks\utils\ofTimer.h" />
<ClInclude Include="..\..\..\openFrameworks\utils\ofTimerFps.h" />
<ClInclude Include="..\..\..\openFrameworks\utils\ofURLFileLoader.h" />
<ClInclude Include="..\..\..\openFrameworks\utils\ofUtils.h" />
<ClInclude Include="..\..\..\openFrameworks\utils\ofXml.h" />
Expand Down Expand Up @@ -288,6 +289,7 @@
<ClCompile Include="..\..\..\openFrameworks\utils\ofSystemUtils.cpp" />
<ClCompile Include="..\..\..\openFrameworks\utils\ofThread.cpp" />
<ClCompile Include="..\..\..\openFrameworks\utils\ofTimer.cpp" />
<ClCompile Include="..\..\..\openFrameworks\utils\ofTimerFps.cpp" />
<ClCompile Include="..\..\..\openFrameworks\utils\ofURLFileLoader.cpp" />
<ClCompile Include="..\..\..\openFrameworks\utils\ofUtils.cpp" />
<ClCompile Include="..\..\..\openFrameworks\utils\ofXml.cpp" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,9 @@
<ClInclude Include="..\..\..\openFrameworks\utils\ofTimer.h">
<Filter>libs\openFrameworks\utils</Filter>
</ClInclude>
<ClInclude Include="..\..\..\openFrameworks\utils\ofTimerFps.h">
<Filter>libs\openFrameworks\utils</Filter>
</ClInclude>
<ClInclude Include="..\..\..\openFrameworks\video\ofDirectShowPlayer.h">
<Filter>libs\openFrameworks\video</Filter>
</ClInclude>
Expand Down Expand Up @@ -499,6 +502,9 @@
<ClCompile Include="..\..\..\openFrameworks\utils\ofTimer.cpp">
<Filter>libs\openFrameworks\utils</Filter>
</ClCompile>
<ClCompile Include="..\..\..\openFrameworks\utils\ofTimerFps.cpp">
<Filter>libs\openFrameworks\utils</Filter>
</ClCompile>
<ClCompile Include="..\..\..\openFrameworks\gl\ofBufferObject.cpp">
<Filter>libs\openFrameworks\gl</Filter>
</ClCompile>
Expand Down

0 comments on commit 202aa24

Please sign in to comment.