From 85becdfd6c01f9dbcc1e07ece8bb881ff45fd502 Mon Sep 17 00:00:00 2001 From: Doyle Thai Date: Thu, 28 Jul 2016 15:19:23 +1000 Subject: [PATCH] Merge the game to dengine, reduce header coupling Until engine architecture can clearly be separated from the game, i.e. notion of entity storage belonging to game and concept of entity belonging to engine, worlds/scenes belonging to game or engine- the project will only refer to Dengine. --- Dengine.vcxproj | 2 +- Dengine.vcxproj.filters | 12 +-- src/AssetManager.c | 2 + src/Debug.c | 90 ++++++++++++++++- src/Entity.c | 98 +++++++++---------- src/Platform.c | 2 +- src/Renderer.c | 3 + src/WorldTraveller.c | 3 +- src/dengine.c | 13 +-- src/include/Dengine/AssetManager.h | 9 +- src/include/Dengine/Assets.h | 5 +- src/include/Dengine/Audio.h | 6 +- src/include/Dengine/Debug.h | 96 ++---------------- src/include/Dengine/Entity.h | 9 +- src/include/Dengine/Renderer.h | 11 ++- src/include/Dengine/Texture.h | 1 + .../WorldTraveller.h | 0 17 files changed, 179 insertions(+), 183 deletions(-) rename src/include/{WorldTraveller => Dengine}/WorldTraveller.h (100%) diff --git a/Dengine.vcxproj b/Dengine.vcxproj index 3c80619..076fd51 100644 --- a/Dengine.vcxproj +++ b/Dengine.vcxproj @@ -154,7 +154,7 @@ - + diff --git a/Dengine.vcxproj.filters b/Dengine.vcxproj.filters index 60e25c8..9c094ae 100644 --- a/Dengine.vcxproj.filters +++ b/Dengine.vcxproj.filters @@ -33,9 +33,6 @@ Source Files - - Source Files - Source Files @@ -51,6 +48,9 @@ Source Files + + Source Files + @@ -80,9 +80,6 @@ Header Files - - Header Files - Header Files @@ -104,5 +101,8 @@ Header Files + + Header Files + \ No newline at end of file diff --git a/src/AssetManager.c b/src/AssetManager.c index cc29c27..37e4e89 100644 --- a/src/AssetManager.c +++ b/src/AssetManager.c @@ -21,6 +21,8 @@ #include "Dengine/OpenGL.h" #include "Dengine/Platform.h" +// TODO(doyle): Switch to hash based lookup +// TODO(doyle): Use pointers, so we can forward declare all assets? AudioVorbis *asset_getVorbis(AssetManager *assetManager, const enum AudioList type) { diff --git a/src/Debug.c b/src/Debug.c index 45999a7..88e49ed 100644 --- a/src/Debug.c +++ b/src/Debug.c @@ -1,8 +1,81 @@ #include "Dengine/Debug.h" -#include "Dengine/Platform.h" #include "Dengine/AssetManager.h" +#include "Dengine/Entity.h" +#include "Dengine/MemoryArena.h" +#include "Dengine/Platform.h" +#include "Dengine/Renderer.h" +#include "Dengine/WorldTraveller.h" -DebugState GLOBAL_debug; +typedef struct DebugState +{ + Font font; + i32 *callCount; + f32 stringLineGap; + + /* Debug strings rendered in top left corner */ + char debugStrings[64][128]; + i32 numDebugStrings; + f32 stringUpdateTimer; + f32 stringUpdateRate; + v2 initialStringP; + v2 currStringP; + + /* Debug gui console log */ + char console[20][128]; + i32 consoleIndex; + v2 initialConsoleP; +} DebugState; + +GLOBAL_VAR DebugState GLOBAL_debug; + +inline char *debug_entitystate_string(i32 val) +{ + char *string; + switch(val) + { + case entitystate_idle: + string = "EntityState_Idle"; + break; + case entitystate_battle: + string = "EntityState_Battle"; + break; + case entitystate_attack: + string = "EntityState_Attack"; + break; + case entitystate_dead: + string = "EntityState_Dead"; + break; + case entitystate_count: + string = "EntityState_Count (Error!)"; + break; + case entitystate_invalid: + string = "EntityState_Invalid (Error!)"; + break; + default: + string = "EntityState Unknown (NOT DEFINED)"; + } + return string; +} + +inline char *debug_entityattack_string(i32 val) +{ + char *string; + switch(val) + { + case entityattack_tackle: + string = "EntityAttack_Tackle"; + break; + case entityattack_count: + string = "EntityAttack_Count (Error!)"; + break; + case entityattack_invalid: + string = "EntityAttack_Invalid"; + break; + default: + string = "EntityAttack Unknown (NOT DEFINED)"; + } + return string; +} void debug_init(MemoryArena *arena, v2 windowSize, Font font) { @@ -30,6 +103,19 @@ void debug_init(MemoryArena *arena, v2 windowSize, Font font) GLOBAL_debug.initialConsoleP = V2(consoleXPos, consoleYPos); } +void debug_callCountIncrement(i32 id) +{ + ASSERT(id < debugcallcount_num); + GLOBAL_debug.callCount[id]++; +} + +void debug_clearCallCounter() +{ + for (i32 i = 0; i < debugcallcount_num; i++) + GLOBAL_debug.callCount[i] = 0; +} + + void debug_consoleLog(char *string, char *file, int lineNum) { diff --git a/src/Entity.c b/src/Entity.c index 2dd9b94..04d2beb 100644 --- a/src/Entity.c +++ b/src/Entity.c @@ -1,11 +1,7 @@ -#include "Dengine/AssetManager.h" -#include "Dengine/MemoryArena.h" -#include "Dengine/Platform.h" -#include "Dengine/Debug.h" - #include "Dengine/Entity.h" - -#include "WorldTraveller/WorldTraveller.h" +#include "Dengine/Debug.h" +#include "Dengine/Platform.h" +#include "Dengine/WorldTraveller.h" void entity_setActiveAnim(Entity *entity, enum AnimList animId) { @@ -25,7 +21,8 @@ void entity_setActiveAnim(Entity *entity, enum AnimList animId) void entity_updateAnim(Entity *entity, f32 dt) { - if (!entity->tex) return; + if (!entity->tex) + return; // TODO(doyle): Recheck why we have this twice EntityAnim_ *entityAnim = &entity->anim[entity->currAnimId]; @@ -47,20 +44,20 @@ void entity_updateAnim(Entity *entity, f32 dt) // may exceed the hitbox size of the entity switch (entity->type) { - case entitytype_hero: - case entitytype_mob: - case entitytype_npc: - entity->renderSize = math_getRectSize(texRect); - default: - break; + case entitytype_hero: + case entitytype_mob: + case entitytype_npc: + entity->renderSize = math_getRectSize(texRect); + default: + break; } } void entity_addAnim(AssetManager *assetManager, Entity *entity, i32 animId) { - Animation *anim = asset_getAnim(assetManager, animId); - entity->anim[animId].anim = anim; - entity->anim[animId].currFrame = 0; + Animation *anim = asset_getAnim(assetManager, animId); + entity->anim[animId].anim = anim; + entity->anim[animId].currFrame = 0; entity->anim[animId].currDuration = anim->frameDuration; } @@ -87,12 +84,11 @@ void entity_addGenericMob(MemoryArena *arena, AssetManager *assetManager, entity_addAnim(assetManager, mob, animlist_hero_battlePose); entity_addAnim(assetManager, mob, animlist_hero_tackle); mob->currAnimId = animlist_hero_idle; - } Entity *entity_add(MemoryArena *arena, World *world, v2 pos, v2 size, - enum EntityType type, enum Direction direction, - Texture *tex, b32 collides) + enum EntityType type, enum Direction direction, Texture *tex, + b32 collides) { #ifdef DENGINE_DEBUG @@ -111,39 +107,39 @@ Entity *entity_add(MemoryArena *arena, World *world, v2 pos, v2 size, entity.tex = tex; entity.collides = collides; - switch(type) + switch (type) { - case entitytype_hero: - entity.stats = PLATFORM_MEM_ALLOC(arena, 1, EntityStats); - entity.stats->maxHealth = 100; - entity.stats->health = entity.stats->maxHealth; - entity.stats->actionRate = 100; - entity.stats->actionTimer = entity.stats->actionRate; - entity.stats->actionSpdMul = 100; - entity.stats->entityIdToAttack = -1; - entity.stats->queuedAttack = entityattack_invalid; - entity.state = entitystate_idle; - break; - case entitytype_mob: - { - entity.stats = PLATFORM_MEM_ALLOC(arena, 1, EntityStats); - entity.stats->maxHealth = 100; - entity.stats->health = entity.stats->maxHealth; - entity.stats->actionRate = 100; - entity.stats->actionTimer = entity.stats->actionRate; - entity.stats->actionSpdMul = 100; - entity.stats->entityIdToAttack = -1; - entity.stats->queuedAttack = entityattack_invalid; - entity.state = entitystate_idle; - break; - } - - default: - break; + case entitytype_hero: + entity.stats = PLATFORM_MEM_ALLOC(arena, 1, EntityStats); + entity.stats->maxHealth = 100; + entity.stats->health = entity.stats->maxHealth; + entity.stats->actionRate = 100; + entity.stats->actionTimer = entity.stats->actionRate; + entity.stats->actionSpdMul = 100; + entity.stats->entityIdToAttack = -1; + entity.stats->queuedAttack = entityattack_invalid; + entity.state = entitystate_idle; + break; + case entitytype_mob: + { + entity.stats = PLATFORM_MEM_ALLOC(arena, 1, EntityStats); + entity.stats->maxHealth = 100; + entity.stats->health = entity.stats->maxHealth; + entity.stats->actionRate = 100; + entity.stats->actionTimer = entity.stats->actionRate; + entity.stats->actionSpdMul = 100; + entity.stats->entityIdToAttack = -1; + entity.stats->queuedAttack = entityattack_invalid; + entity.state = entitystate_idle; + break; + } + + default: + break; } world->entities[world->freeEntityIndex++] = entity; - Entity *result = &world->entities[world->freeEntityIndex-1]; + Entity *result = &world->entities[world->freeEntityIndex - 1]; return result; } @@ -154,8 +150,8 @@ void entity_delete(MemoryArena *arena, World *world, i32 entityIndex) PLATFORM_MEM_FREE(arena, entity->stats, sizeof(EntityStats)); // TODO(doyle): Inefficient shuffle down all elements - for (i32 i = entityIndex; i < world->freeEntityIndex-1; i++) - world->entities[i] = world->entities[i+1]; + for (i32 i = entityIndex; i < world->freeEntityIndex - 1; i++) + world->entities[i] = world->entities[i + 1]; world->freeEntityIndex--; } diff --git a/src/Platform.c b/src/Platform.c index 0257b5c..b46879f 100644 --- a/src/Platform.c +++ b/src/Platform.c @@ -3,7 +3,7 @@ #include #include "Dengine/Platform.h" -#include "Dengine/Debug.h" +#include "Dengine/MemoryArena.h" void platform_memoryFree(MemoryArena *arena, void *data, i32 numBytes) { diff --git a/src/Renderer.c b/src/Renderer.c index 1bc6d15..8374f81 100644 --- a/src/Renderer.c +++ b/src/Renderer.c @@ -1,7 +1,10 @@ #include "Dengine/Renderer.h" #include "Dengine/Debug.h" +#include "Dengine/Entity.h" #include "Dengine/OpenGL.h" #include "Dengine/Platform.h" +#include "Dengine/Shader.h" +#include "Dengine/Texture.h" #define RENDER_BOUNDING_BOX FALSE diff --git a/src/WorldTraveller.c b/src/WorldTraveller.c index e68a20c..9c7d4d0 100644 --- a/src/WorldTraveller.c +++ b/src/WorldTraveller.c @@ -1,5 +1,4 @@ -#include "WorldTraveller/WorldTraveller.h" - +#include "Dengine/WorldTraveller.h" #include "Dengine/Audio.h" #include "Dengine/Debug.h" #include "Dengine/Entity.h" diff --git a/src/dengine.c b/src/dengine.c index 6af4e13..611ea6c 100644 --- a/src/dengine.c +++ b/src/dengine.c @@ -1,16 +1,9 @@ -#if 1 #include "Dengine/AssetManager.h" -#include "Dengine/Audio.h" #include "Dengine/Common.h" #include "Dengine/Debug.h" #include "Dengine/Math.h" #include "Dengine/OpenGL.h" -#include "Dengine/Platform.h" - -#include "WorldTraveller/WorldTraveller.h" - -// TODO(doyle): Temporary -struct AudioRenderer; +#include "Dengine/WorldTraveller.h" void key_callback(GLFWwindow *window, int key, int scancode, int action, int mode) { @@ -176,7 +169,3 @@ int main() glfwTerminate(); return 0; } - -#else -#include -#endif diff --git a/src/include/Dengine/AssetManager.h b/src/include/Dengine/AssetManager.h index 040d7b6..a775813 100644 --- a/src/include/Dengine/AssetManager.h +++ b/src/include/Dengine/AssetManager.h @@ -8,10 +8,6 @@ /* Forward declaration */ typedef struct MemoryArena MemoryArena; -#define MAX_TEXTURE_SIZE 1024 - -// TODO(doyle): Switch to hash based lookup -// TODO(doyle): Use pointers, so we can forward declare all assets? typedef struct AssetManager { Texture textures[32]; @@ -22,7 +18,7 @@ typedef struct AssetManager Font font; } AssetManager; -GLOBAL_VAR AssetManager assetManager; +#define MAX_TEXTURE_SIZE 1024 AudioVorbis *asset_getVorbis(AssetManager *assetManager, const enum AudioList type); @@ -48,8 +44,7 @@ const i32 asset_loadTTFont(AssetManager *assetManager, MemoryArena *arena, inline i32 asset_getVFontSpacing(FontMetrics metrics) { - i32 result = - metrics.ascent - metrics.descent + metrics.lineGap; + i32 result = metrics.ascent - metrics.descent + metrics.lineGap; return result; } diff --git a/src/include/Dengine/Assets.h b/src/include/Dengine/Assets.h index 841bdf4..27bdcf8 100644 --- a/src/include/Dengine/Assets.h +++ b/src/include/Dengine/Assets.h @@ -4,8 +4,11 @@ #define STB_VORBIS_HEADER_ONLY #include +#include "Dengine/Common.h" #include "Dengine/Math.h" -#include "Dengine/Texture.h" + +/* Forward Declaration */ +typedef struct Texture Texture; enum TexList { diff --git a/src/include/Dengine/Audio.h b/src/include/Dengine/Audio.h index a3b2906..06fd87c 100644 --- a/src/include/Dengine/Audio.h +++ b/src/include/Dengine/Audio.h @@ -21,9 +21,10 @@ typedef struct AudioManager } AudioManager; + #define AUDIO_REPEAT_INFINITE -10 #define AUDIO_SOURCE_UNASSIGNED -1 -struct AudioRenderer +typedef struct AudioRenderer { i32 sourceIndex; ALuint bufferId[4]; @@ -31,9 +32,8 @@ struct AudioRenderer AudioVorbis *audio; ALuint format; i32 numPlays; -}; +} AudioRenderer; -typedef struct AudioRenderer AudioRenderer; const i32 audio_init(AudioManager *audioManager); const i32 audio_streamPlayVorbis(AudioManager *audioManager, diff --git a/src/include/Dengine/Debug.h b/src/include/Dengine/Debug.h index 31dd8c7..5d5a742 100644 --- a/src/include/Dengine/Debug.h +++ b/src/include/Dengine/Debug.h @@ -4,10 +4,10 @@ #include "Dengine/Assets.h" #include "Dengine/Common.h" #include "Dengine/Math.h" -#include "Dengine/Renderer.h" -#include "Dengine/Entity.h" -#include "WorldTraveller/WorldTraveller.h" +/* Forward Declaration */ +typedef struct GameState GameState; +typedef struct MemoryArena MemoryArena; #define INVALID_CODE_PATH 0 enum DebugCallCount @@ -16,98 +16,16 @@ enum DebugCallCount debugcallcount_num, }; -typedef struct DebugState -{ - Font font; - i32 *callCount; - f32 stringLineGap; - - /* Debug strings rendered in top left corner */ - char debugStrings[64][128]; - i32 numDebugStrings; - f32 stringUpdateTimer; - f32 stringUpdateRate; - v2 initialStringP; - v2 currStringP; - - /* Debug gui console log */ - char console[20][128]; - i32 consoleIndex; - v2 initialConsoleP; -} DebugState; - -extern DebugState GLOBAL_debug; - -inline void debug_callCountIncrement(i32 id) -{ - ASSERT(id < debugcallcount_num); - GLOBAL_debug.callCount[id]++; -} - -inline void debug_clearCallCounter() -{ - for (i32 i = 0; i < debugcallcount_num; i++) - GLOBAL_debug.callCount[i] = 0; -} - -inline char *debug_entitystate_string(i32 val) -{ - char *string; - switch(val) - { - case entitystate_idle: - string = "EntityState_Idle"; - break; - case entitystate_battle: - string = "EntityState_Battle"; - break; - case entitystate_attack: - string = "EntityState_Attack"; - break; - case entitystate_dead: - string = "EntityState_Dead"; - break; - case entitystate_count: - string = "EntityState_Count (Error!)"; - break; - case entitystate_invalid: - string = "EntityState_Invalid (Error!)"; - break; - default: - string = "EntityState Unknown (NOT DEFINED)"; - } - return string; -} - -inline char *debug_entityattack_string(i32 val) -{ - char *string; - switch(val) - { - case entityattack_tackle: - string = "EntityAttack_Tackle"; - break; - case entityattack_count: - string = "EntityAttack_Count (Error!)"; - break; - case entityattack_invalid: - string = "EntityAttack_Invalid"; - break; - default: - string = "EntityAttack Unknown (NOT DEFINED)"; - } - return string; -} - - void debug_init(MemoryArena *arena, v2 windowSize, Font font); +void debug_callCountIncrement(enum DebugCallCount id); +void debug_clearCallCounter(); #define DEBUG_LOG(string) debug_consoleLog(string, __FILE__, __LINE__); void debug_consoleLog(char *string, char *file, int lineNum); #define DEBUG_PUSH_STRING(string) debug_pushString(string, NULL, "char") -#define DEBUG_PUSH_VAR(formatString, data, type) \ - debug_pushString(formatString, CAST(void *)&data, type) +#define DEBUG_PUSH_VAR(formatString, data, type) \ + debug_pushString(formatString, CAST(void *) & data, type) void debug_pushString(char *formatString, void *data, char *dataType); void debug_drawUi(GameState *state, f32 dt); diff --git a/src/include/Dengine/Entity.h b/src/include/Dengine/Entity.h index 79f6c31..e44541d 100644 --- a/src/include/Dengine/Entity.h +++ b/src/include/Dengine/Entity.h @@ -6,6 +6,7 @@ typedef struct AssetManager AssetManager; typedef struct AudioRenderer AudioRenderer; +typedef struct MemoryArena MemoryArena; typedef struct Texture Texture; typedef struct Animation Animation; typedef struct World World; @@ -101,10 +102,10 @@ typedef struct Entity void entity_setActiveAnim(Entity *entity, enum AnimList animId); void entity_updateAnim(Entity *entity, f32 dt); void entity_addAnim(AssetManager *assetManager, Entity *entity, i32 animId); -void entity_addGenericMob(MemoryArena *arena, AssetManager *assetManager, World *world, - v2 pos); +void entity_addGenericMob(MemoryArena *arena, AssetManager *assetManager, + World *world, v2 pos); Entity *entity_add(MemoryArena *arena, World *world, v2 pos, v2 size, - enum EntityType type, enum Direction direction, Texture *tex, - b32 collides); + enum EntityType type, enum Direction direction, Texture *tex, + b32 collides); void entity_delete(MemoryArena *arena, World *world, i32 entityIndex); #endif diff --git a/src/include/Dengine/Renderer.h b/src/include/Dengine/Renderer.h index 0757be9..6469f3e 100644 --- a/src/include/Dengine/Renderer.h +++ b/src/include/Dengine/Renderer.h @@ -1,12 +1,15 @@ #ifndef DENGINE_RENDERER_H #define DENGINE_RENDERER_H -#include "Dengine/AssetManager.h" #include "Dengine/Common.h" -#include "Dengine/Entity.h" #include "Dengine/Math.h" -#include "Dengine/Shader.h" -#include "Dengine/MemoryArena.h" + +/* Forward Declaration */ +typedef struct Shader Shader; +typedef struct Entity Entity; +typedef struct MemoryArena MemoryArena; +typedef struct Texture Texture; +typedef struct Font Font; typedef struct Renderer { diff --git a/src/include/Dengine/Texture.h b/src/include/Dengine/Texture.h index fb6738d..0b09fbf 100644 --- a/src/include/Dengine/Texture.h +++ b/src/include/Dengine/Texture.h @@ -7,6 +7,7 @@ #define TARGET_TEXTURE_SIZE 1024 #define TARGET_BYTES_PER_PIXEL 4 +// TODO(doyle): Look into merging into assets.h file .. typedef struct Texture { // Holds the ID of the texture object, used for all texture operations to diff --git a/src/include/WorldTraveller/WorldTraveller.h b/src/include/Dengine/WorldTraveller.h similarity index 100% rename from src/include/WorldTraveller/WorldTraveller.h rename to src/include/Dengine/WorldTraveller.h