Draft implementation of hash tabled textures

This commit is contained in:
Doyle Thai 2016-08-27 04:15:13 +10:00
parent 4c0c2808ca
commit 1d09bdfa20
8 changed files with 72 additions and 58 deletions

View File

@ -106,17 +106,17 @@ AudioVorbis *asset_getVorbis(AssetManager *assetManager,
return NULL; return NULL;
} }
Texture *asset_getTexture(AssetManager *const assetManager, Texture *asset_getTex(AssetManager *const assetManager, const char *const key)
const enum TexList type)
{ {
if (type < texlist_count) u32 hashIndex = common_getHashIndex(key, ARRAY_COUNT(assetManager->textures));
return &assetManager->textures[type]; Texture *result = &assetManager->textures[hashIndex];
if (result->key)
{
while (result && common_strcmp(result->key, key) != 0)
result = result->next;
}
#ifdef DENGINE_DEBUG return result;
ASSERT(INVALID_CODE_PATH);
#endif
return NULL;
} }
TexAtlas *asset_makeTexAtlas(AssetManager *const assetManager, TexAtlas *asset_makeTexAtlas(AssetManager *const assetManager,
@ -211,8 +211,35 @@ const i32 asset_loadVorbis(AssetManager *assetManager, MemoryArena *arena,
return 0; return 0;
} }
const i32 asset_loadTextureImage(AssetManager *assetManager, // TODO(doyle): Revise passing in the assetmanager
const char *const path, const enum TexList type) Texture *asset_getAndAllocFreeTexSlot(AssetManager *assetManager,
MemoryArena *arena, const char *const key)
{
i32 texListSize = ARRAY_COUNT(assetManager->textures);
u32 hashIndex = common_getHashIndex(key, ARRAY_COUNT(assetManager->textures));
Texture *result = &assetManager->textures[hashIndex];
if (result->key)
{
while (result->next)
{
if (common_strcmp(result->key, key) == 0)
{
// TODO(doyle): Error correction whereby if a tex atlas already
// exists
ASSERT(INVALID_CODE_PATH);
}
result = result->next;
}
result->next = PLATFORM_MEM_ALLOC(arena, 1, Texture);
result = result->next;
}
return result;
}
const i32 asset_loadTextureImage(AssetManager *assetManager, MemoryArena *arena,
const char *const path, const char *const key)
{ {
/* Open the texture image */ /* Open the texture image */
i32 imgWidth, imgHeight, bytesPerPixel; i32 imgWidth, imgHeight, bytesPerPixel;
@ -235,12 +262,13 @@ const i32 asset_loadTextureImage(AssetManager *assetManager,
return -1; return -1;
} }
Texture tex = texture_gen(CAST(GLuint)(imgWidth), CAST(GLuint)(imgHeight), Texture *tex = asset_getAndAllocFreeTexSlot(assetManager, arena, key);
CAST(GLint)(bytesPerPixel), image); *tex = texture_gen(CAST(GLuint)(imgWidth), CAST(GLuint)(imgHeight),
CAST(GLint)(bytesPerPixel), image);
GL_CHECK_ERROR(); GL_CHECK_ERROR();
stbi_image_free(image); stbi_image_free(image);
assetManager->textures[type] = tex;
return 0; return 0;
} }

View File

@ -154,7 +154,7 @@ INTERNAL v2 mapWorldToCameraSpace(v2 worldPos, v4 cameraBounds)
RenderTex renderer_createNullRenderTex(AssetManager *const assetManager) RenderTex renderer_createNullRenderTex(AssetManager *const assetManager)
{ {
Texture *emptyTex = asset_getTexture(assetManager, texlist_empty); Texture *emptyTex = asset_getTex(assetManager, "nullTex");
RenderTex result = {emptyTex, V4(0, 1, 1, 0)}; RenderTex result = {emptyTex, V4(0, 1, 1, 0)};
return result; return result;
} }

View File

