From 1f364f7024c359d7791229ca4bafc76580ef3d63 Mon Sep 17 00:00:00 2001 From: Doyle Thai Date: Sat, 9 Jul 2016 20:46:04 +1000 Subject: [PATCH] Update debug data architecture and methods --- Dengine.vcxproj | 2 + Dengine.vcxproj.filters | 6 +++ src/AssetManager.c | 13 +++--- src/Common.c | 13 ++++++ src/Debug.c | 78 ++++++++++++++++++++++++++++++++++ src/Renderer.c | 25 ++--------- src/Texture.c | 4 +- src/WorldTraveller.c | 48 ++++++--------------- src/dengine.c | 5 +++ src/include/Dengine/Common.h | 9 ++-- src/include/Dengine/Debug.h | 35 +++++++++++++++ src/include/Dengine/Renderer.h | 18 -------- src/include/Dengine/Texture.h | 2 +- 13 files changed, 169 insertions(+), 89 deletions(-) create mode 100644 src/Debug.c create mode 100644 src/include/Dengine/Debug.h diff --git a/Dengine.vcxproj b/Dengine.vcxproj index 1e69025..b86b50e 100644 --- a/Dengine.vcxproj +++ b/Dengine.vcxproj @@ -122,6 +122,7 @@ + @@ -137,6 +138,7 @@ + diff --git a/Dengine.vcxproj.filters b/Dengine.vcxproj.filters index 4f6acca..812b986 100644 --- a/Dengine.vcxproj.filters +++ b/Dengine.vcxproj.filters @@ -42,6 +42,9 @@ Source Files + + Source Files + @@ -83,5 +86,8 @@ Header Files + + Header Files + \ No newline at end of file diff --git a/src/AssetManager.c b/src/AssetManager.c index f979ee4..f75fff4 100644 --- a/src/AssetManager.c +++ b/src/AssetManager.c @@ -8,6 +8,7 @@ #include "Dengine/Platform.h" #include "Dengine/AssetManager.h" +#include "Dengine/Debug.h" //#define WT_RENDER_FONT_FILE #ifdef WT_RENDER_FONT_FILE @@ -20,7 +21,7 @@ Texture *asset_getTexture(AssetManager *assetManager, const enum TexList type) if (type < texlist_count) return &assetManager->textures[type]; -#ifdef WT_DEBUG +#ifdef DENGINE_DEBUG ASSERT(INVALID_CODE_PATH); #endif @@ -32,7 +33,7 @@ TexAtlas *asset_getTextureAtlas(AssetManager *assetManager, const enum TexList t if (type < texlist_count) return &assetManager->texAtlas[type]; -#ifdef WT_DEBUG +#ifdef DENGINE_DEBUG ASSERT(INVALID_CODE_PATH); #endif return NULL; @@ -67,7 +68,7 @@ Shader *asset_getShader(AssetManager *assetManager, const enum ShaderList type) if (type < shaderlist_count) return &assetManager->shaders[type]; -#ifdef WT_DEBUG +#ifdef DENGINE_DEBUG ASSERT(INVALID_CODE_PATH); #endif return NULL; @@ -214,7 +215,7 @@ const i32 asset_loadTTFont(AssetManager *assetManager, const char *filePath) if (width > largestGlyphDimension.w) largestGlyphDimension.w = width; -#ifdef WT_DEBUG +#ifdef DENGINE_DEBUG if ((largestGlyphDimension.h - CAST(i32)targetFontHeight) >= 50) { printf( @@ -237,7 +238,7 @@ const i32 asset_loadTTFont(AssetManager *assetManager, const char *filePath) font->maxSize = largestGlyphDimension; i32 glyphsPerRow = (MAX_TEXTURE_SIZE / font->maxSize.w); -#ifdef WT_DEBUG +#ifdef DENGINE_DEBUG i32 glyphsPerCol = MAX_TEXTURE_SIZE / font->maxSize.h; if ((glyphsPerRow * glyphsPerCol) <= numGlyphs) { @@ -278,7 +279,7 @@ const i32 asset_loadTTFont(AssetManager *assetManager, const char *filePath) if (verticalPixelsBlitted == 0) { TexAtlas *fontAtlas = &assetManager->texAtlas[texlist_font]; -#ifdef WT_DEBUG +#ifdef DENGINE_DEBUG ASSERT(activeGlyph.codepoint < ARRAY_COUNT(fontAtlas->texRect)); #endif diff --git a/src/Common.c b/src/Common.c index 00c26c7..57960df 100644 --- a/src/Common.c +++ b/src/Common.c @@ -6,3 +6,16 @@ i32 common_strlen(const char *const string) while (string[result]) result++; return result; } + +i32 common_strcmp(const char *a, const char *b) +{ + while (*a == *b) + { + if (!*a) + return 0; + a++; + b++; + } + + return ((*a < *b) ? -1 : 1); +} diff --git a/src/Debug.c b/src/Debug.c new file mode 100644 index 0000000..51f8be1 --- /dev/null +++ b/src/Debug.c @@ -0,0 +1,78 @@ +#include "Dengine/Debug.h" + +DebugState GLOBAL_debugState; + +void debug_init() +{ + GLOBAL_debugState.numDebugStrings = 0; + GLOBAL_debugState.stringUpdateTimer = 0.0f; + GLOBAL_debugState.stringUpdateRate = 0.15f; + + GLOBAL_debugState.stringLineGap = -1; +} + +void debug_pushString(char *formatString, void *data, char *dataType) +{ + if (GLOBAL_debugState.stringUpdateTimer <= 0) + { + i32 numDebugStrings = GLOBAL_debugState.numDebugStrings; + if (common_strcmp(dataType, "v2") == 0) + { + v2 val = *(CAST(v2 *) data); + snprintf(GLOBAL_debugState.debugStrings[numDebugStrings], + ARRAY_COUNT(GLOBAL_debugState.debugStrings[0]), + formatString, val.x, val.y); + } + else if (common_strcmp(dataType, "i32") == 0) + { + i32 val = *(CAST(i32 *) data); + snprintf(GLOBAL_debugState.debugStrings[numDebugStrings], + ARRAY_COUNT(GLOBAL_debugState.debugStrings[0]), + formatString, val); + } + else + { + ASSERT(INVALID_CODE_PATH); + } + GLOBAL_debugState.numDebugStrings++; + } +} + +void debug_stringUpdateAndRender(Renderer *renderer, Font *font, f32 dt) +{ + if (GLOBAL_debugState.stringLineGap == -1) + { + GLOBAL_debugState.stringLineGap = + 1.1f * asset_getVFontSpacing(font->metrics); + GLOBAL_debugState.initialStringPos = + V2(0.0f, + (renderer->size.y - 1.8f * GLOBAL_debugState.stringLineGap)); + GLOBAL_debugState.stringPos = GLOBAL_debugState.initialStringPos; + } + + for (i32 i = 0; i < GLOBAL_debugState.numDebugStrings; i++) + { + f32 rotate = 0; + v4 color = V4(0, 0, 0, 1); + renderer_string(renderer, font, GLOBAL_debugState.debugStrings[i], + GLOBAL_debugState.stringPos, rotate, color); + GLOBAL_debugState.stringPos.y -= + (0.9f * GLOBAL_debugState.stringLineGap); + } + + if (GLOBAL_debugState.stringUpdateTimer <= 0) + { + GLOBAL_debugState.stringUpdateTimer = + GLOBAL_debugState.stringUpdateRate; + } + else + { + GLOBAL_debugState.stringUpdateTimer -= dt; + if (GLOBAL_debugState.stringUpdateTimer <= 0) + { + GLOBAL_debugState.numDebugStrings = 0; + } + } + + GLOBAL_debugState.stringPos = GLOBAL_debugState.initialStringPos; +} diff --git a/src/Renderer.c b/src/Renderer.c index 6e2226b..f59b322 100644 --- a/src/Renderer.c +++ b/src/Renderer.c @@ -4,8 +4,6 @@ #define RENDER_BOUNDING_BOX FALSE -DebugRenderer debugRenderer = {0}; - INTERNAL void updateBufferObject(Renderer *const renderer, RenderQuad *const quads, const i32 numQuads) { @@ -61,25 +59,6 @@ void renderer_string(Renderer *const renderer, Font *const font, } -void renderer_debugString(Renderer *const renderer, Font *const font, - const char *const string) -{ - /* Intialise debug object */ - if (!debugRenderer.init) - { - debugRenderer.stringPos = - V2(0.0f, renderer->size.y - - (1.8f * asset_getVFontSpacing(font->metrics))); - debugRenderer.init = TRUE; - } - - f32 rotate = 0; - v4 color = V4(0, 0, 0, 1); - renderer_string(renderer, font, string, debugRenderer.stringPos, rotate, - color); - debugRenderer.stringPos.y -= (0.9f * asset_getVFontSpacing(font->metrics)); -} - void renderer_entity(Renderer *renderer, v4 cameraBounds, Entity *entity, f32 dt, f32 rotate, v4 color) { @@ -159,6 +138,10 @@ void renderer_object(Renderer *renderer, v2 pos, v2 size, f32 rotate, v4 color, glBindVertexArray(renderer->vao); glDrawArrays(GL_TRIANGLE_STRIP, 0, renderer->numVertexesInVbo); + +#ifdef DENGINE_DEBUG +#endif + glBindVertexArray(0); diff --git a/src/Texture.c b/src/Texture.c index 52bde67..a094efd 100644 --- a/src/Texture.c +++ b/src/Texture.c @@ -39,8 +39,8 @@ Texture genTexture(const GLuint width, const GLuint height, tex.internalFormat = GL_RGBA; tex.wrapS = GL_REPEAT; tex.wrapT = GL_REPEAT; - tex.filterMinification = GL_LINEAR; - tex.filterMagnification = GL_LINEAR; + tex.filterMinification = GL_NEAREST; + tex.filterMagnification = GL_NEAREST; glGenTextures(1, &tex.id); glCheckError(); diff --git a/src/WorldTraveller.c b/src/WorldTraveller.c index c42d25a..c356eb6 100644 --- a/src/WorldTraveller.c +++ b/src/WorldTraveller.c @@ -1,5 +1,5 @@ #include "Dengine/AssetManager.h" -#include "Dengine/Math.h" +#include "Dengine/Debug.h" #include "WorldTraveller/WorldTraveller.h" //TODO(doyle): This is temporary! Maybe abstract into our platform layer, or @@ -11,7 +11,7 @@ INTERNAL Entity *addEntity(World *world, v2 pos, v2 size, Texture *tex, b32 collides) { -#ifdef WT_DEBUG +#ifdef DENGINE_DEBUG ASSERT(tex && world); ASSERT(world->freeEntityIndex < world->maxEntities); ASSERT(type < entitytype_count); @@ -34,7 +34,7 @@ INTERNAL Entity *addEntity(World *world, v2 pos, v2 size, INTERNAL void addAnim(Entity *entity, v4 *rects, i32 numRects, f32 duration) { -#ifdef WT_DEBUG +#ifdef DENGINE_DEBUG ASSERT(rects && numRects >= 0) ASSERT(entity->freeAnimIndex < ARRAY_COUNT(entity->anim)); #endif @@ -102,8 +102,10 @@ void worldTraveller_gameInit(GameState *state, v2i windowSize) TexAtlas *terrainAtlas = asset_getTextureAtlas(assetManager, texlist_terrain); f32 atlasTileSize = 128.0f; + const i32 texSize = 1024; + v2 texOrigin = V2(0, CAST(f32)(texSize - 128)); terrainAtlas->texRect[terraincoords_ground] = - V4(384.0f, 512.0f, 384.0f + atlasTileSize, 512.0f + atlasTileSize); + V4(texOrigin.x, texOrigin.y, texOrigin.x + atlasTileSize, texOrigin.y - atlasTileSize); asset_loadShaderFiles(assetManager, "data/shaders/sprite.vert.glsl", "data/shaders/sprite.frag.glsl", shaderlist_sprite); @@ -144,7 +146,7 @@ void worldTraveller_gameInit(GameState *state, v2i windowSize) { for (i32 x = 0; x < worldDimensionInTiles.x; x++) { -#ifdef WT_DEBUG +#ifdef DENGINE_DEBUG ASSERT(worldDimensionInTiles.x * worldDimensionInTiles.y < world->maxEntities); #endif @@ -415,36 +417,12 @@ void worldTraveller_gameUpdateAndRender(GameState *state, const f32 dt) // TODO(doyle): Clean up lines // Renderer::~Renderer() { glDeleteVertexArrays(1, &this->quadVAO); } -#ifdef WT_DEBUG - LOCAL_PERSIST f32 debugUpdateCounter = 0.0f; - LOCAL_PERSIST char debugStrings[256][64] = {0}; - LOCAL_PERSIST i32 numDebugStrings = 0; - +#ifdef DENGINE_DEBUG Font *font = &assetManager->font; - if (debugUpdateCounter <= 0) - { - numDebugStrings = 0; - Entity *const hero = &world->entities[world->heroIndex]; - snprintf(debugStrings[0], ARRAY_COUNT(debugStrings[0]), - "Hero Pos: %06.2f,%06.2f", hero->pos.x, hero->pos.y); - numDebugStrings++; - - snprintf(debugStrings[1], ARRAY_COUNT(debugStrings[1]), - "Hero dPos: %06.2f,%06.2f", hero->dPos.x, hero->dPos.y); - numDebugStrings++; - - snprintf(debugStrings[2], ARRAY_COUNT(debugStrings[2]), - "FreeEntityIndex: %d", world->freeEntityIndex); - numDebugStrings++; - - const f32 debugUpdateRate = 0.15f; - debugUpdateCounter = debugUpdateRate; - } - - for (i32 i = 0; i < numDebugStrings; i++) - renderer_debugString(&state->renderer, font, debugStrings[i]); - - debugUpdateCounter -= dt; - debugRenderer.init = FALSE; + Entity *hero = &world->entities[world->heroIndex]; + DEBUG_PUSH_STRING("Hero Pos: %06.2f, %06.2f", &hero->pos, "v2"); + DEBUG_PUSH_STRING("Hero dPos: %06.2f, %06.2f", &hero->dPos, "v2"); + DEBUG_PUSH_STRING("FreeEntityIndex: %d", &world->freeEntityIndex, "i32"); + debug_stringUpdateAndRender(&state->renderer, font, dt); #endif } diff --git a/src/dengine.c b/src/dengine.c index 3a2d575..2f225b8 100644 --- a/src/dengine.c +++ b/src/dengine.c @@ -2,6 +2,7 @@ #include "Dengine/AssetManager.h" #include "Dengine/Renderer.h" #include "Dengine/Math.h" +#include "Dengine/Debug.h" #include "WorldTraveller/WorldTraveller.h" @@ -75,6 +76,10 @@ int main() glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glCullFace(GL_BACK); +#ifdef DENGINE_DEBUG + debug_init(); +#endif + GameState worldTraveller = {0}; worldTraveller_gameInit(&worldTraveller, frameBufferSize); diff --git a/src/include/Dengine/Common.h b/src/include/Dengine/Common.h index 7840bfe..c874636 100644 --- a/src/include/Dengine/Common.h +++ b/src/include/Dengine/Common.h @@ -3,8 +3,6 @@ #include -#define WT_DEBUG - typedef uint8_t u8; typedef uint32_t u32; typedef uint64_t u64; @@ -26,10 +24,9 @@ typedef double f64; #define CAST(type) (type) #define ASSERT(expr) if(!(expr)) { *(int *)0 = 0; } +#define DENGINE_DEBUG + i32 common_strlen(const char *const string); - -#ifdef WT_DEBUG -#define INVALID_CODE_PATH TRUE -#endif +i32 common_strcmp(const char *a, const char *b); #endif diff --git a/src/include/Dengine/Debug.h b/src/include/Dengine/Debug.h new file mode 100644 index 0000000..0f1cd00 --- /dev/null +++ b/src/include/Dengine/Debug.h @@ -0,0 +1,35 @@ +#ifndef DENGINE_DEBUG_H +#define DENGINE_DEBUG_H + +#include "Dengine/Renderer.h" + +#define INVALID_CODE_PATH TRUE +#define DEBUG_PUSH_STRING(formatString, data, type) \ + debug_pushString(formatString, CAST(void *) data, type) + +enum DebugCallCount +{ + debugcallcount_drawArrays, + debugcallcount_num, +}; + +typedef struct DebugState +{ + /* Debug strings rendered in top left corner */ + char debugStrings[256][64]; + i32 numDebugStrings; + f32 stringUpdateTimer; + f32 stringUpdateRate; + + v2 initialStringPos; + v2 stringPos; + + f32 stringLineGap; +} DebugState; + +extern DebugState GLOBAL_debugState; + +void debug_init(); +void debug_pushString(char *formatString, void *data, char *dataType); +void debug_stringUpdateAndRender(Renderer *renderer, Font *font, f32 dt); +#endif diff --git a/src/include/Dengine/Renderer.h b/src/include/Dengine/Renderer.h index edbbd05..e8fa585 100644 --- a/src/include/Dengine/Renderer.h +++ b/src/include/Dengine/Renderer.h @@ -14,32 +14,14 @@ typedef struct Renderer v2 size; } Renderer; -typedef struct DebugRenderer -{ - b32 init; - v2 stringPos; - -} DebugRenderer; - typedef struct RenderQuad { v4 vertex[4]; } RenderQuad; -extern DebugRenderer debugRenderer; - -#if 0 -void renderer_backgroundTiles(Renderer *const renderer, const v2 tileSize, - World *const world, TexAtlas *const atlasTexture, - Texture *const tex); -#endif - void renderer_string(Renderer *const renderer, Font *const font, const char *const string, v2 pos, f32 rotate, v4 color); -void renderer_debugString(Renderer *const renderer, Font *const font, - const char *const string); - void renderer_entity(Renderer *renderer, v4 cameraBounds, Entity *entity, f32 dt, f32 rotate, v4 color); diff --git a/src/include/Dengine/Texture.h b/src/include/Dengine/Texture.h index 0f302c6..22299ef 100644 --- a/src/include/Dengine/Texture.h +++ b/src/include/Dengine/Texture.h @@ -1,8 +1,8 @@ #ifndef DENGINE_TEXTURE_H #define DENGINE_TEXTURE_H -#include "Dengine/Common.h" #include "Dengine/OpenGL.h" +#include "Dengine/Common.h" #define TARGET_TEXTURE_SIZE 1024 #define TARGET_BYTES_PER_PIXEL 4