diff --git a/src/component/Texture.cpp b/src/component/Texture.cpp index 7ccd561..778fea1 100644 --- a/src/component/Texture.cpp +++ b/src/component/Texture.cpp @@ -1,7 +1,50 @@ #include "component/Texture.hpp" +#include + +TSHashTable s_cacheTable; +HASHKEY_NONE s_cacheKey; +uint32_t* s_entryHeap; + +void CACHEENTRY::AddRef() { + this->m_refCount++; +} + +CACHEENTRY* TextureCacheAllocEntry() { + if (!s_entryHeap) { + auto heapId = static_cast(SMemAlloc(sizeof(uint32_t), __FILE__, __LINE__, 0)); + *heapId = ObjectAllocAddHeap(sizeof(CACHEENTRY), 1024, "TCACHEENTRY", true); + + s_entryHeap = heapId; + } + + uint32_t memHandle; + void* mem; + + if (!ObjectAlloc(*s_entryHeap, &memHandle, &mem, false)) { + return nullptr; + } + + auto entry = new (mem) CACHEENTRY(); + entry->m_memHandle = memHandle; + + return entry; +} CACHEENTRY* TextureCacheCreateTexture(const char* fileName) { - // TODO + auto hashval = SStrHash(fileName); + auto texture = s_cacheTable.Ptr(hashval, s_cacheKey); + + if (!texture) { + texture = TextureCacheAllocEntry(); + + s_cacheTable.Insert(texture, hashval, s_cacheKey); + + SStrCopy(texture->m_fileName, fileName, sizeof(texture->m_fileName)); + } + + texture->AddRef(); + + return texture; } void TextureCacheDestroyTexture(void* texture) { diff --git a/src/component/Texture.hpp b/src/component/Texture.hpp index b52ea55..6041587 100644 --- a/src/component/Texture.hpp +++ b/src/component/Texture.hpp @@ -11,8 +11,10 @@ class CACHEENTRY : public TSHashObject { CAsyncObject* m_asyncObject = nullptr; char m_fileName[128]; uint32_t m_refCount = 0; - void* m_memHandle = nullptr; + uint32_t m_memHandle = 0; + // Member functions + void AddRef(); }; CACHEENTRY* TextureCacheCreateTexture(const char* fileName);