From 6de3d16fd3e7d98c98cdf73c6a3317e1a6b10184 Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Sat, 26 May 2018 23:15:02 +0200 Subject: Add KeyWatcher to handle pausing via space --- CMakeLists.txt | 1 + src/glfw/key_watcher.cc | 24 +++++++++++++++++++ src/glfw/key_watcher.h | 17 +++++++++++++ src/glfw/window.cc | 4 ++++ src/glfw/window.h | 8 +++++-- src/main.cc | 64 ++++++++++++++++++++++++++++--------------------- 6 files changed, 89 insertions(+), 29 deletions(-) create mode 100644 src/glfw/key_watcher.cc create mode 100644 src/glfw/key_watcher.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 9268050..4a11355 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,6 +16,7 @@ add_executable( src/timer.cc src/glfw/guard.cc src/glfw/window.cc + src/glfw/key_watcher.cc src/shader/util.cc src/shader/wrap/compute_shader.cc src/shader/wrap/graphic_shader.cc diff --git a/src/glfw/key_watcher.cc b/src/glfw/key_watcher.cc new file mode 100644 index 0000000..21a570a --- /dev/null +++ b/src/glfw/key_watcher.cc @@ -0,0 +1,24 @@ +#include "key_watcher.h" + +KeyWatcher::KeyWatcher(GLFWwindow* handle, int key): + _handle(handle), + _key(key), + _last_state(glfwGetKey(_handle, _key)) +{ } + +bool KeyWatcher::wasClicked() { + switch ( glfwGetKey(_handle, _key) ) { + case GLFW_RELEASE: + _last_state = GLFW_RELEASE; + return false; + case GLFW_PRESS: + if ( _last_state == GLFW_RELEASE ) { + _last_state = GLFW_PRESS; + return true; + } else { + return false; + } + default: + return false; + } +} diff --git a/src/glfw/key_watcher.h b/src/glfw/key_watcher.h new file mode 100644 index 0000000..538829f --- /dev/null +++ b/src/glfw/key_watcher.h @@ -0,0 +1,17 @@ +#pragma once + +#include + +class KeyWatcher { +private: + GLFWwindow* const _handle; + + int _key; + int _last_state; + +public: + KeyWatcher(GLFWwindow* handle, int key); + + bool wasClicked(); + +}; diff --git a/src/glfw/window.cc b/src/glfw/window.cc index 17fc36c..6fbbfee 100644 --- a/src/glfw/window.cc +++ b/src/glfw/window.cc @@ -22,3 +22,7 @@ int Window::getWidth() const { int Window::getHeight() const { return _height; } + +KeyWatcher Window::getKeyWatcher(int key) { + return KeyWatcher(_handle, key); +} diff --git a/src/glfw/window.h b/src/glfw/window.h index b858e9a..520b5e8 100644 --- a/src/glfw/window.h +++ b/src/glfw/window.h @@ -5,6 +5,8 @@ #include #include +#include "key_watcher.h" + class Window { private: bool _good = false; @@ -21,6 +23,8 @@ public: int getWidth() const; int getHeight() const; + KeyWatcher getKeyWatcher(int key); + template void init(F f); @@ -39,8 +43,8 @@ template void Window::render(F loop) { glfwMakeContextCurrent(_handle); - while ( glfwGetKey(_handle, GLFW_KEY_ESCAPE ) != GLFW_PRESS && - glfwWindowShouldClose(_handle) == 0 ) { + while ( glfwGetKey(_handle, GLFW_KEY_ESCAPE) != GLFW_PRESS && + glfwWindowShouldClose(_handle) == 0 ) { glfwGetWindowSize(_handle, &_width, &_height); loop(); diff --git a/src/main.cc b/src/main.cc index cd8668e..5236f4c 100644 --- a/src/main.cc +++ b/src/main.cc @@ -26,7 +26,7 @@ #include "timer.h" -const unsigned int particle_count = 2500; +const unsigned int particle_count = 5000; const unsigned int max_ups = 100; const unsigned int texture_count = 20; @@ -94,7 +94,7 @@ int main() { int window_width = window.getWidth(); int window_height = window.getHeight(); - float world_width = 20.0; + float world_width = 10.0; float world_height = getWorldHeight(window_width, window_height, world_width); glm::mat4 MVP = getMVP(world_width, world_height); @@ -122,7 +122,7 @@ int main() { VERTEX_SHADER_CODE, FRAGMENT_SHADER_CODE); compute_shader = std::make_unique( getShaderFunction("cos(v.x*sin(v.y))", - "sin(v.x-v.y)")); + "sin(v.x-v.y)")); compute_shader->workOn(particle_buffer->getBuffer()); display_shader = std::make_unique( DISPLAY_VERTEX_SHADER_CODE, DISPLAY_FRAGMENT_SHADER_CODE); @@ -136,16 +136,24 @@ int main() { return -1; } - auto lastFrame = timer::now(); - auto lastRotate = timer::now(); - bool justRotated = true; + auto last_frame = timer::now(); + auto last_rotate = timer::now(); + + bool just_rotated = true; + bool update_particles = true; std::vector textures; for ( const auto& texture_buffer : texture_framebuffers ) { textures.emplace_back(texture_buffer->getTexture()); } + auto pause_key = window.getKeyWatcher(GLFW_KEY_SPACE); + window.render([&]() { + if ( pause_key.wasClicked() ) { + update_particles = !update_particles; + } + if ( window.getWidth() != window_width || window.getHeight() != window_height ) { window_width = window.getWidth(); @@ -159,35 +167,37 @@ int main() { } } - if ( timer::millisecondsSince(lastFrame) >= 1000/max_ups ) { - auto guard = compute_shader->use(); + if ( update_particles ) { + if ( timer::millisecondsSince(last_frame) >= 1000/max_ups ) { + auto guard = compute_shader->use(); - compute_shader->setUniform("world", world_width, world_height); - compute_shader->dispatch(particle_count); + compute_shader->setUniform("world", world_width, world_height); + compute_shader->dispatch(particle_count); - lastFrame = timer::now(); - } + last_frame = timer::now(); + } - if ( timer::millisecondsSince(lastRotate) >= 1000/10 ) { - std::rotate(textures.begin(), textures.end()-1, textures.end()); - std::rotate(texture_framebuffers.begin(), texture_framebuffers.end()-1, texture_framebuffers.end()); - justRotated = true; + if ( timer::millisecondsSince(last_rotate) >= 1000/10 ) { + std::rotate(textures.begin(), textures.end()-1, textures.end()); + std::rotate(texture_framebuffers.begin(), texture_framebuffers.end()-1, texture_framebuffers.end()); + just_rotated = true; - lastRotate = timer::now(); - } + last_rotate = timer::now(); + } - { - auto texGuard = texture_framebuffers[0]->use(); - auto sdrGuard = scene_shader->use(); + { + auto texGuard = texture_framebuffers[0]->use(); + auto sdrGuard = scene_shader->use(); - scene_shader->setUniform("MVP", MVP); + scene_shader->setUniform("MVP", MVP); - if ( justRotated ) { - glClear(GL_COLOR_BUFFER_BIT); - justRotated = false; - } + if ( just_rotated ) { + glClear(GL_COLOR_BUFFER_BIT); + just_rotated = false; + } - particle_buffer->draw(); + particle_buffer->draw(); + } } { -- cgit v1.2.3