diff options
Diffstat (limited to 'src/glfw')
-rw-r--r-- | src/glfw/guard.cc | 16 | ||||
-rw-r--r-- | src/glfw/guard.h | 11 | ||||
-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 | 32 | ||||
-rw-r--r-- | src/glfw/window.h | 58 |
6 files changed, 158 insertions, 0 deletions
diff --git a/src/glfw/guard.cc b/src/glfw/guard.cc new file mode 100644 index 0000000..5ba853f --- /dev/null +++ b/src/glfw/guard.cc @@ -0,0 +1,16 @@ +#include "guard.h" + +#include <GL/glew.h> +#include <GLFW/glfw3.h> + +GlfwGuard::GlfwGuard() { + _good = glfwInit(); +} + +GlfwGuard::~GlfwGuard() { + glfwTerminate(); +} + +bool GlfwGuard::isGood() const { + return _good; +} diff --git a/src/glfw/guard.h b/src/glfw/guard.h new file mode 100644 index 0000000..f68954d --- /dev/null +++ b/src/glfw/guard.h @@ -0,0 +1,11 @@ +#pragma once + +class GlfwGuard { +private: + bool _good = false; +public: + GlfwGuard(); + ~GlfwGuard(); + + bool isGood() const; +}; 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 new file mode 100644 index 0000000..0e57a65 --- /dev/null +++ b/src/glfw/window.cc @@ -0,0 +1,32 @@ +#include "window.h" + +Window::Window(const std::string& title): + _handle(glfwCreateWindow(_width, _height, title.c_str(), NULL, NULL)) { + if ( _handle != nullptr ) { + glfwMakeContextCurrent(_handle); + if ( glewInit() == GLEW_OK ) { + _good = true; + } + glfwMakeContextCurrent(nullptr); + } +} + +Window::~Window() { + glfwDestroyWindow(_handle); +} + +bool Window::isGood() const { + return _good; +} + +int Window::getWidth() const { + return _width; +} + +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 new file mode 100644 index 0000000..92a1b56 --- /dev/null +++ b/src/glfw/window.h @@ -0,0 +1,58 @@ +#pragma once + +#include <string> + +#include <GL/glew.h> +#include <GLFW/glfw3.h> + +#include "key_watcher.h" + +class Window { +private: + bool _good = false; + int _width = 800; + int _height = 600; + + GLFWwindow* const _handle; + +public: + Window(const std::string& title); + ~Window(); + + bool isGood() const; + + int getWidth() const; + int getHeight() const; + + KeyWatcher getKeyWatcher(int key); + + template <class F> + void init(F f); + + template <class F> + void render(F loop); +}; + +template <class F> +void Window::init(F f) { + glfwMakeContextCurrent(_handle); + f(); + glfwMakeContextCurrent(nullptr); +} + +template <class F> +void Window::render(F loop) { + glfwMakeContextCurrent(_handle); + + while ( glfwGetKey(_handle, GLFW_KEY_ESCAPE) != GLFW_PRESS && + glfwWindowShouldClose(_handle) == 0 ) { + glfwGetWindowSize(_handle, &_width, &_height); + + loop(); + + glfwSwapBuffers(_handle); + glfwPollEvents(); + } + + glfwMakeContextCurrent(nullptr); +} |