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.
This commit is contained in:
Doyle Thai 2016-07-16 23:27:52 +10:00
parent 6da8eff9b1
commit a426461dad
17 changed files with 176 additions and 159 deletions

View File

@ -138,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\Assets.h" />
<ClInclude Include="src\include\Dengine\Debug.h" /> <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" />

View File

@ -89,5 +89,8 @@
<ClInclude Include="src\include\Dengine\Debug.h"> <ClInclude Include="src\include\Dengine\Debug.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="src\include\Dengine\Assets.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -1,5 +1,5 @@
#include "Dengine/Platform.h"
#include "Dengine/Debug.h" #include "Dengine/Debug.h"
#include "Dengine/Platform.h"
DebugState GLOBAL_debugState; DebugState GLOBAL_debugState;

View File

@ -1,4 +1,34 @@
#include <Windows.h>
#include <stdio.h>
#include <stdlib.h>
#include "Dengine/Platform.h" #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, i32 platform_readFileToBuffer(const char *const filePath,
PlatformFileRead *file) PlatformFileRead *file)

View File

@ -1,10 +1,32 @@
#include "Dengine/Platform.h"
#include "Dengine/OpenGL.h"
#include "Dengine/Renderer.h" #include "Dengine/Renderer.h"
#include "Dengine/Debug.h" #include "Dengine/Debug.h"
#include "Dengine/OpenGL.h"
#include "Dengine/Platform.h"
#define RENDER_BOUNDING_BOX FALSE #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, INTERNAL void updateBufferObject(Renderer *const renderer,
RenderQuad *const quads, const i32 numQuads) RenderQuad *const quads, const i32 numQuads)
{ {
@ -174,7 +196,7 @@ void renderer_string(Renderer *const renderer, v4 cameraBounds,
/* Get texture out */ /* Get texture out */
v4 charTexRect = font->atlas->texRect[relativeIndex]; v4 charTexRect = font->atlas->texRect[relativeIndex];
renderer_flipTexCoord(&charTexRect, FALSE, TRUE); flipTexCoord(&charTexRect, FALSE, TRUE);
RenderTex renderTex = {font->tex, charTexRect}; RenderTex renderTex = {font->tex, charTexRect};
RenderQuad charQuad = RenderQuad charQuad =
@ -220,7 +242,7 @@ void renderer_entity(Renderer *renderer, v4 cameraBounds, Entity *entity,
if (entity->direction == direction_east) if (entity->direction == direction_east)
{ {
// NOTE(doyle): Flip the x coordinates to flip the tex // NOTE(doyle): Flip the x coordinates to flip the tex
renderer_flipTexCoord(&texRect, TRUE, FALSE); flipTexCoord(&texRect, TRUE, FALSE);
} }
RenderTex renderTex = {entity->tex, texRect}; RenderTex renderTex = {entity->tex, texRect};
RenderQuad entityQuad = RenderQuad entityQuad =

View File

@ -1,4 +1,4 @@
#include "Dengine\Texture.h" #include "Dengine/Texture.h"
enum BytesPerPixel enum BytesPerPixel
{ {

View File

@ -1,11 +1,14 @@
#include "Dengine/Platform.h"
#include "Dengine/AssetManager.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 #include "Dengine/Debug.h"
// choose to load assets outside of WorldTraveller ! #include "Dengine/Platform.h"
enum State
{
state_active,
state_menu,
state_win,
};
INTERNAL Entity *addEntity(World *world, v2 pos, v2 size, enum EntityType type, INTERNAL Entity *addEntity(World *world, v2 pos, v2 size, enum EntityType type,
enum Direction direction, Texture *tex, b32 collides) enum Direction direction, Texture *tex, b32 collides)

View File

@ -1,8 +1,9 @@
#if 1 #if 1
#include "Dengine/AssetManager.h" #include "Dengine/AssetManager.h"
#include "Dengine/Renderer.h" #include "Dengine/Common.h"
#include "Dengine/Math.h"
#include "Dengine/Debug.h" #include "Dengine/Debug.h"
#include "Dengine/Math.h"
#include "Dengine/OpenGL.h"
#include "WorldTraveller/WorldTraveller.h" #include "WorldTraveller/WorldTraveller.h"
@ -136,8 +137,8 @@ int main()
f32 framesPerSecond = 1.0f / secondsElapsed; f32 framesPerSecond = 1.0f / secondsElapsed;
char textBuffer[256]; char textBuffer[256];
snprintf(textBuffer, ARRAY_COUNT(textBuffer), "Dengine | %f ms/f | %f fps", msPerFrame, snprintf(textBuffer, ARRAY_COUNT(textBuffer),
framesPerSecond); "Dengine | %f ms/f | %f fps", msPerFrame, framesPerSecond);
glfwSetWindowTitle(window, textBuffer); glfwSetWindowTitle(window, textBuffer);
titleUpdateFrequencyInSeconds = 0.5f; titleUpdateFrequencyInSeconds = 0.5f;

View File

@ -1,80 +1,12 @@
#ifndef DENGINE_ASSET_MANAGER_H #ifndef DENGINE_ASSET_MANAGER_H
#define DENGINE_ASSET_MANAGER_H #define DENGINE_ASSET_MANAGER_H
#include "Dengine/Assets.h"
#include "Dengine/Shader.h" #include "Dengine/Shader.h"
#include "Dengine/Texture.h" #include "Dengine/Texture.h"
#define MAX_TEXTURE_SIZE 1024 #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 // TODO(doyle): Switch to hash based lookup
typedef struct AssetManager typedef struct AssetManager
{ {

View File

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

View File

@ -1,6 +1,9 @@
#ifndef DENGINE_DEBUG_H #ifndef DENGINE_DEBUG_H
#define DENGINE_DEBUG_H #define DENGINE_DEBUG_H
#include "Dengine/Assets.h"
#include "Dengine/Common.h"
#include "Dengine/Math.h"
#include "Dengine/Renderer.h" #include "Dengine/Renderer.h"
#define INVALID_CODE_PATH TRUE #define INVALID_CODE_PATH TRUE

View File

@ -1,8 +1,9 @@
#ifndef DENGINE_ENTITY_H #ifndef DENGINE_ENTITY_H
#define DENGINE_ENTITY_H #define DENGINE_ENTITY_H
#include "Dengine/Texture.h" #include "Dengine/Common.h"
#include "Dengine/Math.h" #include "Dengine/Math.h"
#include "Dengine/Texture.h"
enum Direction enum Direction
{ {

View File

@ -1,56 +1,25 @@
#ifndef DENGINE_PLATFORM_H #ifndef DENGINE_PLATFORM_H
#define DENGINE_PLATFORM_H #define DENGINE_PLATFORM_H
#include <Windows.h>
#include <stdio.h>
#include <stdlib.h>
#include "Dengine/Common.h" #include "Dengine/Common.h"
#include "Dengine/Debug.h"
// TODO(doyle): Create own custom memory allocator typedef struct PlatformFileRead
#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
{ {
void *buffer; void *buffer;
i32 size; i32 size;
} PlatformFileRead; } 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 // 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 // allocator since we haven't put in a system to track memory usage per
// allocation // allocation
inline void platform_memoryFree(void *data, i32 numBytes) 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);
}
#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, i32 platform_readFileToBuffer(const char *const filePath,
PlatformFileRead *file); PlatformFileRead *file);

View File

@ -1,14 +1,17 @@
#ifndef DENGINE_RENDERER_H #ifndef DENGINE_RENDERER_H
#define DENGINE_RENDERER_H #define DENGINE_RENDERER_H
#include "Dengine/Entity.h"
#include "Dengine/AssetManager.h" #include "Dengine/AssetManager.h"
#include "Dengine/Common.h"
#include "Dengine/Entity.h"
#include "Dengine/Math.h"
#include "Dengine/Shader.h"
typedef struct Renderer typedef struct Renderer
{ {
Shader *shader; Shader *shader;
GLuint vao; u32 vao;
GLuint vbo; u32 vbo;
i32 numVertexesInVbo; i32 numVertexesInVbo;
v2 vertexNdcFactor; v2 vertexNdcFactor;
v2 size; v2 size;
@ -20,11 +23,6 @@ typedef struct RenderTex
v4 texRect; v4 texRect;
} RenderTex; } RenderTex;
typedef struct RenderQuad
{
v4 vertex[4];
} RenderQuad;
// TODO(doyle): Clean up lines // TODO(doyle): Clean up lines
// Renderer::~Renderer() { glDeleteVertexArrays(1, &this->quadVAO); } // Renderer::~Renderer() { glDeleteVertexArrays(1, &this->quadVAO); }
void renderer_rect(Renderer *const renderer, v4 cameraBounds, v2 pos, v2 size, 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, void renderer_entity(Renderer *renderer, v4 cameraBounds, Entity *entity,
f32 dt, f32 rotate, v4 color); 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 #endif

View File

@ -1,8 +1,8 @@
#ifndef DENGINE_SHADER_H #ifndef DENGINE_SHADER_H
#define DENGINE_SHADER_H #define DENGINE_SHADER_H
#include "Dengine/OpenGL.h"
#include "Dengine/Math.h" #include "Dengine/Math.h"
#include "Dengine/OpenGL.h"
typedef struct Shader typedef struct Shader
{ {

View File

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

View File

@ -1,19 +1,16 @@
#ifndef WORLDTRAVELLER_GAME_H #ifndef WORLDTRAVELLER_GAME_H
#define WORLDTRAVELLER_GAME_H #define WORLDTRAVELLER_GAME_H
#include "Dengine/AssetManager.h"
#include "Dengine/Common.h" #include "Dengine/Common.h"
#include "Dengine/Entity.h" #include "Dengine/Entity.h"
#include "Dengine/Math.h"
#include "Dengine/Renderer.h" #include "Dengine/Renderer.h"
#define NUM_KEYS 1024 #define NUM_KEYS 1024
#define METERS_TO_PIXEL 64 #define METERS_TO_PIXEL 64
enum State enum State;
{
state_active,
state_menu,
state_win,
};
typedef struct World typedef struct World
{ {
@ -27,7 +24,6 @@ typedef struct World
i32 heroIndex; i32 heroIndex;
i32 freeEntityIndex; i32 freeEntityIndex;
} World; } World;
typedef struct GameState typedef struct GameState
@ -44,7 +40,6 @@ typedef struct GameState
AssetManager assetManager; AssetManager assetManager;
} GameState; } GameState;
void worldTraveller_gameInit(GameState *state, v2i windowSize); void worldTraveller_gameInit(GameState *state, v2i windowSize);
void worldTraveller_gameUpdateAndRender(GameState *state, const f32 dt); void worldTraveller_gameUpdateAndRender(GameState *state, const f32 dt);
#endif #endif