Added some debugging tools

This commit is contained in:
2023-03-18 02:15:47 +11:00
parent a04c506811
commit 58763001fe
8 changed files with 179 additions and 18 deletions

View File

@@ -140,6 +140,7 @@ xcopy "$(SolutionDir)Toy\repl\repl_tools.*" "$(SolutionDir)box" /Y /I /E</Comman
<ClCompile Include="box\box_common.c" /> <ClCompile Include="box\box_common.c" />
<ClCompile Include="box\box_engine.c" /> <ClCompile Include="box\box_engine.c" />
<ClCompile Include="box\box_engine_node.c" /> <ClCompile Include="box\box_engine_node.c" />
<ClCompile Include="box\dbg_profiler.c" />
<ClCompile Include="box\lib_about.c" /> <ClCompile Include="box\lib_about.c" />
<ClCompile Include="box\lib_engine.c" /> <ClCompile Include="box\lib_engine.c" />
<ClCompile Include="box\lib_input.c" /> <ClCompile Include="box\lib_input.c" />
@@ -153,6 +154,7 @@ xcopy "$(SolutionDir)Toy\repl\repl_tools.*" "$(SolutionDir)box" /Y /I /E</Comman
<ClInclude Include="box\box_common.h" /> <ClInclude Include="box\box_common.h" />
<ClInclude Include="box\box_engine.h" /> <ClInclude Include="box\box_engine.h" />
<ClInclude Include="box\box_engine_node.h" /> <ClInclude Include="box\box_engine_node.h" />
<ClInclude Include="box\dbg_profiler.h" />
<ClInclude Include="box\lib_about.h" /> <ClInclude Include="box\lib_about.h" />
<ClInclude Include="box\lib_engine.h" /> <ClInclude Include="box\lib_engine.h" />
<ClInclude Include="box\lib_input.h" /> <ClInclude Include="box\lib_input.h" />

2
Toy

Submodule Toy updated: 4b83f1f0d6...17f0e4476b

View File

