From 24a2117c176db961a047313860e95a8882f0abe0 Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Wed, 11 Jul 2012 18:29:22 +0000 Subject: [PATCH] neercs: handle caca canvas size changes automatically. --- neercs/video/text-render.cpp | 33 +++++++++++++++++++++++++++------ neercs/video/text-render.h | 3 ++- 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/neercs/video/text-render.cpp b/neercs/video/text-render.cpp index 40442b2..7eb6b80 100644 --- a/neercs/video/text-render.cpp +++ b/neercs/video/text-render.cpp @@ -36,9 +36,6 @@ TextRender::TextRender(caca_canvas_t *caca, ivec2 font_size) m_fbo_size(m_font_size * m_canvas_size), m_cells(m_canvas_size.x * m_canvas_size.y) { - for (int j = 0; j < m_canvas_size.y; j++) - for (int i = 0; i < m_canvas_size.x; i++) - m_vertices << vec2(i, j); } void TextRender::Init() @@ -60,9 +57,16 @@ void TextRender::Init() VertexStream(VertexUsage::Color), VertexStream(VertexUsage::Color)); - m_vbo1 = new VertexBuffer(m_vertices.Bytes()); - void *vertices = m_vbo1->Lock(0, 0); - memcpy(vertices, &m_vertices[0], m_vertices.Bytes()); + CreateBuffers(); +} + +void TextRender::CreateBuffers() +{ + m_vbo1 = new VertexBuffer(m_cells * sizeof(vec2)); + vec2 *vertices = (vec2 *)m_vbo1->Lock(0, 0); + for (int j = 0; j < m_canvas_size.y; j++) + for (int i = 0; i < m_canvas_size.x; i++) + vertices[j * m_canvas_size.x + i] = vec2(i, j); m_vbo1->Unlock(); m_vbo2 = new VertexBuffer(m_cells * sizeof(int32_t)); @@ -73,6 +77,23 @@ void TextRender::Init() void TextRender::Render() { + /* Handle canvas size changes */ + ivec2 current_size(caca_get_canvas_width(m_caca), + caca_get_canvas_height(m_caca)); + if (current_size != m_canvas_size) + { + delete m_vbo1; + delete m_vbo2; + delete m_vbo3; + delete m_fbo; + + m_canvas_size = current_size; + m_fbo_size = m_font_size * m_canvas_size; + m_cells = m_canvas_size.x * m_canvas_size.y; + + CreateBuffers(); + } + /* Transform matrix for the scene: * - translate to the centre of the glyph * - scale by 2.f * font_size / fbo_size diff --git a/neercs/video/text-render.h b/neercs/video/text-render.h index 2b2bdd2..101af5e 100644 --- a/neercs/video/text-render.h +++ b/neercs/video/text-render.h @@ -13,11 +13,12 @@ struct TextRender void Blit(ivec2 pos, ivec2 size); private: + void CreateBuffers(); + caca_canvas_t *m_caca; ivec2 m_font_size, m_canvas_size, m_fbo_size; int m_cells; - Array m_vertices; TileSet *m_font; Shader *m_shader; ShaderAttrib m_coord, m_color, m_char;