102 lines
2.0 KiB
C++
102 lines
2.0 KiB
C++
#include "gl3w.h"
|
|
#include "ruby.h"
|
|
#include "Window.h"
|
|
#include <SDL.h>
|
|
|
|
#define WIDTH 500
|
|
#define HEIGHT 500
|
|
|
|
typedef struct
|
|
{
|
|
SDL_Window * sdl_window;
|
|
} Window;
|
|
|
|
static VALUE ruby_class;
|
|
|
|
static void Init_SDL(void)
|
|
{
|
|
static bool initialized = false;
|
|
|
|
if (initialized)
|
|
return;
|
|
|
|
if (SDL_Init(SDL_INIT_VIDEO))
|
|
{
|
|
rb_raise(rb_eRuntimeError, "Failed to initialize SDL");
|
|
}
|
|
|
|
atexit(SDL_Quit);
|
|
|
|
initialized = true;
|
|
}
|
|
|
|
static void Init_OpenGL()
|
|
{
|
|
static bool initialized = false;
|
|
|
|
if (initialized)
|
|
return;
|
|
|
|
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");
|
|
}
|
|
|
|
initialized = true;
|
|
}
|
|
|
|
static void Window_free(void * ptr)
|
|
{
|
|
Window * window = (Window *)ptr;
|
|
|
|
if (window->sdl_window)
|
|
SDL_DestroyWindow(window->sdl_window);
|
|
|
|
delete window;
|
|
}
|
|
|
|
static VALUE Window_new(VALUE klass)
|
|
{
|
|
static bool one_created = false;
|
|
|
|
if (one_created)
|
|
{
|
|
rb_raise(rb_eRuntimeError, "Only one Window for now");
|
|
}
|
|
|
|
Init_SDL();
|
|
Window * window = new 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);
|
|
|
|
Init_OpenGL();
|
|
|
|
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);
|
|
}
|