break up Init_Graphics() into Init_SDL() and Init_OpenGL() since they need to be called at separate times

This commit is contained in:
Josh Holtrop 2014-07-19 12:34:25 -04:00
parent b559ef1be0
commit b6c5e9e1d7
3 changed files with 24 additions and 43 deletions

View File

@ -9,43 +9,6 @@ namespace jes
{ {
bool GUI::load() 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); glClearColor (0.0, 0.0, 0.0, 0.0);
m_font_manager = new FontManager(); m_font_manager = new FontManager();

View File

@ -13,11 +13,11 @@ typedef struct
static VALUE ruby_class; 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; return;
if (SDL_Init(SDL_INIT_VIDEO)) if (SDL_Init(SDL_INIT_VIDEO))
@ -27,6 +27,16 @@ static void Init_Graphics(void)
atexit(SDL_Quit); atexit(SDL_Quit);
initialized = true;
}
static void Init_OpenGL()
{
static bool initialized = false;
if (initialized)
return;
if (gl3wInit() != 0) if (gl3wInit() != 0)
{ {
SDL_Quit(); SDL_Quit();
@ -38,11 +48,18 @@ static void Init_Graphics(void)
SDL_Quit(); SDL_Quit();
rb_raise(rb_eRuntimeError, "OpenGL 3.0 is not supported"); rb_raise(rb_eRuntimeError, "OpenGL 3.0 is not supported");
} }
initialized = true;
} }
static void Window_free(void * ptr) 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) 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"); rb_raise(rb_eRuntimeError, "Only one Window for now");
} }
Init_Graphics(); Init_SDL();
Window * window = (Window *)malloc(sizeof(Window)); Window * window = (Window *)malloc(sizeof(Window));
window->sdl_window = SDL_CreateWindow("jes", window->sdl_window = SDL_CreateWindow("jes",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
@ -70,6 +87,8 @@ static VALUE Window_new(VALUE klass)
(void)SDL_GL_CreateContext(window->sdl_window); (void)SDL_GL_CreateContext(window->sdl_window);
Init_OpenGL();
one_created = true; one_created = true;
return Data_Wrap_Struct(ruby_class, NULL, Window_free, window); return Data_Wrap_Struct(ruby_class, NULL, Window_free, window);

View File

@ -1,5 +1,4 @@
#include <iostream> #include <iostream>
#include "GUI.h"
#include "ruby.h" #include "ruby.h"
#include "Window.h" #include "Window.h"