add GL.draw_text and Widget#draw_text

This commit is contained in:
Josh Holtrop 2014-08-05 22:20:21 -04:00
parent 0ca8e044b2
commit 67682df332
2 changed files with 33 additions and 0 deletions

View File

@ -34,4 +34,8 @@ module Widget
def draw_rect(x, y, width, height, r, g, b, a)
GL.draw_rect(@x + x, @y + y, width, height, r, g, b, a)
end
def draw_text(font, x, y, string, r, g, b, a)
GL.draw_text(font, @x + x, @y + y, string, r, g, b, a)
end
end

View File

@ -3,6 +3,7 @@
#include "ruby.h"
#include "GLBuffer.h"
#include "GLProgram.h"
#include "Font.h"
enum
{
@ -42,11 +43,39 @@ static VALUE GL_draw_rect(VALUE klass,
return Qnil;
}
static VALUE GL_draw_text(VALUE klass,
VALUE font, VALUE x, VALUE y, VALUE string,
VALUE r, VALUE g, VALUE b, VALUE a)
{
int x_int = FIX2INT(x);
glUseProgram(programs[PROGRAM_TEXT]->id);
glUniform1i(programs[PROGRAM_TEXT]->uniforms[UNIFORM_TEXTURE], 0);
glUniform4f(programs[PROGRAM_TEXT]->uniforms[UNIFORM_COLOR],
NUM2DBL(r),
NUM2DBL(g),
NUM2DBL(b),
NUM2DBL(a));
int length = RSTRING_LEN(string);
const char * string_ptr = RSTRING_PTR(string);
int advance = FIX2INT(rb_iv_get(font, "@advance"));
for (int i = 0; i < length; i++)
{
glUniform2i(programs[PROGRAM_TEXT]->uniforms[UNIFORM_POSITION], x_int, FIX2INT(y));
Font_RenderGlyph(font, string_ptr[i]);
x_int += advance;
}
return Qnil;
}
void GL_Init()
{
ruby_module = rb_define_module("GL");
rb_define_singleton_method(ruby_module, "draw_rect",
(VALUE(*)(...))GL_draw_rect, 8);
rb_define_singleton_method(ruby_module, "draw_text",
(VALUE(*)(...))GL_draw_text, 8);
}
static void load_gl_buffers()