summaryrefslogtreecommitdiff
path: root/platformer/src/render.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--platformer/src/render.c145
1 files changed, 139 insertions, 6 deletions
diff --git a/platformer/src/render.c b/platformer/src/render.c
index 381a018..127806b 100644
--- a/platformer/src/render.c
+++ b/platformer/src/render.c
@@ -3,8 +3,8 @@
* FILE: render.c
*
* PURPOSE: Draws the whole scene with modern OpenGL 3.3 core and as little
- * code as possible: a ground grid, the player as a box, and the
- * player's drop shadow.
+ * code as possible: a ground grid, the player as a box, the
+ * player's drop shadow, and an FPS counter in the top right.
*
* FITS IN: Implements render.h. Reads Camera (camera.h), Player (player.h)
* and the level (world_half_extent(), world_box_count()/world_box()
@@ -33,7 +33,7 @@
* 3. Shadow disc: a GL_TRIANGLE_FAN circle of radius 1 in the XZ plane
* (~24 segments).
*
- * - render_frame(cam, player, aspect):
+ * - render_frame(cam, player, aspect, fps):
* 1. glClearColor(SKY_COLOR_R/G/B), clear color + depth.
* 2. vp = camera_projection(cam, aspect) * camera_view(cam).
* 3. Ground: the cube scaled into a flat slab whose top is y = 0, drawn
@@ -61,6 +61,17 @@
* Bright color (e.g. red). Also draw a small second box as a "nose" on
* the front face (+Z in model space) so the facing direction is visible.
* That makes the turn rate tunable by eye.
+ * 8. FPS counter (skipped while fps <= 0, i.e. not measured yet): the text
+ * "<fps> FPS", right-aligned in the top-right corner, white with a
+ * dark drop shadow so it reads over both sky and ground. Glyphs come
+ * from a tiny built-in 3x5 pixel font (only the digits, F, P and S,
+ * written as "#" strings so you can see each letter in the source).
+ * Each lit font pixel is a quad in a small mesh that is rebuilt ONLY
+ * when the number changes, so the counter costs two draw calls a
+ * frame. It's drawn last with the depth test off, in screen space
+ * measured in window heights so the font pixels stay square at any
+ * window shape. WHY it exists: to watch the cost of later
+ * optimization work at a glance.
* Everything uses the one shader, changing only u_mvp and u_color.
*
* - render_shutdown: delete the VAOs, VBOs and the program.
@@ -69,13 +80,14 @@
* math3d.h helpers. There's no lighting: flat colors, plus the
* fixed per-face shading on platforms.
*
- * NOT IN SCOPE: Lighting, textures, models, text/HUD, anti-aliasing,
+ * NOT IN SCOPE: Lighting, textures, models, general text/HUD (only the FPS
+ * counter's characters exist), font files, anti-aliasing,
* interpolating between ticks (a README roadmap item).
*
* DONE WHEN: The scene shows a sky-colored background, a grid ground plane
* stretching to the world edge, a box with a visible nose that faces
* where it runs, and a shadow that stays on the ground under the
- * box during a jump.
+ * box during a jump. The top-right corner shows the current FPS.
* ============================================================
*/
@@ -84,6 +96,7 @@
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
#include "config.h"
#include "render.h"
@@ -96,6 +109,13 @@
#define NOSE_SIZE 0.3f /* m, edge length of the facing-direction "nose" box */
#define NOSE_HEIGHT 0.6f /* nose height as a fraction of PLAYER_HEIGHT */
#define GROUND_THICKNESS 1.0f /* m, the ground slab under the grid */
+#define HUD_PIXEL 0.006f /* size of one font pixel, as a fraction of the window height */
+#define HUD_MARGIN 0.02f /* gap between the FPS text and the window corner, in window heights */
+#define HUD_SHADOW_OFFSET 0.5f /* text shadow offset down and right, in font pixels */
+#define HUD_MAX_CHARS 16 /* longest text the FPS counter can hold */
+#define GLYPH_W 3 /* font pixels per glyph, across */
+#define GLYPH_H 5 /* font pixels per glyph, down */
+#define GLYPH_ADVANCE 4 /* glyph width plus a one-pixel gap */
/* Flat colors, RGBA. */
static const float COLOR_GROUND[4] = { 0.36f, 0.56f, 0.32f, 1.0f };
@@ -104,6 +124,8 @@ static const float COLOR_GRID_MAJOR[4] = { 0.62f, 0.82f, 0.55f, 1.0f };
static const float COLOR_SHADOW[4] = { 0.12f, 0.16f, 0.12f, 1.0f };
static const float COLOR_PLAYER[4] = { 0.85f, 0.15f, 0.15f, 1.0f };
static const float COLOR_NOSE[4] = { 1.00f, 0.85f, 0.20f, 1.0f };
+static const float COLOR_HUD_TEXT[4] = { 1.00f, 1.00f, 1.00f, 1.0f };
+static const float COLOR_HUD_SHADOW[4] = { 0.00f, 0.00f, 0.00f, 1.0f };
/* Platform colors, picked per platform by index so neighbors vary. */
static const float PLATFORM_PALETTE[][3] = {
@@ -120,6 +142,30 @@ static const float PLATFORM_PALETTE[][3] = {
static const float FACE_SHADE[6] = { 0.70f, 0.82f, 0.45f, 1.00f, 0.60f, 0.90f };
#define CUBE_FACE_VERTS 6
+/* A tiny pixel font holding only what the FPS counter spells. Each glyph is
+ * GLYPH_H rows, top row first, and "#" marks a lit pixel. */
+typedef struct {
+ char ch;
+ const char *rows[GLYPH_H];
+} Glyph;
+
+static const Glyph FONT[] = {
+ { '0', { "###", "#.#", "#.#", "#.#", "###" } },
+ { '1', { ".#.", "##.", ".#.", ".#.", "###" } },
+ { '2', { "###", "..#", "###", "#..", "###" } },
+ { '3', { "###", "..#", "###", "..#", "###" } },
+ { '4', { "#.#", "#.#", "###", "..#", "..#" } },
+ { '5', { "###", "#..", "###", "..#", "###" } },
+ { '6', { "###", "#..", "###", "#.#", "###" } },
+ { '7', { "###", "..#", "..#", "..#", "..#" } },
+ { '8', { "###", "#.#", "###", "#.#", "###" } },
+ { '9', { "###", "#.#", "###", "..#", "###" } },
+ { 'F', { "###", "#..", "##.", "#..", "#.." } },
+ { 'P', { "###", "#.#", "###", "#..", "#.." } },
+ { 'S', { ".##", "#..", ".#.", "..#", "##." } },
+};
+#define FONT_SIZE ((int)(sizeof FONT / sizeof FONT[0]))
+
/* A VAO + VBO of positions only (3 floats per vertex). */
typedef struct {
GLuint vao, vbo;
@@ -130,6 +176,8 @@ static GLuint s_program;
static GLint s_u_mvp, s_u_color;
static Mesh s_grid, s_cube, s_disc;
static GLsizei s_grid_minor_count; /* grid VBO holds minor lines first, then major */
+static Mesh s_hud_text; /* the FPS text, in font pixels; rebuilt when the number changes */
+static int s_hud_fps = -1; /* the number s_hud_text currently spells (-1 = none yet) */
static const char *VERTEX_SRC =
"#version 330 core\n"
@@ -391,6 +439,88 @@ static void draw_player(Mat4 vp, const Player *player)
draw_mesh(&s_cube, GL_TRIANGLES, mat4_mul(vp, nose), COLOR_NOSE);
}
+/* ---- FPS counter ------------------------------------------------------- */
+
+/* Returns the font's glyph for ch, or NULL (e.g. a space) to leave a gap. */
+static const Glyph *find_glyph(char ch)
+{
+ for (int i = 0; i < FONT_SIZE; i++)
+ if (FONT[i].ch == ch)
+ return &FONT[i];
+ return NULL;
+}
+
+/* Writes one font pixel (a unit square as two triangles, hanging down and to
+ * the right of its top-left corner x, y) at out[0..17]; returns the next slot. */
+static float *put_pixel(float *out, float x, float y)
+{
+ static const float CORNERS[6][2] = {
+ { 0, 0 }, { 1, 0 }, { 1, -1 },
+ { 0, 0 }, { 1, -1 }, { 0, -1 },
+ };
+ for (int i = 0; i < 6; i++) {
+ out[i * 3 + 0] = x + CORNERS[i][0];
+ out[i * 3 + 1] = y + CORNERS[i][1];
+ out[i * 3 + 2] = 0.0f;
+ }
+ return out + 18;
+}
+
+/* Rebuilds s_hud_text to spell `text`, measured in font pixels with the
+ * origin at the text's TOP-RIGHT corner, so it's right-aligned for free. */
+static void build_hud_text(const char *text)
+{
+ static float verts[HUD_MAX_CHARS * GLYPH_W * GLYPH_H * 6 * 3];
+ int len = (int)strlen(text);
+ float left = -(float)(len * GLYPH_ADVANCE - 1); /* no gap after the last glyph */
+ float *out = verts;
+
+ for (int i = 0; i < len && i < HUD_MAX_CHARS; i++) {
+ const Glyph *glyph = find_glyph(text[i]);
+ if (!glyph)
+ continue;
+ float x = left + (float)(i * GLYPH_ADVANCE);
+ for (int row = 0; row < GLYPH_H; row++)
+ for (int col = 0; col < GLYPH_W; col++)
+ if (glyph->rows[row][col] == '#')
+ out = put_pixel(out, x + (float)col, -(float)row);
+ }
+
+ s_hud_text.vertex_count = (GLsizei)((out - verts) / 3);
+ glBindBuffer(GL_ARRAY_BUFFER, s_hud_text.vbo);
+ glBufferData(GL_ARRAY_BUFFER, (GLsizeiptr)((size_t)(out - verts) * sizeof(float)), verts, GL_DYNAMIC_DRAW);
+}
+
+/* "<fps> FPS" in the top-right corner, for keeping an eye on performance. */
+static void draw_fps(int fps, float aspect)
+{
+ if (fps <= 0) /* not measured yet */
+ return;
+ if (fps != s_hud_fps) {
+ char text[HUD_MAX_CHARS + 1];
+ snprintf(text, sizeof text, "%d FPS", fps);
+ build_hud_text(text);
+ s_hud_fps = fps;
+ }
+
+ /* Screen space in window heights: x in [0, aspect], y in [0, 1] from the
+ * bottom left. Using the height as the unit keeps font pixels square. */
+ Mat4 screen = mat4_mul(mat4_translate(vec3_make(-1.0f, -1.0f, 0.0f)),
+ mat4_scale(vec3_make(2.0f / aspect, 2.0f, 1.0f)));
+ Vec3 corner = vec3_make(aspect - HUD_MARGIN, 1.0f - HUD_MARGIN, 0.0f);
+ Vec3 shadow = vec3_add(corner, vec3_make(HUD_SHADOW_OFFSET * HUD_PIXEL,
+ -HUD_SHADOW_OFFSET * HUD_PIXEL, 0.0f));
+ Mat4 pixel = mat4_scale(vec3_make(HUD_PIXEL, HUD_PIXEL, 1.0f));
+
+ /* Drawn last with no depth test, so the scene can never cover it. */
+ glDisable(GL_DEPTH_TEST);
+ draw_mesh(&s_hud_text, GL_TRIANGLES, mat4_mul(screen, mat4_mul(mat4_translate(shadow), pixel)),
+ COLOR_HUD_SHADOW);
+ draw_mesh(&s_hud_text, GL_TRIANGLES, mat4_mul(screen, mat4_mul(mat4_translate(corner), pixel)),
+ COLOR_HUD_TEXT);
+ glEnable(GL_DEPTH_TEST);
+}
+
/* ---- Public interface -------------------------------------------------- */
bool render_init(void)
@@ -407,10 +537,11 @@ bool render_init(void)
}
build_cube();
build_disc();
+ s_hud_text = make_mesh(NULL, 0); /* filled in by build_hud_text() once FPS is known */
return true;
}
-void render_frame(const Camera *cam, const Player *player, float aspect)
+void render_frame(const Camera *cam, const Player *player, float aspect, int fps)
{
glClearColor(SKY_COLOR_R, SKY_COLOR_G, SKY_COLOR_B, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
@@ -423,6 +554,7 @@ void render_frame(const Camera *cam, const Player *player, float aspect)
draw_platforms(vp);
draw_shadow(vp, player); /* before the player, so the player is drawn over it */
draw_player(vp, player);
+ draw_fps(fps, aspect); /* last: it sits on top of everything */
glBindVertexArray(0);
}
@@ -432,5 +564,6 @@ void render_shutdown(void)
delete_mesh(&s_grid);
delete_mesh(&s_cube);
delete_mesh(&s_disc);
+ delete_mesh(&s_hud_text);
glDeleteProgram(s_program);
}