Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

feat: Add GUID-based texture loading to ResourceManager

esengine39c2628c0c50
diff --git a/src/esengine/resource/ResourceManager.cpp b/src/esengine/resource/ResourceManager.cpp
index 6bf8daf57..7ccb4b2bf 100644
--- a/src/esengine/resource/ResourceManager.cpp
+++ b/src/esengine/resource/ResourceManager.cpp
@@ -36,6 +36,7 @@ void ResourceManager::shutdown() {
ES_LOG_INFO("ResourceManager shutting down (shaders: {}, textures: {}, vbos: {}, ibos: {})",
shaders_.size(), textures_.size(), vertexBuffers_.size(), indexBuffers_.size());
+ guidToTexture_.clear();
shaders_.clear();
textures_.clear();
vertexBuffers_.clear();
@@ -153,6 +154,37 @@ void ResourceManager::releaseTexture(TextureHandle handle) {
}
}
+TextureHandle ResourceManager::loadTextureByGUID(const std::string& guid, const std::string& path) {
+ auto it = guidToTexture_.find(guid);
+ if (it != guidToTexture_.end() && it->second.isValid()) {
+ textures_.addRef(it->second);
+ stats_.cacheHits++;
+ return it->second;
+ }
+
+ TextureHandle handle = loadTexture(path);
+ if (handle.isValid()) {
+ guidToTexture_[guid] = handle;
+ }
+ return handle;
+}
+
+TextureHandle ResourceManager::getTextureByGUID(const std::string& guid) const {
+ auto it = guidToTexture_.find(guid);
+ if (it != guidToTexture_.end()) {
+ return it->second;
+ }
+ return TextureHandle();
+}
+
+void ResourceManager::releaseTextureByGUID(const std::string& guid) {
+ auto it = guidToTexture_.find(guid);
+ if (it != guidToTexture_.end()) {
+ releaseTexture(it->second);
+ guidToTexture_.erase(it);
+ }
+}
+
// =============================================================================
// Vertex Buffer Resources
// =============================================================================
diff --git a/src/esengine/resource/ResourceManager.hpp b/src/esengine/resource/ResourceManager.hpp
index 4a962c832..86902fefd 100644
--- a/src/esengine/resource/ResourceManager.hpp
+++ b/src/esengine/resource/ResourceManager.hpp
@@ -26,6 +26,7 @@
// Standard library
#include <string>
+#include <unordered_map>
namespace esengine::resource {
@@ -192,6 +193,27 @@ public:
*/
void releaseTexture(TextureHandle handle);
+ /**
+ * @brief Loads a texture by GUID (with caching)
+ * @param guid Asset GUID from AssetDatabase
+ * @param path File path to load if not cached
+ * @return Handle to the texture, or invalid handle on failure
+ */
+ TextureHandle loadTextureByGUID(const std::string& guid, const std::string& path);
+
+ /**
+ * @brief Gets a texture handle by GUID if already loaded
+ * @param guid Asset GUID
+ * @return Handle to the texture, or invalid handle if not loaded
+ */
+ TextureHandle getTextureByGUID(const std::string& guid) const;
+
+ /**
+ * @brief Releases a texture by GUID
+ * @param guid Asset GUID
+ */
+ void releaseTextureByGUID(const std::string& guid);
+
// =========================================================================
// Vertex Buffer Resources
// =========================================================================
@@ -290,6 +312,7 @@ private:
ResourcePool<Texture> textures_;
ResourcePool<VertexBuffer> vertexBuffers_;
ResourcePool<IndexBuffer> indexBuffers_;
+ std::unordered_map<std::string, TextureHandle> guidToTexture_;
mutable ResourceStats stats_;
bool initialized_ = false;
};