diff options
author | Adrian Kummerlaender | 2018-05-26 23:15:02 +0200 |
---|---|---|
committer | Adrian Kummerlaender | 2018-05-26 23:15:02 +0200 |
commit | 6de3d16fd3e7d98c98cdf73c6a3317e1a6b10184 (patch) | |
tree | 5292545e66c32735f2d157ac4406d211094c12e0 /src/glfw | |
parent | 6ce077f58779376481ab498ca4e1df60c1e2eee3 (diff) | |
download | computicle-6de3d16fd3e7d98c98cdf73c6a3317e1a6b10184.tar computicle-6de3d16fd3e7d98c98cdf73c6a3317e1a6b10184.tar.gz computicle-6de3d16fd3e7d98c98cdf73c6a3317e1a6b10184.tar.bz2 computicle-6de3d16fd3e7d98c98cdf73c6a3317e1a6b10184.tar.lz computicle-6de3d16fd3e7d98c98cdf73c6a3317e1a6b10184.tar.xz computicle-6de3d16fd3e7d98c98cdf73c6a3317e1a6b10184.tar.zst computicle-6de3d16fd3e7d98c98cdf73c6a3317e1a6b10184.zip |
Add KeyWatcher to handle pausing via space
Diffstat (limited to 'src/glfw')
-rw-r--r-- | src/glfw/key_watcher.cc | 24 | ||||
-rw-r--r-- | src/glfw/key_watcher.h | 17 | ||||
-rw-r--r-- | src/glfw/window.cc | 4 | ||||
-rw-r--r-- | src/glfw/window.h | 8 |
4 files changed, 51 insertions, 2 deletions
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 <GLFW/glfw3.h> + +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 <GL/glew.h> #include <GLFW/glfw3.h> +#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 <class F> void init(F f); @@ -39,8 +43,8 @@ template <class F> 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(); |