Text rendering / Proper projection

Author Mitja Felicijan <mitja.felicijan@gmail.com> 2026-05-11 16:14:51 +0200
Committer Mitja Felicijan <mitja.felicijan@gmail.com> 2026-05-11 17:08:26 +0200
Commit 2c04d3a962ec4019f17ba9e570b309e69456a689 (patch)
-rw-r--r-- main.c 44
1 files changed, 40 insertions, 4 deletions
diff --git a/main.c b/main.c
...
12
    #include <GL/glut.h>
12
    #include <GL/glut.h>
13
#endif
13
#endif
14
  
14
  
  
15
typedef struct {
  
16
	float x, y, z;
  
17
} Vec2;
  
18
  
  
19
typedef struct {
  
20
	float x, y, z;
  
21
} Vec3;
  
22
  
  
23
typedef struct {
  
24
	float x, y, z, w;
  
25
} Vec4;
  
26
  
  
27
#define COLOR_RED     (Vec3){ .x = 1.0f, .y = 0.0f, .z = 0.0f }
  
28
#define COLOR_GREEN   (Vec3){ .x = 0.0f, .y = 1.0f, .z = 0.0f }
  
29
#define COLOR_BLUE    (Vec3){ .x = 0.0f, .y = 0.0f, .z = 1.0f }
  
30
#define COLOR_WHITE   (Vec3){ .x = 1.0f, .y = 1.0f, .z = 1.0f }
  
31
#define COLOR_BLACK   (Vec3){ .x = 0.0f, .y = 0.0f, .z = 0.0f }
  
32
#define COLOR_YELLOW  (Vec3){ .x = 1.0f, .y = 1.0f, .z = 0.0f }
  
33
#define COLOR_CYAN    (Vec3){ .x = 0.0f, .y = 1.0f, .z = 1.0f }
  
34
#define COLOR_MAGENTA (Vec3){ .x = 1.0f, .y = 0.0f, .z = 1.0f }
  
35
#define COLOR_ORANGE  (Vec3){ .x = 1.0f, .y = 0.5f, .z = 0.0f }
  
36
#define COLOR_PURPLE  (Vec3){ .x = 0.5f, .y = 0.0f, .z = 0.5f }
15
  
37
  
16
typedef struct {
38
typedef struct {
17
	int target_fps;
39
	int target_fps;
...
98
	free(data);
120
	free(data);
99
}
121
}
100
  
122
  
101
void render_text(float x, float y, void* font, const char* string) {
123
void draw_text(float x, float y, void* font, const char* string, Vec3 color) {
  
124
	glColor3f(color.x, color.y, color.z);
  
125
  
  
126
	glMatrixMode(GL_PROJECTION);
  
127
	glPushMatrix();
  
128
	glLoadIdentity();
  
129
	glOrtho(0, 1, 0, 1, -1, 1);
  
130
  
  
131
	glMatrixMode(GL_MODELVIEW);
  
132
	glPushMatrix();
  
133
	glLoadIdentity();
  
134
  
102
	glRasterPos2f(x, y);
135
	glRasterPos2f(x, y);
103
	for (const char* c = string; *c != '\0'; c++) {
136
	for (const char* c = string; *c != '\0'; c++) {
104
		glutBitmapCharacter(font, *c);
137
		glutBitmapCharacter(font, *c);
105
	}
138
	}
  
139
  
  
140
	glPopMatrix();
  
141
	glMatrixMode(GL_PROJECTION);
  
142
	glPopMatrix();
  
143
	glMatrixMode(GL_MODELVIEW);
106
}
144
}
107
  
145
  
108
void draw_cube(void) {
146
void draw_cube(void) {
...
194
	glPopMatrix();
232
	glPopMatrix();
195
  
233
  
196
	// Render UI Text
234
	// Render UI Text
197
	glLoadIdentity();
  
198
	glColor3f(0.0, 1.0, 0.0);
  
199
	char fps_text[32];
235
	char fps_text[32];
200
	snprintf(fps_text, sizeof fps_text, "FPS: %d", game.target_fps);
236
	snprintf(fps_text, sizeof fps_text, "FPS: %d", game.target_fps);
201
	render_text(0.05f, 0.93f, GLUT_BITMAP_HELVETICA_18, fps_text);
237
	draw_text(0.01f, 0.95f, GLUT_BITMAP_HELVETICA_18, fps_text, COLOR_GREEN);
202
  
238
  
203
	glutSwapBuffers();
239
	glutSwapBuffers();
204
}
240
}
...