dl
dl_factory.h
浏览该文件的文档.
1
13#pragma once
14
15#include <unordered_map>
16#include <optional>
17
18#include "dl_type.h"
19#include "io/dl_log.h"
20#include "io/dl_file.h"
21#include "render/dl_sprite.h"
22#include "render/dl_actor.h"
23#include "render/dl_font.h"
24#include "render/dl_text.h"
25#include "base/dl_color.h"
26
27namespace dl
28{
29
30constexpr Size SIZE_SPRITE_DEFAULT = { 32, 32 };
31constexpr std::string_view STR_PATH_2D = "2d/";
32constexpr std::string_view STR_PATH_3D = "3d/";
33
34
35class FactoryImp;
36class Camera;
42{
43 friend class System;
44 friend class Engine;
45public:
50 void SetPath(
51 std::string_view path_image,
52 std::string_view path_model,
53 std::string_view path_binary,
54 std::string_view path_temp)
55 {
56 if (_bSetPath)
57 {
58 log_damage0("只能设置一次,在调用Start之前!");
59 return;
60 }
61 _bSetPath = true;
62 _strPathImage = path_image;
63 _strPathModel = path_model;
64 _strPathBinary = path_binary;
65 _strPathTemp = path_temp;
66 }
67
73 void LoadModel(std::string_view path_name, std::string_view name = "");
74
78 std::string GetPathImage()
79 {
80 return _strPathImage;
81 }
85 std::string GetPathVoice()
86 {
87 return _strPathVoice;
88 }
89
93 std::string GetPathBinary()
94 {
95 return _strPathBinary;
96 }
100 std::string GetPathBinary3D()
101 {
102 return _strPathBinary + std::string{ STR_PATH_3D };
103 }
107 std::string GetPathBinary2D()
108 {
109 return _strPathBinary + std::string{ STR_PATH_2D };
110 }
114 std::string GetPathTemp()
115 {
116 return _strPathTemp;
117 }
122 std::string GetPathNameTemp(std::string_view str_fmt = "")
123 {
124 std::string ret = str_fmt.empty() ?
125 fmt::format("{}{}", _strPathTemp, _idTemp) :
126 fmt::format("{}{}.{}", _strPathTemp, _idTemp, str_fmt);
127 ++_idTemp;
128 return ret;
129 }
130
136 gcActor CreateActor(std::string_view name);
137
143 //-----------------------------Sprite------------------------------
148
161 std::string_view name,
162 std::size_t index = 0);
169 bool RegisterSprite(std::string_view name, Image* img);
173 bool HasSprite(std::string_view name);
180 std::vector<gcSprite> CreateSpriteMulti(
181 std::string_view name,
182 std::size_t num = 0,
183 std::size_t index = 0);
184
190 {
191 return CloneSprite(const_cast<gcSprite&>(spr));
192 }
197
198 //------------------------Font--------------------------
204 size_t LoadFont(std::string_view path_name);
208 size_t GetFontSize();
214
224 float CreateSpriteChar(const CharInfo& info, gcSprite& spr, gcSprite& spr_outline);
225
226 float CreateSpriteChar(const CharInfo& info, gcSprite& spr)
227 {
228 gcSprite spr_outline;
229 return CreateSpriteChar(info, spr, spr_outline);
230 }
231
244 size_t font_id = 0, unsigned font_size = 16, const Color& col = ColorDef::WHITE,
245 unsigned font_outline = 0, const Color& col_outline = ColorDef::BLACK,
246 unsigned font_shadow = 0, const Color& col_shadow = ColorDef::BLACK);
247
249 {
250 gcText ret = CreateText(0, 16, ColorDef::WHITE);
251 ret->SetString("Default");
252 return ret;
253 }
257 gcText CloneText(gcText txt, bool copy_str = false)
258 {
259 gcText ret = CreateText(
260 txt->_fontId, txt->_fontSize[0], txt->_col[0],
261 txt->_fontSize[1], txt->_col[1],
262 txt->_fontSize[2], txt->_col[2]);
263 ret->GetCoor()->SetParent(txt->GetCoor()->GetParent());
264 ret->_layout = txt->_layout;
265 ret->_order = txt->_order;
266 ret->_bFormat = txt->_bFormat;
267 ret->SetBackground(txt.GetPointer());
268 if (copy_str)
269 ret->SetString(txt->GetString());
270 return ret;
271 }
272 //----------------------Color-----------------------------
280 void AddColor(std::string_view name, const Color& col);
281
289 Color GetColor(std::string_view name);
290
294 std::optional<Color> GetColorCheck(std::string_view name);
295
300 void LoadColorCsv(std::string_view name);
301
302
303 //-----------------------Other----------------------------
307 void DeleteBinary(bool b_all, bool b_2d, bool b_3d);
312 FactoryImp* GetImp() { return _imp; }
313
315private:
316 Factory();
317 ~Factory();
318
319 FactoryImp* _imp;
320 std::string _strPathImage; //图像 根目录
321 std::string _strPathModel; //模型 根目录
322 std::string _strPathVoice; //音频 根目录
323 std::string _strPathBinary; //输出 根目录
324 std::string _strPathTemp; //临时 根目录
325 bool _bSetPath;
326 std::size_t _idTemp;//临时文件使用id
327
328 //颜色tag
329 std::unordered_map<std::string, Color> _hashTagColor;
330
331
332 //init之前会调用一次
333 void _init_path();
334 void _init_defualt_res();//初始化默认资源
335 //init之后,程序结束均调用一次
336 void _save_cache();
337
338 //加载精灵(根据_strPathImage和_strPathBinary)
339 //_init_path里调用
340 void _load_sprite();
341
342};
343
344extern Factory* g_factory;
345
346}
Coor * GetParent() const
void SetParent(Coor *coor)
资源工厂 @info 加载、管理、创建各种资源
Definition: dl_factory.h:42
std::string GetPathBinary3D()
返回3d资源缓存目录,例如"binary/3d/"
Definition: dl_factory.h:100
std::string GetPathBinary()
返回资源缓存目录,例如"binary/"
Definition: dl_factory.h:93
gcSprite CreateSprite(std::string_view name, std::size_t index=0)
创建一个精灵
size_t GetFontSize()
返回加载的字体数量
float CreateSpriteChar(const CharInfo &info, gcSprite &spr)
Definition: dl_factory.h:226
std::string GetPathNameTemp(std::string_view str_fmt="")
返回临时文件名,例如"temp/1"
Definition: dl_factory.h:122
FactoryImp * GetImp()
Definition: dl_factory.h:312
float CreateSpriteChar(const CharInfo &info, gcSprite &spr, gcSprite &spr_outline)
创建一个字符精灵
gcSprite CreateSprite(const SpriteID &spr_id)
创建一个精灵
std::string GetPathBinary2D()
返回2d资源缓存目录,例如"binary/2d/"
Definition: dl_factory.h:107
size_t LoadFont(std::string_view path_name)
加载字体
gcSprite CloneSprite(gcSprite &spr)
复制一个精灵
void LoadModel(std::string_view path_name, std::string_view name="")
加载模型文件
void AddColor(std::string_view name, const Color &col)
添加一个颜色
gcText CreateText(size_t font_id=0, unsigned font_size=16, const Color &col=ColorDef::WHITE, unsigned font_outline=0, const Color &col_outline=ColorDef::BLACK, unsigned font_shadow=0, const Color &col_shadow=ColorDef::BLACK)
创建一个文本
Camera * GetCamera()
返回相机(3d)
gcText CloneText(gcText txt, bool copy_str=false)
复制一个文本
Definition: dl_factory.h:257
gcActor CreateActor(std::string_view name)
创建一个演员,通过指定模型文件
bool HasSprite(std::string_view name)
检测精灵是否存在
std::string GetPathImage()
返回图像目录
Definition: dl_factory.h:78
std::string GetPathVoice()
返回音效目录
Definition: dl_factory.h:85
bool RegisterSprite(std::string_view name, Image *img)
注册一个精灵(动态)
CharImage * GetChar(const CharInfo &info)
获得字符图像
void _render_sprite(Sprite *spr)
gcSprite CloneSprite(const gcSprite &spr)
Definition: dl_factory.h:189
std::string GetPathTemp()
返回临时文件路径,例如"temp/"
Definition: dl_factory.h:114
gcText CreateTextDefault()
Definition: dl_factory.h:248
void SetPath(std::string_view path_image, std::string_view path_model, std::string_view path_binary, std::string_view path_temp)
设置 各种路径
Definition: dl_factory.h:50
void DeleteBinary(bool b_all, bool b_2d, bool b_3d)
删除输出目录内容
void LoadColorCsv(std::string_view name)
从文件加载颜色标签
std::vector< gcSprite > CreateSpriteMulti(std::string_view name, std::size_t num=0, std::size_t index=0)
创建多个精灵,从已加载的精灵表复制
gcSprite CreateSpriteDefault(const Color &col=ColorDef::WHITE)
创建默认精灵
std::optional< Color > GetColorCheck(std::string_view name)
返回一个颜色,会检查
Size GetSpriteTexSize(gcSprite &spr)
返回精灵的纹理大小
void ExportTexture2D()
导出所有2d纹理
Color GetColor(std::string_view name)
返回一个颜色
引用计数对象,通过dnd_gc宏创建
T * GetPointer()
直接获取指针,注意生命周期
std::string GetString()
bool SetString(std::string_view str)
设置内容
Coor * GetCoor()
返回坐标系
Definition: dl_text.h:250
void SetBackground(bool open, const Color &col=ColorDef::TEXT_BG)
设置背景精灵
3d绘图元素,翻译为角色
文件系统
字体解析结构
日志系统
2d绘图元素
文本绘制
通用类型
constexpr Color WHITE
Definition: dl_color.h:250
constexpr Color BLACK
Definition: dl_color.h:251
Definition: dl_array2.h:18
PointU Size
Definition: dl_type_def.h:34
Factory * g_factory
constexpr std::string_view STR_PATH_2D
Definition: dl_factory.h:31
constexpr Size SIZE_SPRITE_DEFAULT
Definition: dl_factory.h:30
std::array< float, 4 > Color
Definition: dl_type_def.h:49
void log_damage0(std::string_view str, const std::source_location location=std::source_location::current())
constexpr std::string_view STR_PATH_3D
Definition: dl_factory.h:32
字符图像数据
Definition: dl_font.h:49
字符信息
Definition: dl_font.h:20