Repository navigation
Expand file tree
/
Copy pathtext.cpp
More file actions
157 lines (126 loc) · 3.6 KB
/
Copy pathtext.cpp
File metadata and controls
157 lines (126 loc) · 3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "text.h"
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include "../grinliz/glutil.h"
#include "../grinliz/glrectangle.h"
#include "gpustatemanager.h"
#include "shadermanager.h"
using namespace grinliz;
UFOText* UFOText::instance = 0;
void UFOText::Create(Texture* texture)
{
instance = new UFOText(texture);
}
void UFOText::Destroy()
{
delete instance;
instance = 0;
}
UFOText::UFOText(Texture* texture )
{
this->texture = texture;
vbo = new GPUVertexBuffer( 0, VBO_MEM );
ShaderManager::Instance()->AddDeviceLossHandler( this );
}
UFOText::~UFOText()
{
delete vbo;
ShaderManager::Instance()->RemoveDeviceLossHandler( this );
}
void UFOText::DeviceLoss()
{
delete vbo;
vbo = new GPUVertexBuffer( 0, VBO_MEM );
}
void UFOText::TextOut( const char* str, int _x, int _y, int _h, int* w )
{
// bool rendering = true;
float x = (float)_x;
float y = (float)_y;
float h = (float)_h;
float width = (float)h * 0.75f;
float advance = (float)h * 0.50f;
if ( w ) {
*w = int(float(strlen(str)) * width);
return;
}
static const float ROWS = 8.0f;
static const float COLS = 16.0f;
while( *str )
{
int index = *str - 32;
int r = index / 16;
int c = index - r * 16;
if (r <0 || r >= 8) {
r = c = 0;
}
GLASSERT(c >= 0 && c < 16);
GLASSERT(r >= 0 && r < 8);
float tx0 = float(c) / COLS;
float tx1 = tx0 + 1.0f / COLS;
float ty0 = 1.0f - float(r) / ROWS;
float ty1 = ty0 - 1.0f / ROWS;
PTVertex2* vBuf = quadBuf.PushArr(4);
vBuf[0].tex.Set( tx0, ty0 );
vBuf[1].tex.Set( tx0, ty1 );
vBuf[2].tex.Set( tx1, ty1 );
vBuf[3].tex.Set( tx1, ty0 );
vBuf[0].pos.Set(x, y);
vBuf[1].pos.Set(x, y + h);
vBuf[2].pos.Set(x + width, y + h);
vBuf[3].pos.Set(x + width, y);
x += advance;
++str;
}
}
void UFOText::FinalDraw()
{
int n = quadBuf.Size();
if ( quadBuf.Size() * sizeof(PTVertex2) > VBO_MEM ) {
n = VBO_MEM / sizeof(PTVertex2);
}
vbo->Upload( quadBuf.Mem(), n*sizeof(PTVertex2), 0 );
CompositingShader shader( BLEND_NORMAL );
PTVertex2 v;
GPUStream stream( v );
GPUStreamData data;
data.texture0 = texture;
data.vertexBuffer = vbo->ID();
GPUDevice::Instance()->DrawQuads( shader, stream, data, n/4 );
quadBuf.Clear();
}
void UFOText::Draw( int x, int y, const char* format, ... )
{
va_list va;
const int size = 1024;
char buffer[size];
//
// format and output the message..
//
va_start( va, format );
#ifdef _MSC_VER
vsnprintf_s( buffer, size, _TRUNCATE, format, va );
#else
// Reading the spec, the size does seem correct. The man pages
// say it will aways be null terminated (whereas the strcpy is not.)
// Pretty nervous about the implementation, so force a null after.
vsnprintf( buffer, size, format, va );
buffer[size-1] = 0;
#endif
va_end( va );
TextOut( buffer, x, y, 16, 0 );
}