Debian Codegraph @

Descent3/debuggraph.cpp

raw symbols SCIP
1 /*
2 * Descent 3
3 * Copyright (C) 2024 Parallax Software
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 --- HISTORICAL COMMENTS FOLLOW ---
19
20 * $Logfile: /DescentIII/main/debuggraph.cpp $
21 * $Revision: 4 $
22 * $Date: 9/23/99 12:02p $
23 * $Author: Jeff $
24 *
25 * Debug graph visual log
26 *
27 * $Log: /DescentIII/main/debuggraph.cpp $
28 *
29 * 4 9/23/99 12:02p Jeff
30 * include stdlib for atexit
31 *
32 * 3 5/10/99 10:21p Ardussi
33 * changes to compile on Mac
34 *
35 * 2 4/04/99 8:14p Jeff
36 * added debug graph stuff
37 *
38 * $NoKeywords: $
39 */
40
41 #include <stdlib.h>
42 #include "debuggraph.h"
43 #include "grdefs.h"
44 #include "mono.h"
45 #include <memory.h>
46 #include "pserror.h"
47 #include "bitmap.h"
48 #include "renderer.h"
49 #include "pstypes.h"
50 #include "stringtable.h"
51 #include "newui.h"
52 #include "newui_core.h"
53 #include "mem.h"
54 #include <string.h>
55 #include "game.h"
56
57 #define DATA_TYPE_INT 0
58 #define DATA_TYPE_FLOAT 1
59 #define NUM_FRAMES_TO_DISPLAY 128
60 #define MAX_GRAPH_NODES 16
61 #define MAX_NAME 38
62
63 int graph_bmp = -1;
64 int graph_mask = 0;
65 int graph_num_nodes = 0;
66
67 struct tGraphColor {
68 const char *color_name;
69 ddgr_color color;
70 };
71
72 tGraphColor GraphColors[MAX_GRAPH_NODES] = {
73 {"Green", GR_RGB(40, 255, 40)}, {"Red", GR_RGB(255, 40, 40)}, {"Blue", GR_RGB(40, 40, 255)},
74 {"Yellow", GR_RGB(255, 255, 40)}, {"Purple", GR_RGB(255, 40, 255)}, {"White", GR_RGB(255, 255, 255)},
75 {"Lt.Grey", GR_RGB(180, 180, 180)}, {"Dk.Grey", GR_RGB(80, 80, 80)}, {"Orange", GR_RGB(255, 140, 5)},
76 {"Pink", GR_RGB(250, 147, 147)}, {"Brown", GR_RGB(119, 68, 2)}, {"Dk.Blue", GR_RGB(3, 20, 124)},
77 {"Dk.Red", GR_RGB(100, 2, 2)}, {"Dk.Green", GR_RGB(2, 100, 9)}, {"Turquoise", GR_RGB(14, 229, 202)},
78 {"Dk.Purple", GR_RGB(100, 4, 104)}};
79
80 struct tAddDebugGraph {
81 uint8_t data_input;
82 int flags;
83
84 union {
85 int i_min_value;
86 float f_min_value;
87 };
88 union {
89 int i_max_value;
90 float f_max_value;
91 };
92
93 };
94
95 class tGraphNode {
96 public:
97 tGraphNode() {}
98 ~tGraphNode() {}
99
100 ddgr_color color;
101
102 void Initialize(tAddDebugGraph *data);
103 void Update(int value);
104 void Update(float value);
105 void Render(void);
106
107 char name[MAX_NAME];
108 tAddDebugGraph init_data;
109
110 private:
111 int cur_write_pos;
112 int first_pos;
113 int num_items_stored;
114
115 union {
116 int i_data[NUM_FRAMES_TO_DISPLAY];
117 float f_data[NUM_FRAMES_TO_DISPLAY];
118 };
119 };
120
121 struct tDebugGraphNode {
122 tGraphNode *node;
123 tDebugGraphNode *next;
124 };
125
126 tDebugGraphNode *DebugGraphNode_root = NULL;
127
128 tGraphNode *DebugGraph_AddNode(void) {
129 if (graph_num_nodes == MAX_GRAPH_NODES)
130 return NULL;
131
132 tDebugGraphNode *curr = DebugGraphNode_root;
133
134 if (!curr) {
135 curr = DebugGraphNode_root = (tDebugGraphNode *)mem_malloc(sizeof(tDebugGraphNode));
136 } else {
137 while (curr->next) {
138 curr = curr->next;
139 }
140 curr->next = (tDebugGraphNode *)mem_malloc(sizeof(tDebugGraphNode));
141 curr = curr->next;
142 }
143
144 if (!curr) {
145 Error("Out of Memory");
146 }
147
148 graph_num_nodes++;
149 curr->next = NULL;
150 curr->node = new tGraphNode;
151
152 if (!curr->node) {
153 Error("Out of Memory");
154 }
155
156 return curr->node;
157 }
158
159 tGraphNode *DebugGraph_FindNode(int index) {
160 if (index >= graph_num_nodes)
161 return NULL;
162 tDebugGraphNode *curr = DebugGraphNode_root;
163 int ci = 0;
164 while (curr) {
165 if (ci == index) {
166 return curr->node;
167 }
168 curr = curr->next;
169 ci++;
170 }
171
172 return NULL;
173 }
174
175 void DebugGraph_Free(void) {
176 tDebugGraphNode *curr, *next;
177 curr = DebugGraphNode_root;
178
179 while (curr) {
180 delete curr->node;
181 next = curr->next;
182 mem_free(curr);
183 curr = next;
184 }
185
186 DebugGraphNode_root = NULL;
187
188 if (graph_bmp > BAD_BITMAP_HANDLE) {
189 bm_FreeBitmap(graph_bmp);
190 }
191 graph_bmp = -1;
192 graph_num_nodes = 0;
193 }
194
195 void DebugGraph_Initialize(void) {
196 atexit(DebugGraph_Free);
197 DebugGraphNode_root = NULL;
198 graph_bmp = -1;
199 graph_mask = 0;
200 graph_num_nodes = 0;
201 }
202
203 int DebugGraph_Add(int min, int max, const char *name, int flags) {
204 tAddDebugGraph data;
205 tGraphNode *node = DebugGraph_AddNode();
206 if (!node)
207 return -1;
208
209 int index = graph_num_nodes - 1;
210 data.data_input = DATA_TYPE_INT;
211 data.i_max_value = max;
212 data.i_min_value = min;
213 data.flags = flags;
214 node->Initialize(&data);
215 node->color = GraphColors[index].color;
216 strncpy(node->name, name, MAX_NAME - 1);
217 node->name[MAX_NAME - 1] = '\0';
218 return index;
219 }
220
221 int DebugGraph_Add(float min, float max, const char *name, int flags) {
222 tAddDebugGraph data;
223 tGraphNode *node = DebugGraph_AddNode();
224 if (!node)
225 return -1;
226
227 int index = graph_num_nodes - 1;
228 data.data_input = DATA_TYPE_FLOAT;
229 data.f_max_value = max;
230 data.f_min_value = min;
231 data.flags = flags;
232 node->Initialize(&data);
233 node->color = GraphColors[index].color;
234 strncpy(node->name, name, MAX_NAME - 1);
235 node->name[MAX_NAME - 1] = '\0';
236 return index;
237 }
238
239 void DebugGraph_Enable(int id) {
240 int bit = 0x01 << id;
241 graph_mask |= bit;
242 }
243
244 void DebugGraph_Disable(int id) {
245 int bit = 0x01 << id;
246 graph_mask &= ~bit;
247 }
248
249 void DebugGraph_Update(int id, int value) {
250 if (id < 0 || id >= graph_num_nodes)
251 return;
252
253 tGraphNode *node = DebugGraph_FindNode(id);
254 if (!node)
255 return;
256
257 node->Update(value);
258 }
259
260 void DebugGraph_Update(int id, float value) {
261 if (id < 0 || id >= graph_num_nodes)
262 return;
263
264 tGraphNode *node = DebugGraph_FindNode(id);
265 if (!node)
266 return;
267
268 node->Update(value);
269 }
270
271 #define TOP_L 10
272 #define TOP_T 50
273
274 void DebugGraph_Render(void) {
275 if (graph_bmp == -1) {
276 // create the bitmap
277 graph_bmp = bm_AllocBitmap(64, 64, 0);
278 if (graph_bmp > BAD_BITMAP_HANDLE) {
279 uint16_t *data = bm_data(graph_bmp, 0);
280 for (int i = 0; i < 64 * 64; i++) {
281 data[i] = GR_RGB16(0, 0, 0) | OPAQUE_FLAG;
282 }
283 }
284 }
285 if (graph_bmp <= BAD_BITMAP_HANDLE)
286 return;
287
288 if (graph_mask == 0)
289 return;
290
291 rend_SetAlphaType(AT_CONSTANT_TEXTURE);
292 rend_SetAlphaValue(240);
293 rend_SetLighting(LS_NONE);
294 rend_SetColorModel(CM_MONO);
295 rend_SetOverlayType(OT_NONE);
296 rend_SetWrapType(WT_CLAMP);
297 rend_SetFiltering(0);
298
299 rend_DrawScaledBitmap(TOP_L, TOP_T, TOP_L + 256, TOP_T + 64, graph_bmp, 0, 0, 1, 1);
300
301 int bit = 0x01;
302 tGraphNode *node;
303 bool multi_game = (bool)((Game_mode & GM_MULTI) != 0);
304 bool ok_to_render;
305
306 for (int i = 0; i < graph_num_nodes; i++) {
307 node = DebugGraph_FindNode(i);
308 ok_to_render = true;
309
310 if (node && (graph_mask & bit)) {
311 if (multi_game) {
312 if (!(node->init_data.flags & DGF_MULTIPLAYER))
313 ok_to_render = false;
314 } else {
315 if (!(node->init_data.flags & DGF_SINGLEPLAYER))
316 ok_to_render = false;
317 }
318
319 if (ok_to_render)
320 node->Render();
321 }
322 bit = bit << 1;
323 }
324
325 rend_SetFiltering(1);
326 }
327
328 ////////////////////////////////////////////
329 void tGraphNode::Initialize(tAddDebugGraph *data) {
330 if (!data)
331 return;
332
333 memcpy(&init_data, data, sizeof(tAddDebugGraph));
334 cur_write_pos = 0;
335 first_pos = 0;
336 num_items_stored = 0;
337
338 color = GR_RGB(255, 40, 40);
339 memset(i_data, 0, sizeof(int) * NUM_FRAMES_TO_DISPLAY);
340 }
341
342 void tGraphNode::Update(int value) {
343 ASSERT(init_data.data_input == DATA_TYPE_INT);
344
345 bool multi_game = (bool)((Game_mode & GM_MULTI) != 0);
346 if (multi_game) {
347 if (!(init_data.flags & DGF_MULTIPLAYER))
348 return;
349 } else {
350 if (!(init_data.flags & DGF_SINGLEPLAYER))
351 return;
352 }
353
354 if (cur_write_pos != 0 && (cur_write_pos % NUM_FRAMES_TO_DISPLAY) == 0) {
355 cur_write_pos = 0;
356 }
357
358 if (num_items_stored == NUM_FRAMES_TO_DISPLAY) {
359 first_pos++;
360 if (first_pos != 0 && (first_pos % NUM_FRAMES_TO_DISPLAY) == 0) {
361 first_pos = 0;
362 }
363 } else {
364 num_items_stored++;
365 }
366
367 if (value < init_data.i_min_value)
368 value = init_data.i_min_value;
369 if (value > init_data.i_max_value)
370 value = init_data.i_max_value;
371
372 i_data[cur_write_pos] = value;
373 cur_write_pos++;
374 }
375
376 void tGraphNode::Update(float value) {
377 ASSERT(init_data.data_input == DATA_TYPE_FLOAT);
378
379 bool multi_game = (bool)((Game_mode & GM_MULTI) != 0);
380 if (multi_game) {
381 if (!(init_data.flags & DGF_MULTIPLAYER))
382 return;
383 } else {
384 if (!(init_data.flags & DGF_SINGLEPLAYER))
385 return;
386 }
387
388 if (cur_write_pos != 0 && (cur_write_pos % NUM_FRAMES_TO_DISPLAY) == 0) {
389 cur_write_pos = 0;
390 }
391
392 if (num_items_stored == NUM_FRAMES_TO_DISPLAY) {
393 first_pos++;
394 if (first_pos != 0 && (first_pos % NUM_FRAMES_TO_DISPLAY) == 0) {
395 first_pos = 0;
396 }
397 } else {
398 num_items_stored++;
399 }
400
401 if (value < init_data.f_min_value)
402 value = init_data.f_min_value;
403 if (value > init_data.f_max_value)
404 value = init_data.f_max_value;
405
406 f_data[cur_write_pos] = value;
407
408 cur_write_pos++;
409 }
410
411 void GetGraphY(int *y, int value, int max, int min) {
412 float range;
413 range = float(value - min) / float(max - min);
414
415 *y = TOP_T + 64 - int(range * 64.0);
416 }
417
418 void GetGraphY(int *y, float value, float max, float min) {
419 float range;
420 range = (value - min) / (max - min);
421
422 *y = TOP_T + 64 - int(range * 64.0);
423 }
424
425 void tGraphNode::Render(void) {
426 if (num_items_stored < 2)
427 return;
428 rend_SetFlatColor(color);
429
430 int last_x, last_y = 0, y;
431 int index = first_pos;
432 int count = num_items_stored;
433 count--;
434
435 switch (init_data.data_input) {
436 case DATA_TYPE_FLOAT:
437 GetGraphY(&last_y, f_data[index], init_data.f_max_value, init_data.f_min_value);
438 break;
439 case DATA_TYPE_INT:
440 GetGraphY(&last_y, i_data[index], init_data.i_max_value, init_data.i_min_value);
441 break;
442 };
443 index++;
444 last_x = TOP_L;
445
446 while (count > 0) {
447 if (index != 0 && (index % NUM_FRAMES_TO_DISPLAY) == 0) {
448 index = 0;
449 }
450
451 switch (init_data.data_input) {
452 case DATA_TYPE_FLOAT:
453 GetGraphY(&y, f_data[index], init_data.f_max_value, init_data.f_min_value);
454 break;
455 case DATA_TYPE_INT:
456 GetGraphY(&y, i_data[index], init_data.i_max_value, init_data.i_min_value);
457 break;
458 };
459
460 rend_DrawLine(last_x, last_y, last_x + 1, y);
461 count--;
462 last_x += 2;
463 last_y = y;
464 index++;
465 }
466 }
467
468 /*
469 ***********************************************************************
470 */
471
472 void DebugGraph_DisplayOptions(void) {
473 if (graph_num_nodes == 0)
474 return;
475
476 newuiTiledWindow window;
477 newuiSheet *sheet;
478
479 bool **bool_values = NULL;
480 bool exit_menu = false;
481 tGraphNode *node;
482
483 bool_values = (bool **)mem_malloc(sizeof(bool *) * graph_num_nodes);
484 if (!bool_values)
485 return;
486
487 window.Create("Debug Graph", 0, 0, 256, 384);
488 sheet = window.GetSheet();
489
490 sheet->NewGroup(NULL, 0, 0);
491 char buffer[256];
492 int bit = 0x01;
493
494 bool multi_game = (bool)((Game_mode & GM_MULTI) != 0);
495
496 for (int i = 0; i < graph_num_nodes; i++) {
497 bool_values[i] = NULL;
498 node = DebugGraph_FindNode(i);
499 if (!node) {
500 bit = bit << 1;
501 continue;
502 }
503
504 // see if we should add it
505 if (multi_game) {
506 if (!(node->init_data.flags & DGF_MULTIPLAYER)) {
507 bit = bit << 1;
508 continue;
509 }
510 } else {
511 if (!(node->init_data.flags & DGF_SINGLEPLAYER)) {
512 bit = bit << 1;
513 continue;
514 }
515 }
516
517 snprintf(buffer, sizeof(buffer), "%s (%s)", node->name, GraphColors[i].color_name);
518 bool_values[i] = sheet->AddLongCheckBox(buffer);
519 if (bool_values[i]) {
520 *bool_values[i] = (bool)((graph_mask & bit) != 0);
521 }
522
523 bit = bit << 1;
524 }
525
526 sheet->NewGroup(NULL, 50, 200);
527 sheet->AddButton(TXT_OK, UID_OK);
528
529 // process
530 window.Open();
531
532 while (!exit_menu) {
533 int res = window.DoUI();
534
535 // handle all UI results.
536 switch (res) {
537 case UID_OK: {
538 for (int x = 0; x < graph_num_nodes; x++) {
539 if (bool_values[x]) {
540 (*bool_values[x]) ? DebugGraph_Enable(x) : DebugGraph_Disable(x);
541 }
542 }
543
544 exit_menu = true;
545 }
546 };
547 }
548
549 window.Close();
550 window.Destroy();
551
552 mem_free(bool_values);
553 }