diff --git a/src/Window.cc b/src/Window.cc new file mode 100644 index 0000000..1adeeca --- /dev/null +++ b/src/Window.cc @@ -0,0 +1,82 @@ +#include "gl3w.h" +#include "ruby.h" +#include "Window.h" +#include + +#define WIDTH 500 +#define HEIGHT 500 + +typedef struct +{ + SDL_Window * sdl_window; +} Window; + +static VALUE ruby_class; + +static void Init_Graphics(void) +{ + static bool graphics_initialized = false; + + if (graphics_initialized) + return; + + if (SDL_Init(SDL_INIT_VIDEO)) + { + rb_raise(rb_eRuntimeError, "Failed to initialize SDL"); + } + + atexit(SDL_Quit); + + if (gl3wInit() != 0) + { + SDL_Quit(); + rb_raise(rb_eRuntimeError, "Failed to gl3wInit()"); + } + + if (!gl3wIsSupported(3, 0)) + { + SDL_Quit(); + rb_raise(rb_eRuntimeError, "OpenGL 3.0 is not supported"); + } +} + +static void Window_free(void * ptr) +{ + /* TODO */ +} + +static VALUE Window_new(VALUE klass) +{ + static bool one_created = false; + + if (one_created) + { + rb_raise(rb_eRuntimeError, "Only one Window for now"); + } + + Init_Graphics(); + Window * window = (Window *)malloc(sizeof(Window)); + window->sdl_window = SDL_CreateWindow("jes", + SDL_WINDOWPOS_UNDEFINED, + SDL_WINDOWPOS_UNDEFINED, + WIDTH, + HEIGHT, + SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE); + if (window->sdl_window == NULL) + { + free(window); + rb_raise(rb_eRuntimeError, "Failed to create SDL window"); + } + + (void)SDL_GL_CreateContext(window->sdl_window); + + one_created = true; + + return Data_Wrap_Struct(ruby_class, NULL, Window_free, window); +} + +void Window_Init(void) +{ + ruby_class = rb_define_class("Window", rb_cObject); + rb_define_singleton_method(ruby_class, "new", (VALUE(*)(...))Window_new, 0); +} diff --git a/src/Window.h b/src/Window.h new file mode 100644 index 0000000..e9a8150 --- /dev/null +++ b/src/Window.h @@ -0,0 +1,6 @@ +#ifndef WINDOW_H +#define WINDOW_H + +void Window_Init(void); + +#endif diff --git a/src/main.cc b/src/main.cc index 33f8c38..7e3f0f3 100644 --- a/src/main.cc +++ b/src/main.cc @@ -1,6 +1,7 @@ #include #include "GUI.h" #include "ruby.h" +#include "Window.h" using namespace std; @@ -47,6 +48,7 @@ static int bootstrap() { int rv = 0; int err_state = 0; + Window_Init(); rb_eval_string_protect( "load File.expand_path('../../runtime/main.rb', $0)", &err_state);