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 "render/dl_sprite.h"
21#include "render/dl_actor.h"
22#include "render/dl_font.h"
23#include "render/dl_text.h"
24#include "base/dl_color.h"
25
26namespace dl
27{
28
29constexpr Size SIZE_SPRITE_DEFAULT = { 32, 32 };
30constexpr std::string_view STR_PATH_2D = "2d/";
31constexpr std::string_view STR_PATH_3D = "3d/";
32
33
34class FactoryImp;
35class Camera;
41{
42 friend class System;
43 friend class Engine;
44public:
49 void SetPath(
50 std::string_view path_image = "image/",
51 std::string_view path_model = "model/",
52 std::string_view path_binary = "binary/",
53 std::string_view path_temp = "temp/")
54 {
55 if (_bSetPath)
56 {
57 log_damage0("只能设置一次,在调用Start之前!");
58 return;
59 }
60 _bSetPath = true;
61 _strPathImage = path_image;
62 _strPathModel = path_model;
63 _strPathBinary = path_binary;
64 _strPathTemp = path_temp;
65 }
66
72 void LoadModel(std::string_view path_name, std::string_view name = "");
76 std::string GetPathBinary()
77 {
78 return _strPathBinary;
79 }
83 std::string GetPathBinary3D()
84 {
85 return _strPathBinary + std::string{ STR_PATH_3D };
86 }
90 std::string GetPathBinary2D()
91 {
92 return _strPathBinary + std::string{ STR_PATH_2D };
93 }
97 std::string GetPathTemp()
98 {
99 return _strPathTemp;
100 }
105 std::string GetPathNameTemp(std::string_view str_fmt = "")
106 {
107 std::string ret = str_fmt.empty() ?
108 fmt::format("{}{}", _strPathTemp, _idTemp) :
109 fmt::format("{}{}.{}", _strPathTemp, _idTemp, str_fmt);
110 ++_idTemp;
111 return ret;
112 }
113
119 gcActor CreateActor(std::string_view name);
120
126 //-----------------------------Sprite------------------------------
131
144 std::string_view name,
145 std::size_t index = 0);
146
147
154 std::vector<gcSprite> CreateSpriteMulti(
155 std::string_view name,
156 std::size_t num = 0,
157 std::size_t index = 0);
158
164 {
165 return CloneSprite(const_cast<gcSprite&>(spr));
166 }
171
172 //------------------------Font--------------------------
178 size_t LoadFont(std::string_view path_name);
182 size_t GetFontSize();
188
198 float CreateSpriteChar(const CharInfo& info, gcSprite& spr, gcSprite& spr_outline);
199
200 float CreateSpriteChar(const CharInfo& info, gcSprite& spr)
201 {
202 gcSprite spr_outline;
203 return CreateSpriteChar(info, spr, spr_outline);
204 }
205
218 size_t font_id = 0, unsigned font_size = 16, const Color& col = ColorDef::WHITE,
219 unsigned font_outline = 0, const Color& col_outline = ColorDef::BLACK,
220 unsigned font_shadow = 0, const Color& col_shadow = ColorDef::BLACK);
221
223 {
225 ret->SetString("Default");
226 return ret;
227 }
231 gcText CloneText(gcText txt, bool copy_str = false)
232 {
233 gcText ret = CreateText(
234 txt->_fontId, txt->_fontSize[0], txt->_col[0],
235 txt->_fontSize[1], txt->_col[1],
236 txt->_fontSize[2], txt->_col[2]);
237 ret->GetCoor()->SetParent(txt->GetCoor()->GetParent());
238 ret->_layout = txt->_layout;
239 ret->_order = txt->_order;
240 ret->_bFormat = txt->_bFormat;
241 ret->SetBackground(txt.GetPointer());
242 if (copy_str)
243 ret->SetString(txt->GetString());
244 return ret;
245 }
246 //----------------------Color-----------------------------
254 void AddColor(std::string_view name, const Color& col);
255
263 Color GetColor(std::string_view name);
264
268 std::optional<Color> GetColorCheck(std::string_view name);
269
274 void LoadColorCsv(std::string_view name);
275
276
277 //-----------------------Other----------------------------
281 void DeleteBinary(bool b_all, bool b_2d, bool b_3d);
286 FactoryImp* GetImp() { return _imp; }
287
289private:
290 Factory();
291 ~Factory();
292
293 FactoryImp* _imp;
294 std::string _strPathImage;//图像 根目录
295 std::string _strPathModel;//模型 根目录
296 std::string _strPathBinary;//输出 根目录
297 std::string _strPathTemp;//临时 根目录
298 bool _bSetPath;
299 std::size_t _idTemp;//临时文件使用id
300
301 //颜色tag
302 std::unordered_map<std::string, Color> _hashTagColor;
303
304
305 //init之前会调用一次
306 void _init_path();
307 void _init_defualt_res();//初始化默认资源
308 //init之后,程序结束均调用一次
309 void _save_cache();
310
311 //加载精灵(根据_strPathImage和_strPathBinary)
312 //_init_path里调用
313 void _load_sprite();
314
315};
316
317extern Factory* g_factory;
318
319}
Coor * GetParent() const
void SetParent(Coor *coor)
资源工厂 @info 加载、管理、创建各种资源
Definition: dl_factory.h:41
std::string GetPathBinary3D()
返回3d资源缓存目录,例如"binary/3d/"
Definition: dl_factory.h:83
std::string GetPathBinary()
返回资源缓存目录,例如"binary/"
Definition: dl_factory.h:76
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:200
std::string GetPathNameTemp(std::string_view str_fmt="")
返回临时文件名,例如"temp/1"
Definition: dl_factory.h:105
FactoryImp * GetImp()
Definition: dl_factory.h:286
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:90
size_t LoadFont(std::string_view path_name)
加载字体
gcSprite CloneSprite(gcSprite &spr)
复制一个精灵
void SetPath(std::string_view path_image="image/", std::string_view path_model="model/", std::string_view path_binary="binary/", std::string_view path_temp="temp/")
设置 各种路径
Definition: dl_factory.h:49
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:231
gcActor CreateActor(std::string_view name)
创建一个演员,通过指定模型文件
CharImage * GetChar(const CharInfo &info)
获得字符图像
void _render_sprite(Sprite *spr)
gcSprite CloneSprite(const gcSprite &spr)
Definition: dl_factory.h:163
std::string GetPathTemp()
返回临时文件路径,例如"temp/"
Definition: dl_factory.h:97
gcText CreateTextDefault()
Definition: dl_factory.h:222
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()
Coor * GetCoor()
返回坐标系
Definition: dl_text.h:238
void SetBackground(bool open, const Color &col=ColorDef::TEXT_BG)
设置背景精灵
void SetString(std::string_view str)
设置内容
3d绘图元素,翻译为角色
字体解析结构
日志系统
2d绘图元素
文本绘制
constexpr Color RED
Definition: dl_color.h:252
constexpr Color WHITE
Definition: dl_color.h:250
constexpr Color BLACK
Definition: dl_color.h:251
constexpr Color GREEN
Definition: dl_color.h:253
Definition: dl_array2.h:17
PointU Size
Definition: dl_type_def.h:34
Factory * g_factory
constexpr std::string_view STR_PATH_2D
Definition: dl_factory.h:30
constexpr Size SIZE_SPRITE_DEFAULT
Definition: dl_factory.h:29
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:31
字符图像数据
Definition: dl_font.h:49
字符信息
Definition: dl_font.h:20