aboutsummaryrefslogtreecommitdiff
path: root/src/debug.c
blob: b87aa318e488a3aea2732263358ddc56114b2554 (plain)
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
#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 ci = c % 32;
    uint8_t cj = c / 32;
    src.x = (float)(ci * 4);
    src.y = (float)(cj * 8);
    ok = ok && SDL_RenderTexture(renderer, debug->font, &src, &dst);
    dst.x += dst.w;
  }

  SDL_free(text);
  return ok;
}

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);
}