From 5157658ec0cc07d2c56c978ca010cbb78236439f Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Fri, 25 May 2018 23:17:33 +0200 Subject: Guard GLFW init and termination --- src/glfw_guard.h | 20 ++++++++++++++++++++ src/main.cc | 16 +++++++--------- src/window.h | 3 +++ 3 files changed, 30 insertions(+), 9 deletions(-) create mode 100644 src/glfw_guard.h diff --git a/src/glfw_guard.h b/src/glfw_guard.h new file mode 100644 index 0000000..37d3ffb --- /dev/null +++ b/src/glfw_guard.h @@ -0,0 +1,20 @@ +#pragma once + +#include +#include + +class GlfwGuard { +private: + bool _good = false; +public: + GlfwGuard() { + _good = glfwInit(); + } + ~GlfwGuard() { + glfwTerminate(); + } + + bool isGood() const { + return _good; + } +}; diff --git a/src/main.cc b/src/main.cc index 7446609..b2262e6 100644 --- a/src/main.cc +++ b/src/main.cc @@ -1,6 +1,3 @@ -#include -#include - #include #include @@ -8,6 +5,7 @@ #include #include +#include "glfw_guard.h" #include "window.h" #include "particle_vertex_buffer.h" @@ -33,13 +31,13 @@ float getWorldHeight(int window_width, int window_height, float world_width) { } glm::mat4 getMVP(float world_width, float world_height) { - glm::mat4 projection = glm::ortho( + const glm::mat4 projection = glm::ortho( -(world_width /2), world_width/2, -(world_height/2), world_height/2, 0.1f, 100.0f ); - glm::mat4 view = glm::lookAt( + const glm::mat4 view = glm::lookAt( glm::vec3(0,0,1), glm::vec3(0,0,0), glm::vec3(0,1,0) @@ -75,8 +73,10 @@ std::string getShaderFunction(const std::string& fx, const std::string& fy) { } int main() { - if( !glfwInit() ) { - std::cerr << "Failed to initialize GLFW." << std::endl; + GlfwGuard glfw; + + if( !glfw.isGood() ) { + std::cerr << "Failed to initialize GLFW." << std::endl; return -1; } @@ -84,7 +84,6 @@ int main() { if ( !window.isGood() ) { std::cerr << "Failed to open window." << std::endl; - glfwTerminate(); return -1; } @@ -191,6 +190,5 @@ int main() { } }); - glfwTerminate(); return 0; } diff --git a/src/window.h b/src/window.h index 76f1e6a..794dd5f 100644 --- a/src/window.h +++ b/src/window.h @@ -1,5 +1,8 @@ #pragma once +#include +#include + class Window { private: bool _good = false; -- cgit v1.2.3