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,9 +27,9 @@
* Hash Table Operations
*********************************
*/
INTERNAL HashTableEntry *getFreeHashSlot(HashTable *const table,
MemoryArena *arena,
const char *const key)
INTERNAL HashTableEntry *const getFreeHashSlot(HashTable *const table,
MemoryArena *const arena,
const char *const key)
{
u32 hashIndex = common_getHashIndex(key, table->size);
HashTableEntry *result = &table->entries[hashIndex];
@ -63,7 +63,7 @@ INTERNAL HashTableEntry *getFreeHashSlot(HashTable *const table,
return result;
}
INTERNAL HashTableEntry *getEntryFromHash(HashTable *const table,
INTERNAL HashTableEntry *const getEntryFromHash(HashTable *const table,
const char *const key)
{
u32 hashIndex = common_getHashIndex(key, table->size);
@ -83,15 +83,15 @@ INTERNAL HashTableEntry *getEntryFromHash(HashTable *const table,
*********************************
*/
INTERNAL Rect *getFreeAtlasSubTexSlot(TexAtlas *const atlas,
MemoryArena *const arena,
const char *const key)
MemoryArena *const arena,
const char *const key)
{
HashTableEntry *entry = getFreeHashSlot(&atlas->subTex, arena, key);
if (entry)
{
entry->data = PLATFORM_MEM_ALLOC(arena, 1, Rect);
Rect *result = CAST(Rect *) entry->data;
Rect *result = CAST(Rect *)entry->data;
return result;
}
else
@ -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);
Rect *result = NULL;
if (entry) result = CAST(Rect *) entry->data;
Rect result = {0};
if (entry)
{
result = *(CAST(Rect *) entry->data);
return result;
}
DEBUG_LOG("asset_getAtlasSubTex() failed: Sub texture does not exist");
return result;
}

View File

@ -3,7 +3,7 @@
#include "Dengine/Platform.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 */
EntityAnim *currEntityAnim = &entity->animList[entity->currAnimId];
@ -16,6 +16,7 @@ void entity_setActiveAnim(Entity *entity, char *animName)
Animation *anim = entity->animList[i].anim;
if (anim)
{
// TODO(doyle): Linear search, but not a problem if list is small
if (common_strcmp(anim->key, animName) == 0)
{
entity->currAnimId = i;
@ -31,7 +32,7 @@ void entity_setActiveAnim(Entity *entity, char *animName)
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)
return;
@ -55,15 +56,16 @@ void entity_updateAnim(Entity *entity, f32 dt)
case entitytype_mob:
case entitytype_npc:
char *frameName = anim->frameList[currEntityAnim->currFrame];
Rect *texRect =
Rect texRect =
asset_getAtlasSubTex(anim->atlas, frameName);
entity->renderSize = texRect->size;
entity->renderSize = texRect.size;
default:
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;
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");
}
Entity *entity_add(MemoryArena *arena, World *world, v2 pos, v2 size,
enum EntityType type, enum Direction direction, Texture *tex,
b32 collides)
Entity *const entity_add(MemoryArena *const arena, World *const world,
const v2 pos, const v2 size,
const enum EntityType type,
const enum Direction direction,
Texture *const tex, const b32 collides)
{
#ifdef DENGINE_DEBUG
@ -139,7 +143,8 @@ Entity *entity_add(MemoryArena *arena, World *world, v2 pos, v2 size,
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)
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;
}
i32 entity_getIndex(World *world, i32 entityId)
i32 entity_getIndex(World *const world, const i32 entityId)
{
i32 first = 0;
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;
/* Get texture out */
Rect *charTexRect =
Rect charTexRect =
asset_getAtlasSubTex(font->atlas, &CAST(char)codepoint);
v4 deprecatedTexRect = {0};
deprecatedTexRect.vec2[0] = charTexRect->pos;
deprecatedTexRect.vec2[0] = charTexRect.pos;
deprecatedTexRect.vec2[1] =
v2_add(charTexRect->pos, charTexRect->size);
v2_add(charTexRect.pos, charTexRect.size);
flipTexCoord(&deprecatedTexRect, FALSE, TRUE);
@ -252,12 +252,12 @@ void renderer_entity(Renderer *renderer, Rect camera, Entity *entity,
EntityAnim *entityAnim = &entity->animList[entity->currAnimId];
Animation *anim = entityAnim->anim;
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
v4 animTexRect = {0};
animTexRect.vec2[0] = animRect->pos;
animTexRect.vec2[1] = v2_add(animRect->pos, animRect->size);
animTexRect.vec2[0] = animRect.pos;
animTexRect.vec2[1] = v2_add(animRect.pos, animRect.size);
if (entity->direction == direction_east)
{

View File

@ -1641,18 +1641,18 @@ void worldTraveller_gameUpdateAndRender(GameState *state, f32 dt)
/* Draw hero avatar */
TexAtlas *heroAtlas =
asset_getTexAtlas(assetManager, "ClaudeSprite.png");
Rect *heroAvatarRect =
Rect heroAvatarRect =
asset_getAtlasSubTex(heroAtlas, "ClaudeSprite_Avatar_01");
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
v4 heroAvatarTexRect = {0};
heroAvatarTexRect.vec2[0] = heroAvatarRect->pos;
heroAvatarTexRect.vec2[1] = v2_add(heroAvatarRect->pos, heroAvatarRect->size);
heroAvatarTexRect.vec2[0] = heroAvatarRect.pos;
heroAvatarTexRect.vec2[1] = v2_add(heroAvatarRect.pos, heroAvatarRect.size);
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));
char heroAvatarStr[20];
@ -1661,7 +1661,7 @@ void worldTraveller_gameUpdateAndRender(GameState *state, f32 dt)
f32 strLenInPixels =
CAST(f32)(font->maxSize.w * common_strlen(heroAvatarStr));
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,
strPos, V2(0, 0), 0, V4(0, 0, 1, 1));

View File

@ -28,7 +28,7 @@ typedef struct AssetManager
* 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);
TexAtlas *asset_getFreeTexAtlasSlot(AssetManager *const assetManager,
MemoryArena *arena, const char *const key,

View File

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

View File

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