Const asset and entity functions, minor clean up

This commit is contained in:
Doyle Thai 2016-08-31 16:40:41 +10:00
parent e9db7b0570
commit f6943e5efb
7 changed files with 55 additions and 49 deletions

View File

@ -27,8 +27,8 @@
* Hash Table Operations * Hash Table Operations
********************************* *********************************
*/ */
INTERNAL HashTableEntry *getFreeHashSlot(HashTable *const table, INTERNAL HashTableEntry *const getFreeHashSlot(HashTable *const table,
MemoryArena *arena, MemoryArena *const arena,
const char *const key) const char *const key)
{ {
u32 hashIndex = common_getHashIndex(key, table->size); u32 hashIndex = common_getHashIndex(key, table->size);
@ -63,7 +63,7 @@ INTERNAL HashTableEntry *getFreeHashSlot(HashTable *const table,
return result; return result;
} }
INTERNAL HashTableEntry *getEntryFromHash(HashTable *const table, INTERNAL HashTableEntry *const getEntryFromHash(HashTable *const table,
const char *const key) const char *const key)
{ {
u32 hashIndex = common_getHashIndex(key, table->size); u32 hashIndex = common_getHashIndex(key, table->size);
@ -100,14 +100,19 @@ INTERNAL Rect *getFreeAtlasSubTexSlot(TexAtlas *const atlas,
} }
} }
Rect *asset_getAtlasSubTex(TexAtlas *const atlas, const char *const key) const Rect asset_getAtlasSubTex(TexAtlas *const atlas, const char *const key)
{ {
HashTableEntry *entry = getEntryFromHash(&atlas->subTex, key); HashTableEntry *entry = getEntryFromHash(&atlas->subTex, key);
Rect *result = NULL; Rect result = {0};
if (entry) result = CAST(Rect *) entry->data; if (entry)
{
result = *(CAST(Rect *) entry->data);
return result;
}
DEBUG_LOG("asset_getAtlasSubTex() failed: Sub texture does not exist");
return result; return result;
} }

View File

