Update debug data architecture and methods

This commit is contained in:
Doyle Thai 2016-07-09 20:46:04 +10:00
parent ea50aeb44e
commit 1f364f7024
13 changed files with 169 additions and 89 deletions

View File

@ -122,6 +122,7 @@
<ItemGroup> <ItemGroup>
<ClCompile Include="src\AssetManager.c" /> <ClCompile Include="src\AssetManager.c" />
<ClCompile Include="src\Common.c" /> <ClCompile Include="src\Common.c" />
<ClCompile Include="src\Debug.c" />
<ClCompile Include="src\dengine.c" /> <ClCompile Include="src\dengine.c" />
<ClCompile Include="src\Platform.c" /> <ClCompile Include="src\Platform.c" />
<ClCompile Include="src\Renderer.c" /> <ClCompile Include="src\Renderer.c" />
@ -137,6 +138,7 @@
<None Include="data\shaders\sprite.vert.glsl" /> <None Include="data\shaders\sprite.vert.glsl" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="src\include\Dengine\Debug.h" />
<ClInclude Include="src\include\Dengine\Platform.h" /> <ClInclude Include="src\include\Dengine\Platform.h" />
<ClInclude Include="src\include\Dengine\AssetManager.h" /> <ClInclude Include="src\include\Dengine\AssetManager.h" />
<ClInclude Include="src\include\Dengine\Common.h" /> <ClInclude Include="src\include\Dengine\Common.h" />

View File

