~vladh/peony

peony/src/debug_ui.cpp -rw-r--r-- 10.3 KiB
7fc9922bVlad-Stefan Harbuz changes shaders namespace into class a month ago
                                                                                
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/*
  Peony Game Engine
  Copyright (C) 2020 Vlad-Stefan Harbuz <vlad@vladh.net>
  All rights reserved.
*/

#include "debug_ui.hpp"
#include "entities.hpp"
#include "engine.hpp"
#include "intrinsics.hpp"


namespace debug_ui {
  pny_internal void get_entity_text_representation(
    char *text,
    EngineState *engine_state,
    Entity *entity,
    uint8 depth
  ) {
    EntityHandle handle = entity->handle;
    SpatialComponent *spatial_component =
      engine_state->spatial_component_set.components[handle];

    // Children will be drawn under their parents.
    if (
      depth == 0 &&
      spatial::is_spatial_component_valid(spatial_component) &&
      spatial_component->parent_entity_handle != entities::NO_ENTITY_HANDLE
    ) {
      return;
    }

    bool32 has_spatial_component = spatial::is_spatial_component_valid(
      spatial_component
    );
    bool32 has_drawable_component = models::is_drawable_component_valid(
      engine_state->drawable_component_set.components[handle]
    );
    bool32 has_light_component = lights::is_light_component_valid(
      engine_state->light_component_set.components[handle]
    );
    bool32 has_behavior_component = behavior::is_behavior_component_valid(
      engine_state->behavior_component_set.components[handle]
    );
    bool32 has_animation_component = anim::is_animation_component_valid(
      engine_state->animation_component_set.components[handle]
    );

    for (uint8 level = 0; level < depth; level++) {
      strcat(text, "  ");
    }

    strcat(text, "- ");
    strcat(text, entity->debug_name);

    strcat(text, "@");
    sprintf(text + strlen(text), "%d", entity->handle);

    if (
      !has_spatial_component &&
      !has_drawable_component &&
      !has_light_component &&
      !has_behavior_component &&
      !has_animation_component
    ) {
      strcat(text, " (orphan)");
    }

    if (has_spatial_component) {
      strcat(text, " +S");
    }
    if (has_drawable_component) {
      strcat(text, " +D");
    }
    if (has_light_component) {
      strcat(text, " +L");
    }
    if (has_behavior_component) {
      strcat(text, " +B");
    }
    if (has_animation_component) {
      strcat(text, " +A");
    }

    if (spatial::is_spatial_component_valid(spatial_component)) {
      // NOTE: This is super slow lol.
      uint32 n_children_found = 0;
      each (child_spatial_component, engine_state->spatial_component_set.components) {
        if (
          child_spatial_component->parent_entity_handle ==
            spatial_component->entity_handle
        ) {
          n_children_found++;
          if (n_children_found > 5) {
            continue;
          }
          EntityHandle child_handle = child_spatial_component->entity_handle;
          Entity *child_entity = engine_state->entity_set.entities[child_handle];

          if (text[strlen(text) - 1] != '\n') {
            strcat(text, "\n");
          }
          get_entity_text_representation(text, engine_state, child_entity, depth + 1);
        }
      }
      if (n_children_found > 5) {
        for (uint8 level = 0; level < (depth + 1); level++) {
          strcat(text, "  ");
        }
        strcat(text, "(and ");
        sprintf(
          text + strlen(text), "%d", n_children_found - 5
        );
        strcat(text, " more)");
      }
    }

    if (text[strlen(text) - 1] != '\n') {
      strcat(text, "\n");
    }
  }


  pny_internal void get_scene_text_representation(
    char *text,
    EngineState *engine_state
  ) {
    text[0] = '\0';

    constexpr uint32 const MAX_N_SHOWN_ENTITIES = 35;
    uint32 idx_entity = 0;
    each (entity, engine_state->entity_set.entities) {
      if (idx_entity > MAX_N_SHOWN_ENTITIES) {
        sprintf(
          text + strlen(text),
          "...and %d more\n",
          engine_state->entity_set.entities.length - idx_entity
        );
        break;;
      }
      get_entity_text_representation(text, engine_state, entity, 0);
      idx_entity++;
    }

    if (text[strlen(text) - 1] == '\n') {
      text[strlen(text) - 1] = '\0';
    }
  }


  pny_internal void get_materials_text_representation(
    char *text,
    MaterialsState *materials_state,
    EngineState *engine_state
  ) {
    text[0] = '\0';

    strcat(text, "Internal:\n");

    uint32 idx = 0;
    each (material, materials_state->materials) {
      strcat(text, "- ");
      strcat(text, material->name);
      strcat(text, "\n");
      if (idx == engine_state->first_non_internal_material_idx - 1) {
        strcat(text, "Non-internal: \n");
      }
      idx++;
    }

    if (text[strlen(text) - 1] == '\n') {
      text[strlen(text) - 1] = '\0';
    }
  }
}


