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;
41class Factory
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
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 }
89
92 std::string GetPathVoice()
93 {
94 return _strPathVoice;
95 }
96
100 std::string GetPathBinary()
101 {
102 return _strPathBinary;
103 }
104
107 std::string GetPathBinary3D()
108 {
109 return _strPathBinary + std::string{ STR_PATH_3D };
110 }
111
114 std::string GetPathBinary2D()
115 {
116 return _strPathBinary + std::string{ STR_PATH_2D };
117 }
118
121 std::string GetPathTemp()
122 {
123 return _strPathTemp;
124 }
125
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------------------------------
154 [[nodiscard]]
156
169 std::string_view name,
170 std::size_t index = 0);
178 bool RegisterSprite(std::string_view name, Image* img, const Point& anchor = {});
182 bool HasSprite(std::string_view name);
189 std::vector<gcSprite> CreateSpriteMulti(
190 std::string_view name,
191 std::size_t num = 0,
192 std::size_t index = 0);
193
199 {
200 return CloneSprite(const_cast<gcSprite&>(spr));
201 }
202
210 {
211 return GetSpriteTexSize(spr->GetID());
212 }
213
216 std::string GetSpriteName(const SpriteID& spr_id);
217
218 //------------------------Font--------------------------
224 size_t LoadFont(std::string_view path_name);
228 size_t GetFontSize();
234
244 float CreateSpriteChar(const CharInfo& info, gcSprite& spr, gcSprite& spr_outline);
245
246 float CreateSpriteChar(const CharInfo& info, gcSprite& spr)
247 {
248 gcSprite spr_outline;
249 return CreateSpriteChar(info, spr, spr_outline);
250 }
251
264 size_t font_id = 0, unsigned font_size = 16, const Color& col = ColorDef::WHITE,
265 unsigned font_outline = 0, const Color& col_outline = ColorDef::BLACK,
266 unsigned font_shadow = 0, const Color& col_shadow = ColorDef::BLACK);
267
269 {
270 gcText ret = CreateText(0, 16, ColorDef::WHITE);
271 ret->SetString("Default");
272 return ret;
273 }
274
277 gcText CloneText(gcText txt, bool copy_str = false)
278 {
279 gcText ret = CreateText(
280 txt->_fontId, txt->_fontSize[0], txt->_col[0],
281 txt->_fontSize[1], txt->_col[1],
282 txt->_fontSize[2], txt->_col[2]);
283 ret->GetCoor()->SetParent(txt->GetCoor()->GetParent());
284 ret->_layout = txt->_layout;
285 ret->_order = txt->_order;
286 ret->_bFormat = txt->_bFormat;
287 ret->SetBackground(txt.GetPointer());
288 if (copy_str)
289 ret->SetString(txt->GetString());
290 return ret;
291 }
292
297 Coor* GetRootCoor(size_t i);
298 //----------------------Color-----------------------------
306 void AddColor(std::string_view name, const Color& col);
307
315 Color GetColor(std::string_view name);
316
320 std::optional<Color> GetColorCheck(std::string_view name);
321
326 void LoadColorCsv(std::string_view name);
327
328
329 //-----------------------Other----------------------------
333 void DeleteBinary(bool b_all, bool b_2d, bool b_3d);
338 FactoryImp* GetImp() { return _imp; }
339
341private:
342 Factory();
343 ~Factory();
344
345 FactoryImp* _imp;
346 std::string _strPathImage; //图像 根目录
347 std::string _strPathModel; //模型 根目录
348 std::string _strPathVoice; //音频 根目录
349 std::string _strPathBinary; //输出 根目录
350 std::string _strPathTemp; //临时 根目录
351 bool _bSetPath;
352 bool _bUseBinary; // 是否使用输出目录
353 std::size_t _idTemp;//临时文件使用id
354
355 //颜色tag
356 std::unordered_map<std::string, Color> _hashTagColor;
357
358
359 //init之前会调用一次
360 void _init_path();
361 void _init_defualt_res();//初始化默认资源
362 //init之后,程序结束均调用一次
363 void _save_cache();
364
365 //加载精灵(根据_strPathImage和_strPathBinary)
366 //_init_path里调用
367 void _load_sprite();
368
369};
370
371extern Factory* g_factory;
372
373}
2d坐标系
Coor * GetParent() const
void SetParent(Coor *coor)
资源工厂 @info 加载、管理、创建各种资源
std::string GetPathBinary3D()
返回3d资源缓存目录,例如"binary/3d/"
std::string GetPathBinary()
返回资源缓存目录,例如"binary/"
gcSprite CreateSprite(std::string_view name, std::size_t index=0)
创建一个精灵
size_t GetFontSize()
返回加载的字体数量
float CreateSpriteChar(const CharInfo &info, gcSprite &spr)
std::string GetPathNameTemp(std::string_view str_fmt="")
返回临时文件名,例如"temp/1"
FactoryImp * GetImp()
float CreateSpriteChar(const CharInfo &info, gcSprite &spr, gcSprite &spr_outline)
创建一个字符精灵
gcSprite CreateSprite(const SpriteID &spr_id)
创建一个精灵
std::string GetPathBinary2D()
返回2d资源缓存目录,例如"binary/2d/"
size_t LoadFont(std::string_view path_name)
加载字体
friend class Engine
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)
复制一个文本
gcActor CreateActor(std::string_view name)
创建一个演员,通过指定模型文件
bool HasSprite(std::string_view name)
检测精灵是否存在
std::string GetPathImage()
返回图像目录
std::string GetSpriteName(const SpriteID &spr_id)
返回精灵的名字
std::string GetPathVoice()
返回音效目录
CharImage * GetChar(const CharInfo &info)
获得字符图像
void _render_sprite(Sprite *spr)
gcSprite CloneSprite(const gcSprite &spr)
std::string GetPathTemp()
返回临时文件路径,例如"temp/"
bool RegisterSprite(std::string_view name, Image *img, const Point &anchor={})
注册一个精灵(动态)
Size GetSpriteTexSize(const SpriteID &spr_id)
返回精灵的纹理大小
gcText CreateTextDefault()
void SetPath(std::string_view path_image, std::string_view path_model, std::string_view path_binary, std::string_view path_temp)
设置 各种路径
void SetReleaseMode()
设置发布模式,使用binary资源
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纹理
friend class System
Color GetColor(std::string_view name)
返回一个颜色
T * GetPointer()
直接获取指针,注意生命周期
SpriteID GetID() const
std::string GetString()
bool SetString(std::string_view str)
设置内容
Coor * GetCoor()
返回坐标系
void SetBackground(bool open, const Color &col=ColorDef::TEXT_BG)
设置背景精灵
3d绘图元素,翻译为角色
文件系统
字体解析结构
日志系统
2d绘图元素
文本绘制
通用类型
constexpr Color WHITE
constexpr Color BLACK
Factory * g_factory
constexpr std::string_view STR_PATH_2D
GC< Actor > gcActor
constexpr Size SIZE_SPRITE_DEFAULT
Vector2< uint32_t > Image
GC< Sprite > gcSprite
GC< Text > gcText
std::array< float, 4 > Color
PointU Size
Point2 Point
void log_damage0(std::string_view str, const std::source_location location=std::source_location::current())
constexpr std::string_view STR_PATH_3D
字符图像数据
字符信息