aboutsummaryrefslogtreecommitdiff
path: root/src/debug.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/debug.c')
-rw-r--r--src/debug.c169
1 files changed, 169 insertions, 0 deletions
diff --git a/src/debug.c b/src/debug.c
new file mode 100644
index 0000000..dc06c51
--- /dev/null
+++ b/src/debug.c
@@ -0,0 +1,169 @@
+#include <SDL3/SDL_timer.h>
+
+#include "debug.h"
+#include "png.h"
+#include "sysroot.h"
+
+#define PW_FONT_WIDTH 128
+#define PW_FONT_HEIGHT 128
+
+#ifndef PW_FONT_SIZE
+#define PW_FONT_SIZE 4.
+#endif
+
+struct pw_debug *pw_debug_init(SDL_Renderer *renderer) {
+ if (!renderer) {
+ SDL_InvalidParamError("renderer");
+ return NULL;
+ }
+
+ SDL_Palette *palette = NULL;
+ SDL_Surface *surface = NULL;
+ union pw_png_pixel *image = NULL;
+ SDL_Texture *font = NULL;
+ struct pw_debug *debug = NULL;
+
+ SDL_Color colors[] = {
+ {0x00, 0x00, 0x00, 0x00},
+ {0xff, 0x00, 0xff, 0xff},
+ };
+ palette = SDL_CreatePalette(2);
+ if (!palette ||
+ !SDL_SetPaletteColors(palette, colors, 0, SDL_arraysize(colors))) {
+ goto error;
+ }
+
+ surface =
+ SDL_CreateSurface(PW_FONT_WIDTH, PW_FONT_HEIGHT, SDL_PIXELFORMAT_INDEX8);
+ if (!surface || !SDL_SetSurfacePalette(surface, palette)) {
+ goto error;
+ }
+ SDL_SetSurfaceColorKey(surface, true, 0);
+ SDL_DestroyPalette(palette);
+ palette = NULL;
+
+ image =
+ pw_png_load(font_png.data, font_png.size, PW_FONT_WIDTH, PW_FONT_HEIGHT);
+ if (!image) {
+ goto error;
+ }
+ union pw_png_pixel mask = {.ch = {0xff, 0xff, 0xff, 0x00}};
+ uint8_t *pixels = surface->pixels;
+ for (size_t i = 0; i < PW_FONT_WIDTH * PW_FONT_HEIGHT; ++i) {
+ pixels[i] = !!(image[i].raw & mask.raw);
+ }
+ SDL_free(image);
+ image = NULL;
+
+ font = SDL_CreateTextureFromSurface(renderer, surface);
+ if (!font || !SDL_SetTextureScaleMode(font, SDL_SCALEMODE_NEAREST)) {
+ goto error;
+ }
+ SDL_DestroySurface(surface);
+ surface = NULL;
+
+ debug = SDL_calloc(1, sizeof(*debug));
+ if (!debug) {
+ goto error;
+ }
+
+ debug->font = font;
+ return debug;
+
+error:
+ if (palette) {
+ SDL_DestroyPalette(palette);
+ }
+ if (surface) {
+ SDL_DestroySurface(surface);
+ }
+ if (image) {
+ SDL_free(image);
+ }
+ if (font) {
+ SDL_DestroyTexture(font);
+ }
+ if (debug) {
+ SDL_free(debug);
+ }
+ return NULL;
+}
+
+void pw_debug_free(struct pw_debug *debug) {
+ if (!debug) {
+ SDL_InvalidParamError("debug");
+ return;
+ }
+
+ SDL_DestroyTexture(debug->font);
+ SDL_free(debug);
+}
+
+bool pw_debug_iterate(struct pw_debug *debug) {
+ if (!debug) {
+ SDL_InvalidParamError("debug");
+ return false;
+ }
+
+ if (!debug->fps.clock) {
+ debug->fps.clock = SDL_GetTicks();
+ } else if (++debug->fps.count > 10) {
+ uint64_t count = debug->fps.count;
+ uint64_t start = debug->fps.clock;
+ debug->fps.clock = SDL_GetTicks();
+ debug->fps.count = 0;
+ debug->fps.sample =
+ 1000. * (double)count / (double)(debug->fps.clock - start);
+ }
+
+ return true;
+}
+
+static bool debug_print(struct pw_debug *debug, float x, float y,
+ char const *fmt, ...) {
+ SDL_Renderer *renderer = SDL_GetRendererFromTexture(debug->font);
+ if (!renderer) {
+ return false;
+ }
+ float sx, sy;
+ if (!SDL_GetRenderScale(renderer, &sx, &sy)) {
+ return false;
+ }
+ sx *= PW_FONT_SIZE;
+ sy *= PW_FONT_SIZE;
+
+ va_list ap;
+ char *text;
+ va_start(ap, fmt);
+ int n = SDL_vasprintf(&text, fmt, ap);
+ va_end(ap);
+
+ if (n < 0) {
+ return false;
+ }
+
+ bool ok = true;
+ SDL_FRect src = {0., 0., 4., 5.};
+ SDL_FRect dst = {x * sx, y * sy, src.w * sx, src.h * sy};
+ for (int i = 0; i < n; i++) {
+ uint8_t c = (uint8_t)text[i];
+ uint8_t i = c % 32;
+ uint8_t j = c / 32;
+ src.x = (float)(i * 4);
+ src.y = (float)(j * 8);
+ ok = ok && SDL_RenderTexture(renderer, debug->font, &src, &dst);
+ dst.x += dst.w;
+ }
+
+ SDL_free(text);
+ return true;
+}
+
+bool pw_debug_render(struct pw_debug *debug) {
+ if (!debug) {
+ SDL_InvalidParamError("debug");
+ return false;
+ }
+
+ return debug_print(debug, 2, 2, "FPS: %.2f", debug->fps.sample);
+}