GUI: add load_buffers(), draw_rect()

This commit is contained in:
Josh Holtrop 2014-06-26 12:50:48 -04:00
parent 7c9c176c90
commit dd5b7bf8fd
2 changed files with 38 additions and 0 deletions

View File

@ -57,6 +57,9 @@ namespace jes
if (!load_shaders())
return false;
if (!load_buffers())
return false;
return true;
}
@ -98,6 +101,25 @@ namespace jes
return true;
}
bool GUI::load_buffers()
{
static const GLint rect_coords[4][2] = {
{0, 0},
{1, 0},
{0, 1},
{1, 1},
};
m_rect_vbo = new GLBuffer();
if (!m_rect_vbo->create(GL_ARRAY_BUFFER, GL_STATIC_DRAW, &rect_coords, sizeof(rect_coords)))
{
std::cerr << "Failed to create rectangle VBO" << std::endl;
return false;
}
return true;
}
int GUI::run()
{
resize();
@ -148,4 +170,16 @@ namespace jes
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
SDL_GL_SwapWindow(m_window);
}
void GUI::draw_rect(int x, int y, int width, int height, float r, float g, float b, float a)
{
m_programs[PROGRAM_RECT]->use();
glUniform2i(m_programs[PROGRAM_RECT]->get_uniform("position"), x, y);
glUniform2i(m_programs[PROGRAM_RECT]->get_uniform("size"), width, height);
glUniform4f(m_programs[PROGRAM_RECT]->get_uniform("color"), r, g, b, a);
m_rect_vbo->bind();
glEnableVertexAttribArray(0);
glVertexAttribIPointer(0, 2, GL_INT, 0, 0);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}
}

View File

@ -5,6 +5,7 @@
#include <SDL.h>
#include "FontManager.h"
#include "GLProgram.h"
#include "GLBuffer.h"
namespace jes
{
@ -22,12 +23,15 @@ namespace jes
int run();
protected:
bool load_shaders();
bool load_buffers();
void resize();
void draw();
void draw_rect(int x, int y, int width, int height, float r, float g, float b, float a);
SDL_Window * m_window;
FontManagerRef m_font_manager;
GLProgramRef m_programs[PROGRAM_COUNT];
GLBufferRef m_rect_vbo;
};
typedef Ref<GUI> GUIRef;