diff --git a/src/GUI.cc b/src/GUI.cc index 1d8925c..2e6bc8a 100644 --- a/src/GUI.cc +++ b/src/GUI.cc @@ -9,43 +9,6 @@ namespace jes { bool GUI::load() { - if (SDL_Init(SDL_INIT_VIDEO)) - { - std::cerr << "Failed to initialize SDL!" << std::endl; - return false; - } - - atexit(SDL_Quit); - - m_window = SDL_CreateWindow("jes", - SDL_WINDOWPOS_UNDEFINED, - SDL_WINDOWPOS_UNDEFINED, - WIDTH, - HEIGHT, - SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE); - if (!m_window) - { - std::cerr << "Failed to create window!" << std::endl; - SDL_Quit(); - return false; - } - - (void)SDL_GL_CreateContext(m_window); - - if (gl3wInit() != 0) - { - std::cerr << "Failed to gl3wInit()" << std::endl; - SDL_Quit(); - return false; - } - - if (!gl3wIsSupported(3, 0)) - { - std::cerr << "OpenGL 3.0 is not supported!" << std::endl; - SDL_Quit(); - return false; - } - glClearColor (0.0, 0.0, 0.0, 0.0); m_font_manager = new FontManager(); diff --git a/src/Window.cc b/src/Window.cc index 1adeeca..8ef6b2e 100644 --- a/src/Window.cc +++ b/src/Window.cc @@ -13,11 +13,11 @@ typedef struct static VALUE ruby_class; -static void Init_Graphics(void) +static void Init_SDL(void) { - static bool graphics_initialized = false; + static bool initialized = false; - if (graphics_initialized) + if (initialized) return; if (SDL_Init(SDL_INIT_VIDEO)) @@ -27,6 +27,16 @@ static void Init_Graphics(void) atexit(SDL_Quit); + initialized = true; +} + +static void Init_OpenGL() +{ + static bool initialized = false; + + if (initialized) + return; + if (gl3wInit() != 0) { SDL_Quit(); @@ -38,11 +48,18 @@ static void Init_Graphics(void) SDL_Quit(); rb_raise(rb_eRuntimeError, "OpenGL 3.0 is not supported"); } + + initialized = true; } static void Window_free(void * ptr) { - /* TODO */ + Window * window = (Window *)ptr; + + if (window->sdl_window) + SDL_DestroyWindow(window->sdl_window); + + free(window); } static VALUE Window_new(VALUE klass) @@ -54,7 +71,7 @@ static VALUE Window_new(VALUE klass) rb_raise(rb_eRuntimeError, "Only one Window for now"); } - Init_Graphics(); + Init_SDL(); Window * window = (Window *)malloc(sizeof(Window)); window->sdl_window = SDL_CreateWindow("jes", SDL_WINDOWPOS_UNDEFINED, @@ -70,6 +87,8 @@ static VALUE Window_new(VALUE klass) (void)SDL_GL_CreateContext(window->sdl_window); + Init_OpenGL(); + one_created = true; return Data_Wrap_Struct(ruby_class, NULL, Window_free, window); diff --git a/src/main.cc b/src/main.cc index 7e3f0f3..eb9640d 100644 --- a/src/main.cc +++ b/src/main.cc @@ -1,5 +1,4 @@ #include -#include "GUI.h" #include "ruby.h" #include "Window.h"