From 3f573cac407be7b047b7de8b13d2ac876a62fe2a Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Fri, 25 Nov 2016 09:44:53 -0500 Subject: [PATCH] move GL logic to new GL module --- src/gui/Window.cc | 79 +++++------------------------------------------ src/gui/Window.h | 20 ++---------- src/gui/gl/GL.cc | 49 +++++++++++++++++++++++++++++ src/gui/gl/GL.h | 33 ++++++++++++++++++++ 4 files changed, 93 insertions(+), 88 deletions(-) create mode 100644 src/gui/gl/GL.cc create mode 100644 src/gui/gl/GL.h diff --git a/src/gui/Window.cc b/src/gui/Window.cc index 8c4de47..b8254b6 100644 --- a/src/gui/Window.cc +++ b/src/gui/Window.cc @@ -112,36 +112,7 @@ bool Window::create(std::shared_ptr buffer) return false; } - m_shaders.text = std::make_shared(); - m_shaders.text->use(); - m_shaders.text->set_texture(0); - m_shaders.text->set_color(1.0, 1.0, 1.0, 1.0); - - m_shaders.flat = std::make_shared(); - m_shaders.rect = std::make_shared(); - - GLint cursor_bounds[] = {0, 0, - m_font.get_advance(), 0, - m_font.get_advance(), m_font.get_line_height(), - 0, m_font.get_line_height()}; - m_cursor_array = glcxx::Array::create(); - m_cursor_array->bind(); - m_cursor_buffer = glcxx::Buffer::create(GL_ARRAY_BUFFER, GL_STATIC_DRAW, - cursor_bounds, sizeof(cursor_bounds)); - glEnableVertexAttribArray(0); - glVertexAttribPointer(0, 2, GL_INT, GL_FALSE, 0, 0); - - GLint rect_coords[] = { - 0, 0, - 1, 0, - 1, 1, - 0, 1}; - m_rect_array = glcxx::Array::create(); - m_rect_array->bind(); - m_rect_buffer = glcxx::Buffer::create(GL_ARRAY_BUFFER, GL_STATIC_DRAW, - rect_coords, sizeof(rect_coords)); - glEnableVertexAttribArray(0); - glVertexAttribPointer(0, 2, GL_INT, GL_FALSE, 0, 0); + m_gl = std::make_shared(); m_buffer = buffer; m_cursor = buffer->add_cursor(); @@ -415,7 +386,7 @@ void Window::draw_buffer_line(int screen_row, const GapBuffer::Cursor & cursor) uint32_t c = *iter_cursor; if ((c != '\t') && (c != ' ')) { - draw_character(x, y, c); + m_gl->draw_character(x, y, c, m_font); } iter_cursor.go_right(false); } @@ -425,27 +396,14 @@ void Window::draw_buffer_character(int screen_column, int screen_row, uint32_t c { int x, y; colrow_to_xy(screen_column, screen_row, &x, &y); - draw_character(x, y, character); -} - -void Window::draw_character(int x, int y, uint32_t character) -{ - m_shaders.text->use(); - auto g = m_font.get_glyph(character); - m_shaders.text->set_position(x, y + m_font.get_baseline_offset()); - g->render(); + m_gl->draw_character(x, y, character, m_font); } void Window::resize() { SDL_GetWindowSize(m_window, &m_width, &m_height); glViewport(0, 0, m_width, m_height); - m_shaders.text->use(); - m_shaders.text->set_viewport_size(m_width, m_height); - m_shaders.flat->use(); - m_shaders.flat->set_viewport_size(m_width, m_height); - m_shaders.rect->use(); - m_shaders.rect->set_viewport_size(m_width, m_height); + m_gl->resize(m_width, m_height); m_columns = std::max(1, m_width / m_font.get_advance()); m_rows = std::max(1, (m_height - 2) / m_font.get_line_height()); } @@ -466,18 +424,7 @@ void Window::draw_cursor(int screen_column, int screen_row, bool insert_mode) colrow_to_xy(screen_column, screen_row, &x, &y); int width = insert_mode ? 1 : m_font.get_advance(); int height = m_font.get_line_height(); - draw_rect(x, y, width, height, 1.0, 0.2, 1.0, 1.0); -} - -void Window::draw_crosshair(int screen_column, int screen_row) -{ - int x, y; - colrow_to_xy(screen_column, screen_row, &x, &y); - m_cursor_array->bind(); - m_shaders.flat->use(); - m_shaders.flat->set_color(0.1, 0.1, 0.1, 1.0); - m_shaders.flat->set_position(x, y); - glDrawArrays(GL_TRIANGLE_FAN, 0, 4); + m_gl->draw_rect(x, y, width, height, 1.0, 0.2, 1.0, 1.0); } void Window::colrow_to_xy(int col, int row, int * x, int * y) @@ -486,20 +433,10 @@ void Window::colrow_to_xy(int col, int row, int * x, int * y) *y = m_height - (row + 1) * m_font.get_line_height(); } -void Window::draw_rect(int x, int y, int width, int height, float r, float g, float b, float a) -{ - m_rect_array->bind(); - m_shaders.rect->use(); - m_shaders.rect->set_color(r, g, b, a); - m_shaders.rect->set_position(x, y); - m_shaders.rect->set_size(width, height); - glDrawArrays(GL_TRIANGLE_FAN, 0, 4); -} - void Window::draw_status_bar() { - draw_rect(0, m_font.get_line_height(), m_width, 1, 0.5, 0.5, 0.5, 1.0); - draw_rect(0, 0, m_width, m_font.get_line_height(), 0.0, 0.0, 0.0, 1.0); + m_gl->draw_rect(0, m_font.get_line_height(), m_width, 1, 0.5, 0.5, 0.5, 1.0); + m_gl->draw_rect(0, 0, m_width, m_font.get_line_height(), 0.0, 0.0, 0.0, 1.0); #if 0 char cursor_position[20]; sprintf(cursor_position, "%d, %d", m_cursor->line + 1, m_cursor->column + 1u); @@ -507,7 +444,7 @@ void Window::draw_status_bar() int x = m_width - m_font.get_advance() * cursor_position_length; for (int i = 0; i < cursor_position_length; i++) { - draw_character(x, 0, cursor_position[i]); + m_gl->draw_character(x, 0, cursor_position[i], m_font); x += m_font.get_advance(); } #endif diff --git a/src/gui/Window.h b/src/gui/Window.h index 5bd2ba0..7a5e128 100644 --- a/src/gui/Window.h +++ b/src/gui/Window.h @@ -1,14 +1,12 @@ #ifndef WINDOW_H #define WINDOW_H +#include #include #include -#include "TextShader.h" -#include "FlatShader.h" -#include "RectShader.h" #include "Font.h" #include "Buffer.h" -#include "glcxx.hpp" +#include "GL.h" class Window { @@ -38,7 +36,6 @@ protected: void resize(); void redraw(); void draw_cursor(int screen_column, int screen_row, bool insert_mode); - void draw_crosshair(int screen_column, int screen_row); void colrow_to_xy(int col, int row, int * x, int * y); void handle_event(SDL_Event & event); void handle_keysym(uint32_t keysym); @@ -47,9 +44,7 @@ protected: void draw_buffer(); void draw_buffer_line(int screen_row, const GapBuffer::Cursor & cursor); void draw_buffer_character(int screen_column, int screen_row, uint32_t character); - void draw_character(int x, int y, uint32_t character); void update_cursor_row(); - void draw_rect(int x, int y, int width, int height, float r, float g, float b, float a); void draw_status_bar(); uint32_t get_keyval(SDL_Keycode keysym); uint32_t get_shifted(uint32_t keysym); @@ -64,12 +59,6 @@ protected: bool m_exit_requested; int m_width; int m_height; - struct - { - std::shared_ptr text; - std::shared_ptr flat; - std::shared_ptr rect; - } m_shaders; Font m_font; int m_columns; @@ -81,10 +70,7 @@ protected: std::shared_ptr m_buffer; std::shared_ptr m_cursor; - std::shared_ptr m_cursor_array; - std::shared_ptr m_cursor_buffer; - std::shared_ptr m_rect_array; - std::shared_ptr m_rect_buffer; + std::shared_ptr m_gl; Uint16 m_keymod; }; diff --git a/src/gui/gl/GL.cc b/src/gui/gl/GL.cc new file mode 100644 index 0000000..27da600 --- /dev/null +++ b/src/gui/gl/GL.cc @@ -0,0 +1,49 @@ +#include "GL.h" + +GL::GL() +{ + m_shaders.text = std::make_shared(); + m_shaders.text->use(); + m_shaders.text->set_texture(0); + m_shaders.text->set_color(1.0, 1.0, 1.0, 1.0); + + m_shaders.rect = std::make_shared(); + + GLint rect_coords[] = { + 0, 0, + 1, 0, + 1, 1, + 0, 1}; + m_rect_array = glcxx::Array::create(); + m_rect_array->bind(); + m_rect_buffer = glcxx::Buffer::create(GL_ARRAY_BUFFER, GL_STATIC_DRAW, + rect_coords, sizeof(rect_coords)); + glEnableVertexAttribArray(0); + glVertexAttribPointer(0, 2, GL_INT, GL_FALSE, 0, 0); +} + +void GL::draw_rect(int x, int y, int width, int height, + float r, float g, float b, float a) +{ + m_rect_array->bind(); + m_shaders.rect->use(); + m_shaders.rect->set_color(r, g, b, a); + m_shaders.rect->set_position(x, y); + m_shaders.rect->set_size(width, height); + glDrawArrays(GL_TRIANGLE_FAN, 0, 4); +} + +void GL::draw_character(int x, int y, uint32_t character, Font & font) +{ + m_shaders.text->use(); + m_shaders.text->set_position(x, y + font.get_baseline_offset()); + font.get_glyph(character)->render(); +} + +void GL::resize(int width, int height) +{ + m_shaders.text->use(); + m_shaders.text->set_viewport_size(width, height); + m_shaders.rect->use(); + m_shaders.rect->set_viewport_size(width, height); +} diff --git a/src/gui/gl/GL.h b/src/gui/gl/GL.h new file mode 100644 index 0000000..2cd7478 --- /dev/null +++ b/src/gui/gl/GL.h @@ -0,0 +1,33 @@ +#ifndef GL_H +#define GL_H + +#include "glcxx.hpp" +#include "TextShader.h" +#include "FlatShader.h" +#include "RectShader.h" +#include "Font.h" +#include + +class GL +{ +public: + GL(); + + void draw_rect(int x, int y, int width, int height, + float r, float g, float b, float a); + void draw_character(int x, int y, uint32_t character, Font & font); + void resize(int width, int height); + +protected: + struct + { + std::shared_ptr text; + std::shared_ptr flat; + std::shared_ptr rect; + } m_shaders; + + std::shared_ptr m_rect_array; + std::shared_ptr m_rect_buffer; +}; + +#endif