From a426461dad4a862e74f0d4b05bd995f841335f2e Mon Sep 17 00:00:00 2001 From: Doyle Thai Date: Sat, 16 Jul 2016 23:27:52 +1000 Subject: [PATCH] Fix up include headers, try to reduce dependencies Ensure that all headers are self-sufficient instead of relying on included headers to include headers for execution. --- Dengine.vcxproj | 1 + Dengine.vcxproj.filters | 3 + src/Debug.c | 2 +- src/Platform.c | 32 ++++++++- src/Renderer.c | 30 +++++++-- src/Texture.c | 2 +- src/WorldTraveller.c | 15 +++-- src/dengine.c | 9 +-- src/include/Dengine/AssetManager.h | 70 +------------------ src/include/Dengine/Assets.h | 75 +++++++++++++++++++++ src/include/Dengine/Debug.h | 3 + src/include/Dengine/Entity.h | 3 +- src/include/Dengine/Platform.h | 45 ++----------- src/include/Dengine/Renderer.h | 30 ++------- src/include/Dengine/Shader.h | 2 +- src/include/Dengine/Texture.h | 2 +- src/include/WorldTraveller/WorldTraveller.h | 11 +-- 17 files changed, 176 insertions(+), 159 deletions(-) create mode 100644 src/include/Dengine/Assets.h diff --git a/Dengine.vcxproj b/Dengine.vcxproj index b86b50e..7d888bb 100644 --- a/Dengine.vcxproj +++ b/Dengine.vcxproj @@ -138,6 +138,7 @@ + diff --git a/Dengine.vcxproj.filters b/Dengine.vcxproj.filters index 6ba3d69..c9f3287 100644 --- a/Dengine.vcxproj.filters +++ b/Dengine.vcxproj.filters @@ -89,5 +89,8 @@ Header Files + + Header Files + \ No newline at end of file diff --git a/src/Debug.c b/src/Debug.c index c75ab69..084febc 100644 --- a/src/Debug.c +++ b/src/Debug.c @@ -1,5 +1,5 @@ -#include "Dengine/Platform.h" #include "Dengine/Debug.h" +#include "Dengine/Platform.h" DebugState GLOBAL_debugState; diff --git a/src/Platform.c b/src/Platform.c index be4d5d2..acad980 100644 --- a/src/Platform.c +++ b/src/Platform.c @@ -1,7 +1,37 @@ +#include +#include +#include + #include "Dengine/Platform.h" +#include "Dengine/Debug.h" + +void platform_memoryFree(void *data, i32 numBytes) +{ + if (data) free(data); + +#ifdef DENGINE_DEBUG + GLOBAL_debugState.totalMemoryAllocated -= numBytes; +#endif +} + +void *platform_memoryAlloc(i32 numBytes) +{ + void *result = calloc(1, numBytes); + +#ifdef DENGINE_DEBUG + if (result) + GLOBAL_debugState.totalMemoryAllocated += numBytes; +#endif + return result; +} + +void platform_closeFileRead(PlatformFileRead *file) +{ + PLATFORM_MEM_FREE(file->buffer, file->size); +} i32 platform_readFileToBuffer(const char *const filePath, - PlatformFileRead *file) + PlatformFileRead *file) { HANDLE fileHandle = CreateFile(filePath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_ALWAYS, 0, NULL); diff --git a/src/Renderer.c b/src/Renderer.c index 2829cd1..769bf48 100644 --- a/src/Renderer.c +++ b/src/Renderer.c @@ -1,10 +1,32 @@ -#include "Dengine/Platform.h" -#include "Dengine/OpenGL.h" #include "Dengine/Renderer.h" #include "Dengine/Debug.h" +#include "Dengine/OpenGL.h" +#include "Dengine/Platform.h" #define RENDER_BOUNDING_BOX FALSE +typedef struct RenderQuad +{ + v4 vertex[4]; +} RenderQuad; + +INTERNAL inline void flipTexCoord(v4 *texCoords, b32 flipX, b32 flipY) +{ + if (flipX) + { + v4 tmp = *texCoords; + texCoords->x = tmp.z; + texCoords->z = tmp.x; + } + + if (flipY) + { + v4 tmp = *texCoords; + texCoords->y = tmp.w; + texCoords->w = tmp.y; + } +} + INTERNAL void updateBufferObject(Renderer *const renderer, RenderQuad *const quads, const i32 numQuads) { @@ -174,7 +196,7 @@ void renderer_string(Renderer *const renderer, v4 cameraBounds, /* Get texture out */ v4 charTexRect = font->atlas->texRect[relativeIndex]; - renderer_flipTexCoord(&charTexRect, FALSE, TRUE); + flipTexCoord(&charTexRect, FALSE, TRUE); RenderTex renderTex = {font->tex, charTexRect}; RenderQuad charQuad = @@ -220,7 +242,7 @@ void renderer_entity(Renderer *renderer, v4 cameraBounds, Entity *entity, if (entity->direction == direction_east) { // NOTE(doyle): Flip the x coordinates to flip the tex - renderer_flipTexCoord(&texRect, TRUE, FALSE); + flipTexCoord(&texRect, TRUE, FALSE); } RenderTex renderTex = {entity->tex, texRect}; RenderQuad entityQuad = diff --git a/src/Texture.c b/src/Texture.c index 7b36088..584651f 100644 --- a/src/Texture.c +++ b/src/Texture.c @@ -1,4 +1,4 @@ -#include "Dengine\Texture.h" +#include "Dengine/Texture.h" enum BytesPerPixel { diff --git a/src/WorldTraveller.c b/src/WorldTraveller.c index e34bc82..2719cff 100644 --- a/src/WorldTraveller.c +++ b/src/WorldTraveller.c @@ -1,11 +1,14 @@ -#include "Dengine/Platform.h" -#include "Dengine/AssetManager.h" -#include "Dengine/Debug.h" - #include "WorldTraveller/WorldTraveller.h" -// TODO(doyle): This is temporary! Maybe abstract into our platform layer, or -// choose to load assets outside of WorldTraveller ! +#include "Dengine/Debug.h" +#include "Dengine/Platform.h" + +enum State +{ + state_active, + state_menu, + state_win, +}; INTERNAL Entity *addEntity(World *world, v2 pos, v2 size, enum EntityType type, enum Direction direction, Texture *tex, b32 collides) diff --git a/src/dengine.c b/src/dengine.c index 2f225b8..e9d45d3 100644 --- a/src/dengine.c +++ b/src/dengine.c @@ -1,8 +1,9 @@ #if 1 #include "Dengine/AssetManager.h" -#include "Dengine/Renderer.h" -#include "Dengine/Math.h" +#include "Dengine/Common.h" #include "Dengine/Debug.h" +#include "Dengine/Math.h" +#include "Dengine/OpenGL.h" #include "WorldTraveller/WorldTraveller.h" @@ -136,8 +137,8 @@ int main() f32 framesPerSecond = 1.0f / secondsElapsed; char textBuffer[256]; - snprintf(textBuffer, ARRAY_COUNT(textBuffer), "Dengine | %f ms/f | %f fps", msPerFrame, - framesPerSecond); + snprintf(textBuffer, ARRAY_COUNT(textBuffer), + "Dengine | %f ms/f | %f fps", msPerFrame, framesPerSecond); glfwSetWindowTitle(window, textBuffer); titleUpdateFrequencyInSeconds = 0.5f; diff --git a/src/include/Dengine/AssetManager.h b/src/include/Dengine/AssetManager.h index b2446e3..fdf1328 100644 --- a/src/include/Dengine/AssetManager.h +++ b/src/include/Dengine/AssetManager.h @@ -1,80 +1,12 @@ #ifndef DENGINE_ASSET_MANAGER_H #define DENGINE_ASSET_MANAGER_H +#include "Dengine/Assets.h" #include "Dengine/Shader.h" #include "Dengine/Texture.h" #define MAX_TEXTURE_SIZE 1024 -enum TexList -{ - texlist_empty, - texlist_hero, - texlist_terrain, - texlist_font, - texlist_count, -}; - -enum ShaderList -{ - shaderlist_sprite, - shaderlist_count, -}; - -enum TerrainCoords -{ - terraincoords_ground, - terraincoords_count, -}; - -enum HeroCoords -{ - herocoords_idle, - herocoords_walkA, - herocoords_walkB, - herocoords_head, - herocoords_waveA, - herocoords_waveB, - herocoords_count, -}; - -typedef struct TexAtlas -{ - // TODO(doyle): String hash based lookup - v4 texRect[128]; -} TexAtlas; - -// TODO(doyle): We only use the offset and advance metric at the moment, remove? -typedef struct FontMetrics -{ - i32 ascent; - i32 descent; - i32 lineGap; -} FontMetrics; - -typedef struct CharMetrics -{ - i32 advance; - i32 leftSideBearing; - // TODO(doyle): Utilise kerning - i32 *kerning; - v2i offset; - v2i trueSize; -} CharMetrics; - -typedef struct Font -{ - TexAtlas *atlas; - Texture *tex; - - FontMetrics metrics; - CharMetrics *charMetrics; - - v2i codepointRange; - v2i maxSize; - -} Font; - // TODO(doyle): Switch to hash based lookup typedef struct AssetManager { diff --git a/src/include/Dengine/Assets.h b/src/include/Dengine/Assets.h new file mode 100644 index 0000000..577f2ae --- /dev/null +++ b/src/include/Dengine/Assets.h @@ -0,0 +1,75 @@ +#ifndef DENGINE_ASSETS_H +#define DENGINE_ASSETS_H + +#include "Dengine/Math.h" +#include "Dengine/Texture.h" + +enum TexList +{ + texlist_empty, + texlist_hero, + texlist_terrain, + texlist_font, + texlist_count, +}; + +enum ShaderList +{ + shaderlist_sprite, + shaderlist_count, +}; + +enum TerrainCoords +{ + terraincoords_ground, + terraincoords_count, +}; + +enum HeroCoords +{ + herocoords_idle, + herocoords_walkA, + herocoords_walkB, + herocoords_head, + herocoords_waveA, + herocoords_waveB, + herocoords_count, +}; + +typedef struct TexAtlas +{ + // TODO(doyle): String hash based lookup + v4 texRect[128]; +} TexAtlas; + +// TODO(doyle): We only use the offset and advance metric at the moment, remove? +typedef struct FontMetrics +{ + i32 ascent; + i32 descent; + i32 lineGap; +} FontMetrics; + +typedef struct CharMetrics +{ + i32 advance; + i32 leftSideBearing; + // TODO(doyle): Utilise kerning + i32 *kerning; + v2i offset; + v2i trueSize; +} CharMetrics; + +typedef struct Font +{ + TexAtlas *atlas; + Texture *tex; + + FontMetrics metrics; + CharMetrics *charMetrics; + + v2i codepointRange; + v2i maxSize; + +} Font; +#endif diff --git a/src/include/Dengine/Debug.h b/src/include/Dengine/Debug.h index 11c4b20..b0f3714 100644 --- a/src/include/Dengine/Debug.h +++ b/src/include/Dengine/Debug.h @@ -1,6 +1,9 @@ #ifndef DENGINE_DEBUG_H #define DENGINE_DEBUG_H +#include "Dengine/Assets.h" +#include "Dengine/Common.h" +#include "Dengine/Math.h" #include "Dengine/Renderer.h" #define INVALID_CODE_PATH TRUE diff --git a/src/include/Dengine/Entity.h b/src/include/Dengine/Entity.h index b078914..6c74bb7 100644 --- a/src/include/Dengine/Entity.h +++ b/src/include/Dengine/Entity.h @@ -1,8 +1,9 @@ #ifndef DENGINE_ENTITY_H #define DENGINE_ENTITY_H -#include "Dengine/Texture.h" +#include "Dengine/Common.h" #include "Dengine/Math.h" +#include "Dengine/Texture.h" enum Direction { diff --git a/src/include/Dengine/Platform.h b/src/include/Dengine/Platform.h index 9d3485b..2052dd7 100644 --- a/src/include/Dengine/Platform.h +++ b/src/include/Dengine/Platform.h @@ -1,56 +1,25 @@ #ifndef DENGINE_PLATFORM_H #define DENGINE_PLATFORM_H -#include -#include -#include - #include "Dengine/Common.h" -#include "Dengine/Debug.h" -// TODO(doyle): Create own custom memory allocator -#define PLATFORM_MEM_ALLOC(num, type) \ - CAST(type *) platform_memoryAlloc(num * sizeof(type)) -#define PLATFORM_MEM_FREE(ptr, bytes) \ - platform_memoryFree(CAST(void *) ptr, bytes) - -typedef struct +typedef struct PlatformFileRead { void *buffer; i32 size; } PlatformFileRead; +// TODO(doyle): Create own custom memory allocator +#define PLATFORM_MEM_FREE(ptr, bytes) platform_memoryFree(CAST(void *) ptr, bytes) // TODO(doyle): numBytes in mem free is temporary until we create custom // allocator since we haven't put in a system to track memory usage per // allocation -inline void platform_memoryFree(void *data, i32 numBytes) -{ - if (data) free(data); - -#ifdef DENGINE_DEBUG - GLOBAL_debugState.totalMemoryAllocated -= numBytes; -#endif - -} - -inline void *platform_memoryAlloc(i32 numBytes) -{ - void *result = calloc(1, numBytes); - -#ifdef DENGINE_DEBUG - if (result) - GLOBAL_debugState.totalMemoryAllocated += numBytes; -#endif - - return result; -} - -inline void platform_closeFileRead(PlatformFileRead *file) -{ - PLATFORM_MEM_FREE(file->buffer, file->size); -} +void platform_memoryFree(void *data, i32 numBytes); +#define PLATFORM_MEM_ALLOC(num, type) CAST(type *) platform_memoryAlloc(num * sizeof(type)) +void *platform_memoryAlloc(i32 numBytes); +void platform_closeFileRead(PlatformFileRead *file); i32 platform_readFileToBuffer(const char *const filePath, PlatformFileRead *file); diff --git a/src/include/Dengine/Renderer.h b/src/include/Dengine/Renderer.h index 58af830..f4296ca 100644 --- a/src/include/Dengine/Renderer.h +++ b/src/include/Dengine/Renderer.h @@ -1,14 +1,17 @@ #ifndef DENGINE_RENDERER_H #define DENGINE_RENDERER_H -#include "Dengine/Entity.h" #include "Dengine/AssetManager.h" +#include "Dengine/Common.h" +#include "Dengine/Entity.h" +#include "Dengine/Math.h" +#include "Dengine/Shader.h" typedef struct Renderer { Shader *shader; - GLuint vao; - GLuint vbo; + u32 vao; + u32 vbo; i32 numVertexesInVbo; v2 vertexNdcFactor; v2 size; @@ -20,11 +23,6 @@ typedef struct RenderTex v4 texRect; } RenderTex; -typedef struct RenderQuad -{ - v4 vertex[4]; -} RenderQuad; - // TODO(doyle): Clean up lines // Renderer::~Renderer() { glDeleteVertexArrays(1, &this->quadVAO); } void renderer_rect(Renderer *const renderer, v4 cameraBounds, v2 pos, v2 size, @@ -55,20 +53,4 @@ inline void renderer_staticString(Renderer *const renderer, Font *const font, void renderer_entity(Renderer *renderer, v4 cameraBounds, Entity *entity, f32 dt, f32 rotate, v4 color); -INTERNAL inline void renderer_flipTexCoord(v4 *texCoords, b32 flipX, b32 flipY) -{ - if (flipX) - { - v4 tmp = *texCoords; - texCoords->x = tmp.z; - texCoords->z = tmp.x; - } - - if (flipY) - { - v4 tmp = *texCoords; - texCoords->y = tmp.w; - texCoords->w = tmp.y; - } -} #endif diff --git a/src/include/Dengine/Shader.h b/src/include/Dengine/Shader.h index e47bf46..e587ce2 100644 --- a/src/include/Dengine/Shader.h +++ b/src/include/Dengine/Shader.h @@ -1,8 +1,8 @@ #ifndef DENGINE_SHADER_H #define DENGINE_SHADER_H -#include "Dengine/OpenGL.h" #include "Dengine/Math.h" +#include "Dengine/OpenGL.h" typedef struct Shader { diff --git a/src/include/Dengine/Texture.h b/src/include/Dengine/Texture.h index dc4b368..fb6738d 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/OpenGL.h" #include "Dengine/Common.h" +#include "Dengine/OpenGL.h" #define TARGET_TEXTURE_SIZE 1024 #define TARGET_BYTES_PER_PIXEL 4 diff --git a/src/include/WorldTraveller/WorldTraveller.h b/src/include/WorldTraveller/WorldTraveller.h index 58ff465..62d9428 100644 --- a/src/include/WorldTraveller/WorldTraveller.h +++ b/src/include/WorldTraveller/WorldTraveller.h @@ -1,19 +1,16 @@ #ifndef WORLDTRAVELLER_GAME_H #define WORLDTRAVELLER_GAME_H +#include "Dengine/AssetManager.h" #include "Dengine/Common.h" #include "Dengine/Entity.h" +#include "Dengine/Math.h" #include "Dengine/Renderer.h" #define NUM_KEYS 1024 #define METERS_TO_PIXEL 64 -enum State -{ - state_active, - state_menu, - state_win, -}; +enum State; typedef struct World { @@ -27,7 +24,6 @@ typedef struct World i32 heroIndex; i32 freeEntityIndex; - } World; typedef struct GameState @@ -44,7 +40,6 @@ typedef struct GameState AssetManager assetManager; } GameState; - void worldTraveller_gameInit(GameState *state, v2i windowSize); void worldTraveller_gameUpdateAndRender(GameState *state, const f32 dt); #endif