From 4f9ea0a09a9399e693b5e54985fc00bc5607b75d Mon Sep 17 00:00:00 2001 From: Edgaru089 Date: Tue, 5 Mar 2024 14:17:53 +0800 Subject: [PATCH] Debug text & debug build options use 'winedbg --gdb ' to start debugger --- Makefile.flags | 6 +++--- app.h | 3 +++ app_debug.c | 30 ++++++++++++++++++++++++++++++ main.cpp | 11 +++++++++++ physics.c | 4 ++-- render_util.cpp | 44 ++++++++++++++++++++++++++++++++++++++++++++ render_util.h | 20 ++++++++++++++++++++ types.h | 2 +- util/vector.c | 2 +- 9 files changed, 115 insertions(+), 7 deletions(-) create mode 100644 app_debug.c create mode 100644 render_util.cpp create mode 100644 render_util.h diff --git a/Makefile.flags b/Makefile.flags index 85700e7..d3cc648 100644 --- a/Makefile.flags +++ b/Makefile.flags @@ -38,9 +38,9 @@ endif export INCLUDEFLAGS := $(Platform_INCLUDEFLAGS) export CPPFLAGS = -DWIN32_LEAN_AND_MEAN -export CFLAGS = $(INCLUDEFLAGS) $(BUILD_OPTIONS) -O2 -Wno-attributes -flto -export CXXFLAGS = $(INCLUDEFLAGS) -DHELOS $(BUILD_OPTIONS) $(Platform_CXXFLAGS) -O2 -Wno-unused-result -flto -std=c++17 -export LDFLAGS = $(Platform_LDFLAGS) -s -O2 -flto +export CFLAGS = $(INCLUDEFLAGS) $(BUILD_OPTIONS) -Wno-attributes -g +export CXXFLAGS = $(INCLUDEFLAGS) -DHELOS $(BUILD_OPTIONS) $(Platform_CXXFLAGS) -Wno-unused-result -std=c++17 -g +export LDFLAGS = $(Platform_LDFLAGS) -g export LDLIBS = $(Platform_LDLIBS) -lm -lstdc++ # Pattern rule for FASM assembly diff --git a/app.h b/app.h index f79a841..1d35789 100644 --- a/app.h +++ b/app.h @@ -5,6 +5,7 @@ #include "player.h" #include "input.h" #include "types.h" +#include "util/vector.h" #ifdef __cplusplus extern "C" { @@ -26,6 +27,8 @@ void app_DeleteApp(App *app); void app_Advance(App *app, Duration deltaTime); void app_Render(App *app); +void app_DebugText(App *app, vector_Vector *vec_string); + #ifdef __cplusplus } diff --git a/app_debug.c b/app_debug.c new file mode 100644 index 0000000..4464371 --- /dev/null +++ b/app_debug.c @@ -0,0 +1,30 @@ + +#include "app.h" +#include "util/vector.h" + +#include + + +#define PUSH_STRING(str) vector_Append(vec_string, str, strlen(str)); + +void app_DebugText(App *app, vector_Vector *vec_string) { + vector_Clear(vec_string); + char buf[256] = {}; + + Component_Player *player = app->player->player; + if (!player) { + PUSH_STRING("Player not available\n"); + } else { + snprintf( + buf, sizeof(buf) - 1, + "Player:\n Pos: [%.4lf, %.4lf], Vec:[%.4lf, %.4lf]\n", + player->super->position->position.x, + player->super->position->position.y, + player->super->position->velocity.x, + player->super->position->velocity.y); + PUSH_STRING(buf); + } + + char zero = '\0'; + vector_Push(vec_string, &zero); +} diff --git a/main.cpp b/main.cpp index f74c763..74ac45c 100644 --- a/main.cpp +++ b/main.cpp @@ -3,7 +3,10 @@ #include #include "app.h" +#include "easyx.h" #include "types.h" +#include "render_util.h" +#include "util/vector.h" int main() { @@ -15,6 +18,9 @@ int main() { HWND win = initgraph(1280, 720); SetWindowTextA(win, "JacksEscape"); + settextstyle(TEXTHEIGHT, 0, "Courier New"); + vector_Vector *debugText = vector_Create(1); + App *app = app_NewApp(); while (!app->wantQuit) { if (time_Since(frameCounter).microseconds >= 1000000) { // 1 sec @@ -31,8 +37,13 @@ int main() { BeginBatchDraw(); cleardevice(); app_Render(app); + + app_DebugText(app, debugText); + render_DrawText(10, 10, (const char *)vector_Data(debugText)); + EndBatchDraw(); + Duration toSleep = {.microseconds = 1000000 / 60 - time_Reset(&lastFrame).microseconds}; duration_Sleep(toSleep); } diff --git a/physics.c b/physics.c index af2bac8..b6375c7 100644 --- a/physics.c +++ b/physics.c @@ -94,8 +94,8 @@ void physics_Advance(System_Physics *sys, Duration deltaTime) { i = tree_Node_Next(i)) { Component_Position *pos = *((Component_Position **)i->data); - // if (pos->super->hitbox && !pos->super->hitbox->fixed) - // pos->velocity.y += gravity * duration_Seconds(deltaTime); + if (pos->super->hitbox && !pos->super->hitbox->fixed) + pos->velocity.y += gravity * duration_Seconds(deltaTime); _physics_AdvanceEntity(sys, pos->super, deltaTime); } diff --git a/render_util.cpp b/render_util.cpp new file mode 100644 index 0000000..876aaa3 --- /dev/null +++ b/render_util.cpp @@ -0,0 +1,44 @@ + +#include "render_util.h" +#include "easyx.h" +#include "util/vector.h" +#include +#include + + +extern "C" { + + +static vector_Vector *tbuf; + +void render_DrawText(int x, int y, const char *str) { + if (!tbuf) + tbuf = vector_Create(1); + + int cx = x, cy = y; + const char zero = 0; + + vector_Clear(tbuf); + int len = strlen(str); + printf("%s, len=%d\n", str, len); + int i = 0; + while (i < len) { + if (str[i] == '\n') { + vector_Push(tbuf, &zero); + printf("outtext: \"%s\"\n", vector_Data(tbuf)); + outtextxy(cx, cy, (LPCTSTR)vector_Data(tbuf)); + + cy += TEXTHEIGHT; + vector_Clear(tbuf); + } else + vector_Push(tbuf, &str[i]); + i++; + } + + if (vector_Size(tbuf) > 0) { + vector_Push(tbuf, &zero); + outtextxy(cx, cy, (LPCTSTR)vector_Data(tbuf)); + vector_Clear(tbuf); + } +} +} diff --git a/render_util.h b/render_util.h new file mode 100644 index 0000000..831c3b9 --- /dev/null +++ b/render_util.h @@ -0,0 +1,20 @@ +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif + + +#define TEXTHEIGHT 18 + + +// Draws text at the given on-screen position. +// +// unlike outtext(), this function takes consideration +// for newlines. +void render_DrawText(int x, int y, const char *str); + + +#ifdef __cplusplus +} +#endif diff --git a/types.h b/types.h index b701c8a..1c8c10b 100644 --- a/types.h +++ b/types.h @@ -23,7 +23,7 @@ typedef struct { } Vec2; -inline Vec2 vec2(double x, double y) { +static inline Vec2 vec2(double x, double y) { Vec2 v = {.x = x, .y = y}; return v; } diff --git a/util/vector.c b/util/vector.c index e64975a..e6db647 100644 --- a/util/vector.c +++ b/util/vector.c @@ -59,7 +59,7 @@ void vector_Append(vector_Vector *vec, const void *data, uintptr_t n) { } void vector_Resize(vector_Vector *vec, uintptr_t size) { - uintptr_t newsize = vec->size + size * vec->objectSize; + uintptr_t newsize = size; if (newsize > vec->cap) { // grow the buffer exponentially uint64_t newcap = vec->cap;