Browse Source

Mac fixes, sleep GUI thread if framerate is too high

tags/v0.5.0
Andrew Belt 7 years ago
parent
commit
e76122bb60
3 changed files with 31 additions and 22 deletions
  1. +3
    -1
      compile.mk
  2. +6
    -6
      src/engine.cpp
  3. +22
    -15
      src/gui.cpp

+ 3
- 1
compile.mk View File

@@ -6,9 +6,11 @@ endif
FLAGS += -MMD
FLAGS += -g
# Optimization
FLAGS += -O3 -march=nocona -ffast-math
FLAGS += -O3 -march=nocona -ffast-math -fno-finite-math-only
FLAGS += -Wall -Wextra -Wno-unused-parameter
ifneq ($(ARCH), mac)
CXXFLAGS += -Wsuggest-override
endif
CXXFLAGS += -std=c++11




+ 6
- 6
src/engine.cpp View File

@@ -84,7 +84,7 @@ static void engineRun() {
// Every time the engine waits and locks a mutex, it steps this many frames
const int mutexSteps = 64;
// Time in seconds that the engine is rushing ahead of the estimated clock time
float ahead = 0.0;
double ahead = 0.0;
auto lastTime = std::chrono::high_resolution_clock::now();

while (running) {
@@ -97,19 +97,19 @@ static void engineRun() {
}
}

float stepTime = mutexSteps / sampleRate;
double stepTime = mutexSteps / sampleRate;
ahead += stepTime;
auto currTime = std::chrono::high_resolution_clock::now();
const float aheadFactor = 2.0;
ahead -= aheadFactor * std::chrono::duration<float>(currTime - lastTime).count();
const double aheadFactor = 2.0;
ahead -= aheadFactor * std::chrono::duration<double>(currTime - lastTime).count();
lastTime = currTime;
ahead = fmaxf(ahead, 0.0);

// Avoid pegging the CPU at 100% when there are no "blocking" modules like AudioInterface, but still step audio at a reasonable rate
// The number of steps to wait before possibly sleeping
const float aheadMax = 1.0; // seconds
const double aheadMax = 1.0; // seconds
if (ahead > aheadMax) {
std::this_thread::sleep_for(std::chrono::duration<float>(stepTime));
std::this_thread::sleep_for(std::chrono::duration<double>(stepTime));
}
}
}


+ 22
- 15
src/gui.cpp View File

@@ -1,9 +1,11 @@
#include <map>
#include <queue>

#include "gui.hpp"
#include "app.hpp"
#include "asset.hpp"

#include <map>
#include <queue>
#include <thread>

#include "../ext/osdialog/osdialog.h"

#define NANOVG_GL2_IMPLEMENTATION
@@ -223,12 +225,9 @@ void renderGui() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
nvgBeginFrame(gVg, width, height, gPixelRatio);

bool visible = glfwGetWindowAttrib(gWindow, GLFW_VISIBLE) && !glfwGetWindowAttrib(gWindow, GLFW_ICONIFIED);
if (visible) {
nvgReset(gVg);
nvgScale(gVg, gPixelRatio, gPixelRatio);
gScene->draw(gVg);
}
nvgReset(gVg);
nvgScale(gVg, gPixelRatio, gPixelRatio);
gScene->draw(gVg);

nvgEndFrame(gVg);
glfwSwapBuffers(gWindow);
@@ -311,8 +310,8 @@ void guiRun() {
windowSizeCallback(gWindow, width, height);
}
gGuiFrame = 0;
double lastTime = 0.0;
while(!glfwWindowShouldClose(gWindow)) {
double startTime = glfwGetTime();
gGuiFrame++;

// Poll events
@@ -336,12 +335,20 @@ void guiRun() {
gScene->step();

// Render
renderGui();
bool visible = glfwGetWindowAttrib(gWindow, GLFW_VISIBLE) && !glfwGetWindowAttrib(gWindow, GLFW_ICONIFIED);
if (visible) {
renderGui();
}

double currTime = glfwGetTime();
// printf("%lf fps\n", 1.0/(currTime - lastTime));
lastTime = currTime;
(void) lastTime;
// Limit framerate manually if vsync isn't working
double endTime = glfwGetTime();
double frameTime = endTime - startTime;
double minTime = 1.0 / 90.0;
if (frameTime < minTime) {
std::this_thread::sleep_for(std::chrono::duration<double>(minTime - frameTime));
}
endTime = glfwGetTime();
// printf("%lf fps\n", 1.0 / (endTime - startTime));
}
}



Loading…
Cancel
Save