Skip to content
2 changes: 1 addition & 1 deletion .github/workflows/of.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
steps:
- uses: actions/checkout@v4
- name: Docker Step
run: "docker run -di --name emscripten -v $PWD:/src emscripten/emsdk:3.1.73 bash"
run: "docker run -di --name emscripten -v $PWD:/src emscripten/emsdk:3.1.74 bash"
# - name: Determine Release
# id: vars
# shell: bash
Expand Down
33 changes: 20 additions & 13 deletions libs/openFrameworks/events/ofEvents.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ double ofGetLastFrameTime() {
if (window) {
return window->events().getLastFrameTime();
} else {
return 0.f;
return 0.0;
}
}

Expand Down Expand Up @@ -123,9 +123,10 @@ int ofGetPreviousMouseY() {
}

ofCoreEvents::ofCoreEvents()
: targetRate(0)
: targetRate(60.0)
, fixedRateTimeNanos(std::chrono::nanoseconds(ofGetFixedStepForFps(60.0)))
, bFrameRateSet(false)
, fps(60)
, fps(60.0)
, currentMouseX(0)
, currentMouseY(0)
, previousMouseX(0)
Expand Down Expand Up @@ -183,6 +184,7 @@ void ofCoreEvents::enable() {

void ofCoreEvents::setTimeModeSystem() {
timeMode = System;
fps.setTimeMode(timeMode);
}

ofTimeMode ofCoreEvents::getTimeMode() const {
Expand All @@ -192,11 +194,13 @@ ofTimeMode ofCoreEvents::getTimeMode() const {
void ofCoreEvents::setTimeModeFixedRate(uint64_t nanosecsPerFrame) {
timeMode = FixedRate;
fixedRateTimeNanos = std::chrono::nanoseconds(nanosecsPerFrame);
fps.setTimeMode(timeMode);
}

void ofCoreEvents::setTimeModeFiltered(float alpha) {
timeMode = Filtered;
fps.setFilterAlpha(alpha);
fps.setTimeMode(timeMode);
}

//--------------------------------------
Expand All @@ -210,13 +214,18 @@ void ofCoreEvents::setFrameRate(int _targetRate) {
bFrameRateSet = false;
} else {
bFrameRateSet = true;
targetRate = _targetRate;
targetRate = static_cast<float>(_targetRate);

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

timerFps.setFps(targetRate);
timerFps.setFps(_targetRate);
fps.setTargetFPS(targetRate);
if (timeMode == FixedRate) {
ofSetTimeModeFixedRate(ofGetFixedStepForFps(targetRate));
}
}

}

bool ofCoreEvents::getTargetFrameRateEnabled() const {
Expand Down Expand Up @@ -301,15 +310,8 @@ bool ofCoreEvents::notifyUpdate() {

//------------------------------------------
bool ofCoreEvents::notifyDraw() {
auto attended = ofNotifyEvent(draw, voidEventArgs);

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

if (fps.getNumFrames() == 0) {
if (bFrameRateSet) fps = ofFpsCounter(targetRate);
if (bFrameRateSet) fps = ofFpsCounter(targetRate, timeMode);
} else {
/*if(ofIsVerticalSyncEnabled()){
float rate = ofGetRefreshRate();
Expand All @@ -318,7 +320,12 @@ bool ofCoreEvents::notifyDraw() {
lastFrameTime = intervals*1000000/rate;
}*/
}
if (bFrameRateSet) {
// timer.waitNext();
timerFps.waitNext();
}
fps.newFrame();
auto attended = ofNotifyEvent(draw, voidEventArgs);
return attended;
}

Expand Down
4 changes: 2 additions & 2 deletions libs/openFrameworks/events/ofEvents.h
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ class ofCoreEvents {
bool notifyDragEvent(ofDragInfo info);

private:
float targetRate;
float targetRate = 60.0f;
bool bFrameRateSet;
ofTimerFps timerFps;
// ofTimer timer;
Expand All @@ -420,7 +420,7 @@ class ofCoreEvents {
int modifiers = 0;

enum TimeMode {
System,
System = 0,
FixedRate,
Filtered,
} timeMode
Expand Down
8 changes: 6 additions & 2 deletions libs/openFrameworks/utils/ofConstants.h
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,12 @@ enum ofTargetPlatform{
#else // desktop linux
#define TARGET_GLFW_WINDOW
#define OF_RTAUDIO
#define __LINUX_PULSE__
#define __LINUX_ALSA__
#ifndef __LINUX_PULSE__
#define __LINUX_PULSE__
#endif
#ifndef __LINUX_ALSA__
#define __LINUX_ALSA__
#endif
#define __LINUX_OSS__
#include <GL/glew.h>
#endif
Expand Down
63 changes: 47 additions & 16 deletions libs/openFrameworks/utils/ofFpsCounter.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,37 @@
#include "ofFpsCounter.h"
#include <ofUtils.h>
using namespace std::chrono;

ofFpsCounter::ofFpsCounter() {}
ofFpsCounter::ofFpsCounter()
: lastFrameTime(std::chrono::duration<long long, std::nano>(0))
, diff(std::chrono::duration<long long, std::nano>(0))
, then(std::chrono::steady_clock::now())
, timeMode(0) {
timestamps.clear();
timestamps.resize(targetFPS + 7);
}

ofFpsCounter::ofFpsCounter(double targetFPS) : fps(targetFPS) {}
ofFpsCounter::ofFpsCounter(double targetFPS, int mode)
: targetFPS(targetFPS)
, lastFrameTime(std::chrono::duration<long long, std::nano>(0))
, diff(std::chrono::duration<long long, std::nano>(0))
, then(std::chrono::steady_clock::now())
, timeMode(mode) {
timestamps.clear();
timestamps.resize(targetFPS + 7);
}

void ofFpsCounter::newFrame(){
now = steady_clock::now();
update(now);
timestamps.push(now);
timestamps.push_back(now);
lastFrameTime = now - then;

update(now);
// std::lerp from c++20 on
filteredTime = filteredTime * filterAlpha + getLastFrameSecs() * (1-filterAlpha);
if (timeMode == 2) { // Filtered
filterAlpha = std::clamp(filterAlpha, 0.0, 1.0);
filteredTime = filteredTime * filterAlpha + getLastFrameSecs() * (1.0 - filterAlpha);
filteredTime = std::clamp(filteredTime, 0.0, 1.0 / targetFPS);
}
then = now;
nFrameCount++;
}
Expand All @@ -24,18 +43,19 @@ void ofFpsCounter::update(){

void ofFpsCounter::update(time_point<steady_clock> now){
while(!timestamps.empty() && timestamps.front() + 2s < now){
timestamps.pop();
timestamps.pop_front();
}

space diff;
if(!timestamps.empty() && timestamps.front() + 0.5s < now){
diff = now - timestamps.front();
if (timestamps.size() < 2) {
fps = targetFPS; // if no sample size then set fps to target until can sample
return;
}
if(diff > 0.0s){
fps = (double)timestamps.size() / std::chrono::duration<double>(diff).count();
}else{
diff = now - timestamps.front();
if (diff > std::chrono::duration<double>(0)) {
fps = static_cast<double>(timestamps.size()) / std::chrono::duration<double>(diff).count();
} else {
fps = timestamps.size();
}

}

double ofFpsCounter::getFps() const{
Expand All @@ -51,17 +71,28 @@ uint64_t ofFpsCounter::getLastFrameNanos() const{
}

double ofFpsCounter::getLastFrameSecs() const{
return duration_cast<seconds>(lastFrameTime).count();
return std::chrono::duration<double>(lastFrameTime).count();
}

uint64_t ofFpsCounter::getLastFrameFilteredNanos() const{
return duration_cast<nanoseconds>(lastFrameTime).count();
}

double ofFpsCounter::getLastFrameFilteredSecs() const{
return filteredTime;
return std::chrono::duration<double>(filteredTime).count();
}

void ofFpsCounter::setFilterAlpha(float alpha){
filterAlpha = alpha;
}

void ofFpsCounter::setTimeMode(int mode) {
timeMode = mode;
}

void ofFpsCounter::setTargetFPS(double fps) {
targetFPS = fps;
if (fps > timestamps.max_size()) {
timestamps.resize(fps);
}
}
15 changes: 10 additions & 5 deletions libs/openFrameworks/utils/ofFpsCounter.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#pragma once

#include <queue>
#include <deque>
#include <chrono>

class ofFpsCounter {
public:
ofFpsCounter();
ofFpsCounter(double targetFps);
ofFpsCounter(double targetFps, int mode = 0);
void newFrame();

// no need to call it usually, useful if
Expand All @@ -22,17 +22,22 @@ class ofFpsCounter {
uint64_t getLastFrameFilteredNanos() const;
double getLastFrameFilteredSecs() const;
void setFilterAlpha(float alpha);
void setTimeMode(int mode);
void setTargetFPS(double fps);

private:
void update(std::chrono::time_point<std::chrono::steady_clock> now);
uint64_t nFrameCount = 0;
double fps = 0;
double fps = 0.0;
double targetFPS = 60.0;

using space = std::chrono::duration<long long, std::nano>;
std::chrono::time_point<std::chrono::steady_clock> now = std::chrono::steady_clock::now();
std::chrono::time_point<std::chrono::steady_clock> then = std::chrono::steady_clock::now();
space lastFrameTime;
double filteredTime = 0;
space diff;
double filteredTime = 0.0;
double filterAlpha = 0.9;
std::queue<std::chrono::time_point<std::chrono::steady_clock>> timestamps;
std::deque<std::chrono::time_point<std::chrono::steady_clock>> timestamps;
int timeMode = 0;
};
10 changes: 8 additions & 2 deletions libs/openFrameworks/utils/ofTimerFps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,19 @@ void ofTimerFps::reset() {
wakeTime = steady_clock::now();
}

ofTimerFps::ofTimerFps(){
ofTimerFps::ofTimerFps() {
reset();
interval = duration_cast<microseconds>(1s) / currentFPS;
};

void ofTimerFps::setFps(int fps) {
// interval = std::ratio<1s, fps>;
interval = duration_cast<microseconds>(1s) / fps;
if (fps <= 0) {
fps = 60; // fallback
}
currentFPS = fps;

interval = duration_cast<microseconds>(1s) / currentFPS;
}

void ofTimerFps::waitNext() {
Expand Down
1 change: 1 addition & 0 deletions libs/openFrameworks/utils/ofTimerFps.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ class ofTimerFps {
space interval;
std::chrono::time_point<std::chrono::steady_clock> wakeTime;
std::chrono::time_point<std::chrono::steady_clock> lastWakeTime;
int currentFPS = 60;

};
30 changes: 3 additions & 27 deletions scripts/ci/emscripten/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,10 @@ cd $ROOT
#echo "PLATFORM_CFLAGS += $CUSTOMFLAGS" >> libs/openFrameworksCompiled/project/emscripten/config.emscripten.default.mk
sed -i "s/PLATFORM_OPTIMIZATION_CFLAGS_DEBUG = .*/PLATFORM_OPTIMIZATION_CFLAGS_DEBUG = -g0/" libs/openFrameworksCompiled/project/emscripten/config.emscripten.default.mk
cd libs/openFrameworksCompiled/project
emmake make -j Debug
EMCC_DEBUG=1 emmake make -j Debug

echo "**** Building emptyExample ****"
cd $ROOT/scripts/templates/linux64
emmake make -j Debug
EMCC_DEBUG=1 emmake make -j Debug

echo "**** Building allAddonsExample ****"
cd $ROOT
cp scripts/templates/linux64/Makefile examples/templates/allAddonsExample/
cp scripts/templates/linux64/config.make examples/templates/allAddonsExample/
cd examples/templates/allAddonsExample/
sed -i s/ofxOsc// addons.make
sed -i s/ofxNetwork// addons.make
sed -i s/ofxKinect// addons.make
sed -i s/ofxThreadedImageLoader// addons.make
sed -i s/ofxPoco// addons.make

sed -i "s/#include \"ofxOsc.h\"//" src/ofApp.h
sed -i "s/#include \"ofxNetwork.h\"//" src/ofApp.h
sed -i "s/#include \"ofxKinect.h\"//" src/ofApp.h
sed -i "s/#include \"ofxThreadedImageLoader.h\"//" src/ofApp.h
sed -i "s/#include \"ofxXmlPoco.h\"//" src/ofApp.h

sed -i "s/ofxTCPClient client;//" src/ofApp.h
sed -i "s/ofxTCPServer server;//" src/ofApp.h
sed -i "s/ofxOscSender osc_sender;//" src/ofApp.h
sed -i "s/ofxKinect kinect;//" src/ofApp.h
sed -i "s/ofxThreadedImageLoader .*;//" src/ofApp.h
sed -i "s/ofxXmlPoco .*;//" src/ofApp.h

emmake make -j Debug
echo "**** Building default template complete ****"
37 changes: 37 additions & 0 deletions scripts/ci/emscripten/build_addons.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/bin/bash
set -ev
ROOT=${TRAVIS_BUILD_DIR:-"$( cd "$(dirname "$0")/../../.." ; pwd -P )"}

echo "**** Building OF core ****"
cd $ROOT
# this carries over to subsequent compilations of examples
#echo "PLATFORM_CFLAGS += $CUSTOMFLAGS" >> libs/openFrameworksCompiled/project/emscripten/config.emscripten.default.mk
sed -i "s/PLATFORM_OPTIMIZATION_CFLAGS_DEBUG = .*/PLATFORM_OPTIMIZATION_CFLAGS_DEBUG = -g0/" libs/openFrameworksCompiled/project/emscripten/config.emscripten.default.mk
cd libs/openFrameworksCompiled/project
EMCC_DEBUG=1 emmake make -j Debug

echo "**** Building allAddonsExample ****"
cd $ROOT
cp scripts/templates/linux64/Makefile examples/templates/allAddonsExample/
cp scripts/templates/linux64/config.make examples/templates/allAddonsExample/
cd examples/templates/allAddonsExample/
sed -i s/ofxOsc// addons.make
sed -i s/ofxNetwork// addons.make
sed -i s/ofxKinect// addons.make
sed -i s/ofxThreadedImageLoader// addons.make
sed -i s/ofxPoco// addons.make

sed -i "s/#include \"ofxOsc.h\"//" src/ofApp.h
sed -i "s/#include \"ofxNetwork.h\"//" src/ofApp.h
sed -i "s/#include \"ofxKinect.h\"//" src/ofApp.h
sed -i "s/#include \"ofxThreadedImageLoader.h\"//" src/ofApp.h
sed -i "s/#include \"ofxXmlPoco.h\"//" src/ofApp.h

sed -i "s/ofxTCPClient client;//" src/ofApp.h
sed -i "s/ofxTCPServer server;//" src/ofApp.h
sed -i "s/ofxOscSender osc_sender;//" src/ofApp.h
sed -i "s/ofxKinect kinect;//" src/ofApp.h
sed -i "s/ofxThreadedImageLoader .*;//" src/ofApp.h
sed -i "s/ofxXmlPoco .*;//" src/ofApp.h

EMCC_DEBUG=1 emmake make -j Debug
Loading
Loading