Browse Source

Refactor Window methods for getting frame times.

tags/v2.0.0
Andrew Belt 4 years ago
parent
commit
c9bd5e9a3c
5 changed files with 32 additions and 27 deletions
  1. +4
    -5
      include/window.hpp
  2. +1
    -1
      src/app/MenuBar.cpp
  3. +1
    -1
      src/app/Scene.cpp
  4. +1
    -1
      src/widget/FramebufferWidget.cpp
  5. +25
    -19
      src/window.cpp

+ 4
- 5
include/window.hpp View File

@@ -66,9 +66,7 @@ struct Window {
This is not equal to gPixelRatio in general.
*/
float windowRatio = 1.f;
int frame = 0;
std::shared_ptr<Font> uiFont;
double frameTimeStart = 0.f;

/** Use load*() instead of modifying these directly. */
std::map<std::string, std::weak_ptr<Font>> fontCache;
@@ -90,9 +88,10 @@ struct Window {
int getMods();
void setFullScreen(bool fullScreen);
bool isFullScreen();
bool isFrameOverdue();
float getMonitorRefreshRate();
float getLastFrameRate();
double getMonitorRefreshRate();
double getLastFrameTime();
double getLastFrameDuration();
double getFrameTimeOverdue();

std::shared_ptr<Font> loadFont(const std::string& filename);
std::shared_ptr<Image> loadImage(const std::string& filename);


+ 1
- 1
src/app/MenuBar.cpp View File

@@ -398,7 +398,7 @@ struct FrameRateItem : ui::MenuItem {
ui::Menu* menu = new ui::Menu;

for (int i = 1; i <= 6; i++) {
float frameRate = APP->window->getMonitorRefreshRate() / i;
double frameRate = APP->window->getMonitorRefreshRate() / i;

FrameRateValueItem* item = new FrameRateValueItem;
item->frameSwapInterval = i;


+ 1
- 1
src/app/Scene.cpp View File

@@ -23,7 +23,7 @@ struct Scene::Internal {

struct FrameRateWidget : widget::TransparentWidget {
void draw(const DrawArgs& args) override {
std::string text = string::f("%.2f Hz", APP->window->getLastFrameRate());
std::string text = string::f("%.2lf Hz", 1.0 / APP->window->getLastFrameDuration());
bndLabel(args.vg, 0.0, 0.0, INFINITY, INFINITY, -1, text.c_str());
}
};


+ 1
- 1
src/widget/FramebufferWidget.cpp View File

@@ -54,7 +54,7 @@ void FramebufferWidget::step() {
Widget::step();

// It's more important to not lag the frame than to draw the framebuffer
if (APP->window->isFrameOverdue())
if (APP->window->getFrameTimeOverdue() > 0.0)
return;

// Check that scale has been set by `draw()` yet.


+ 25
- 19
src/window.cpp View File

@@ -94,10 +94,12 @@ struct Window::Internal {
int lastWindowWidth = 0;
int lastWindowHeight = 0;

int frame = 0;
bool ignoreNextMouseDelta = false;
int frameSwapInterval = -1;
float monitorRefreshRate = 0.f;
float lastFrameRate = 0.f;
double monitorRefreshRate = 0.0;
double lastFrameDuration = 0.0;
double lastFrameTime = 0.0;

math::Vec lastMousePos;
};
@@ -337,12 +339,12 @@ Window::~Window() {
}

void Window::run() {
frame = 0;
internal->frame = 0;
while (!glfwWindowShouldClose(win)) {
double frameTime = glfwGetTime();
internal->lastFrameRate = 1.f / float(frameTime - frameTimeStart);
// DEBUG("%.2f Hz", internal->lastFrameRate);
frameTimeStart = frameTime;
internal->lastFrameDuration = frameTime - internal->lastFrameTime;
// DEBUG("%.2lf Hz", 1.0 / internal->lastFrameDuration);
internal->lastFrameTime = frameTime;

// Make event handlers and step() have a clean nanovg context
nvgReset(vg);
@@ -354,8 +356,8 @@ void Window::run() {
// In case glfwPollEvents() sets another OpenGL context
glfwMakeContextCurrent(win);
if (settings::frameSwapInterval != internal->frameSwapInterval) {
internal->frameSwapInterval = settings::frameSwapInterval;
glfwSwapInterval(settings::frameSwapInterval);
internal->frameSwapInterval = settings::frameSwapInterval;
}

// Call cursorPosCallback every frame, not just when the mouse moves
@@ -423,7 +425,7 @@ void Window::run() {

glfwSwapBuffers(win);

frame++;
internal->frame++;
}
}

@@ -449,8 +451,10 @@ void Window::screenshot(float zoom) {
app::ModuleWidget* mw = model->createModuleWidget(NULL);
fbw->addChild(mw);

// Reset the frame time so FramebufferWidgets are guaranteed to draw
internal->lastFrameTime = 0.0;

// Draw to framebuffer
frameTimeStart = glfwGetTime();
fbw->step();
nvgluBindFramebuffer(fbw->getFramebuffer());

@@ -539,20 +543,22 @@ bool Window::isFullScreen() {
return monitor != NULL;
}

bool Window::isFrameOverdue() {
if (settings::frameSwapInterval == 0)
return false;
double frameDuration = glfwGetTime() - frameTimeStart;
double frameDeadline = settings::frameSwapInterval / internal->monitorRefreshRate;
return frameDuration > frameDeadline;
double Window::getMonitorRefreshRate() {
return internal->monitorRefreshRate;
}

float Window::getMonitorRefreshRate() {
return internal->monitorRefreshRate;
double Window::getLastFrameTime() {
return internal->lastFrameTime;
}

double Window::getLastFrameDuration() {
return internal->lastFrameDuration;
}

float Window::getLastFrameRate() {
return internal->lastFrameRate;
double Window::getFrameTimeOverdue() {
double desiredFrameDuration = internal->frameSwapInterval / internal->monitorRefreshRate;
double frameDuration = glfwGetTime() - internal->lastFrameTime;
return frameDuration - desiredFrameDuration;
}

std::shared_ptr<Font> Window::loadFont(const std::string& filename) {


Loading…
Cancel
Save