Added some debugging tools
This commit is contained in:
@@ -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"
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
116
box/dbg_profiler.c
Normal file
116
box/dbg_profiler.c
Normal 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
29
box/dbg_profiler.h
Normal 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*);
|
||||
Reference in New Issue
Block a user