@ -3,7 +3,7 @@
#include "Dengine/Platform.h" #include "Dengine/Platform.h"
#include "Dengine/WorldTraveller.h" #include "Dengine/WorldTraveller.h"
void entity_setActiveAnim(Entity *entity, char *animName) void entity_setActiveAnim(Entity *const entity, const char *const animName)
{ {
/* Reset current anim data */ /* Reset current anim data */
EntityAnim *currEntityAnim = &entity->animList[entity->currAnimId]; EntityAnim *currEntityAnim = &entity->animList[entity->currAnimId];
@ -16,6 +16,7 @@ void entity_setActiveAnim(Entity *entity, char *animName)
Animation *anim = entity->animList[i].anim; Animation *anim = entity->animList[i].anim;
if (anim) if (anim)
{ {
// TODO(doyle): Linear search, but not a problem if list is small
if (common_strcmp(anim->key, animName) == 0) if (common_strcmp(anim->key, animName) == 0)
{ {
entity->currAnimId = i; entity->currAnimId = i;
@ -31,7 +32,7 @@ void entity_setActiveAnim(Entity *entity, char *animName)
DEBUG_LOG("Entity does not have access to desired anim"); DEBUG_LOG("Entity does not have access to desired anim");
} }
void entity_updateAnim(Entity *entity, f32 dt) void entity_updateAnim(Entity *const entity, const f32 dt)
{ {
if (!entity->tex) if (!entity->tex)
return; return;
@ -55,15 +56,16 @@ void entity_updateAnim(Entity *entity, f32 dt)
case entitytype_mob: case entitytype_mob:
case entitytype_npc: case entitytype_npc:
char *frameName = anim->frameList[currEntityAnim->currFrame]; char *frameName = anim->frameList[currEntityAnim->currFrame];
Rect *texRect = Rect texRect =
asset_getAtlasSubTex(anim->atlas, frameName); asset_getAtlasSubTex(anim->atlas, frameName);
entity->renderSize = texRect->size; entity->renderSize = texRect.size;
default: default:
break; break;
} }
} }
void entity_addAnim(AssetManager *assetManager, Entity *entity, char *animName) void entity_addAnim(AssetManager *const assetManager, Entity *const entity,
const char *const animName)
{ {
i32 freeAnimIndex = 0; i32 freeAnimIndex = 0;
for (i32 i = 0; i < ARRAY_COUNT(entity->animList); i++) for (i32 i = 0; i < ARRAY_COUNT(entity->animList); i++)
@ -81,9 +83,11 @@ void entity_addAnim(AssetManager *assetManager, Entity *entity, char *animName)
DEBUG_LOG("No more free entity animation slots"); DEBUG_LOG("No more free entity animation slots");
} }
Entity *entity_add(MemoryArena *arena, World *world, v2 pos, v2 size, Entity *const entity_add(MemoryArena *const arena, World *const world,
enum EntityType type, enum Direction direction, Texture *tex, const v2 pos, const v2 size,
b32 collides) const enum EntityType type,
const enum Direction direction,
Texture *const tex, const b32 collides)
{ {
#ifdef DENGINE_DEBUG #ifdef DENGINE_DEBUG
@ -139,7 +143,8 @@ Entity *entity_add(MemoryArena *arena, World *world, v2 pos, v2 size,
return result; return result;
} }
void entity_clearData(MemoryArena *arena, World *world, Entity *entity) void entity_clearData(MemoryArena *const arena, World *const world,
Entity *const entity)
{ {
if (entity->stats) if (entity->stats)
PLATFORM_MEM_FREE(arena, entity->stats, sizeof(EntityStats)); PLATFORM_MEM_FREE(arena, entity->stats, sizeof(EntityStats));
@ -150,7 +155,7 @@ void entity_clearData(MemoryArena *arena, World *world, Entity *entity)
entity->type = entitytype_null; entity->type = entitytype_null;
} }
i32 entity_getIndex(World *world, i32 entityId) i32 entity_getIndex(World *const world, const i32 entityId)
{ {
i32 first = 0; i32 first = 0;
i32 last = world->freeEntityIndex - 1; i32 last = world->freeEntityIndex - 1;

View File

@ -210,13 +210,13 @@ void renderer_string(Renderer *const renderer, MemoryArena *arena, Rect camera,
pos.x += charMetric.advance; pos.x += charMetric.advance;
/* Get texture out */ /* Get texture out */
Rect *charTexRect = Rect charTexRect =
asset_getAtlasSubTex(font->atlas, &CAST(char)codepoint); asset_getAtlasSubTex(font->atlas, &CAST(char)codepoint);
v4 deprecatedTexRect = {0}; v4 deprecatedTexRect = {0};
deprecatedTexRect.vec2[0] = charTexRect->pos; deprecatedTexRect.vec2[0] = charTexRect.pos;
deprecatedTexRect.vec2[1] = deprecatedTexRect.vec2[1] =
v2_add(charTexRect->pos, charTexRect->size); v2_add(charTexRect.pos, charTexRect.size);
flipTexCoord(&deprecatedTexRect, FALSE, TRUE); flipTexCoord(&deprecatedTexRect, FALSE, TRUE);
@ -252,12 +252,12 @@ void renderer_entity(Renderer *renderer, Rect camera, Entity *entity,
EntityAnim *entityAnim = &entity->animList[entity->currAnimId]; EntityAnim *entityAnim = &entity->animList[entity->currAnimId];
Animation *anim = entityAnim->anim; Animation *anim = entityAnim->anim;
char *frameName = anim->frameList[entityAnim->currFrame]; char *frameName = anim->frameList[entityAnim->currFrame];
Rect *animRect = asset_getAtlasSubTex(anim->atlas, frameName); Rect animRect = asset_getAtlasSubTex(anim->atlas, frameName);
// TODO(doyle): Switch to rect // TODO(doyle): Switch to rect
v4 animTexRect = {0}; v4 animTexRect = {0};
animTexRect.vec2[0] = animRect->pos; animTexRect.vec2[0] = animRect.pos;
animTexRect.vec2[1] = v2_add(animRect->pos, animRect->size); animTexRect.vec2[1] = v2_add(animRect.pos, animRect.size);
if (entity->direction == direction_east) if (entity->direction == direction_east)
{ {

View File

@ -1641,18 +1641,18 @@ void worldTraveller_gameUpdateAndRender(GameState *state, f32 dt)
/* Draw hero avatar */ /* Draw hero avatar */
TexAtlas *heroAtlas = TexAtlas *heroAtlas =
asset_getTexAtlas(assetManager, "ClaudeSprite.png"); asset_getTexAtlas(assetManager, "ClaudeSprite.png");
Rect *heroAvatarRect = Rect heroAvatarRect =
asset_getAtlasSubTex(heroAtlas, "ClaudeSprite_Avatar_01"); asset_getAtlasSubTex(heroAtlas, "ClaudeSprite_Avatar_01");
v2 heroAvatarP = v2 heroAvatarP =
V2(10.0f, (renderer->size.h * 0.5f) - (0.5f * heroAvatarRect->size.h)); V2(10.0f, (renderer->size.h * 0.5f) - (0.5f * heroAvatarRect.size.h));
// TODO(doyle): Use rect in rendering not V4 // TODO(doyle): Use rect in rendering not V4
v4 heroAvatarTexRect = {0}; v4 heroAvatarTexRect = {0};
heroAvatarTexRect.vec2[0] = heroAvatarRect->pos; heroAvatarTexRect.vec2[0] = heroAvatarRect.pos;
heroAvatarTexRect.vec2[1] = v2_add(heroAvatarRect->pos, heroAvatarRect->size); heroAvatarTexRect.vec2[1] = v2_add(heroAvatarRect.pos, heroAvatarRect.size);
RenderTex heroRenderTex = {hero->tex, heroAvatarTexRect}; RenderTex heroRenderTex = {hero->tex, heroAvatarTexRect};
renderer_staticRect(renderer, heroAvatarP, heroAvatarRect->size, V2(0, 0), 0, renderer_staticRect(renderer, heroAvatarP, heroAvatarRect.size, V2(0, 0), 0,
heroRenderTex, V4(1, 1, 1, 1)); heroRenderTex, V4(1, 1, 1, 1));
char heroAvatarStr[20]; char heroAvatarStr[20];
@ -1661,7 +1661,7 @@ void worldTraveller_gameUpdateAndRender(GameState *state, f32 dt)
f32 strLenInPixels = f32 strLenInPixels =
CAST(f32)(font->maxSize.w * common_strlen(heroAvatarStr)); CAST(f32)(font->maxSize.w * common_strlen(heroAvatarStr));
v2 strPos = v2 strPos =
V2(heroAvatarP.x, heroAvatarP.y - (0.5f * heroAvatarRect->size.h)); V2(heroAvatarP.x, heroAvatarP.y - (0.5f * heroAvatarRect.size.h));
renderer_staticString(&state->renderer, &state->arena, font, heroAvatarStr, renderer_staticString(&state->renderer, &state->arena, font, heroAvatarStr,
strPos, V2(0, 0), 0, V4(0, 0, 1, 1)); strPos, V2(0, 0), 0, V4(0, 0, 1, 1));

View File

@ -28,7 +28,7 @@ typedef struct AssetManager
* Texture Operations * Texture Operations
********************************* *********************************
*/ */
Rect *asset_getAtlasSubTex(TexAtlas *const atlas, const char *const key); const Rect asset_getAtlasSubTex(TexAtlas *const atlas, const char *const key);
Texture *asset_getTex(AssetManager *const assetManager, const char *const key); Texture *asset_getTex(AssetManager *const assetManager, const char *const key);
TexAtlas *asset_getFreeTexAtlasSlot(AssetManager *const assetManager, TexAtlas *asset_getFreeTexAtlasSlot(AssetManager *const assetManager,
MemoryArena *arena, const char *const key, MemoryArena *arena, const char *const key,

View File

@ -94,12 +94,16 @@ typedef struct Entity
i32 numAudioRenderers; i32 numAudioRenderers;
} Entity; } Entity;
void entity_setActiveAnim(Entity *entity, char *animName); void entity_setActiveAnim(Entity *const entity, const char *const animName);
void entity_updateAnim(Entity *entity, f32 dt); void entity_updateAnim(Entity *const entity, const f32 dt);
void entity_addAnim(AssetManager *assetManager, Entity *entity, char *animName); void entity_addAnim(AssetManager *const assetManager, Entity *const entity,
Entity *entity_add(MemoryArena *arena, World *world, v2 pos, v2 size, const char *const animName);
enum EntityType type, enum Direction direction, Texture *tex, Entity *const entity_add(MemoryArena *const arena, World *const world,
b32 collides); const v2 pos, const v2 size,
void entity_clearData(MemoryArena *arena, World *world, Entity *entity); const enum EntityType type,
i32 entity_getIndex(World *world, i32 entityId); const enum Direction direction, Texture *const tex,
const b32 collides);
void entity_clearData(MemoryArena *const arena, World *const world,
Entity *const entity);
i32 entity_getIndex(World *const world, const i32 entityId);
#endif #endif

View File

@ -10,12 +10,6 @@
// TODO(doyle): Look into merging into assets.h file .. // TODO(doyle): Look into merging into assets.h file ..
typedef struct Texture typedef struct Texture
{ {
union
{
char *key;
char *name;
};
// Holds the ID of the texture object, used for all texture operations to // Holds the ID of the texture object, used for all texture operations to
// reference to this particlar texture // reference to this particlar texture
GLuint id; GLuint id;
@ -36,8 +30,6 @@ typedef struct Texture
GLuint filterMinification; GLuint filterMinification;
// Filtering mode if texture pixels > screen pixels // Filtering mode if texture pixels > screen pixels
GLuint filterMagnification; GLuint filterMagnification;
struct Texture *next;
} Texture; } Texture;
// Generates texture from image data // Generates texture from image data