-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathhello_text_ttf.cpp
More file actions
349 lines (296 loc) · 12.8 KB
/
Copy pathhello_text_ttf.cpp
File metadata and controls
349 lines (296 loc) · 12.8 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
//
// Emscripten/SDL2/OpenGLES2 sample that displays TrueType text by loading a font and building a string texture
//
// Setup:
// Install emscripten: http://kripken.github.io/emscripten-site/docs/getting_started/downloads.html
//
// Build:
// emcc -std=c++11 hello_text_ttf.cpp events.cpp camera.cpp -s USE_SDL=2 -s USE_SDL_TTF=2 -s FULL_ES2=1 -s WASM=0 --preload-file media/LiberationSansBold.ttf -o hello_text_ttf.html
//
// Run:
// emrun hello_text_ttf.html
//
// Result:
// A TTF text quad and colorful triangle. Left mouse pans, mouse wheel zooms in/out.
//
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#endif
#include <SDL.h>
#include <SDL_ttf.h>
#include <SDL_opengles2.h>
#include "events.h"
// Geometry
GLuint triangleVbo = 0;
GLuint quadVbo = 0;
// Texture
GLuint textureObj = 0;
// Text
const char* cFontName = "media/LiberationSansBold.ttf";
const int cFontPointSize = 64;
const char* message = "Hello Text";
// Shader vars
const GLint positionAttrib = 0;
GLint shaderPan, shaderZoom, shaderAspect, shaderViewport, shaderTextSize, shaderTexSize;
GLfloat textSize[2] = {0.0f, 0.0f}, texSize[2] = {0.0f, 0.0f};
// Text quad vertex & fragment shaders
GLuint quadShaderProgram = 0;
const GLchar* quadVertexSource =
"attribute vec4 position; \n"
"varying vec2 texCoord; \n"
"uniform vec2 viewport; \n"
"uniform vec2 textSize; \n"
"uniform vec2 texSize; \n"
"void main() \n"
"{ \n"
" gl_Position = vec4(position.xyz, 1.0); \n"
" gl_Position.x *= textSize.x; \n"
" gl_Position.y *= textSize.y; \n"
" \n"
" // Translate to lower left viewport \n"
" gl_Position.x -= viewport.x / 2.0; \n"
" gl_Position.y -= viewport.y / 2.0; \n"
" \n"
" // Ortho projection \n"
" gl_Position.x += 1.0; \n"
" gl_Position.x *= 2.0 / viewport.x; \n"
" gl_Position.y += 1.0; \n"
" gl_Position.y *= 2.0 / viewport.y; \n"
" \n"
" // Text subrectangle from overall texture \n"
" texCoord.x = position.x * textSize.x / texSize.x; \n"
" texCoord.y = -position.y * textSize.y / texSize.y; \n"
"} \n";
const GLchar* quadFragmentSource =
"precision mediump float; \n"
"varying vec2 texCoord; \n"
"uniform sampler2D texSampler; \n"
"void main() \n"
"{ \n"
" gl_FragColor = texture2D(texSampler, texCoord); \n"
"} \n";
// Colorful triangle vertex & fragment shaders
GLuint triShaderProgram = 0;
const GLchar* triVertexSource =
"uniform vec2 pan; \n"
"uniform float zoom; \n"
"uniform float aspect; \n"
"attribute vec4 position; \n"
"varying vec3 color; \n"
"void main() \n"
"{ \n"
" gl_Position = vec4(position.xyz, 1.0); \n"
" gl_Position.xy += pan; \n"
" gl_Position.xy *= zoom; \n"
" gl_Position.y *= aspect; \n"
" color = gl_Position.xyz + vec3(0.5); \n"
"} \n";
const GLchar* triFragmentSource =
"precision mediump float; \n"
"varying vec3 color; \n"
"void main() \n"
"{ \n"
" gl_FragColor = vec4 ( color, 1.0 ); \n"
"} \n";
int nextPowerOfTwo(int val)
{
int power = 1;
while (power < val)
power *= 2;
return power;
}
void updateShader(EventHandler& eventHandler)
{
Camera& camera = eventHandler.camera();
glUseProgram(quadShaderProgram);
glUniform2fv(shaderViewport, 1, camera.viewport());
glUniform2fv(shaderTextSize, 1, textSize);
glUniform2fv(shaderTexSize, 1, texSize);
glUseProgram(triShaderProgram);
glUniform2fv(shaderPan, 1, camera.pan());
glUniform1f(shaderZoom, camera.zoom());
glUniform1f(shaderAspect, camera.aspect());
}
GLuint initShader(const GLchar* vertexSource, const GLchar* fragmentSource)
{
// Create and compile vertex shader
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexSource, NULL);
glCompileShader(vertexShader);
// Create and compile fragment shader
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentSource, NULL);
glCompileShader(fragmentShader);
// Link vertex and fragment shader into shader program
GLuint shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glBindAttribLocation(shaderProgram, positionAttrib, "position");
glEnableVertexAttribArray(positionAttrib);
glLinkProgram(shaderProgram);
return shaderProgram;
}
void initShaders(EventHandler& eventHandler)
{
// Compile & link shaders
quadShaderProgram = initShader(quadVertexSource, quadFragmentSource);
triShaderProgram = initShader(triVertexSource, triFragmentSource);
// Get shader variables and initalize them
shaderViewport = glGetUniformLocation(quadShaderProgram, "viewport");
shaderTextSize = glGetUniformLocation(quadShaderProgram, "textSize");
shaderTexSize = glGetUniformLocation(quadShaderProgram, "texSize");
shaderPan = glGetUniformLocation(triShaderProgram, "pan");
shaderZoom = glGetUniformLocation(triShaderProgram, "zoom");
shaderAspect = glGetUniformLocation(triShaderProgram, "aspect");
updateShader(eventHandler);
}
void initGeometry()
{
// Create vertex buffer objects and copy vertex data into them
glGenBuffers(1, &quadVbo);
glBindBuffer(GL_ARRAY_BUFFER, quadVbo);
GLfloat quadVertices[] =
{
0.0f, 1.0f, 0.0f,
1.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f,
1.0f, 0.0f, 0.0f
};
glBufferData(GL_ARRAY_BUFFER, sizeof(quadVertices), quadVertices, GL_STATIC_DRAW);
glGenBuffers(1, &triangleVbo);
glBindBuffer(GL_ARRAY_BUFFER, triangleVbo);
GLfloat triangleVertices[] =
{
0.0f, 0.5f, 0.0f,
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f
};
glBufferData(GL_ARRAY_BUFFER, sizeof(triangleVertices), triangleVertices, GL_STATIC_DRAW);
}
void debugPrintSurface(SDL_Surface* surface, const char* name, bool dumpPixels)
{
printf ("%s dimensions %dx%d, %d bits per pixel\n", name, surface->w, surface->h, surface->format->BitsPerPixel);
if (dumpPixels)
{
for (int i = 0; i < surface->w * surface->h; ++i)
printf("%x ", ((unsigned int*)surface->pixels)[i]);
printf("\n");
}
}
void initTextTexture(EventHandler& eventHandler)
{
TTF_Init();
// Load the font
TTF_Font *font = TTF_OpenFont(cFontName, cFontPointSize);
if (font)
{
// Render text to surface
SDL_Color foregroundColor = {255,255,255,255};
SDL_Surface* textImage8Bit = TTF_RenderText_Solid(font, message, foregroundColor);
if (textImage8Bit)
{
// Convert surface from 8 to 32 bit for GL
SDL_Surface* textImage = SDL_ConvertSurfaceFormat(textImage8Bit, SDL_PIXELFORMAT_RGBA8888, 0);
debugPrintSurface(textImage, "textImage", false);
// Create power of 2 dimensioned texture for GL with 1 texel border, clear it, and copy text image into it
SDL_Surface* texture = SDL_CreateRGBSurface(0, nextPowerOfTwo(textImage->w + 2), nextPowerOfTwo(textImage->h + 2),
textImage->format->BitsPerPixel, 0, 0, 0, 0);
memset(texture->pixels, 0x0, texture->w * texture->h * texture->format->BytesPerPixel);
SDL_Rect destRect = {1, texture->h - textImage->h - 1, textImage->w + 1, texture->h - 1};
SDL_SetSurfaceBlendMode(textImage, SDL_BLENDMODE_NONE);
SDL_BlitSurface(textImage, NULL, texture, &destRect);
// Emscripten/SDL bug? SDL_BlitSurface should copy source alpha when source surface is set to
// SDL_BLENDMODE_NONE, however this is not happening, so fix it up here
unsigned int* pixels = (unsigned int*)texture->pixels;
for (int i = 0; i < texture->w*texture->h; ++i)
{
if (pixels[i] != 0)
pixels[i] |= 0xff000000;
else
pixels[i] = 0x80808080;
}
debugPrintSurface(texture, "texture", false);
// Determine GL texture format
GLint format = -1;
if (texture->format->BitsPerPixel == 24)
format = GL_RGB;
else if (texture->format->BitsPerPixel == 32)
format = GL_RGBA;
if (format != -1)
{
// Enable blending for texture alpha component
glEnable( GL_BLEND );
glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
// Generate a GL texture object
glGenTextures(1, &textureObj);
// Bind GL texture
glBindTexture(GL_TEXTURE_2D, textureObj);
// Set the GL texture's wrapping and stretching properties
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
// Copy SDL surface image to GL texture
glTexImage2D(GL_TEXTURE_2D, 0, format,
texture->w, texture->h,
0, format, GL_UNSIGNED_BYTE, texture->pixels);
// Update quad shader
texSize[0] = (GLfloat)texture->w;
texSize[1] = (GLfloat)texture->h;
textSize[0] = (GLfloat)textImage->w + 2;
textSize[1] = (GLfloat)textImage->h + 2;
updateShader(eventHandler);
}
SDL_FreeSurface (textImage);
SDL_FreeSurface (texture);
}
TTF_CloseFont(font);
}
else
printf("Failed to load font %s, due to %s\n", cFontName, TTF_GetError());
}
void redraw(EventHandler& eventHandler)
{
// Clear screen
glClear(GL_COLOR_BUFFER_BIT);
// Draw the triangle VBO with a colorful shader
glUseProgram(triShaderProgram);
glBindBuffer(GL_ARRAY_BUFFER, triangleVbo);
glVertexAttribPointer(positionAttrib, 3, GL_FLOAT, GL_FALSE, 0, 0);
glDrawArrays(GL_TRIANGLES, 0, 3);
// Draw the quad VBO with a text texture shader
glUseProgram(quadShaderProgram);
glBindBuffer(GL_ARRAY_BUFFER, quadVbo);
glVertexAttribPointer(positionAttrib, 3, GL_FLOAT, GL_FALSE, 0, 0);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
// Swap front/back framebuffers
eventHandler.swapWindow();
}
void mainLoop(void* mainLoopArg)
{
EventHandler& eventHandler = *((EventHandler*)mainLoopArg);
eventHandler.processEvents();
// Update shader if camera changed
if (eventHandler.camera().updated())
updateShader(eventHandler);
redraw(eventHandler);
}
int main(int argc, char** argv)
{
EventHandler eventHandler("Hello TTF Text");
// Initialize graphics
initShaders(eventHandler);
initGeometry();
initTextTexture(eventHandler);
// Start the main loop
void* mainLoopArg = &eventHandler;
#ifdef __EMSCRIPTEN__
int fps = 0; // Use browser's requestAnimationFrame
emscripten_set_main_loop_arg(mainLoop, mainLoopArg, fps, true);
#else
while(true)
mainLoop(mainLoopArg);
#endif
glDeleteTextures(1, &textureObj);
return 0;
}