bettola/client/src/ui/ui_renderer.cpp

65 lines
1.9 KiB
C++

#include <GL/glew.h>
#include "ui_renderer.h"
#include "gfx/shape_renderer.h"
#include "gfx/txt_renderer.h"
UIRenderer::UIRenderer(ShapeRenderer* shape_renderer, TextRenderer* txt_renderer, int screen_height)
: _shape_renderer(shape_renderer), _txt_renderer(txt_renderer), _screen_height(screen_height) {}
void UIRenderer::draw_rect(int x, int y, int width, int height, const Color& color) {
if(!_shape_renderer) return;
/* Convert top-left screen coord to bottom-left GL coord. */
int y_gl = _screen_height - y - height;
_shape_renderer->draw_rect(x, y_gl, width, height, color);
}
void UIRenderer::draw_triangle(int x1, int y1, int x2, int y2, int x3, int y3, const Color& color) {
if(!_shape_renderer) return;
/* Convert top-left screen coord to bottom-left GL coord. */
int y1_gl = _screen_height - y1;
int y2_gl = _screen_height - y2;
int y3_gl = _screen_height - y3;
_shape_renderer->draw_triangle(x1, y1_gl, x2, y2_gl, x3, y3_gl, color);
}
void UIRenderer::render_text(const char* text, int x, int y, const Color& color) {
if(!_txt_renderer) return;
/* Convert the screen-space baseline y-coord to GL-space baseline y-coord. */
int y_gl = _screen_height - y;
_txt_renderer->render_text(text, x, y_gl, color);
}
void UIRenderer::begin_text(void) {
if(!_txt_renderer) return;
_txt_renderer->begin();
}
void UIRenderer::flush_text(void) {
if(!_txt_renderer) return;
_txt_renderer->flush();
}
void UIRenderer::begin_shapes(void) {
if(!_shape_renderer) return;
_shape_renderer->begin();
}
void UIRenderer::flush_shapes(void) {
if(!_shape_renderer) return;
_shape_renderer->flush();
}
TextRenderer* UIRenderer::get_text_renderer(void) {
return _txt_renderer;
}
void UIRenderer::enable_scissor(int x, int y, int width, int height) {
glEnable(GL_SCISSOR_TEST);
glScissor(x, y, width, height);
}
void UIRenderer::disable_scissor(void) {
glDisable(GL_SCISSOR_TEST);
}