@ -34,12 +34,12 @@ Texture texture_gen(const GLuint width, const GLuint height,
// TODO(doyle): Let us set the parameters gl params as well // TODO(doyle): Let us set the parameters gl params as well
GL_CHECK_ERROR(); GL_CHECK_ERROR();
Texture tex = {0}; Texture tex = {0};
tex.width = width; tex.width = width;
tex.height = height; tex.height = height;
tex.internalFormat = GL_RGBA; tex.internalFormat = GL_RGBA;
tex.wrapS = GL_REPEAT; tex.wrapS = GL_REPEAT;
tex.wrapT = GL_REPEAT; tex.wrapT = GL_REPEAT;
tex.filterMinification = GL_NEAREST; tex.filterMinification = GL_NEAREST;
tex.filterMagnification = GL_NEAREST; tex.filterMagnification = GL_NEAREST;
glGenTextures(1, &tex.id); glGenTextures(1, &tex.id);

View File

@ -53,7 +53,7 @@ INTERNAL void addGenericMob(MemoryArena *arena, AssetManager *assetManager,
v2 size = V2(58.0f, 98.0f); v2 size = V2(58.0f, 98.0f);
enum EntityType type = entitytype_mob; enum EntityType type = entitytype_mob;
enum Direction dir = direction_west; enum Direction dir = direction_west;
Texture *tex = asset_getTexture(assetManager, texlist_claude); Texture *tex = asset_getTex(assetManager, "ClaudeSprite.png");
b32 collides = TRUE; b32 collides = TRUE;
Entity *mob = entity_add(arena, world, pos, size, type, dir, tex, collides); Entity *mob = entity_add(arena, world, pos, size, type, dir, tex, collides);
@ -458,25 +458,15 @@ INTERNAL void parseXmlTreeToGame(AssetManager *assetManager, MemoryArena *arena,
common_strncat(imagePath, dataDir, common_strlen(dataDir)); common_strncat(imagePath, dataDir, common_strlen(dataDir));
common_strncat(imagePath, imageName, common_strlen(imageName)); common_strncat(imagePath, imageName, common_strlen(imageName));
// TODO(doyle): Fixme proper loading of texture into right slot asset_loadTextureImage(assetManager, arena, imagePath,
if (common_strcmp(imageName, "ClaudeSprite.png") == 0) imageName);
{
asset_loadTextureImage(assetManager, imagePath,
texlist_claude);
}
else
{
asset_loadTextureImage(assetManager, imagePath,
texlist_terrain);
}
atlasEntry->key = PLATFORM_MEM_ALLOC( atlasEntry->key = PLATFORM_MEM_ALLOC(
arena, common_strlen(imageName) + 1, char); arena, common_strlen(imageName) + 1, char);
common_strncpy(atlasEntry->key, imageName, common_strncpy(atlasEntry->key, imageName,
common_strlen(imageName)); common_strlen(imageName));
atlasEntry->tex = atlasEntry->tex = asset_getTex(assetManager, imageName);
asset_getTexture(assetManager, texlist_claude);
XmlNode *atlasChildNode = atlasXmlNode->child; XmlNode *atlasChildNode = atlasXmlNode->child;
while (atlasChildNode) while (atlasChildNode)
@ -663,8 +653,8 @@ INTERNAL void assetInit(GameState *state)
/* Create empty 1x1 4bpp black texture */ /* Create empty 1x1 4bpp black texture */
u32 bitmap = (0xFF << 24) | (0xFF << 16) | (0xFF << 8) | (0xFF << 0); u32 bitmap = (0xFF << 24) | (0xFF << 16) | (0xFF << 8) | (0xFF << 0);
Texture emptyTex = texture_gen(1, 1, 4, CAST(u8 *)(&bitmap)); Texture *tex = asset_getAndAllocFreeTexSlot(assetManager, arena, "nullTex");
assetManager->textures[texlist_empty] = emptyTex; *tex = texture_gen(1, 1, 4, CAST(u8 *)(&bitmap));
PlatformFileRead terrainXml = {0}; PlatformFileRead terrainXml = {0};
i32 result = platform_readFileToBuffer( i32 result = platform_readFileToBuffer(
@ -833,7 +823,6 @@ INTERNAL void entityInit(GameState *state, v2 windowSize)
world->entityIdInBattle = world->entityIdInBattle =
PLATFORM_MEM_ALLOC(arena, world->maxEntities, i32); PLATFORM_MEM_ALLOC(arena, world->maxEntities, i32);
world->numEntitiesInBattle = 0; world->numEntitiesInBattle = 0;
world->texType = texlist_terrain;
world->bounds = world->bounds =
math_getRect(V2(0, 0), v2_scale(worldDimensionInTiles, math_getRect(V2(0, 0), v2_scale(worldDimensionInTiles,
CAST(f32) state->tileSize)); CAST(f32) state->tileSize));
@ -856,7 +845,7 @@ INTERNAL void entityInit(GameState *state, v2 windowSize)
V2(CAST(f32) state->tileSize, CAST(f32) state->tileSize); V2(CAST(f32) state->tileSize, CAST(f32) state->tileSize);
enum EntityType type = entitytype_tile; enum EntityType type = entitytype_tile;
enum Direction dir = direction_null; enum Direction dir = direction_null;
Texture *tex = asset_getTexture(assetManager, world->texType); Texture *tex = asset_getTex(assetManager, "terrain.png");
b32 collides = FALSE; b32 collides = FALSE;
Entity *tile = entity_add(arena, world, pos, size, type, dir, Entity *tile = entity_add(arena, world, pos, size, type, dir,
tex, collides); tex, collides);
@ -891,7 +880,7 @@ INTERNAL void entityInit(GameState *state, v2 windowSize)
pos = V2(size.x, CAST(f32) state->tileSize); pos = V2(size.x, CAST(f32) state->tileSize);
type = entitytype_hero; type = entitytype_hero;
dir = direction_east; dir = direction_east;
tex = asset_getTexture(assetManager, texlist_claude); tex = asset_getTex(assetManager, "ClaudeSprite.png");
collides = TRUE; collides = TRUE;
Entity *hero = Entity *hero =
entity_add(arena, world, pos, size, type, dir, tex, collides); entity_add(arena, world, pos, size, type, dir, tex, collides);

View File

@ -26,8 +26,7 @@ Rect asset_getAtlasSubTexRect(TexAtlas *atlas, char *key);
AudioVorbis *asset_getVorbis(AssetManager *assetManager, AudioVorbis *asset_getVorbis(AssetManager *assetManager,
const enum AudioList type); const enum AudioList type);
Texture *asset_getTexture(AssetManager *const assetManager, Texture *asset_getTex(AssetManager *const assetManager, const char *const key);
const enum TexList type);
TexAtlas *asset_makeTexAtlas(AssetManager *const assetManager, TexAtlas *asset_makeTexAtlas(AssetManager *const assetManager,
MemoryArena *arena, const char *const key); MemoryArena *arena, const char *const key);
@ -37,13 +36,17 @@ Shader *asset_getShader(AssetManager *assetManager, const enum ShaderList type);
TexAtlas *asset_getTexAtlas(AssetManager *const assetManager, TexAtlas *asset_getTexAtlas(AssetManager *const assetManager,
const char *const key); const char *const key);
Texture *asset_getAndAllocFreeTexSlot(AssetManager *assetManager,
MemoryArena *arena,
const char *const key);
Animation *asset_getAnim(AssetManager *assetManager, char *key); Animation *asset_getAnim(AssetManager *assetManager, char *key);
const i32 asset_loadVorbis(AssetManager *assetManager, MemoryArena *arena, const i32 asset_loadVorbis(AssetManager *assetManager, MemoryArena *arena,
const char *const path, const enum AudioList type); const char *const path, const enum AudioList type);
const i32 asset_loadTextureImage(AssetManager *assetManager, const i32 asset_loadTextureImage(AssetManager *assetManager, MemoryArena *arena,
const char *const path, const char *const path,
const enum TexList type); const char *const key);
const i32 asset_loadShaderFiles(AssetManager *assetManager, MemoryArena *arena, const i32 asset_loadShaderFiles(AssetManager *assetManager, MemoryArena *arena,
const char *const vertexPath, const char *const vertexPath,

View File

@ -32,18 +32,6 @@ enum TerrainRects
terrainrects_count, terrainrects_count,
}; };
enum AnimList
{
animlist_hero_idle,
animlist_hero_walk,
animlist_hero_wave,
animlist_hero_battlePose,
animlist_hero_tackle,
animlist_terrain,
animlist_count,
animlist_invalid,
};
enum AudioList enum AudioList
{ {
audiolist_battle, audiolist_battle,

View File

@ -10,6 +10,12 @@
// 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;
@ -30,6 +36,8 @@ 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

View File

@ -45,8 +45,6 @@ typedef struct World
b32 *entityIdInBattle; b32 *entityIdInBattle;
i32 numEntitiesInBattle; i32 numEntitiesInBattle;
enum TexList texType;
i32 cameraFollowingId; i32 cameraFollowingId;
v2 cameraPos; // In pixels v2 cameraPos; // In pixels
v4 bounds; // In pixels v4 bounds; // In pixels