ShitEngine
1.4.2
基于 C++20 与 SDL3 的轻量级 2D 游戏引擎
Toggle main menu visibility
载入中...
搜索中...
未找到
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
10
#include "
ShitEngine/Core/Core.h
"
11
12
struct
SDL_Texture;
13
struct
TTF_Font;
14
struct
MIX_Audio;
15
struct
MIX_Mixer;
16
17
namespace
Shit
{
18
20
SHIT_API
std::uint64_t
GenerateResourceUuid
();
21
23
enum class
ResourceState
{
Unloaded
,
Loaded
,
Failed
};
24
36
class
SHIT_API
Resource
{
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
;
59
ResourceState
m_state
=
ResourceState::Unloaded
;
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
137
class
SHIT_API
TypedCacheBase
{
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
}
Core.h
SHIT_API
#define SHIT_API
定义
Core.h:14
Shit::Audio::setMixer
void setMixer(MIX_Mixer *mixer)
定义
Resource.h:119
Shit::Audio::Audio
Audio(std::string path)
定义
Resource.h:112
Shit::Audio::load
bool load() override
加载底层句柄(子类实现;失败记录 m_error 并返回 false,内部已打日志)
Shit::Audio::release
void release() override
释放底层句柄(幂等;子类析构与管理器回收时调用;基类无句柄可释放)
Shit::Audio::~Audio
~Audio() override
定义
Resource.h:113
Shit::Audio::get
MIX_Audio * get() const
定义
Resource.h:118
Shit::Font::~Font
~Font() override
定义
Resource.h:96
Shit::Font::load
bool load() override
加载底层句柄(子类实现;失败记录 m_error 并返回 false,内部已打日志)
Shit::Font::getSize
float getSize() const
定义
Resource.h:102
Shit::Font::get
TTF_Font * get() const
定义
Resource.h:101
Shit::Font::release
void release() override
释放底层句柄(幂等;子类析构与管理器回收时调用;基类无句柄可释放)
Shit::Font::Font
Font(std::string path, float size)
定义
Resource.h:95
Shit::Resource
资源基类 —— 所有引擎资产(Texture / Font / Audio / 自定义类型)的统一封装
定义
Resource.h:36
Shit::Resource::m_error
std::string m_error
定义
Resource.h:58
Shit::Resource::load
virtual bool load()=0
加载底层句柄(子类实现;失败记录 m_error 并返回 false,内部已打日志)
Shit::Resource::Resource
Resource(std::string path)
定义
Resource.h:38
Shit::Resource::getError
const std::string & getError() const
Failed 时的诊断信息
定义
Resource.h:48
Shit::Resource::m_path
std::string m_path
定义
Resource.h:57
Shit::Resource::getPath
const std::string & getPath() const
定义
Resource.h:45
Shit::Resource::operator=
Resource & operator=(const Resource &)=delete
Shit::Resource::getState
ResourceState getState() const
定义
Resource.h:46
Shit::Resource::~Resource
virtual ~Resource()=default
Shit::Resource::getUuid
std::uint64_t getUuid() const
会话级 uuid(进程内随机,不落盘)
定义
Resource.h:49
Shit::Resource::m_state
ResourceState m_state
定义
Resource.h:59
Shit::Resource::isLoaded
bool isLoaded() const
定义
Resource.h:47
Shit::Resource::release
virtual void release()
释放底层句柄(幂等;子类析构与管理器回收时调用;基类无句柄可释放)
定义
Resource.h:54
Shit::Resource::Resource
Resource(const Resource &)=delete
Shit::Texture::Texture
Texture(std::string path)
定义
Resource.h:68
Shit::Texture::load
bool load() override
加载底层句柄(子类实现;失败记录 m_error 并返回 false,内部已打日志)
Shit::Texture::~Texture
~Texture() override
定义
Resource.h:69
Shit::Texture::get
SDL_Texture * get() const
借用句柄(空 = 未加载/失败)
定义
Resource.h:74
Shit::Texture::release
void release() override
释放底层句柄(幂等;子类析构与管理器回收时调用;基类无句柄可释放)
Shit::TypedCacheBase
类型擦除缓存接口 —— ResourceManager 按资源类型注册缓存,经此统一遍历/清理
定义
Resource.h:137
Shit::TypedCacheBase::~TypedCacheBase
virtual ~TypedCacheBase()=default
Shit::TypedCacheBase::typeName
virtual std::string_view typeName() const =0
日志用类型名(来自 ResourceTraits<T>::kTypeName)
Shit::TypedCacheBase::clear
virtual void clear()=0
Shit::TypedCacheBase::size
virtual size_t size() const =0
Shit::TypedCacheBase::forEachResource
virtual void forEachResource(const std::function< void(Resource *)> &fn) const =0
遍历缓存内全部资源封装(uuid 查询用;顺序不保证)
Shit
定义
AnimationClip.h:12
Shit::ResourceState
ResourceState
资源加载状态
定义
Resource.h:23
Shit::ResourceState::Loaded
@ Loaded
定义
Resource.h:23
Shit::ResourceState::Unloaded
@ Unloaded
定义
Resource.h:23
Shit::ResourceState::Failed
@ Failed
定义
Resource.h:23
Shit::GenerateResourceUuid
std::uint64_t GenerateResourceUuid()
会话级资源 UUID 生成(与组件 UUID 同源的随机数方案;持久化资产注册表留待后续)
Shit::FontKeyHash
定义
Resource.h:86
Shit::FontKeyHash::operator()
std::size_t operator()(const FontKey &k) const
定义
Resource.h:87
Shit::FontKey
字体缓存键:同字体不同字号各缓存一份
定义
Resource.h:81
Shit::FontKey::operator==
bool operator==(const FontKey &o) const
定义
Resource.h:84
Shit::FontKey::path
std::string path
定义
Resource.h:82
Shit::FontKey::size
float size
定义
Resource.h:83
include
ShitEngine
Resource
Resource.h
制作者
1.17.0