record all Windows, add Window.event_loop

This commit is contained in:
Josh Holtrop 2014-07-23 22:21:59 -04:00
parent 8604d6d5ad
commit b908c98c8c
3 changed files with 51 additions and 37 deletions

View File

@ -1,7 +1,26 @@
require "set"
class Window class Window
@windows = Set.new
class << self
def register(window)
@windows << window
end
def remove(window)
@windows.delete(window)
end
end
def initialize def initialize
@programs = {} @programs = {}
load_shaders load_shaders
Window.register(self)
end
def close
Window.remove(self)
end end
private private

View File

@ -1,8 +1,4 @@
#if 0 #if 0
#include "GUI.h"
#include "gl3w.h"
#include <iostream>
bool GUI::load() bool GUI::load()
{ {
glClearColor (0.0, 0.0, 0.0, 0.0); glClearColor (0.0, 0.0, 0.0, 0.0);
@ -40,39 +36,6 @@ bool GUI::load_buffers()
return true; return true;
} }
int GUI::run()
{
resize();
draw();
SDL_Event event;
while (SDL_WaitEvent(&event))
{
if (event.type == SDL_QUIT)
break;
else if (event.type == SDL_KEYDOWN)
{
if (event.key.keysym.sym == SDLK_ESCAPE)
break;
}
else if (event.type == SDL_WINDOWEVENT)
{
switch (event.window.event)
{
case SDL_WINDOWEVENT_EXPOSED:
draw();
break;
case SDL_WINDOWEVENT_RESIZED:
resize();
draw();
break;
}
}
}
return 0;
}
void GUI::resize() void GUI::resize()
{ {
GLint viewport_size[2]; GLint viewport_size[2];

View File

@ -97,8 +97,40 @@ static VALUE Window_new(VALUE klass)
return rv; return rv;
} }
static VALUE Window_event_loop(VALUE klass)
{
SDL_Event event;
while (SDL_WaitEvent(&event))
{
if (event.type == SDL_QUIT)
break;
else if (event.type == SDL_KEYDOWN)
{
if (event.key.keysym.sym == SDLK_ESCAPE)
break;
}
else if (event.type == SDL_WINDOWEVENT)
{
switch (event.window.event)
{
case SDL_WINDOWEVENT_EXPOSED:
// draw();
break;
case SDL_WINDOWEVENT_RESIZED:
// resize();
// draw();
break;
}
}
}
return Qnil;
}
void Window_Init(void) void Window_Init(void)
{ {
ruby_class = rb_define_class("Window", rb_cObject); ruby_class = rb_define_class("Window", rb_cObject);
rb_define_singleton_method(ruby_class, "new", (VALUE(*)(...))Window_new, 0); rb_define_singleton_method(ruby_class, "new", (VALUE(*)(...))Window_new, 0);
rb_define_singleton_method(ruby_class, "event_loop", (VALUE(*)(...))Window_event_loop, 0);
} }