diff --git a/include/audio/init.hpp b/include/audio/init.hpp index 5551100..ba29bec 100644 --- a/include/audio/init.hpp +++ b/include/audio/init.hpp @@ -28,8 +28,13 @@ namespace audio{ pa_system(); ~pa_system(); + public: + int status()const; + void try_init()const; + public: static pa_system& instance(); + static inline int s_status = 0; }; } diff --git a/include/graphics/init.hpp b/include/graphics/init.hpp new file mode 100644 index 0000000..6514eeb --- /dev/null +++ b/include/graphics/init.hpp @@ -0,0 +1,43 @@ +/** + This file is a part of our_dick + Copyright (C) 2020 rexy712 + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . +*/ + +#ifndef OUR_DICK_GRAPHICS_INIT_HPP +#define OUR_DICK_GRAPHICS_INIT_HPP + +namespace graphics{ + + //initializer/deinitializer for glfw + class glfw_system + { + private: + glfw_system(); + ~glfw_system(); + + public: + int status()const; + void try_init()const; + + public: + static glfw_system& instance(); + + private: + static inline int s_status = 0; + }; +} + +#endif diff --git a/include/graphics/window.hpp b/include/graphics/window.hpp index 2523c22..554c57e 100644 --- a/include/graphics/window.hpp +++ b/include/graphics/window.hpp @@ -117,9 +117,6 @@ namespace graphics{ bool is_fullscreen()const; bool should_close()const; mousemode get_mousemode()const; - - private: - void copy_title(const char* title, size_t titlelen); }; } diff --git a/src/audio/init.cpp b/src/audio/init.cpp index ba19f5e..51b8784 100644 --- a/src/audio/init.cpp +++ b/src/audio/init.cpp @@ -42,11 +42,17 @@ namespace audio{ //silence excessive stderr warnings from alsa snd_lib_error_set_handler(detail::linux_alsa_error_handler); #endif - Pa_Initialize(); + try_init(); } pa_system::~pa_system(){ Pa_Terminate(); } + int pa_system::status()const{ + return s_status; + } + void pa_system::try_init()const{ + s_status = Pa_Initialize(); + } pa_system& pa_system::instance(){ static pa_system inst; diff --git a/src/graphics/init.cpp b/src/graphics/init.cpp new file mode 100644 index 0000000..fe4db82 --- /dev/null +++ b/src/graphics/init.cpp @@ -0,0 +1,43 @@ +/** + This file is a part of our_dick + Copyright (C) 2020 rexy712 + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . +*/ + +#include "graphics/init.hpp" +#include "config.hpp" + +#include "graphics/gl_include.hpp" + +namespace graphics{ + + glfw_system::glfw_system(){ + try_init(); + } + glfw_system::~glfw_system(){ + glfwTerminate(); + } + int glfw_system::status()const{ + return s_status; + } + + void glfw_system::try_init()const{ + s_status = glfwInit(); + } + glfw_system& glfw_system::instance(){ + static glfw_system inst; + return inst; + } +} diff --git a/src/graphics/window.cpp b/src/graphics/window.cpp index 067a20c..c204992 100644 --- a/src/graphics/window.cpp +++ b/src/graphics/window.cpp @@ -17,19 +17,32 @@ */ #include "graphics/window.hpp" +#include "graphics/init.hpp" #include //exchange, swap, pair #include //strlen, strncpy namespace graphics{ + static int initialize_global_glfw(){ + glfw_system& system = glfw_system::instance(); + return system.status(); + } + static char* copy_title(const char* src, size_t titlelen){ + char* dest = new char[titlelen + 1]; + strncpy(dest, src, titlelen); + dest[titlelen] = 0; + return dest; + } window::window(int width, int height, const char* title): window(s_default_cver_maj, s_default_cver_min, width, height, title){} window::window(int cver_maj, int cver_min, int width, int height, const char* title, int antialias, int refresh): + m_title(copy_title(title, strlen(title))), m_antialias_level(antialias), m_refresh_rate(refresh) { - copy_title(title, strlen(title)); + if(initialize_global_glfw() != GLFW_TRUE) + return; //TODO: user access to errors glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE); glfwWindowHint(GLFW_DECORATED, GLFW_TRUE); @@ -44,6 +57,7 @@ namespace graphics{ m_window = glfwCreateWindow(width, height, title, nullptr, nullptr); } window::window(const window& w): + m_title(copy_title(w.m_title, strlen(w.m_title))), m_antialias_level(w.m_antialias_level), m_refresh_rate(w.m_refresh_rate), m_swap_interval(w.m_swap_interval) @@ -271,9 +285,4 @@ namespace graphics{ //TODO } */ - void window::copy_title(const char* title, size_t titlelen){ - m_title = new char[titlelen + 1]; - strncpy(m_title, title, titlelen); - m_title[titlelen] = 0; - } } diff --git a/src/main.cpp b/src/main.cpp index 8b3e022..5d3e061 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -187,7 +187,6 @@ int main(){ }; srand(time(NULL)); - glfwInit(); //TODO: handle in a similar way as PortAudio library initialization game_state gs = {}; audio::mixer mix(audio::mixer::mode::STEREO, 1, 44100); @@ -226,5 +225,4 @@ int main(){ game_turn(gs, get_player_input()); } t.join(); - //TODO: cannot call glfwTerminate before the window is closed }