source: trunk/tools/neercs/video/text-render.cpp @ 2049

Last change on this file since 2049 was 2049, checked in by rez, 7 years ago

neercs: added theme support + 2 new themes [F4/F5 to switch]

  • Property svn:keywords set to Id
File size: 5.3 KB
Line 
1//
2// Neercs
3//
4
5#if defined HAVE_CONFIG_H
6#   include "config.h"
7#endif
8
9#include <cmath>
10#include <cstdio>
11#include <cstdlib>
12#include <ctime>
13#include <string>
14
15#include "core.h"
16#include "lolgl.h"
17
18using namespace std;
19using namespace lol;
20
21#include "../neercs.h"
22#include "render.h"
23#include "text-render.h"
24
25extern char const *lolfx_text;
26
27/*
28 * Text rendering interface
29 */
30
31TextRender::TextRender(caca_canvas_t *caca, ivec2 font_size)
32  : m_caca(caca),
33    m_font_size(font_size),
34    m_canvas_size(caca_get_canvas_width(m_caca),
35                  caca_get_canvas_height(m_caca)),
36    m_fbo_size(m_font_size * m_canvas_size),
37    m_cells(m_canvas_size.x * m_canvas_size.y)
38{
39}
40
41void TextRender::Init()
42{
43    m_font = new TileSet("tools/neercs/video/resource/charset_amiga.png",
44                         ivec2(256, 256), ivec2(1));
45
46    m_shader = Shader::Create(lolfx_text);
47    m_coord = m_shader->GetAttribLocation("in_Position",
48                                          VertexUsage::Position, 0);
49    m_color = m_shader->GetAttribLocation("in_Attr",
50                                          VertexUsage::Color, 0);
51    m_char = m_shader->GetAttribLocation("in_Char",
52                                         VertexUsage::Color, 1);
53    m_texture = m_shader->GetUniformLocation("in_Texture");
54    m_transform = m_shader->GetUniformLocation("in_Transform");
55    m_vdecl
56      = new VertexDeclaration(VertexStream<vec2>(VertexUsage::Position),
57                              VertexStream<uint32_t>(VertexUsage::Color),
58                              VertexStream<uint32_t>(VertexUsage::Color));
59
60    CreateBuffers();
61}
62
63void TextRender::CreateBuffers()
64{
65    m_vbo1 = new VertexBuffer(m_cells * sizeof(vec2));
66    vec2 *vertices = (vec2 *)m_vbo1->Lock(0, 0);
67    for (int j = 0; j < m_canvas_size.y; j++)
68    for (int i = 0; i < m_canvas_size.x; i++)
69        vertices[j * m_canvas_size.x + i] = vec2(i, j);
70    m_vbo1->Unlock();
71
72    m_vbo2 = new VertexBuffer(m_cells * sizeof(int32_t));
73    m_vbo3 = new VertexBuffer(m_cells * sizeof(int32_t));
74
75    m_fbo = new FrameBuffer(m_fbo_size);
76}
77
78void TextRender::Render()
79{
80    /* Handle canvas size changes */
81    ivec2 current_size(caca_get_canvas_width(m_caca),
82                       caca_get_canvas_height(m_caca));
83    if (current_size != m_canvas_size)
84    {
85        delete m_vbo1;
86        delete m_vbo2;
87        delete m_vbo3;
88        delete m_fbo;
89
90        m_canvas_size = current_size;
91        m_fbo_size = m_font_size * m_canvas_size;
92        m_cells = m_canvas_size.x * m_canvas_size.y;
93
94        CreateBuffers();
95    }
96
97    /* Transform matrix for the scene:
98     *  - translate to the centre of the glyph
99     *  - scale by 2.f * font_size / fbo_size
100     *  - translate to the lower left corner */
101    mat4 xform = mat4::translate(-1.f, -1.f + 2.0f * m_font_size.y
102                                        * m_canvas_size.y / m_fbo_size.y, 0.f)
103               * mat4::scale(vec3(2.f * m_font_size / m_fbo_size, 1.f)
104                              * vec3(1.f, -1.f, 1.f))
105               * mat4::translate(0.5f, 0.5f, 0.f);
106
107    /* Upload libcaca canvas contents to the vertex buffers */
108    uint32_t *colors = (uint32_t *)m_vbo2->Lock(0, 0);
109    for (int j = 0; j < m_canvas_size.y; j++)
110    for (int i = 0; i < m_canvas_size.x; i++)
111    {
112        uint32_t attr = caca_get_attr(m_caca, i, j);
113        uint16_t fg = caca_attr_to_rgb12_fg(attr);
114        uint16_t bg = caca_attr_to_rgb12_bg(attr);
115        caca_set_color_argb(m_caca, fg, bg);
116        attr = caca_get_attr(m_caca, -1, -1);
117        caca_put_attr(m_caca, i, j, attr);
118    }
119    memcpy(colors, caca_get_canvas_attrs(m_caca),
120           m_cells * sizeof(uint32_t));
121    m_vbo2->Unlock();
122
123    uint32_t *chars = (uint32_t *)m_vbo3->Lock(0, 0);
124    memcpy(chars, caca_get_canvas_chars(m_caca),
125           m_cells * sizeof(uint32_t));
126    m_vbo3->Unlock();
127
128    m_fbo->Bind();
129    glViewport(0, 0, m_fbo_size.x, m_fbo_size.y);
130    glDisable(GL_DEPTH_TEST);
131    glEnable(GL_POINT_SPRITE);
132    //glEnable(GL_VERTEX_PROGRAM_POINT_SIZE);
133    glDisable(GL_POINT_SMOOTH);
134    glPointSize((float)max(m_font_size.x, m_font_size.y));
135    m_shader->Bind();
136    m_font->Bind();
137    m_shader->SetUniform(m_texture, 0);
138    m_shader->SetUniform(m_transform, xform);
139    m_vdecl->SetStream(m_vbo1, m_coord);
140    m_vdecl->SetStream(m_vbo2, m_color);
141    m_vdecl->SetStream(m_vbo3, m_char);
142    m_vdecl->Bind();
143    m_vdecl->DrawElements(MeshPrimitive::Points, 0, m_cells);
144    m_vdecl->Unbind();
145    m_font->Unbind();
146    m_shader->Unbind();
147    glDisable(GL_POINT_SPRITE);
148    m_fbo->Unbind();
149}
150
151void TextRender::Blit(ivec2 pos, ivec2 size)
152{
153    /* FIXME: this is ugly! But we will get rid of it when we
154     * do the Direct3D port, so don't worry too much. */
155    ShaderTexture t = m_fbo->GetTexture();
156    uint64_t const &x = *(uint64_t const *)&t;
157
158    glDisable(GL_BLEND);
159    glEnable(GL_TEXTURE_2D);
160    glBindTexture(GL_TEXTURE_2D, (int)x);
161    glColor3f(1.0f, 1.0f, 1.0f);
162
163    vec2 tc = (vec2)m_canvas_size * m_font_size / m_fbo_size;
164
165    glLoadIdentity();
166    glBegin(GL_QUADS);
167        glTexCoord2f(tc.x, tc.y);
168        glVertex2i(pos.x + size.x, pos.y);
169        glTexCoord2f(0.0f, tc.y);
170        glVertex2i(pos.x, pos.y);
171        glTexCoord2f(0.0f, 0.0f);
172        glVertex2i(pos.x, pos.y + size.y);
173        glTexCoord2f(tc.x, 0.0f);
174        glVertex2i(pos.x + size.x, pos.y + size.y);
175    glEnd();
176}
177
Note: See TracBrowser for help on using the repository browser.