From f465b26a8aeac95cf7e0186a86ab1262dc771fb4 Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Mon, 17 Dec 2018 19:31:38 +0100 Subject: Keep track of window size in its wrapper class --- src/glfw/window.cc | 10 ++++++++++ src/glfw/window.h | 10 ++++++++-- src/main.cc | 21 +++++++-------------- 3 files changed, 25 insertions(+), 16 deletions(-) diff --git a/src/glfw/window.cc b/src/glfw/window.cc index 0e57a65..9e76b57 100644 --- a/src/glfw/window.cc +++ b/src/glfw/window.cc @@ -1,5 +1,15 @@ #include "window.h" +bool Window::updateSize() { + const int old_width = _width; + const int old_height = _height; + + glfwGetWindowSize(_handle, &_width, &_height); + + return old_width != _width + || old_height != _height; +} + Window::Window(const std::string& title): _handle(glfwCreateWindow(_width, _height, title.c_str(), NULL, NULL)) { if ( _handle != nullptr ) { diff --git a/src/glfw/window.h b/src/glfw/window.h index 92a1b56..ea6075b 100644 --- a/src/glfw/window.h +++ b/src/glfw/window.h @@ -15,6 +15,8 @@ private: GLFWwindow* const _handle; + bool updateSize(); + public: Window(const std::string& title); ~Window(); @@ -46,9 +48,13 @@ void Window::render(F loop) { while ( glfwGetKey(_handle, GLFW_KEY_ESCAPE) != GLFW_PRESS && glfwWindowShouldClose(_handle) == 0 ) { - glfwGetWindowSize(_handle, &_width, &_height); + const bool window_size_changed = updateSize(); + + if ( window_size_changed ) { + glViewport(0, 0, getWidth(), getHeight()); + } - loop(); + loop(window_size_changed); glfwSwapBuffers(_handle); glfwPollEvents(); diff --git a/src/main.cc b/src/main.cc index 20d7666..a3a1701 100644 --- a/src/main.cc +++ b/src/main.cc @@ -26,6 +26,8 @@ constexpr GLuint nX = 128; constexpr GLuint nY = 128; +constexpr int lups = 25; // max lattice updates per second + float getWorldHeight(int window_width, int window_height, float world_width) { return world_width / window_width * window_height; } @@ -54,11 +56,8 @@ int renderWindow() { return -1; } - int window_width = window.getWidth(); - int window_height = window.getHeight(); - float world_width = 2*nX; - float world_height = getWorldHeight(window_width, window_height, world_width); + float world_height = getWorldHeight(window.getWidth(), window.getHeight(), world_width); glm::mat4 MVP = getMVP(world_width, world_height); @@ -98,24 +97,18 @@ int renderWindow() { auto tick_buffers = { lattice_a->getBuffer(), lattice_b->getBuffer(), fluid->getBuffer() }; auto tock_buffers = { lattice_b->getBuffer(), lattice_a->getBuffer(), fluid->getBuffer() }; - window.render([&]() { + window.render([&](bool window_size_changed) { if ( pause_key.wasClicked() ) { update_lattice = !update_lattice; } - if ( window.getWidth() != window_width - || window.getHeight() != window_height ) { - window_width = window.getWidth(); - window_height = window.getHeight(); - - glViewport(0, 0, window_width, window_height); - - world_height = getWorldHeight(window_width, window_height, world_width); + if ( window_size_changed ) { + world_height = getWorldHeight(window.getWidth(), window.getHeight(), world_width); MVP = getMVP(world_width, world_height); } if ( update_lattice ) { - if ( timer::millisecondsSince(last_frame) >= 1000/25 ) { + if ( timer::millisecondsSince(last_frame) >= 1000/lups ) { if ( tick ) { collide_shader->workOn(tick_buffers); stream_shader->workOn(tick_buffers); -- cgit v1.2.3