Extra2D/include/resource/resource_manager.h

101 lines
2.9 KiB
C++

#pragma once
#include <resource/resource_cache.h>
#include <resource/texture.h>
#include <resource/shader.h>
#include <resource/material.h>
#include <resource/font.h>
#include <resource/text.h>
#include <resource/audio.h>
#include <module/imodule.h>
#include <string>
#include <unordered_map>
#include <vector>
namespace extra2d {
/**
* @brief 资源管理器类
*
* 统一管理所有资源的加载和缓存
*/
class ResourceManager : public IModule {
public:
ResourceManager();
~ResourceManager() override;
// IModule 接口
const char* name() const override { return "ResourceManager"; }
ModuleType type() const override { return ModuleType::Core; }
int priority() const override { return 10; }
bool init() override;
void shutdown() override;
// 纹理管理
Ptr<Texture> getTexture(const std::string& path);
Ptr<Texture> createTexture(uint32 width, uint32 height, TextureFormat format);
// 着色器管理
Ptr<Shader> getShader(const std::string& vsPath, const std::string& fsPath);
Ptr<Shader> getShaderFromSource(const std::string& name,
const std::string& vsSource,
const std::string& fsSource);
// 材质管理
Ptr<Material> createMaterial(Ptr<Shader> shader);
Ptr<Material> getMaterial(const std::string& name);
void registerMaterial(const std::string& name, Ptr<Material> material);
// 字体管理
Ptr<Font> getFont(const std::string& path, const FontConfig& config = {});
// 文本管理
Ptr<Text> createText(Ptr<Font> font, const std::string& content = "");
// 音频管理
Ptr<Audio> getAudio(const std::string& path, AudioType type);
// 默认资源(延迟初始化,确保 OpenGL 上下文已创建)
Ptr<Texture> getDefaultTexture();
Ptr<Shader> getDefaultShader();
Ptr<Material> getDefaultMaterial();
Ptr<Font> getDefaultFont();
// 检查默认资源是否已创建
bool areDefaultResourcesCreated() const { return defaultResourcesCreated_; }
// 垃圾回收
void collectGarbage();
// 统计信息
struct Stats {
size_t textureCount;
size_t shaderCount;
size_t materialCount;
size_t fontCount;
size_t textCount;
size_t audioCount;
};
Stats getStats() const;
private:
ResourceCache<Texture> textureCache_;
std::unordered_map<std::string, Ptr<Shader>> shaderCache_;
std::unordered_map<std::string, Ptr<Material>> materials_;
std::unordered_map<std::string, Ptr<Font>> fontCache_;
std::vector<Ptr<Text>> texts_;
std::unordered_map<std::string, Ptr<Audio>> audioCache_;
// 默认资源
Ptr<Texture> defaultTexture_;
Ptr<Shader> defaultShader_;
Ptr<Material> defaultMaterial_;
Ptr<Font> defaultFont_;
bool defaultResourcesCreated_ = false;
bool createDefaultResources();
};
} // namespace extra2d