Skip to content

Commit

Permalink
engine: Fix rendering under 1FPS
Browse files Browse the repository at this point in the history
Refactor a bit, add Period to optimize FPS division

It was also tested in Ubuntu-Core on Pi3 at 0.30 FPS
Using VC4 V3D 2.1 backend from GL 2.1 Mesa 20.2.6.

While Ubuntu classic on weston is way faster,
I'll may check again with Mir on next classic Release.

Relate-to: #21
Signed-off-by: Philippe Coval <[email protected]>
Change-Id: I544a7407d0cec5eb3def99c1353a7eb27193de9f
  • Loading branch information
rzr committed Mar 31, 2021
1 parent 7749d12 commit b110621
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 27 deletions.
37 changes: 25 additions & 12 deletions base/Engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#include <cstring>
#include <ctime>

#include <iostream>

#if EM_THREADS
#include <sched.h>
#endif
Expand Down Expand Up @@ -45,7 +47,7 @@
#endif
#endif

volatile int g_iStartTime = -1;
volatile unsigned int g_iStartTime = 0;
//volatile int g_iDesiredTime = -1;
volatile int g_iLastRender = 0;

Expand All @@ -71,7 +73,7 @@ extern "C" {
#endif

#if EM_USE_SDL
#define GET_TIME (signed)SDL_GetTicks()
#define GET_TIME ((unsigned int) SDL_GetTicks())
#endif

#if EM_USE_ALLEGRO
Expand All @@ -82,7 +84,10 @@ float Engine::m_fFps = 0.0f;

Engine * Engine::p_Engine = NULL;

Engine::Engine(int & argc, char *argv[]) {
Engine::Engine(int & argc, char *argv[])
: Group{},
m_iPeriod(1000/50)
{
Config * config = Config::getInstance();
config->loadArgs(argc, argv);

Expand Down Expand Up @@ -339,17 +344,25 @@ void Engine::delay(int ms) {
#endif
}

bool Engine::nextTickFPS(int fps) {
// default 100 FPS
int delay = 10;
if (fps > 0) {
delay = 1000/fps;
void Engine::setSpeed(int fps) {
m_iPeriod = 1000/fps;
}

bool Engine::nextTick() {
if (GET_TIME >= g_iStartTime + 1000) {
#ifdef EM_DEBUG
cerr << "warning: Too slow (less than 1FPS) :"
<< g_iStartTime << " " << GET_TIME << " +" << m_iPeriod << endl;
#endif
tick(); // Animate world once before rendering
g_iStartTime = GET_TIME; // Trying to catch up
return false;
}
if ((g_iStartTime + delay) <= GET_TIME) {
g_iStartTime += delay;
return true;
if (GET_TIME <= (g_iStartTime + m_iPeriod)) {
return false;
}
return false;
g_iStartTime += m_iPeriod;
return true;
}

void Engine::resetTick() {
Expand Down
13 changes: 8 additions & 5 deletions base/Engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,10 @@ class Engine : public Group {
void setLightning(float diffuse, float ambient);
void addLight(Light*);
/** Use this function to limit the main loop to a desired frame rate.
* @return false if the thread is behind schedule else true.
** Available FPS: 100, 50, 40, 33, 25, 20 */
bool nextTickFPS(int fps);
* @return false if the thread is behind schedule else true. see limitFPS */
bool nextTick();
/** Available FPS: 100, 50, 40, 33, 25, 20 */
void setSpeed(int fps=25*4);
void resetTick();
// bool limitFPS(int fps);
void delay(int ms);
Expand All @@ -68,9 +69,11 @@ class Engine : public Group {
void resumeTickThread();
void endTickThread();
#endif
private:
protected:
unsigned int m_iPeriod; ///< Allocated time for tick
private:
static float m_fFps;
static Engine * p_Engine;
static Engine * p_Engine;
};

#endif // ENGINE_H
3 changes: 2 additions & 1 deletion src/Pinball.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,7 @@ int Pinball::setup(int argc, char *argv[])
// cerr<<"log: pinball: Draw to the screen"<<endl;
miCount = 0;

mpEngine->setSpeed(25*8);
mpEngine->resetTick();
mpEngine->swap();

Expand Down Expand Up @@ -889,7 +890,7 @@ int Pinball::loop()
if (Keyboard::isKeyDown(SDLK_r)) {
SendSignal(PBL_SIG_RESET_ALL, 0, mpEngine, NULL);
}
if (mpEngine->nextTickFPS(200)) {
if (mpEngine->nextTick()) {
mpEngine->tick();
} else {
mpEngine->render();
Expand Down
2 changes: 1 addition & 1 deletion test/explode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ int main(int argc, char *argv[]) {

engine->resetTick();
while (!Keyboard::isKeyDown(SDLK_ESCAPE)) {
if (engine->nextTickFPS(50)) {
if (engine->nextTick()) {
engine->tick();
} else {
engine->render();
Expand Down
2 changes: 1 addition & 1 deletion test/load.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ int main(int argc, char *argv[]) {
engine->resetTick();
bool keydown = false;
while (!Keyboard::isKeyDown(SDLK_ESCAPE)) {
if (engine->nextTickFPS(50)) {
if (engine->nextTick()) {
engine->tick();
Group * g = engine->getGroup(1);
if (g != NULL) {
Expand Down
2 changes: 1 addition & 1 deletion test/menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ int main(int argc, char *argv[]) {
bool done = false;
engine->resetTick();
while (!Keyboard::isKeyDown(SDLK_SPACE) && !done) {
if (engine->nextTickFPS(50)) {
if (engine->nextTick()) {
engine->tick();
} else {
engine->render();
Expand Down
3 changes: 2 additions & 1 deletion test/misc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,9 @@ int main(int argc, char *argv[]) {
Vertex3D vtx = {0.0f, 0.0f, 0.0f};
Vertex3D vtxDist;
engine->resetTick();
engine->setSpeed(10);
while (!Keyboard::isKeyDown(SDLK_ESCAPE)) {
if (engine->nextTickFPS(10)) {
if (engine->nextTick()) {
engine->tick();
float sqrdist = CollisionVisitor::getInstance()->
vtxPolySqrDist(vtx, tri->getPolygon(0), vtxDist);
Expand Down
4 changes: 2 additions & 2 deletions test/scale.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ int main(int argc, char *argv[]) {
float x=0, y=0, z=0;
float dx=-1, dy=-1, dz=-1;
engine->resetTick();
while (!Keyboard::isKeyDown(SDLK_ESCAPE)) {
if (engine->nextTickFPS(50)) {
while (!Keyboard::isKeyDown(SDLK_ESCAPE)) {
if (engine->nextTick()) {
if (x > 2) dx=-0.012;
if (y > 2) dy=-0.021;
if (z > 2) dz=-0.027;
Expand Down
2 changes: 1 addition & 1 deletion test/signal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ int main(int argc, char *argv[]) {
// multicast a signal with delay
SendSignal(2, 50, engine, NULL);
}
if (engine->nextTickFPS(50)) {
if (engine->nextTick()) {
engine->tick();
} else {
engine->render();
Expand Down
2 changes: 1 addition & 1 deletion test/texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ int main(int argc, char *argv[]) {

engine->resetTick();
while (!Keyboard::isKeyDown(SDLK_ESCAPE)) {
if (engine->nextTickFPS(50)) {
if (engine->nextTick()) {
engine->tick();
} else {
engine->render();
Expand Down
2 changes: 1 addition & 1 deletion test/trans.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ int main(int argc, char *argv[]) {

engine->resetTick();
while (!Keyboard::isKeyDown(SDLK_ESCAPE)) {
if (engine->nextTickFPS(50)) {
if (engine->nextTick()) {
engine->tick();
} else {
engine->render();
Expand Down

0 comments on commit b110621

Please sign in to comment.