NXEngine v1.0.0.6
[NXEngine.git] / debug.cpp
blob8da0da4cca4b80540c7f9938ce5cc894a055c663
2 #include "nx.h"
3 #include <stdarg.h>
4 #include <string.h>
6 #include "debug.fdh"
8 #define MAX_DEBUG_MARKS 80
9 static struct
11 int x, y, x2, y2;
12 char type;
13 uchar r, g, b;
14 } debugmarks[MAX_DEBUG_MARKS];
16 static int ndebugmarks = 0;
17 static StringList DebugList;
20 void DrawDebug(void)
22 if (settings->enable_debug_keys)
24 // handle debug keys
25 if (justpushed(DEBUG_GOD_KEY))
27 game.debug.god ^= 1;
28 sound(SND_MENU_SELECT);
31 if (justpushed(DEBUG_SAVE_KEY))
33 game_save(settings->last_save_slot);
34 sound(SND_SWITCH_WEAPON);
35 console.Print("Game saved.");
38 if (justpushed(F6KEY))
40 game.debug.DrawBoundingBoxes ^= 1;
41 sound(SND_COMPUTER_BEEP);
44 if (justpushed(F9KEY))
46 AddXP(1);
49 if (inputs[DEBUG_FLY_KEY])
51 player->yinertia = -0x880;
52 if (!player->hurt_time) player->hurt_time = 20; // make invincible
56 /*if (game.debug.debugmode)
58 //debug("%d fps", game.debug.fps);
60 if (game.debug.god)
62 //debug("<GOD MODE>");
63 player->weapons[player->curWeapon].level = 2;
64 player->weapons[player->curWeapon].xp = player->weapons[player->curWeapon].max_xp[2];
65 player->weapons[player->curWeapon].ammo = player->weapons[player->curWeapon].maxammo;
66 player->hp = player->maxHealth;
69 debug("%d,%d", (player->x>>CSF)/TILE_W, (player->y>>CSF)/TILE_H);
70 debug("[%c%c%c%c]", player->blockl?'l':' ', player->blockr?'r':' ', player->blocku?'u':' ', player->blockd?'d':' ');
71 //debug("%d", player->xinertia);
72 //debug("%d", player->yinertia);*/
74 debug("Have Puppy: %d", game.flags[274]);
75 debug("Kakeru: %d", game.flags[275]);
76 debug("Runner Gone: %d", game.flags[276]);
77 debug("No Shinobu: %d", game.flags[277]);
78 debug("Door Open: %d", game.flags[278]);
79 debug("Mick: %d", game.flags[279]);
80 debug("Gave 1st: %d", game.flags[590]);
81 debug("Gave 2nd: %d", game.flags[591]);
82 debug("Gave 3rd: %d", game.flags[592]);
83 debug("Gave 4th: %d", game.flags[593]);
84 debug("Gave 5th: %d", game.flags[594]);
85 debug("-");
87 int i;
88 for(i=0;i<player->ninventory;i++)
89 debug("%d", player->inventory[i]);
92 //}
94 debug_draw();
95 DrawDebugMarks();
99 void DrawBoundingBoxes()
101 Object *o;
102 FOREACH_OBJECT(o)
104 if (o->onscreen || o == player)
106 uint32_t color;
108 if (o == player)
110 color = 0xffff00;
112 else if (o->flags & FLAG_INVULNERABLE)
114 color = 0xffffff;
116 else if (o->flags & FLAG_SHOOTABLE)
118 color = 0x00ff00;
120 else if (o->flags & FLAG_SOLID_MUSHY)
122 color = 0xff0080;
124 else
126 color = 0xff0000;
129 AddDebugMark(o->Left(), o->Top(), o->Right(), o->Bottom(),
130 DM_BOX, color>>16, (color>>8)&0xff, color&0xff);
135 void DrawAttrPoints()
137 Object *o;
138 FOREACH_OBJECT(o)
140 draw_pointlist(o, &sprites[o->sprite].block_l);
141 draw_pointlist(o, &sprites[o->sprite].block_r);
142 draw_pointlist(o, &sprites[o->sprite].block_u);
143 draw_pointlist(o, &sprites[o->sprite].block_d);
147 static void draw_pointlist(Object *o, SIFPointList *points)
149 int xoff = (o->x >> CSF);
150 int yoff = (o->y >> CSF);
152 for(int i=0;i<points->count;i++)
154 DebugPixel((xoff + points->point[i].x) << CSF, \
155 (yoff + points->point[i].y) << CSF, 255, 0, 255);
160 void c------------------------------() {}
163 // debug text display debug() useful for reporting game vars etc
164 void debug(const char *fmt, ...)
166 char buffer[128];
167 va_list ar;
169 va_start(ar, fmt);
170 vsnprintf(buffer, sizeof(buffer), fmt, ar);
171 va_end(ar);
173 DebugList.AddString(buffer);
176 void debug_draw(void)
178 for(int i=0;;i++)
180 const char *text = DebugList.StringAt(i);
181 if (!text) break;
183 int x = (SCREEN_WIDTH - 8) - GetFontWidth(text, 0, true);
184 int y = 4 + (i * (GetFontHeight() + 1));
185 font_draw_shaded(x, y, text, 0, &greenfont);
189 void debug_clear()
191 DebugList.MakeEmpty();
195 void c------------------------------() {}
198 extern char *object_names[]; // from autogen'd objnames.cpp
200 // given an object type returns the name of the object e.g. "OBJ_TOROKO"
201 const char *DescribeObjectType(int type)
203 if (type >= 0 && type < OBJ_LAST && object_names[type])
204 return stprintf("OBJ_%s(%d)", object_names[type], type);
206 return stprintf("[Type %d]", type);
209 // tries to convert a string, such as OBJ_TOROKO, into it's numeric type,
210 // with a small bit of english-language intelligence.
211 int ObjectNameToType(const char *name_in)
213 // if all characters are numeric they're specifying by number
214 // so simply return the atoi
215 for(int i=0;;i++)
217 if (name_in[i] == 0)
219 return atoi(name_in);
222 if (!isdigit(name_in[i])) break;
225 char *name = strdup(name_in); // make string writeable
227 // some string preprocessing
228 for(int i=0;name[i];i++)
230 if (name[i] == ' ')
232 name[i] = '_';
234 else
236 name[i] = toupper(name[i]);
240 // remove the "OBJ_" suffix if it's present
241 const char *searchstring = name;
242 if (strbegin(name, "OBJ_"))
243 searchstring += 4;
245 // search for it in the object_names table
246 for(int i=0;i<OBJ_LAST;i++)
248 if (object_names[i] && !strcmp(object_names[i], searchstring))
250 free(name);
251 return i;
255 stat("ObjectNameToType: couldn't find object 'OBJ_%s'", searchstring);
256 free(name);
257 return -1;
260 const char *DescribeDir(int dir)
262 switch(dir)
264 case LEFT: return "LEFT";
265 case RIGHT: return "RIGHT";
266 case UP: return "UP";
267 case DOWN: return "DOWN";
268 case CENTER: return "CENTER";
269 default: return stprintf("[Invalid Direction %d]", dir);
274 void c------------------------------() {}
277 const char *strhex(int value)
279 if (value < 0)
280 return stprintf("-0x%x", -value);
281 else
282 return stprintf("0x%x", value);
287 void c------------------------------() {}
290 void DrawDebugMarks(void)
292 int i;
293 int x, y, x2, y2;
294 uchar r, g, b;
296 for(i=0;i<ndebugmarks;i++)
298 x = (debugmarks[i].x >> CSF) - (map.displayed_xscroll >> CSF);
299 y = (debugmarks[i].y >> CSF) - (map.displayed_yscroll >> CSF);
300 x2 = (debugmarks[i].x2 >> CSF) - (map.displayed_xscroll >> CSF);
301 y2 = (debugmarks[i].y2 >> CSF) - (map.displayed_yscroll >> CSF);
302 r = debugmarks[i].r;
303 g = debugmarks[i].g;
304 b = debugmarks[i].b;
306 switch(debugmarks[i].type)
308 case DM_PIXEL:
309 DrawPixel(x, y, r, g, b);
310 break;
312 case DM_CROSSHAIR:
313 DrawPixel(x, y, r, g, b);
314 DrawPixel(x+1, y, r, g, b);
315 DrawPixel(x-1, y, r, g, b);
316 DrawPixel(x, y+1, r, g, b);
317 DrawPixel(x, y-1, r, g, b);
318 break;
320 case DM_XLINE:
321 FillRect(x, 0, x, SCREEN_HEIGHT, r, g, b);
322 break;
324 case DM_YLINE:
325 FillRect(0, y, SCREEN_WIDTH, y, r, g, b);
326 break;
328 case DM_BOX:
329 DrawRect(x, y, x2, y2, r, g, b);
330 break;
334 ndebugmarks = 0;
337 void AddDebugMark(int x, int y, int x2, int y2, char type, uchar r, uchar g, uchar b)
339 if (ndebugmarks >= MAX_DEBUG_MARKS)
340 return;
342 debugmarks[ndebugmarks].x = x;
343 debugmarks[ndebugmarks].y = y;
344 debugmarks[ndebugmarks].x2 = x2;
345 debugmarks[ndebugmarks].y2 = y2;
346 debugmarks[ndebugmarks].r = r;
347 debugmarks[ndebugmarks].g = g;
348 debugmarks[ndebugmarks].b = b;
349 debugmarks[ndebugmarks].type = type;
350 ndebugmarks++;
353 // draw a pixel of the specified color at [x,y] in object coordinates
354 void DebugPixel(int x, int y, uchar r, uchar g, uchar b)
356 AddDebugMark(x, y, 0, 0, DM_PIXEL, r, g, b);
359 void DebugCrosshair(int x, int y, uchar r, uchar g, uchar b)
361 AddDebugMark(x, y, 0, 0, DM_CROSSHAIR, r, g, b);
364 void crosshair(int x, int y)
366 debugVline(x, 255, 0, 0);
367 debugHline(y, 0, 255, 0);
370 void DebugPixelNonCSF(int x, int y, uchar r, uchar g, uchar b) { DebugPixel(x<<CSF,y<<CSF,r,g,b); }
371 void DebugCrosshairNonCSF(int x, int y, uchar r, uchar g, uchar b) { DebugCrosshair(x<<CSF,y<<CSF,r,g,b); }
373 void debugVline(int x, uchar r, uchar g, uchar b)
375 AddDebugMark(x, 0, 0, 0, DM_XLINE, r, g, b);
378 void debugHline(int y, uchar r, uchar g, uchar b)
380 AddDebugMark(0, y, 0, 0, DM_YLINE, r, g, b);
383 void debugbox(int x1, int y1, int x2, int y2, uchar r, uchar g, uchar b)
385 AddDebugMark(x1, y1, x2, y2, DM_BOX, r, g, b);
388 void debugtile(int x, int y, uchar r, uchar g, uchar b)
390 int x1, y1, x2, y2;
392 x *= (TILE_W << CSF);
393 y *= (TILE_H << CSF);
395 x1 = x; y1 = y;
396 x2 = x1 + (TILE_W << CSF);
397 y2 = y1 + (TILE_H << CSF);
398 AddDebugMark(x1, y1, x2, y2, DM_BOX, r, g, b);