Window: store GL programs for fast access after #initialize completes

This commit is contained in:
Josh Holtrop 2014-07-28 10:33:33 -04:00
parent 1081903730
commit cad9bc056f

View File

@ -1,5 +1,6 @@
#include "gl3w.h" #include "gl3w.h"
#include "GLBuffer.h" #include "GLBuffer.h"
#include "GLProgram.h"
#include "ruby.h" #include "ruby.h"
#include "Window.h" #include "Window.h"
#include <SDL.h> #include <SDL.h>
@ -7,10 +8,25 @@
#define WIDTH 500 #define WIDTH 500
#define HEIGHT 500 #define HEIGHT 500
enum
{
PROGRAM_TEXT,
PROGRAM_BASIC,
PROGRAM_RECT,
PROGRAM_COUNT
};
const char * program_names[PROGRAM_COUNT] = {
"text",
"basic",
"rect",
};
typedef struct typedef struct
{ {
SDL_Window * sdl_window; SDL_Window * sdl_window;
GLBufferRef rect_buffer; GLBufferRef rect_buffer;
GLuint programs[PROGRAM_COUNT];
} Window; } Window;
static VALUE ruby_class; static VALUE ruby_class;
@ -133,10 +149,30 @@ static VALUE Window_new(VALUE klass)
one_created = true; one_created = true;
VALUE rv = Data_Wrap_Struct(ruby_class, NULL, Window_free, window); VALUE rv = Data_Wrap_Struct(ruby_class, NULL, Window_free, window);
rb_obj_call_init(rv, 0, NULL);
VALUE programs = rb_iv_get(rv, "@programs");
for (int i = 0; i < PROGRAM_COUNT; i++)
{
VALUE program = rb_funcall(programs,
rb_intern("[]"),
1,
ID2SYM(rb_intern(program_names[i])));
if (RTEST(program))
{
window->programs[i] = GLProgram_GetID(program);
}
else
{
delete window;
rb_raise(rb_eLoadError,
"Error resolving %s program",
program_names[i]);
}
}
/* TODO: remove window_instance and map SDL_Window * to Window Ruby object /* TODO: remove window_instance and map SDL_Window * to Window Ruby object
* to support multiple windows. */ * to support multiple windows. */
window_instance = rv; window_instance = rv;
rb_obj_call_init(rv, 0, NULL);
resize(); resize();
return rv; return rv;