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 }
71 {
72 _bUseBinary = true;
73 }
74
80 void LoadModel(std::string_view path_name, std::string_view name = "");
81
85 std::string GetPathImage()
86 {
87 return _strPathImage;
88 }
92 std::string GetPathVoice()
93 {
94 return _strPathVoice;
95 }
96
100 std::string GetPathBinary()
101 {
102 return _strPathBinary;
103 }
107 std::string GetPathBinary3D()
108 {
109 return _strPathBinary + std::string{ STR_PATH_3D };
110 }
114 std::string GetPathBinary2D()
115 {
116 return _strPathBinary + std::string{ STR_PATH_2D };
117 }
121 std::string GetPathTemp()
122 {
123 return _strPathTemp;
124 }
129 std::string GetPathNameTemp(std::string_view str_fmt = "")
130 {
131 std::string ret = str_fmt.empty() ?
132 fmt::format("{}{}", _strPathTemp, _idTemp) :
133 fmt::format("{}{}.{}", _strPathTemp, _idTemp, str_fmt);
134 ++_idTemp;
135 return ret;
136 }
137
143 gcActor CreateActor(std::string_view name);
144
150 //-----------------------------Sprite------------------------------
155
168 std::string_view name,
169 std::size_t index = 0);
177 bool RegisterSprite(std::string_view name, Image* img, const Point& anchor = {});
181 bool HasSprite(std::string_view name);
188 std::vector<gcSprite> CreateSpriteMulti(
189 std::string_view name,
190 std::size_t num = 0,
191 std::size_t index = 0);
192
198 {
199 return CloneSprite(const_cast<gcSprite&>(spr));
200 }
209 {
210 return GetSpriteTexSize(spr->GetID());
211 }
215 std::string GetSpriteName(const SpriteID& spr_id);
216
217 //------------------------Font--------------------------
223 size_t LoadFont(std::string_view path_name);
227 size_t GetFontSize();
233
243 float CreateSpriteChar(const CharInfo& info, gcSprite& spr, gcSprite& spr_outline);
244
245 float CreateSpriteChar(const CharInfo& info, gcSprite& spr)
246 {
247 gcSprite spr_outline;
248 return CreateSpriteChar(info, spr, spr_outline);
249 }
250
263 size_t font_id = 0, unsigned font_size = 16, const Color& col = ColorDef::WHITE,
264 unsigned font_outline = 0, const Color& col_outline = ColorDef::BLACK,
265 unsigned font_shadow = 0, const Color& col_shadow = ColorDef::BLACK);
266
268 {
269 gcText ret = CreateText(0, 16, ColorDef::WHITE);
270 ret->SetString("Default");
271 return ret;
272 }
276 gcText CloneText(gcText txt, bool copy_str = false)
277 {
278 gcText ret = CreateText(
279 txt->_fontId, txt->_fontSize[0], txt->_col[0],
280 txt->_fontSize[1], txt->_col[1],
281 txt->_fontSize[2], txt->_col[2]);
282 ret->GetCoor()->SetParent(txt->GetCoor()->GetParent());
283 ret->_layout = txt->_layout;
284 ret->_order = txt->_order;
285 ret->_bFormat = txt->_bFormat;
286 ret->SetBackground(txt.GetPointer());
287 if (copy_str)
288 ret->SetString(txt->GetString());
289 return ret;
290 }
296 Coor* GetRootCoor(size_t i);
297 //----------------------Color-----------------------------
305 void AddColor(std::string_view name, const Color& col);
306
314 Color GetColor(std::string_view name);
315
319 std::optional<Color> GetColorCheck(std::string_view name);
320
325 void LoadColorCsv(std::string_view name);
326
327
328 //-----------------------Other----------------------------
332 void DeleteBinary(bool b_all, bool b_2d, bool b_3d);
337 FactoryImp* GetImp() { return _imp; }
338
340private:
341 Factory();
342 ~Factory();
343
344 FactoryImp* _imp;
345 std::string _strPathImage; //图像 根目录
346 std::string _strPathModel; //模型 根目录
347 std::string _strPathVoice; //音频 根目录
348 std::string _strPathBinary; //输出 根目录
349 std::string _strPathTemp; //临时 根目录
350 bool _bSetPath;
351 bool _bUseBinary; // 是否使用输出目录
352 std::size_t _idTemp;//临时文件使用id
353
354 //颜色tag
355 std::unordered_map<std::string, Color> _hashTagColor;
356
357
358 //init之前会调用一次
359 void _init_path();
360 void _init_defualt_res();//初始化默认资源
361 //init之后,程序结束均调用一次
362 void _save_cache();
363
364 //加载精灵(根据_strPathImage和_strPathBinary)
365 //_init_path里调用
366 void _load_sprite();
367
368};
369
370extern Factory* g_factory;
371
372}
2d坐标系
Definition: dl_coor.h:27
Coor * GetParent() const
void SetParent(Coor *coor)
资源工厂 @info 加载、管理、创建各种资源
Definition: dl_factory.h:42
std::string GetPathBinary3D()
返回3d资源缓存目录,例如"binary/3d/"
Definition: dl_factory.h:107
std::string GetPathBinary()
返回资源缓存目录,例如"binary/"
Definition: dl_factory.h:100
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:245
std::string GetPathNameTemp(std::string_view str_fmt="")
返回临时文件名,例如"temp/1"
Definition: dl_factory.h:129
FactoryImp * GetImp()
Definition: dl_factory.h:337
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:114
size_t LoadFont(std::string_view path_name)
加载字体
gcSprite CloneSprite(gcSprite &spr)
复制一个精灵
Coor * GetRootCoor(size_t i)
返回根坐标系
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:276
gcActor CreateActor(std::string_view name)
创建一个演员,通过指定模型文件
bool HasSprite(std::string_view name)
检测精灵是否存在
std::string GetPathImage()
返回图像目录
Definition: dl_factory.h:85
std::string GetSpriteName(const SpriteID &spr_id)
返回精灵的名字
std::string GetPathVoice()
返回音效目录
Definition: dl_factory.h:92
CharImage * GetChar(const CharInfo &info)
获得字符图像
void _render_sprite(Sprite *spr)
gcSprite CloneSprite(const gcSprite &spr)
Definition: dl_factory.h:197
std::string GetPathTemp()
返回临时文件路径,例如"temp/"
Definition: dl_factory.h:121
bool RegisterSprite(std::string_view name, Image *img, const Point &anchor={})
注册一个精灵(动态)
Size GetSpriteTexSize(const SpriteID &spr_id)
返回精灵的纹理大小
gcText CreateTextDefault()
Definition: dl_factory.h:267
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 SetReleaseMode()
设置发布模式,使用binary资源
Definition: dl_factory.h:70
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)
返回精灵的纹理大小
Definition: dl_factory.h:208
void ExportTexture2D()
导出所有2d纹理
Color GetColor(std::string_view name)
返回一个颜色
引用计数对象,通过dnd_gc宏创建
T * GetPointer()
直接获取指针,注意生命周期
SpriteID GetID() const
Definition: dl_sprite.h:74
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
Point2 Point
Definition: dl_type_def.h:23
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