working on texture manager

This commit is contained in:
Vladislav Khorev committed 2026-05-01 21:00:51 +03:00
1 parent e04fcbb9bd
commit be6a8cca9e
9 files changed
+92 -39

No files matched your search

+2
View File
@@ -5,6 +5,7 @@
#include <exception>
#include <stdexcept>
#include "ShaderManager.h"
#include "TextureManager.h"
#include <Eigen/Dense>
namespace ZL {
@@ -104,6 +105,7 @@ namespace ZL {
public:
ShaderManager shaderManager;
TextureManager textureManager;
void InitOpenGL();
+49
View File
@@ -416,5 +416,54 @@ namespace ZL
#endif
std::string TextureManager::MakeKey(const std::string& fileName, const std::string& zipFile)
{
return zipFile.empty() ? fileName : fileName + "|" + zipFile;
}
std::shared_ptr<Texture> TextureManager::LoadFromBmp24(const std::string& fileName, const std::string& zipFile)
{
std::string key = MakeKey(fileName, zipFile);
auto it = textureMap.find(key);
if (it != textureMap.end())
return it->second;
auto tex = std::make_shared<Texture>(CreateTextureDataFromBmp24(fileName, zipFile));
textureMap[key] = tex;
return tex;
}
std::shared_ptr<Texture> TextureManager::LoadFromBmp32(const std::string& fileName, const std::string& zipFile)
{
std::string key = MakeKey(fileName, zipFile);
auto it = textureMap.find(key);
if (it != textureMap.end())
return it->second;
auto tex = std::make_shared<Texture>(CreateTextureDataFromBmp32(fileName, zipFile));
textureMap[key] = tex;
return tex;
}
#ifdef PNG_ENABLED
std::shared_ptr<Texture> TextureManager::LoadFromPng(const std::string& fileName, const std::string& zipFile)
{
std::string key = MakeKey(fileName, zipFile);
auto it = textureMap.find(key);
if (it != textureMap.end())
return it->second;
auto tex = std::make_shared<Texture>(CreateTextureDataFromPng(fileName, zipFile));
textureMap[key] = tex;
return tex;
}
#endif
void TextureManager::Unload(const std::string& fileName, const std::string& zipFile)
{
textureMap.erase(MakeKey(fileName, zipFile));
}
void TextureManager::UnloadAll()
{
textureMap.clear();
}
}
+19
View File
@@ -2,6 +2,8 @@
#include "render/OpenGlExtensions.h"
#include "utils/Utils.h"
#include <memory>
#include <unordered_map>
#if defined EMSCRIPTEN || defined __ANDROID__
#define PNG_ENABLED
@@ -30,6 +32,7 @@ namespace ZL
Format format = RGBA;
MipmapType mipmap = GENERATE;
};
class Texture
{
size_t width = 0;
@@ -59,5 +62,21 @@ namespace ZL
TextureDataStruct CreateTextureDataFromPng(const std::string& fullFileName, const std::string& ZIPFileName = "");
#endif
class TextureManager
{
public:
std::shared_ptr<Texture> LoadFromBmp24(const std::string& fileName, const std::string& zipFile = "");
std::shared_ptr<Texture> LoadFromBmp32(const std::string& fileName, const std::string& zipFile = "");
#ifdef PNG_ENABLED
std::shared_ptr<Texture> LoadFromPng(const std::string& fileName, const std::string& zipFile = "");
#endif
void Unload(const std::string& fileName, const std::string& zipFile = "");
void UnloadAll();
private:
std::unordered_map<std::string, std::shared_ptr<Texture>> textureMap;
static std::string MakeKey(const std::string& fileName, const std::string& zipFile);
};
}