From cad9bc056fafc41b1aa4c04c74d564e904973a75 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Mon, 28 Jul 2014 10:33:33 -0400 Subject: [PATCH] Window: store GL programs for fast access after #initialize completes --- src/Window.cc | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/src/Window.cc b/src/Window.cc index b9c4501..f222e10 100644 --- a/src/Window.cc +++ b/src/Window.cc @@ -1,5 +1,6 @@ #include "gl3w.h" #include "GLBuffer.h" +#include "GLProgram.h" #include "ruby.h" #include "Window.h" #include @@ -7,10 +8,25 @@ #define WIDTH 500 #define HEIGHT 500 +enum +{ + PROGRAM_TEXT, + PROGRAM_BASIC, + PROGRAM_RECT, + PROGRAM_COUNT +}; + +const char * program_names[PROGRAM_COUNT] = { + "text", + "basic", + "rect", +}; + typedef struct { SDL_Window * sdl_window; GLBufferRef rect_buffer; + GLuint programs[PROGRAM_COUNT]; } Window; static VALUE ruby_class; @@ -133,10 +149,30 @@ static VALUE Window_new(VALUE klass) one_created = true; 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 * to support multiple windows. */ window_instance = rv; - rb_obj_call_init(rv, 0, NULL); resize(); return rv;