From 58763001fefd5bc0b1889b3fd26850200845831d Mon Sep 17 00:00:00 2001 From: Ratstail91 Date: Sat, 18 Mar 2023 02:15:47 +1100 Subject: [PATCH] Added some debugging tools --- Box.vcxproj | 2 + Toy | 2 +- assets/scripts/gameplay/scene.toy | 11 --- box/box_common.h | 3 + box/box_engine.c | 30 +++++++- box/dbg_profiler.c | 116 ++++++++++++++++++++++++++++++ box/dbg_profiler.h | 29 ++++++++ source/main.c | 4 +- 8 files changed, 179 insertions(+), 18 deletions(-) create mode 100644 box/dbg_profiler.c create mode 100644 box/dbg_profiler.h diff --git a/Box.vcxproj b/Box.vcxproj index 5d353f9..447d2b8 100644 --- a/Box.vcxproj +++ b/Box.vcxproj @@ -140,6 +140,7 @@ xcopy "$(SolutionDir)Toy\repl\repl_tools.*" "$(SolutionDir)box" /Y /I /E + @@ -153,6 +154,7 @@ xcopy "$(SolutionDir)Toy\repl\repl_tools.*" "$(SolutionDir)box" /Y /I /E + diff --git a/Toy b/Toy index 4b83f1f..17f0e44 160000 --- a/Toy +++ b/Toy @@ -1 +1 @@ -Subproject commit 4b83f1f0d691517594df5b2f3fc95862414ad205 +Subproject commit 17f0e4476bc6da2cbbc28f092ca847600f00a105 diff --git a/assets/scripts/gameplay/scene.toy b/assets/scripts/gameplay/scene.toy index f13dec8..51c848f 100644 --- a/assets/scripts/gameplay/scene.toy +++ b/assets/scripts/gameplay/scene.toy @@ -22,9 +22,6 @@ var collisionMap: [bool] = null; //cache this, since it won't change during a le var rng: opaque = null; -//debugging tools -var debugStepCounter: int = 0; -var debugDrawCounter: int = 0; //lifecycle functions fn onLoad(node: opaque) { @@ -43,18 +40,10 @@ fn onFree(node: opaque) { } fn onStep(node: opaque) { - if (++debugStepCounter >= 30) { - print "FPS: " + string debugDrawCounter + " / 30"; - debugStepCounter = 0; - debugDrawCounter = 0; - } - entities = entities.sort(depthComparator); } fn onDraw(node: opaque) { - debugDrawCounter++; - //call each child's custom draw fn (with parent shifting) tilemap.callNodeFn("customOnDraw", 0, 32); for (var i = 0; i < entities.length(); i++) { diff --git a/box/box_common.h b/box/box_common.h index 363c004..5e10b94 100644 --- a/box/box_common.h +++ b/box/box_common.h @@ -47,3 +47,6 @@ //test variable sizes based on platform #define STATIC_ASSERT(test_for_true) static_assert((test_for_true), "(" #test_for_true ") failed") + +//debugging +#include "dbg_profiler.h" diff --git a/box/box_engine.c b/box/box_engine.c index ce96a36..95cbb7d 100644 --- a/box/box_engine.c +++ b/box/box_engine.c @@ -419,14 +419,31 @@ void Box_execEngine() { engine.simTime = engine.realTime; clock_t delta = (double) CLOCKS_PER_SEC / 30.0; - while (engine.running) { - execLoadRootNode(); + Dbg_Timer dbgTimer; + 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(); + Dbg_stopTimer(&dbgTimer); //calc the time passed engine.realTime = clock(); + Dbg_startTimer(&dbgTimer, "execStep() (variable)"); //while not enough time has passed while(engine.simTime < engine.realTime) { //simulate the world @@ -435,13 +452,20 @@ void Box_execEngine() { //calc the time simulation engine.simTime += delta; } + Dbg_stopTimer(&dbgTimer); //render the world + Dbg_startTimer(&dbgTimer, "clear screen"); 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 + Dbg_stopTimer(&dbgTimer); + Dbg_startTimer(&dbgTimer, "draw screen"); Box_callRecursiveEngineNode(engine.rootNode, &engine.interpreter, "onDraw", NULL); - SDL_RenderPresent(engine.renderer); + Dbg_stopTimer(&dbgTimer); } + + Dbg_freeTimer(&dbgTimer); + Dbg_freeFPSCounter(&fps); } diff --git a/box/dbg_profiler.c b/box/dbg_profiler.c new file mode 100644 index 0000000..757d858 --- /dev/null +++ b/box/dbg_profiler.c @@ -0,0 +1,116 @@ +#include "dbg_profiler.h" + +#include "toy_memory.h" + +#include + +#ifdef _WIN32 +#define WIN32_LEAN_AND_MEAN +#include + +//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 +#include + +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); +} \ No newline at end of file diff --git a/box/dbg_profiler.h b/box/dbg_profiler.h new file mode 100644 index 0000000..92c9f7b --- /dev/null +++ b/box/dbg_profiler.h @@ -0,0 +1,29 @@ +#pragma once + +#include + +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*); diff --git a/source/main.c b/source/main.c index baea9a6..bbd9ff3 100644 --- a/source/main.c +++ b/source/main.c @@ -2,9 +2,7 @@ #define SDL_MAIN_HANDLED #include "box_engine.h" - -//the runner library needs a little more setup since it accesses the disk -#include "lib_runner.h" +#include "toy_drive_system.h" int main(int argc, char* argv[]) { //debugging tools