@ -42,6 +42,9 @@
<ClCompile Include="src\Common.c"> <ClCompile Include="src\Common.c">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="src\Debug.c">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="data\shaders\default.vert.glsl" /> <None Include="data\shaders\default.vert.glsl" />
@ -83,5 +86,8 @@
<ClInclude Include="src\include\Dengine\Platform.h"> <ClInclude Include="src\include\Dengine\Platform.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="src\include\Dengine\Debug.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -8,6 +8,7 @@
#include "Dengine/Platform.h" #include "Dengine/Platform.h"
#include "Dengine/AssetManager.h" #include "Dengine/AssetManager.h"
#include "Dengine/Debug.h"
//#define WT_RENDER_FONT_FILE //#define WT_RENDER_FONT_FILE
#ifdef 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) if (type < texlist_count)
return &assetManager->textures[type]; return &assetManager->textures[type];
#ifdef WT_DEBUG #ifdef DENGINE_DEBUG
ASSERT(INVALID_CODE_PATH); ASSERT(INVALID_CODE_PATH);
#endif #endif
@ -32,7 +33,7 @@ TexAtlas *asset_getTextureAtlas(AssetManager *assetManager, const enum TexList t
if (type < texlist_count) if (type < texlist_count)
return &assetManager->texAtlas[type]; return &assetManager->texAtlas[type];
#ifdef WT_DEBUG #ifdef DENGINE_DEBUG
ASSERT(INVALID_CODE_PATH); ASSERT(INVALID_CODE_PATH);
#endif #endif
return NULL; return NULL;
@ -67,7 +68,7 @@ Shader *asset_getShader(AssetManager *assetManager, const enum ShaderList type)
if (type < shaderlist_count) if (type < shaderlist_count)
return &assetManager->shaders[type]; return &assetManager->shaders[type];
#ifdef WT_DEBUG #ifdef DENGINE_DEBUG
ASSERT(INVALID_CODE_PATH); ASSERT(INVALID_CODE_PATH);
#endif #endif
return NULL; return NULL;
@ -214,7 +215,7 @@ const i32 asset_loadTTFont(AssetManager *assetManager, const char *filePath)
if (width > largestGlyphDimension.w) if (width > largestGlyphDimension.w)
largestGlyphDimension.w = width; largestGlyphDimension.w = width;
#ifdef WT_DEBUG #ifdef DENGINE_DEBUG
if ((largestGlyphDimension.h - CAST(i32)targetFontHeight) >= 50) if ((largestGlyphDimension.h - CAST(i32)targetFontHeight) >= 50)
{ {
printf( printf(
@ -237,7 +238,7 @@ const i32 asset_loadTTFont(AssetManager *assetManager, const char *filePath)
font->maxSize = largestGlyphDimension; font->maxSize = largestGlyphDimension;
i32 glyphsPerRow = (MAX_TEXTURE_SIZE / font->maxSize.w); i32 glyphsPerRow = (MAX_TEXTURE_SIZE / font->maxSize.w);
#ifdef WT_DEBUG #ifdef DENGINE_DEBUG
i32 glyphsPerCol = MAX_TEXTURE_SIZE / font->maxSize.h; i32 glyphsPerCol = MAX_TEXTURE_SIZE / font->maxSize.h;
if ((glyphsPerRow * glyphsPerCol) <= numGlyphs) if ((glyphsPerRow * glyphsPerCol) <= numGlyphs)
{ {
@ -278,7 +279,7 @@ const i32 asset_loadTTFont(AssetManager *assetManager, const char *filePath)
if (verticalPixelsBlitted == 0) if (verticalPixelsBlitted == 0)
{ {
TexAtlas *fontAtlas = &assetManager->texAtlas[texlist_font]; TexAtlas *fontAtlas = &assetManager->texAtlas[texlist_font];
#ifdef WT_DEBUG #ifdef DENGINE_DEBUG
ASSERT(activeGlyph.codepoint < ARRAY_COUNT(fontAtlas->texRect)); ASSERT(activeGlyph.codepoint < ARRAY_COUNT(fontAtlas->texRect));
#endif #endif

View File

@ -6,3 +6,16 @@ i32 common_strlen(const char *const string)
while (string[result]) result++; while (string[result]) result++;
return 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);
}

78
src/Debug.c Normal file
View File

@ -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;
}

View File

@ -4,8 +4,6 @@
#define RENDER_BOUNDING_BOX FALSE #define RENDER_BOUNDING_BOX FALSE
DebugRenderer debugRenderer = {0};
INTERNAL void updateBufferObject(Renderer *const renderer, INTERNAL void updateBufferObject(Renderer *const renderer,
RenderQuad *const quads, const i32 numQuads) 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, void renderer_entity(Renderer *renderer, v4 cameraBounds, Entity *entity, f32 dt, f32 rotate,
v4 color) v4 color)
{ {
@ -159,6 +138,10 @@ void renderer_object(Renderer *renderer, v2 pos, v2 size, f32 rotate, v4 color,
glBindVertexArray(renderer->vao); glBindVertexArray(renderer->vao);
glDrawArrays(GL_TRIANGLE_STRIP, 0, renderer->numVertexesInVbo); glDrawArrays(GL_TRIANGLE_STRIP, 0, renderer->numVertexesInVbo);
#ifdef DENGINE_DEBUG
#endif
glBindVertexArray(0); glBindVertexArray(0);

View File

@ -39,8 +39,8 @@ Texture genTexture(const GLuint width, const GLuint height,
tex.internalFormat = GL_RGBA; tex.internalFormat = GL_RGBA;
tex.wrapS = GL_REPEAT; tex.wrapS = GL_REPEAT;
tex.wrapT = GL_REPEAT; tex.wrapT = GL_REPEAT;
tex.filterMinification = GL_LINEAR; tex.filterMinification = GL_NEAREST;
tex.filterMagnification = GL_LINEAR; tex.filterMagnification = GL_NEAREST;
glGenTextures(1, &tex.id); glGenTextures(1, &tex.id);
glCheckError(); glCheckError();

View File

@ -1,5 +1,5 @@
#include "Dengine/AssetManager.h" #include "Dengine/AssetManager.h"
#include "Dengine/Math.h" #include "Dengine/Debug.h"
#include "WorldTraveller/WorldTraveller.h" #include "WorldTraveller/WorldTraveller.h"
//TODO(doyle): This is temporary! Maybe abstract into our platform layer, or //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) Texture *tex, b32 collides)
{ {
#ifdef WT_DEBUG #ifdef DENGINE_DEBUG
ASSERT(tex && world); ASSERT(tex && world);
ASSERT(world->freeEntityIndex < world->maxEntities); ASSERT(world->freeEntityIndex < world->maxEntities);
ASSERT(type < entitytype_count); 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) INTERNAL void addAnim(Entity *entity, v4 *rects, i32 numRects, f32 duration)
{ {
#ifdef WT_DEBUG #ifdef DENGINE_DEBUG
ASSERT(rects && numRects >= 0) ASSERT(rects && numRects >= 0)
ASSERT(entity->freeAnimIndex < ARRAY_COUNT(entity->anim)); ASSERT(entity->freeAnimIndex < ARRAY_COUNT(entity->anim));
#endif #endif
@ -102,8 +102,10 @@ void worldTraveller_gameInit(GameState *state, v2i windowSize)
TexAtlas *terrainAtlas = TexAtlas *terrainAtlas =
asset_getTextureAtlas(assetManager, texlist_terrain); asset_getTextureAtlas(assetManager, texlist_terrain);
f32 atlasTileSize = 128.0f; f32 atlasTileSize = 128.0f;
const i32 texSize = 1024;
v2 texOrigin = V2(0, CAST(f32)(texSize - 128));
terrainAtlas->texRect[terraincoords_ground] = 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", asset_loadShaderFiles(assetManager, "data/shaders/sprite.vert.glsl",
"data/shaders/sprite.frag.glsl", shaderlist_sprite); "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++) for (i32 x = 0; x < worldDimensionInTiles.x; x++)
{ {
#ifdef WT_DEBUG #ifdef DENGINE_DEBUG
ASSERT(worldDimensionInTiles.x * worldDimensionInTiles.y < ASSERT(worldDimensionInTiles.x * worldDimensionInTiles.y <
world->maxEntities); world->maxEntities);
#endif #endif
@ -415,36 +417,12 @@ void worldTraveller_gameUpdateAndRender(GameState *state, const f32 dt)
// TODO(doyle): Clean up lines // TODO(doyle): Clean up lines
// Renderer::~Renderer() { glDeleteVertexArrays(1, &this->quadVAO); } // Renderer::~Renderer() { glDeleteVertexArrays(1, &this->quadVAO); }
#ifdef WT_DEBUG #ifdef DENGINE_DEBUG
LOCAL_PERSIST f32 debugUpdateCounter = 0.0f;
LOCAL_PERSIST char debugStrings[256][64] = {0};
LOCAL_PERSIST i32 numDebugStrings = 0;
Font *font = &assetManager->font; Font *font = &assetManager->font;
if (debugUpdateCounter <= 0) Entity *hero = &world->entities[world->heroIndex];
{ DEBUG_PUSH_STRING("Hero Pos: %06.2f, %06.2f", &hero->pos, "v2");
numDebugStrings = 0; DEBUG_PUSH_STRING("Hero dPos: %06.2f, %06.2f", &hero->dPos, "v2");
Entity *const hero = &world->entities[world->heroIndex]; DEBUG_PUSH_STRING("FreeEntityIndex: %d", &world->freeEntityIndex, "i32");
snprintf(debugStrings[0], ARRAY_COUNT(debugStrings[0]), debug_stringUpdateAndRender(&state->renderer, font, dt);
"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;
#endif #endif
} }

View File

@ -2,6 +2,7 @@
#include "Dengine/AssetManager.h" #include "Dengine/AssetManager.h"
#include "Dengine/Renderer.h" #include "Dengine/Renderer.h"
#include "Dengine/Math.h" #include "Dengine/Math.h"
#include "Dengine/Debug.h"
#include "WorldTraveller/WorldTraveller.h" #include "WorldTraveller/WorldTraveller.h"
@ -75,6 +76,10 @@ int main()
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glCullFace(GL_BACK); glCullFace(GL_BACK);
#ifdef DENGINE_DEBUG
debug_init();
#endif
GameState worldTraveller = {0}; GameState worldTraveller = {0};
worldTraveller_gameInit(&worldTraveller, frameBufferSize); worldTraveller_gameInit(&worldTraveller, frameBufferSize);

View File

@ -3,8 +3,6 @@
#include <stdint.h> #include <stdint.h>
#define WT_DEBUG
typedef uint8_t u8; typedef uint8_t u8;
typedef uint32_t u32; typedef uint32_t u32;
typedef uint64_t u64; typedef uint64_t u64;
@ -26,10 +24,9 @@ typedef double f64;
#define CAST(type) (type) #define CAST(type) (type)
#define ASSERT(expr) if(!(expr)) { *(int *)0 = 0; } #define ASSERT(expr) if(!(expr)) { *(int *)0 = 0; }
#define DENGINE_DEBUG
i32 common_strlen(const char *const string); i32 common_strlen(const char *const string);
i32 common_strcmp(const char *a, const char *b);
#ifdef WT_DEBUG
#define INVALID_CODE_PATH TRUE
#endif
#endif #endif

View File

@ -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

View File

@ -14,32 +14,14 @@ typedef struct Renderer
v2 size; v2 size;
} Renderer; } Renderer;
typedef struct DebugRenderer
{
b32 init;
v2 stringPos;
} DebugRenderer;
typedef struct RenderQuad typedef struct RenderQuad
{ {
v4 vertex[4]; v4 vertex[4];
} RenderQuad; } 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, void renderer_string(Renderer *const renderer, Font *const font,
const char *const string, v2 pos, f32 rotate, v4 color); 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, void renderer_entity(Renderer *renderer, v4 cameraBounds, Entity *entity,
f32 dt, f32 rotate, v4 color); f32 dt, f32 rotate, v4 color);

View File

@ -1,8 +1,8 @@
#ifndef DENGINE_TEXTURE_H #ifndef DENGINE_TEXTURE_H
#define DENGINE_TEXTURE_H #define DENGINE_TEXTURE_H
#include "Dengine/Common.h"
#include "Dengine/OpenGL.h" #include "Dengine/OpenGL.h"
#include "Dengine/Common.h"
#define TARGET_TEXTURE_SIZE 1024 #define TARGET_TEXTURE_SIZE 1024
#define TARGET_BYTES_PER_PIXEL 4 #define TARGET_BYTES_PER_PIXEL 4