Add glfw global initialization

This commit is contained in:
rexy712 2020-09-23 09:27:18 -07:00
parent fb692b80b4
commit 7503a2320d
7 changed files with 113 additions and 12 deletions

View File

@ -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;
};
}

43
include/graphics/init.hpp Normal file
View File

@ -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 <http://www.gnu.org/licenses/>.
*/
#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

View File

@ -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);
};
}

View File

@ -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;

43
src/graphics/init.cpp Normal file
View File

@ -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 <http://www.gnu.org/licenses/>.
*/
#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;
}
}

View File

@ -17,19 +17,32 @@
*/
#include "graphics/window.hpp"
#include "graphics/init.hpp"
#include <utility> //exchange, swap, pair
#include <cstring> //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;
}
}

View File

@ -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
}