void debug_ui::render_debug_ui(
  EngineState *engine_state,
  RendererState *renderer_state,
  GuiState *gui_state,
  MaterialsState *materials_state,
  InputState *input_state,
  WindowSize *window_size
) {
  char debug_text[1 << 14];
  size_t dt_size = sizeof(debug_text);

  gui::start_drawing(gui_state);

  if (gui_state->heading_opacity > 0.0f) {
    gui::draw_heading(
      gui_state,
      gui_state->heading_text,
      v4(0.0f, 0.33f, 0.93f, gui_state->heading_opacity)
    );
    if (gui_state->heading_fadeout_delay > 0.0f) {
      gui_state->heading_fadeout_delay -= (real32)(*engine::g_dt);
    } else {
      gui_state->heading_opacity -=
        gui_state->heading_fadeout_duration * (real32)(*engine::g_dt);
    }
  }

  {
    strcpy(debug_text, "Peony debug info: ");
    strcat(debug_text, engine_state->current_scene_name);
    GuiContainer *container = gui::make_container(
      gui_state, debug_text, v2(25.0f, 25.0f)
    );

    snprintf(
      debug_text, dt_size, "%ux%u", window_size->width, window_size->height
    );
    gui::draw_named_value(gui_state, container, "screen size", debug_text);

    snprintf(
      debug_text, dt_size, "%ux%u",
      window_size->screencoord_width, window_size->screencoord_height
    );
    gui::draw_named_value(gui_state, container, "window size", debug_text);

    snprintf(debug_text, dt_size, "%u fps", engine_state->perf_counters.last_fps);
    gui::draw_named_value(gui_state, container, "fps", debug_text);

    snprintf(
      debug_text, dt_size, "%.2f ms", engine_state->perf_counters.dt_average * 1000.0f
    );
    gui::draw_named_value(gui_state, container, "dt", debug_text);

    snprintf(debug_text, dt_size, "%.2f", 1.0f + engine_state->timescale_diff);
    gui::draw_named_value(gui_state, container, "ts", debug_text);

    snprintf(debug_text, dt_size, engine_state->is_world_loaded ? "yes" : "no");
    gui::draw_named_value(gui_state, container, "is_world_loaded", debug_text);

    snprintf(debug_text, dt_size, "%u", materials_state->materials.length);
    gui::draw_named_value(
      gui_state, container, "materials.length", debug_text
    );

    snprintf(debug_text, dt_size, "%u", engine_state->entity_set.entities.length);
    gui::draw_named_value(gui_state, container, "entities.length", debug_text);

    snprintf(debug_text, dt_size, "%u", engine_state->model_loaders.length);
    gui::draw_named_value(
      gui_state, container, "model_loaders.length", debug_text
    );

    snprintf(debug_text, dt_size, "%u", engine_state->n_valid_model_loaders);
    gui::draw_named_value(
      gui_state, container, "n_valid_model_loaders", debug_text
    );

    snprintf(debug_text, dt_size, "%u", engine_state->entity_loader_set.loaders.length);
    gui::draw_named_value(
      gui_state, container, "entity_loader_set.length", debug_text
    );

    snprintf(debug_text, dt_size, "%u", engine_state->n_valid_entity_loaders);
    gui::draw_named_value(
      gui_state, container, "n_valid_entity_loaders", debug_text
    );

    if (gui::draw_toggle(
      gui_state, container, "Wireframe mode",
      &renderer_state->should_use_wireframe
    )) {
      renderer_state->should_use_wireframe =
        !renderer_state->should_use_wireframe;
      if (renderer_state->should_use_wireframe) {
        gui::set_heading(gui_state, "Wireframe mode on.", 1.0f, 1.0f, 1.0f);
      } else {
        gui::set_heading(gui_state, "Wireframe mode off.", 1.0f, 1.0f, 1.0f);
      }
    }

    if (gui::draw_toggle(
      gui_state, container, "FPS limit", &engine_state->should_limit_fps
    )) {
      engine_state->should_limit_fps = !engine_state->should_limit_fps;
      if (engine_state->should_limit_fps) {
        gui::set_heading(gui_state, "FPS limit enabled.", 1.0f, 1.0f, 1.0f);
      } else {
        gui::set_heading(gui_state, "FPS limit disabled.", 1.0f, 1.0f, 1.0f);
      }
    }

    if (gui::draw_toggle(
      gui_state, container, "Manual frame advance",
      &engine_state->is_manual_frame_advance_enabled
    )) {
      engine_state->is_manual_frame_advance_enabled =
        !engine_state->is_manual_frame_advance_enabled;
      if (engine_state->is_manual_frame_advance_enabled) {
        gui::set_heading(
          gui_state, "Manual frame advance enabled.", 1.0f, 1.0f, 1.0f
        );
      } else {
        gui::set_heading(
          gui_state, "Manual frame advance disabled.", 1.0f, 1.0f, 1.0f
        );
      }
    }

    if (gui::draw_toggle(
      gui_state, container, "Pause", &engine_state->should_pause
    )) {
      engine_state->should_pause = !engine_state->should_pause;
      if (engine_state->should_pause) {
        gui::set_heading(gui_state, "Pause enabled.", 1.0f, 1.0f, 1.0f);
      } else {
        gui::set_heading(gui_state, "Pause disabled.", 1.0f, 1.0f, 1.0f);
      }
    }

    if (gui::draw_button(
      gui_state, container, "Reload shaders"
    )) {
      materials::reload_shaders(&materials_state->materials);
      gui::set_heading(gui_state, "Shaders reloaded.", 1.0f, 1.0f, 1.0f);
    }

    if (gui::draw_button(
      gui_state, container, "Delete PBO"
    )) {
      materials::delete_persistent_pbo(&materials_state->persistent_pbo);
      gui::set_heading(gui_state, "PBO deleted.", 1.0f, 1.0f, 1.0f);
    }
  }

  {
    GuiContainer *container = gui::make_container(
      gui_state, "Entities", v2(window_size->width - 400.0f, 25.0f)
    );
    get_scene_text_representation(debug_text, engine_state);
    gui::draw_body_text(gui_state, container, debug_text);
  }

  {
    GuiContainer *container = gui::make_container(
      gui_state, "Materials", v2(window_size->width - 600.0f, 25.0f)
    );
    get_materials_text_representation(debug_text, materials_state, engine_state);
    gui::draw_body_text(gui_state, container, debug_text);
  }

  gui::draw_console(gui_state, input_state->text_input);
  gui::render(gui_state);
}