ShitEngine 1.4.2
基于 C++20 与 SDL3 的轻量级 2D 游戏引擎
载入中...
搜索中...
未找到
Resource.h
浏览该文件的文档.
1#pragma once
2
3#include <cstdint>
4#include <functional>
5#include <memory>
6#include <string>
7#include <string_view>
8#include <unordered_map>
9
11
12struct SDL_Texture;
13struct TTF_Font;
14struct MIX_Audio;
15struct MIX_Mixer;
16
17namespace Shit {
18
21
24
37 public:
38 explicit Resource(std::string path)
39 : m_path(std::move(path)), m_uuid(GenerateResourceUuid()) {}
40 virtual ~Resource() = default; // 句柄由各子类析构经 release() 释放
41
42 Resource(const Resource&) = delete;
43 Resource& operator=(const Resource&) = delete;
44
45 const std::string& getPath() const { return m_path; }
46 ResourceState getState() const { return m_state; }
47 bool isLoaded() const { return m_state == ResourceState::Loaded; }
48 const std::string& getError() const { return m_error; }
49 std::uint64_t getUuid() const { return m_uuid; }
50
52 virtual bool load() = 0;
54 virtual void release() {}
55
56 protected:
57 std::string m_path;
58 std::string m_error;
60
61 private:
62 std::uint64_t m_uuid;
63 };
64
66 class SHIT_API Texture final : public Resource {
67 public:
68 explicit Texture(std::string path) : Resource(std::move(path)) {}
69 ~Texture() override { release(); }
70
71 bool load() override;
72 void release() override;
73
74 SDL_Texture* get() const { return m_texture; }
75
76 private:
77 SDL_Texture* m_texture = nullptr;
78 };
79
81 struct FontKey {
82 std::string path;
83 float size;
84 bool operator==(const FontKey& o) const { return path == o.path && size == o.size; }
85 };
86 struct FontKeyHash {
87 std::size_t operator()(const FontKey& k) const {
88 return std::hash<std::string>{}(k.path) ^ std::hash<float>{}(k.size);
89 }
90 };
91
93 class SHIT_API Font final : public Resource {
94 public:
95 Font(std::string path, float size) : Resource(std::move(path)), m_size(size) {}
96 ~Font() override { release(); }
97
98 bool load() override;
99 void release() override;
100
101 TTF_Font* get() const { return m_font; }
102 float getSize() const { return m_size; }
103
104 private:
105 float m_size;
106 TTF_Font* m_font = nullptr;
107 };
108
110 class SHIT_API Audio final : public Resource {
111 public:
112 explicit Audio(std::string path) : Resource(std::move(path)) {}
113 ~Audio() override { release(); }
114
115 bool load() override;
116 void release() override;
117
118 MIX_Audio* get() const { return m_audio; }
119 void setMixer(MIX_Mixer* mixer) { m_mixer = mixer; }
120
121 private:
122 MIX_Mixer* m_mixer = nullptr; // 非所有,AudioPlayer 注入(生命周期由 AudioPlayer 管理)
123 MIX_Audio* m_audio = nullptr;
124 };
125
138 public:
139 virtual ~TypedCacheBase() = default;
140
141 virtual void clear() = 0;
142 virtual size_t size() const = 0;
143 virtual std::string_view typeName() const = 0;
145 virtual void forEachResource(const std::function<void(Resource*)>& fn) const = 0;
146 };
147}
#define SHIT_API
定义 Core.h:14
void setMixer(MIX_Mixer *mixer)
定义 Resource.h:119
Audio(std::string path)
定义 Resource.h:112
bool load() override
加载底层句柄(子类实现;失败记录 m_error 并返回 false,内部已打日志)
void release() override
释放底层句柄(幂等;子类析构与管理器回收时调用;基类无句柄可释放)
~Audio() override
定义 Resource.h:113
MIX_Audio * get() const
定义 Resource.h:118
~Font() override
定义 Resource.h:96
bool load() override
加载底层句柄(子类实现;失败记录 m_error 并返回 false,内部已打日志)
float getSize() const
定义 Resource.h:102
TTF_Font * get() const
定义 Resource.h:101
void release() override
释放底层句柄(幂等;子类析构与管理器回收时调用;基类无句柄可释放)
Font(std::string path, float size)
定义 Resource.h:95
资源基类 —— 所有引擎资产(Texture / Font / Audio / 自定义类型)的统一封装
定义 Resource.h:36
std::string m_error
定义 Resource.h:58
virtual bool load()=0
加载底层句柄(子类实现;失败记录 m_error 并返回 false,内部已打日志)
Resource(std::string path)
定义 Resource.h:38
const std::string & getError() const
Failed 时的诊断信息
定义 Resource.h:48
std::string m_path
定义 Resource.h:57
const std::string & getPath() const
定义 Resource.h:45
Resource & operator=(const Resource &)=delete
ResourceState getState() const
定义 Resource.h:46
virtual ~Resource()=default
std::uint64_t getUuid() const
会话级 uuid(进程内随机,不落盘)
定义 Resource.h:49
ResourceState m_state
定义 Resource.h:59
bool isLoaded() const
定义 Resource.h:47
virtual void release()
释放底层句柄(幂等;子类析构与管理器回收时调用;基类无句柄可释放)
定义 Resource.h:54
Resource(const Resource &)=delete
Texture(std::string path)
定义 Resource.h:68
bool load() override
加载底层句柄(子类实现;失败记录 m_error 并返回 false,内部已打日志)
~Texture() override
定义 Resource.h:69
SDL_Texture * get() const
借用句柄(空 = 未加载/失败)
定义 Resource.h:74
void release() override
释放底层句柄(幂等;子类析构与管理器回收时调用;基类无句柄可释放)
类型擦除缓存接口 —— ResourceManager 按资源类型注册缓存,经此统一遍历/清理
定义 Resource.h:137
virtual ~TypedCacheBase()=default
virtual std::string_view typeName() const =0
日志用类型名(来自 ResourceTraits<T>::kTypeName)
virtual void clear()=0
virtual size_t size() const =0
virtual void forEachResource(const std::function< void(Resource *)> &fn) const =0
遍历缓存内全部资源封装(uuid 查询用;顺序不保证)
定义 AnimationClip.h:12
ResourceState
资源加载状态
定义 Resource.h:23
@ Loaded
定义 Resource.h:23
@ Unloaded
定义 Resource.h:23
@ Failed
定义 Resource.h:23
std::uint64_t GenerateResourceUuid()
会话级资源 UUID 生成(与组件 UUID 同源的随机数方案;持久化资产注册表留待后续)
定义 Resource.h:86
std::size_t operator()(const FontKey &k) const
定义 Resource.h:87
字体缓存键:同字体不同字号各缓存一份
定义 Resource.h:81
bool operator==(const FontKey &o) const
定义 Resource.h:84
std::string path
定义 Resource.h:82
float size
定义 Resource.h:83