@@ -22,9 +22,6 @@ var collisionMap: [bool] = null; //cache this, since it won't change during a le
var rng: opaque = null; var rng: opaque = null;
//debugging tools
var debugStepCounter: int = 0;
var debugDrawCounter: int = 0;
//lifecycle functions //lifecycle functions
fn onLoad(node: opaque) { fn onLoad(node: opaque) {
@@ -43,18 +40,10 @@ fn onFree(node: opaque) {
} }
fn onStep(node: opaque) { fn onStep(node: opaque) {
if (++debugStepCounter >= 30) {
print "FPS: " + string debugDrawCounter + " / 30";
debugStepCounter = 0;
debugDrawCounter = 0;
}
entities = entities.sort(depthComparator); entities = entities.sort(depthComparator);
} }
fn onDraw(node: opaque) { fn onDraw(node: opaque) {
debugDrawCounter++;
//call each child's custom draw fn (with parent shifting) //call each child's custom draw fn (with parent shifting)
tilemap.callNodeFn("customOnDraw", 0, 32); tilemap.callNodeFn("customOnDraw", 0, 32);
for (var i = 0; i < entities.length(); i++) { for (var i = 0; i < entities.length(); i++) {

View File

@@ -47,3 +47,6 @@
//test variable sizes based on platform //test variable sizes based on platform
#define STATIC_ASSERT(test_for_true) static_assert((test_for_true), "(" #test_for_true ") failed") #define STATIC_ASSERT(test_for_true) static_assert((test_for_true), "(" #test_for_true ") failed")
//debugging
#include "dbg_profiler.h"

View File

@@ -419,14 +419,31 @@ void Box_execEngine() {
engine.simTime = engine.realTime; engine.simTime = engine.realTime;
clock_t delta = (double) CLOCKS_PER_SEC / 30.0; clock_t delta = (double) CLOCKS_PER_SEC / 30.0;
while (engine.running) { Dbg_Timer dbgTimer;
execLoadRootNode(); Dbg_FPSCounter fps;
Dbg_initTimer(&dbgTimer);
Dbg_initFPSCounter(&fps);
while (engine.running) {
Dbg_tickFPSCounter(&fps);
Dbg_clearConsole();
Dbg_printTimerLog(&dbgTimer);
Dbg_printFPSCounter(&fps);
Dbg_startTimer(&dbgTimer, "execLoadRootNode()");
execLoadRootNode();
Dbg_stopTimer(&dbgTimer);
Dbg_startTimer(&dbgTimer, "execEvents()");
execEvents(); execEvents();
Dbg_stopTimer(&dbgTimer);
//calc the time passed //calc the time passed
engine.realTime = clock(); engine.realTime = clock();
Dbg_startTimer(&dbgTimer, "execStep() (variable)");
//while not enough time has passed //while not enough time has passed
while(engine.simTime < engine.realTime) { while(engine.simTime < engine.realTime) {
//simulate the world //simulate the world
@@ -435,13 +452,20 @@ void Box_execEngine() {
//calc the time simulation //calc the time simulation
engine.simTime += delta; engine.simTime += delta;
} }
Dbg_stopTimer(&dbgTimer);
//render the world //render the world
Dbg_startTimer(&dbgTimer, "clear screen");
SDL_SetRenderDrawColor(engine.renderer, 128, 128, 128, 255); //NOTE: This line can be disabled later SDL_SetRenderDrawColor(engine.renderer, 128, 128, 128, 255); //NOTE: This line can be disabled later
SDL_RenderClear(engine.renderer); //NOTE: This line can be disabled later SDL_RenderClear(engine.renderer); //NOTE: This line can be disabled later
Dbg_stopTimer(&dbgTimer);
Dbg_startTimer(&dbgTimer, "draw screen");
Box_callRecursiveEngineNode(engine.rootNode, &engine.interpreter, "onDraw", NULL); Box_callRecursiveEngineNode(engine.rootNode, &engine.interpreter, "onDraw", NULL);
SDL_RenderPresent(engine.renderer); SDL_RenderPresent(engine.renderer);
Dbg_stopTimer(&dbgTimer);
} }
Dbg_freeTimer(&dbgTimer);
Dbg_freeFPSCounter(&fps);
} }

116
box/dbg_profiler.c Normal file
View File

@@ -0,0 +1,116 @@
#include "dbg_profiler.h"
#include "toy_memory.h"
#include <stdio.h>
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
//https://stackoverflow.com/questions/2347770/how-do-you-clear-the-console-screen-in-c
void Dbg_clearConsole()
{
HANDLE hStdOut;
CONSOLE_SCREEN_BUFFER_INFO csbi;
DWORD count;
DWORD cellCount;
COORD homeCoords = { 0, 0 };
hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
if (hStdOut == INVALID_HANDLE_VALUE) return;
/* Get the number of cells in the current buffer */
if (!GetConsoleScreenBufferInfo(hStdOut, &csbi)) return;
cellCount = csbi.dwSize.X * csbi.dwSize.Y;
/* Fill the entire buffer with spaces */
if (!FillConsoleOutputCharacter(
hStdOut,
(TCHAR)' ',
cellCount,
homeCoords,
&count
)) return;
/* Fill the entire buffer with the current colors and attributes */
if (!FillConsoleOutputAttribute(
hStdOut,
csbi.wAttributes,
cellCount,
homeCoords,
&count
)) return;
/* Move the cursor home */
SetConsoleCursorPosition(hStdOut, homeCoords);
}
#else // !_WIN32
#include <unistd.h>
#include <term.h>
void Dbg_clearConsole()
{
if (!cur_term)
{
int result;
setupterm(NULL, STDOUT_FILENO, &result);
if (result <= 0) return;
}
putp(tigetstr("clear"));
}
#endif
void Dbg_initTimer(Dbg_Timer* timer) {
timer->name = NULL;
timer->start = 0;
timer->log = TOY_ALLOCATE(char, 2048);
memset(timer->log, 0, 2048);
timer->logPos = 0;
}
void Dbg_startTimer(Dbg_Timer* timer, const char* name) {
timer->name = name;
timer->start = clock();
}
void Dbg_stopTimer(Dbg_Timer* timer) {
double duration = (clock() - timer->start) / (double) CLOCKS_PER_SEC * 1000;
timer->logPos += sprintf(&(timer->log[timer->logPos]), "%.3fms %s\n", duration, timer->name);
}
void Dbg_printTimerLog(Dbg_Timer* timer) {
printf("%.*s", timer->logPos, timer->log);
timer->logPos = 0;
}
void Dbg_freeTimer(Dbg_Timer* timer) {
TOY_FREE_ARRAY(char, timer->log, 2048);
}
void Dbg_initFPSCounter(Dbg_FPSCounter* counter) {
counter->count = 0;
counter->start = clock();
counter->log = TOY_ALLOCATE(char, 256);
memset(counter->log, 0, 256);
}
void Dbg_tickFPSCounter(Dbg_FPSCounter* counter) {
counter->count++;
if (clock() - counter->start > CLOCKS_PER_SEC) {
sprintf(counter->log, "%d FPS", counter->count);
counter->start = clock();
counter->count = 0;
}
}
void Dbg_printFPSCounter(Dbg_FPSCounter* counter) {
printf("%s\n", counter->log);
}
void Dbg_freeFPSCounter(Dbg_FPSCounter* counter) {
TOY_FREE_ARRAY(char, counter->log, 256);
}

29
box/dbg_profiler.h Normal file
View File

@@ -0,0 +1,29 @@
#pragma once
#include <time.h>
void Dbg_clearConsole();
typedef struct Dbg_Timer {
const char* name;
clock_t start;
char* log;
int logPos;
} Dbg_Timer;
void Dbg_initTimer(Dbg_Timer*);
void Dbg_startTimer(Dbg_Timer*, const char* name);
void Dbg_stopTimer(Dbg_Timer*);
void Dbg_printTimerLog(Dbg_Timer*);
void Dbg_freeTimer(Dbg_Timer*);
typedef struct Dbg_FPSCounter {
int count;
clock_t start;
char* log;
} Dbg_FPSCounter;
void Dbg_initFPSCounter(Dbg_FPSCounter*);
void Dbg_tickFPSCounter(Dbg_FPSCounter*);
void Dbg_printFPSCounter(Dbg_FPSCounter*);
void Dbg_freeFPSCounter(Dbg_FPSCounter*);

View File

@@ -2,9 +2,7 @@
#define SDL_MAIN_HANDLED #define SDL_MAIN_HANDLED
#include "box_engine.h" #include "box_engine.h"
#include "toy_drive_system.h"
//the runner library needs a little more setup since it accesses the disk
#include "lib_runner.h"
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
//debugging tools //debugging tools