move GL logic to new GL module

This commit is contained in:
Josh Holtrop 2016-11-25 09:44:53 -05:00
parent 59ce578415
commit 3f573cac40
4 changed files with 93 additions and 88 deletions

View File

@ -112,36 +112,7 @@ bool Window::create(std::shared_ptr<Buffer> buffer)
return false;
}
m_shaders.text = std::make_shared<TextShader>();
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<FlatShader>();
m_shaders.rect = std::make_shared<RectShader>();
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<GL>();
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

View File

@ -1,14 +1,12 @@
#ifndef WINDOW_H
#define WINDOW_H
#include <memory>
#include <utility>
#include <SDL.h>
#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<TextShader> text;
std::shared_ptr<FlatShader> flat;
std::shared_ptr<RectShader> rect;
} m_shaders;
Font m_font;
int m_columns;
@ -81,10 +70,7 @@ protected:
std::shared_ptr<Buffer> m_buffer;
std::shared_ptr<GapBuffer::Cursor> m_cursor;
std::shared_ptr<glcxx::Array> m_cursor_array;
std::shared_ptr<glcxx::Buffer> m_cursor_buffer;
std::shared_ptr<glcxx::Array> m_rect_array;
std::shared_ptr<glcxx::Buffer> m_rect_buffer;
std::shared_ptr<GL> m_gl;
Uint16 m_keymod;
};

49
src/gui/gl/GL.cc Normal file
View File

@ -0,0 +1,49 @@
#include "GL.h"
GL::GL()
{
m_shaders.text = std::make_shared<TextShader>();
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<RectShader>();
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);
}

33
src/gui/gl/GL.h Normal file
View File

@ -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 <memory>
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<TextShader> text;
std::shared_ptr<FlatShader> flat;
std::shared_ptr<RectShader> rect;
} m_shaders;
std::shared_ptr<glcxx::Array> m_rect_array;
std::shared_ptr<glcxx::Buffer> m_rect_buffer;
};
#endif