working on texture manager
This commit is contained in:
parent
e04fcbb9bd
commit
be6a8cca9e
@ -95,7 +95,7 @@ namespace ZL
|
||||
renderer.shaderManager.AddShaderFromFiles("defaultColor", "resources/shaders/defaultColor.vertex", "resources/shaders/defaultColor_desktop.fragment", CONST_ZIP_FILE);
|
||||
renderer.shaderManager.AddShaderFromFiles("default", "resources/shaders/default.vertex", "resources/shaders/default_desktop.fragment", CONST_ZIP_FILE);
|
||||
#endif
|
||||
loadingTexture = std::make_unique<Texture>(CreateTextureDataFromPng("resources/loading.png", ""));
|
||||
loadingTexture = renderer.textureManager.LoadFromPng("resources/loading.png", "");
|
||||
|
||||
float minDimension;
|
||||
float width = Environment::projectionWidth;
|
||||
|
||||
@ -49,7 +49,7 @@ namespace ZL
|
||||
|
||||
void Location::setup(const LocationSetup& params)
|
||||
{
|
||||
roomTexture = std::make_unique<Texture>(CreateTextureDataFromPng(params.roomTexturePath, CONST_ZIP_FILE));
|
||||
roomTexture = renderer.textureManager.LoadFromPng(params.roomTexturePath, CONST_ZIP_FILE);
|
||||
roomMesh.data = LoadFromTextFile02(params.roomMeshPath, CONST_ZIP_FILE);
|
||||
roomMesh.data.RotateByMatrix(Eigen::Quaternionf(Eigen::AngleAxisf(-M_PI * 0.5, Eigen::Vector3f::UnitY())).toRotationMatrix());
|
||||
roomMesh.RefreshVBO();
|
||||
@ -62,9 +62,9 @@ namespace ZL
|
||||
|
||||
|
||||
//auto playerTexture = std::make_shared<Texture>(CreateTextureDataFromPng("resources/w/gg/IMG_20260413_182354_992.png", CONST_ZIP_FILE));
|
||||
auto playerTexture = std::make_shared<Texture>(CreateTextureDataFromPng("resources/w/gg/UniV_Grid_2K_Base_color.png", CONST_ZIP_FILE));
|
||||
auto playerTexture = renderer.textureManager.LoadFromPng("resources/w/gg/UniV_Grid_2K_Base_color.png", CONST_ZIP_FILE);
|
||||
|
||||
auto sparkTexture = std::make_shared<Texture>(CreateTextureDataFromPng("resources/w/spark.png", CONST_ZIP_FILE));
|
||||
auto sparkTexture = renderer.textureManager.LoadFromPng("resources/w/spark.png", CONST_ZIP_FILE);
|
||||
|
||||
player = std::make_unique<Character>();
|
||||
/*
|
||||
@ -90,7 +90,7 @@ namespace ZL
|
||||
|
||||
|
||||
|
||||
player->weaponTexture = std::make_shared<Texture>(CreateTextureDataFromPng("resources/w/white.png", CONST_ZIP_FILE));
|
||||
player->weaponTexture = renderer.textureManager.LoadFromPng("resources/w/white.png", CONST_ZIP_FILE);
|
||||
//player->weaponMesh.AssignFrom(LoadFromTextFile02("resources/w/gg/knife001.txt", CONST_ZIP_FILE));
|
||||
|
||||
player->weaponMesh.data = LoadFromTextFile02("resources/w/gg/knife002.txt", CONST_ZIP_FILE);
|
||||
@ -132,7 +132,7 @@ namespace ZL
|
||||
}
|
||||
}
|
||||
|
||||
auto sparkTexture2 = std::make_shared<Texture>(CreateTextureDataFromPng("resources/w/star.png", CONST_ZIP_FILE));
|
||||
auto sparkTexture2 = renderer.textureManager.LoadFromPng("resources/w/star.png", CONST_ZIP_FILE);
|
||||
|
||||
// Teleport zone visuals: an upward fountain of sparks parked at the
|
||||
// teleport position, so the player can spot the zone from a distance.
|
||||
|
||||
@ -490,8 +490,7 @@ namespace ZL {
|
||||
std::string path = t[key].get<std::string>();
|
||||
try {
|
||||
std::cout << "UiManager: loading texture for button '" << btn->name << "' : " << path << " Zip file: " << zipFile << std::endl;
|
||||
auto data = CreateTextureDataFromPng(path.c_str(), zipFile.c_str());
|
||||
return std::make_shared<Texture>(data);
|
||||
return renderer.textureManager.LoadFromPng(path, zipFile);
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
std::cerr << "UiManager: failed load texture " << path << " : " << e.what() << std::endl;
|
||||
@ -523,8 +522,7 @@ namespace ZL {
|
||||
std::string path = t[key].get<std::string>();
|
||||
try {
|
||||
std::cout << "UiManager: --loading texture for slider '" << s->name << "' : " << path << " Zip file: " << zipFile << std::endl;
|
||||
auto data = CreateTextureDataFromPng(path.c_str(), zipFile.c_str());
|
||||
return std::make_shared<Texture>(data);
|
||||
return renderer.textureManager.LoadFromPng(path, zipFile);
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
std::cerr << "UiManager: failed load texture " << path << " : " << e.what() << std::endl;
|
||||
@ -674,8 +672,7 @@ namespace ZL {
|
||||
|
||||
if (!texPath.empty()) {
|
||||
try {
|
||||
auto data = CreateTextureDataFromPng(texPath.c_str(), zipFile.c_str());
|
||||
img->texture = std::make_shared<Texture>(data);
|
||||
img->texture = renderer.textureManager.LoadFromPng(texPath, zipFile);
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
std::cerr << "UiManager: failed load texture for StaticImage '" << img->name << "' : " << e.what() << std::endl;
|
||||
@ -1058,14 +1055,10 @@ namespace ZL {
|
||||
s->vertical = vertical;
|
||||
|
||||
try {
|
||||
if (!trackPath.empty()) {
|
||||
auto data = CreateTextureDataFromPng(trackPath.c_str(), zipFile.c_str());
|
||||
s->texTrack = std::make_shared<Texture>(data);
|
||||
}
|
||||
if (!knobPath.empty()) {
|
||||
auto data = CreateTextureDataFromPng(knobPath.c_str(), zipFile.c_str());
|
||||
s->texKnob = std::make_shared<Texture>(data);
|
||||
}
|
||||
if (!trackPath.empty())
|
||||
s->texTrack = renderer.textureManager.LoadFromPng(trackPath, zipFile);
|
||||
if (!knobPath.empty())
|
||||
s->texKnob = renderer.textureManager.LoadFromPng(knobPath, zipFile);
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
std::cerr << "UiManager: addSlider failed to load textures: " << e.what() << std::endl;
|
||||
|
||||
@ -63,12 +63,12 @@ bool DialogueOverlay::init(Renderer& renderer, const std::string& zipFile) {
|
||||
rendererRef = &renderer;
|
||||
zipFilename = zipFile;
|
||||
|
||||
textboxTexture = std::make_shared<Texture>(CreateTextureDataFromPng("resources/dialogue/textbox_bg.png", zipFile));
|
||||
portraitFrameTexture = std::make_shared<Texture>(CreateTextureDataFromPng("resources/dialogue/portrait_frame.png", zipFile));
|
||||
choiceMainTexture = std::make_shared<Texture>(CreateTextureDataFromPng("resources/dialogue/choice_main.png", zipFile));
|
||||
choiceOptionalTexture = std::make_shared<Texture>(CreateTextureDataFromPng("resources/dialogue/choice_optional.png", zipFile));
|
||||
choiceSelectedTexture = std::make_shared<Texture>(CreateTextureDataFromPng("resources/dialogue/choice_selected.png", zipFile));
|
||||
cutsceneSubtitleTexture = std::make_shared<Texture>(CreateTextureDataFromPng("resources/dialogue/cutscene_subtitle_bg.png", zipFile));
|
||||
textboxTexture = renderer.textureManager.LoadFromPng("resources/dialogue/textbox_bg.png", zipFile);
|
||||
portraitFrameTexture = renderer.textureManager.LoadFromPng("resources/dialogue/portrait_frame.png", zipFile);
|
||||
choiceMainTexture = renderer.textureManager.LoadFromPng("resources/dialogue/choice_main.png", zipFile);
|
||||
choiceOptionalTexture = renderer.textureManager.LoadFromPng("resources/dialogue/choice_optional.png", zipFile);
|
||||
choiceSelectedTexture = renderer.textureManager.LoadFromPng("resources/dialogue/choice_selected.png", zipFile);
|
||||
cutsceneSubtitleTexture = renderer.textureManager.LoadFromPng("resources/dialogue/cutscene_subtitle_bg.png", zipFile);
|
||||
|
||||
nameRenderer = std::make_unique<TextRenderer>();
|
||||
bodyRenderer = std::make_unique<TextRenderer>();
|
||||
@ -648,14 +648,7 @@ std::shared_ptr<Texture> DialogueOverlay::loadTextureCached(const std::string& p
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto it = textureCache.find(path);
|
||||
if (it != textureCache.end()) {
|
||||
return it->second;
|
||||
}
|
||||
|
||||
auto texture = std::make_shared<Texture>(CreateTextureDataFromPng(path, zipFilename));
|
||||
textureCache[path] = texture;
|
||||
return texture;
|
||||
return rendererRef->textureManager.LoadFromPng(path, zipFilename);
|
||||
}
|
||||
|
||||
void DialogueOverlay::drawQuad(Renderer& renderer, const TexturedQuad& quad, const std::shared_ptr<Texture>& texture) const {
|
||||
|
||||
@ -7,7 +7,6 @@
|
||||
#include "UiManager.h"
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
namespace ZL::Dialogue {
|
||||
@ -90,8 +89,6 @@ private:
|
||||
TexturedQuad skipProgressFillQuad;
|
||||
mutable std::vector<TexturedQuad> choiceQuads;
|
||||
|
||||
std::unordered_map<std::string, std::shared_ptr<Texture>> textureCache;
|
||||
|
||||
void drawDialogue(Renderer& renderer, const PresentationModel& model);
|
||||
void drawCutscene(Renderer& renderer, const PresentationModel& model);
|
||||
|
||||
|
||||
@ -129,7 +129,7 @@ namespace ZL {
|
||||
|
||||
// Load texture
|
||||
try {
|
||||
gameObj.texture = std::make_shared<Texture>(CreateTextureDataFromPng(objData.texturePath, zipPath.c_str()));
|
||||
gameObj.texture = renderer.textureManager.LoadFromPng(objData.texturePath, zipPath);
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
std::cerr << "GameObjectLoader: Failed to load texture for '" << objData.name << "': " << e.what() << std::endl;
|
||||
@ -202,7 +202,7 @@ namespace ZL {
|
||||
|
||||
// Load texture
|
||||
try {
|
||||
intObj.texture = std::make_shared<Texture>(CreateTextureDataFromPng(objData.texturePath, zipPath.c_str()));
|
||||
intObj.texture = renderer.textureManager.LoadFromPng(objData.texturePath, zipPath);
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
std::cerr << "GameObjectLoader: Failed to load texture for interactive '" << objData.name << "': " << e.what() << std::endl;
|
||||
|
||||
@ -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();
|
||||
|
||||
|
||||
@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
@ -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);
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user