From 44f5ac32a68a617f93704d44c4339f7db13b323e Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Sat, 15 Dec 2018 23:09:32 +0100 Subject: Hacky D2Q9 BGK LBM on GPU using GLSL compute shaders Improvised on top of computicles's scaffolding. Works in a world where _works_ is defined as "displays stuff on screen that invokes thoughts of fluid movement". --- src/glfw/guard.cc | 16 ++++++++++++++ src/glfw/guard.h | 11 ++++++++++ src/glfw/key_watcher.cc | 24 ++++++++++++++++++++ src/glfw/key_watcher.h | 17 +++++++++++++++ src/glfw/window.cc | 32 +++++++++++++++++++++++++++ src/glfw/window.h | 58 +++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 158 insertions(+) create mode 100644 src/glfw/guard.cc create mode 100644 src/glfw/guard.h create mode 100644 src/glfw/key_watcher.cc create mode 100644 src/glfw/key_watcher.h create mode 100644 src/glfw/window.cc create mode 100644 src/glfw/window.h (limited to 'src/glfw') 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 +#include + +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 + +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 + +#include +#include + +#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 + void init(F f); + + template + void render(F loop); +}; + +template +void Window::init(F f) { + glfwMakeContextCurrent(_handle); + f(); + glfwMakeContextCurrent(nullptr); +} + +template +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); +} -- cgit v1.2.3