1 | // Copyright (C) 2016-2016 ChaosForge Ltd
|
---|
2 | // http://chaosforge.org/
|
---|
3 | //
|
---|
4 | // This file is part of Nova libraries.
|
---|
5 | // For conditions of distribution and use, see copying.txt file in root folder.
|
---|
6 |
|
---|
7 | #include "nv/gfx/gfx_terminal.hh"
|
---|
8 |
|
---|
9 | #include "nv/core/logging.hh"
|
---|
10 |
|
---|
11 | using namespace nv;
|
---|
12 |
|
---|
13 | static const char *nv_gfx_terminal_vs = R"(
|
---|
14 | #version 330
|
---|
15 | in vec2 nv_position;
|
---|
16 | in vec2 nv_texcoord;
|
---|
17 | out vec2 v_texcoord;
|
---|
18 | void main(void)
|
---|
19 | {
|
---|
20 | gl_Position = vec4(nv_position, 0.0, 1.0);
|
---|
21 | v_texcoord = nv_texcoord;
|
---|
22 | };
|
---|
23 | )";
|
---|
24 | static const char *nv_gfx_terminal_fs = R"(
|
---|
25 | #version 330
|
---|
26 | in vec2 v_texcoord;
|
---|
27 | out vec4 o_frag_color;
|
---|
28 |
|
---|
29 | uniform vec2 term_size;
|
---|
30 | uniform vec2 dev_size;
|
---|
31 | //uniform vec2 gylph_size;
|
---|
32 | uniform sampler2D nv_t_diffuse;
|
---|
33 |
|
---|
34 | struct term_data
|
---|
35 | {
|
---|
36 | uint fg;
|
---|
37 | uint bg;
|
---|
38 | uint gylph;
|
---|
39 | uint pad;
|
---|
40 | };
|
---|
41 |
|
---|
42 | layout(std140) uniform terminal_block
|
---|
43 | {
|
---|
44 | term_data term[80*50];
|
---|
45 | };
|
---|
46 |
|
---|
47 | void main(void)
|
---|
48 | {
|
---|
49 | vec2 coord = v_texcoord * term_size;
|
---|
50 | ivec2 icoord = ivec2( coord );
|
---|
51 | vec2 tc = coord - vec2( icoord );
|
---|
52 | tc.y = 1.0f - tc.y;
|
---|
53 |
|
---|
54 | coord.y = term_size.y - coord.y;
|
---|
55 |
|
---|
56 | int index = int( coord.x ) + int( term_size.x ) * int( coord.y );
|
---|
57 | uint fg = term[ index ].fg;
|
---|
58 | int gylph = int( term[ index ].gylph );
|
---|
59 | ivec2 gxy = ivec2( gylph % 16, gylph / 16 );
|
---|
60 | vec2 gpos = vec2( ( float(gxy.x) + tc.x ) / 16.0f, ( float(gxy.y) + tc.y ) / 16.0f );
|
---|
61 | vec4 tt = texture( nv_t_diffuse, gpos );
|
---|
62 |
|
---|
63 |
|
---|
64 | vec4 color = vec4(
|
---|
65 | float( ( fg & uint(0x00FF0000) ) >> 16 ) / 255.0f,
|
---|
66 | float( ( fg & uint(0x0000FF00) ) >> 8 ) / 255.0f,
|
---|
67 | float( fg & uint(0x000000FF) ) / 255.0f,
|
---|
68 | 1.0 //fg & uint(0xFF000000)
|
---|
69 | );
|
---|
70 | o_frag_color = vec4( color.xyz * tt.xyz + vec3( 0.0, 0.2, 0.0 ) * ( 1.0 - tt.xyz ), 1.0 * tt.x );
|
---|
71 | // o_frag_color = vec4( fg, 0.0, 0.0, 1.0) ;
|
---|
72 | }
|
---|
73 | )";
|
---|
74 |
|
---|
75 | struct gfx_terminal_uniform_block
|
---|
76 | {
|
---|
77 | uint32 fgcolor = 0;
|
---|
78 | uint32 bgcolor = 0;
|
---|
79 | uint32 gylph = 0;
|
---|
80 | uint32 pad;
|
---|
81 |
|
---|
82 | void set( term_color fg, term_color bg, char ch )
|
---|
83 | {
|
---|
84 | fgcolor = fg.get_argb32();
|
---|
85 | bgcolor = bg.get_argb32();
|
---|
86 | gylph = uint32( uint8(ch) );
|
---|
87 | }
|
---|
88 | };
|
---|
89 |
|
---|
90 | struct nv::gfx_terminal_data
|
---|
91 | {
|
---|
92 | gfx_terminal_uniform_block* data;
|
---|
93 | dimension size;
|
---|
94 | buffer buffer;
|
---|
95 |
|
---|
96 | gfx_terminal_data( dimension dim )
|
---|
97 | {
|
---|
98 | size = dim;
|
---|
99 | data = new gfx_terminal_uniform_block[dim.x * dim.y];
|
---|
100 | }
|
---|
101 |
|
---|
102 | gfx_terminal_uniform_block& operator[]( position p )
|
---|
103 | {
|
---|
104 | int index = size.x * p.y + p.x;
|
---|
105 | return data[index];
|
---|
106 | }
|
---|
107 |
|
---|
108 | ~gfx_terminal_data()
|
---|
109 | {
|
---|
110 | delete[] data;
|
---|
111 | }
|
---|
112 | };
|
---|
113 |
|
---|
114 | gfx_terminal::gfx_terminal( context* ctx, texture t, dimension tsize, dimension /*psize*/ )
|
---|
115 | : terminal( tsize ), m_context( ctx )
|
---|
116 | {
|
---|
117 | m_data = new gfx_terminal_data( tsize );
|
---|
118 |
|
---|
119 | struct qvtx
|
---|
120 | {
|
---|
121 | vec2 position;
|
---|
122 | vec2 texcoord;
|
---|
123 | };
|
---|
124 |
|
---|
125 | qvtx quad[] = {
|
---|
126 | qvtx{ vec2( -1.0f,-1.0f ), vec2( 0.0f, 0.0f ) },
|
---|
127 | qvtx{ vec2( 1.0f,-1.0f ), vec2( 1.0f, 0.0f ) },
|
---|
128 | qvtx{ vec2( 1.0f,1.0f ), vec2( 1.0f, 1.0f ) },
|
---|
129 | qvtx{ vec2( 1.0f,1.0f ), vec2( 1.0f, 1.0f ) },
|
---|
130 | qvtx{ vec2( -1.0f,1.0f ), vec2( 0.0f, 1.0f ) },
|
---|
131 | qvtx{ vec2( -1.0f,-1.0f ), vec2( 0.0f, 0.0f ) },
|
---|
132 | };
|
---|
133 | m_dc.va = m_context->create_vertex_array( quad, 6, nv::STATIC_DRAW );
|
---|
134 | m_dc.va_count = 6;
|
---|
135 | m_dc.image = t;
|
---|
136 |
|
---|
137 | m_dc.p = m_context->create_program( nv_gfx_terminal_vs, nv_gfx_terminal_fs );
|
---|
138 |
|
---|
139 | m_data->buffer = m_context->create_buffer( nv::UNIFORM_BUFFER, nv::STREAM_DRAW, tsize.x * tsize.y * sizeof( gfx_terminal_uniform_block ), m_data->data );
|
---|
140 | m_context->bind( m_data->buffer, 7 );
|
---|
141 | m_context->get_device()->set_opt_uniform( m_dc.p, "term_size", vec2( tsize ) );
|
---|
142 | // m_context->get_device()->set_opt_uniform( m_dc.p, "dev_size", vec2( psize ) );
|
---|
143 | m_context->get_device()->set_opt_uniform( m_dc.p, "gylph_size", vec2( 8,8 ) );
|
---|
144 | m_context->get_device()->set_opt_uniform( m_dc.p, "nv_t_diffuse", int( nv::TEX_DIFFUSE ) );
|
---|
145 | m_context->get_device()->bind_block( m_dc.p, "terminal_block", 7 );
|
---|
146 | m_update_needed = false;
|
---|
147 | }
|
---|
148 |
|
---|
149 | void gfx_terminal::update()
|
---|
150 | {
|
---|
151 | if ( m_update_needed )
|
---|
152 | {
|
---|
153 | m_context->bind( m_data->buffer, 7 );
|
---|
154 | m_context->update( m_data->buffer, m_data->data, 0, m_data->size.x * m_data->size.y * sizeof( gfx_terminal_uniform_block ) );
|
---|
155 | m_update_needed = false;
|
---|
156 | }
|
---|
157 | }
|
---|
158 |
|
---|
159 | void gfx_terminal::print( position p, term_color fgcolor, term_color bgcolor, char ch )
|
---|
160 | {
|
---|
161 | m_update_needed = true;
|
---|
162 | ( *m_data )[position( p.x - 1, p.y - 1)].set( fgcolor, bgcolor, ch );
|
---|
163 | }
|
---|
164 |
|
---|
165 | void gfx_terminal::clear( rectangle r, term_color fgcolor, term_color bgcolor )
|
---|
166 | {
|
---|
167 | m_update_needed = true;
|
---|
168 | for ( int x = r.ul.x - 1; x <= r.lr.x - 1; ++x )
|
---|
169 | for ( int y = r.ul.y - 1; y <= r.lr.y - 1; ++y )
|
---|
170 | (*m_data)[ position( x, y ) ].set( fgcolor, bgcolor, ' ' );
|
---|
171 | }
|
---|
172 |
|
---|
173 | void gfx_terminal::clear()
|
---|
174 | {
|
---|
175 | m_update_needed = true;
|
---|
176 | for ( int i = 0; i < m_data->size.x * m_data->size.y; ++i )
|
---|
177 | m_data->data[i].set( term_color::LIGHTGRAY, term_color::TRANSPARENT, ' ' );
|
---|
178 | }
|
---|
179 |
|
---|
180 | bool gfx_terminal::poll( io_event & /*kevent*/ )
|
---|
181 | {
|
---|
182 | return false;
|
---|
183 | }
|
---|
184 |
|
---|
185 | void gfx_terminal::set_cursor( position /*p*/ )
|
---|
186 | {
|
---|
187 | }
|
---|
188 |
|
---|
189 | void gfx_terminal::show_cursor()
|
---|
190 | {
|
---|
191 | }
|
---|
192 |
|
---|
193 | void gfx_terminal::hide_cursor()
|
---|
194 | {
|
---|
195 | }
|
---|
196 |
|
---|
197 | gfx_terminal::~gfx_terminal()
|
---|
198 | {
|
---|
199 | delete m_data;
|
---|
200 | }
|
---|
201 |
|
---|