Skip to content

Commit a9e46b7

Browse files
committed
✨ Add simple text rendering via FontStash.
Uses BastiaanOlij fork of FontStash for OpenGL 3.0 rendering. memononen/fontstash#23 Adds a simple frametime counter in the topleft.
1 parent 7f06f98 commit a9e46b7

File tree

5 files changed

+4220
-6
lines changed

5 files changed

+4220
-6
lines changed

assets/DroidSerif-Regular.ttf

159 KB
Binary file not shown.

src/main.c

Lines changed: 61 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@
1717
#define MATH_3D_IMPLEMENTATION
1818
#include "third-party/math_3d.h"
1919

20+
#define FONTSTASH_IMPLEMENTATION
21+
#include "third-party/fontstash.h"
22+
23+
#define GLFONTSTASH_IMPLEMENTATION
24+
#include "third-party/gl3fontstash.h"
25+
2026
#define WINDOW_NAME "blocks"
2127
#define WINDOW_WIDTH 1024
2228
#define WINDOW_HEIGHT 768
@@ -97,6 +103,12 @@ static camera Camera;
97103
static GLuint Texture;
98104
static GLuint TextureID;
99105

106+
// FONTS
107+
static GLfloat mat[16];
108+
static FONScontext *fontcontext;
109+
static int DroidRegular = 0;
110+
static float lineHeight = 0.0f;
111+
100112
int render_init() {
101113
// Ensure we can capture the escape key being pressed below
102114
glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
@@ -139,23 +151,43 @@ int render_init() {
139151
// Set Projection matrix to perspective, with 1 rad FoV
140152
Projection = m4_perspective(80.0f, width / height, 0.1f, 200.0f);
141153

142-
// Enable face culling
143-
glEnable(GL_CULL_FACE);
154+
// Set text rendering matrix
155+
memset(mat, 0, 16 * sizeof(GLfloat));
156+
mat[0] = 2.0 / width;
157+
mat[5] = -2.0 / height;
158+
mat[10] = 2.0;
159+
mat[12] = -1.0;
160+
mat[13] = 1.0;
161+
mat[14] = -1.0;
162+
mat[15] = 1.0;
144163

145-
// Enable depth test
146-
glEnable(GL_DEPTH_TEST);
164+
glEnable(GL_BLEND);
165+
glBlendEquation(GL_FUNC_ADD);
166+
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
147167

148168
// Accept fragment if it closer to the camera than the former one
149169
glDepthFunc(GL_LESS);
150170

171+
// Add fonts
172+
fontcontext = gl3fonsCreate(1024, 1024, FONS_ZERO_TOPLEFT);
173+
DroidRegular = fonsAddFont(fontcontext, "sans", "assets/DroidSerif-Regular.ttf");
174+
175+
fonsSetColor(fontcontext, gl3fonsRGBA(255,255,255,255)); // white
176+
fonsSetSize(fontcontext, 36.0f); // 16 point font
177+
fonsSetAlign(fontcontext, FONS_ALIGN_LEFT | FONS_ALIGN_TOP); // left/top aligned
178+
fonsVertMetrics(fontcontext, NULL, NULL, &lineHeight);
179+
151180
check_opengl_error();
152181

153182
return 0;
154183
}
155184

156185
static unsigned char BLOCKS_DEBUG = 0;
157186

158-
void render() {
187+
void render(double starttime) {
188+
// Use our shaders
189+
glUseProgram(program);
190+
159191
// Clear the screen
160192
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
161193

@@ -165,6 +197,12 @@ void render() {
165197
GLint projection_view = glGetUniformLocation(program, "ProjectionView");
166198
glUniformMatrix4fv(projection_view, 1, GL_FALSE, (GLfloat *) &ProjectionView);
167199

200+
// Enable face culling
201+
glEnable(GL_CULL_FACE);
202+
203+
// Enable depth test
204+
glEnable(GL_DEPTH_TEST);
205+
168206
// Add timer
169207
glUniform1f(glGetUniformLocation(program, "timer"), glfwGetTime());
170208

@@ -182,6 +220,21 @@ void render() {
182220
render_chunk(program, Chunks[i]);
183221
}
184222
}
223+
// Enable face culling
224+
glDisable(GL_CULL_FACE);
225+
226+
// Enable depth test
227+
glDisable(GL_DEPTH_TEST);
228+
229+
// Render text
230+
gl3fonsProjection(fontcontext, mat);
231+
232+
double endtime = glfwGetTime();
233+
char performance[256];
234+
sprintf(performance, "frametime: %2.4fms", (endtime - starttime) * 1000);
235+
236+
float dx = 10.0, dy = 10.0f;
237+
fonsDrawText(fontcontext, dx, dy, performance, NULL);
185238

186239
// Swap buffers
187240
glfwSwapBuffers(window);
@@ -262,7 +315,9 @@ int main(void) {
262315
ongoingTime += deltaTime;
263316
}
264317

265-
render();
318+
double starttime = glfwGetTime();
319+
render(starttime);
320+
266321
glfwPollEvents();
267322
}
268323

0 commit comments

Comments
 (0)