From dd5b7bf8fda7446b4751c2eae6232b81323db1f5 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Thu, 26 Jun 2014 12:50:48 -0400 Subject: [PATCH] GUI: add load_buffers(), draw_rect() --- src/gui/GUI.cc | 34 ++++++++++++++++++++++++++++++++++ src/gui/GUI.h | 4 ++++ 2 files changed, 38 insertions(+) diff --git a/src/gui/GUI.cc b/src/gui/GUI.cc index ce56e2f..4f593f3 100644 --- a/src/gui/GUI.cc +++ b/src/gui/GUI.cc @@ -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); + } } diff --git a/src/gui/GUI.h b/src/gui/GUI.h index 45b36fa..cd6e501 100644 --- a/src/gui/GUI.h +++ b/src/gui/GUI.h @@ -5,6 +5,7 @@ #include #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 GUIRef;