dl
dl_factory.h
浏览该文件的文档.
1
13#pragma once
14
15#include <unordered_map>
16#include <optional>
17
18#include "dl_type.h"
19#include "base/dl_color.h"
20#include "io/dl_log.h"
21#include "io/dl_file.h"
22#include "render/dl_sprite.h"
23#include "render/dl_actor.h"
24#include "render/dl_font.h"
25#include "render/dl_text.h"
27#include "render/dl_animation.h"
28
29namespace dl
30{
31
32constexpr Size SIZE_SPRITE_DEFAULT = { 32, 32 };
33constexpr std::string_view STR_PATH_2D = "2d/";
34constexpr std::string_view STR_PATH_3D = "3d/";
35
36class FactoryImp;
37class Camera;
39class Factory
40{
41 friend class System;
42 friend class Engine;
43public:
48 void SetPath(
49 std::string_view path_image = "image/",
50 std::string_view path_model = "model/",
51 std::string_view path_binary = "binary/",
52 std::string_view path_temp = "temp/")
53 {
54 if (_bSetPath)
55 {
56 log_damage0("只能设置一次,在调用Start之前!");
57 return;
58 }
59 _bSetPath = true;
60 _mapPathImage[""] = path_image;
61 _strPathModel = path_model;
62 _strPathBinary = path_binary;
63 _strPathTemp = path_temp;
64 }
65
69 {
70 _bUseBinary = true;
71 }
72
77 void RegisterImagePath(std::string_view path, std::string_view name)
78 {
79 if (_bSetPath)
80 {
81 log_err("请在调用SetPath之前添加图像路径:{},{}", path, name);
82 return;
83 }
84 auto iter = _mapPathImage.find(name);
85 if (iter != _mapPathImage.end())
86 {
87 log_err("重复添加图像路径:{},{}", path, name);
88 return;
89 }
90 _mapPathImage[std::string{ name }] = path;
91 }
92
97 void LoadModel(std::string_view path_name, std::string_view name = "");
98
102 std::string GetPathImage()
103 {
104 return _mapPathImage[""];
105 }
106
109 std::string GetPathVoice()
110 {
111 return _strPathVoice;
112 }
113
117 std::string GetPathBinary()
118 {
119 return _strPathBinary;
120 }
121
124 std::string GetPathBinary3D()
125 {
126 return _strPathBinary + std::string{ STR_PATH_3D };
127 }
128
131 std::string GetPathBinary2D()
132 {
133 return _strPathBinary + std::string{ STR_PATH_2D };
134 }
135
138 std::string GetPathTemp()
139 {
140 return _strPathTemp;
141 }
142
146 std::string GetPathNameTemp(std::string_view str_fmt = "")
147 {
148 std::string ret = str_fmt.empty() ?
149 fmt::format("{}{}", _strPathTemp, _idTemp) :
150 fmt::format("{}{}.{}", _strPathTemp, _idTemp, str_fmt);
151 ++_idTemp;
152 return ret;
153 }
154
160 gcActor CreateActor(std::string_view name);
161
167 //-----------------------------Sprite------------------------------
171 [[nodiscard]]
173
186 std::string_view name,
187 std::size_t index = 0);
192 gcSprite CreateSpriteRandom(std::string_view name)
193 {
194 // TODO
195 return CreateSprite(name, Random::SizeT(0, GetSpriteSheetNum(name) - 1));
196 }
197
204 bool RegisterSprite(std::string_view name, Image* img, const Position& anchor = {});
208 bool HasSprite(std::string_view name);
215 std::vector<gcSprite> CreateSpriteMulti(
216 std::string_view name,
217 std::size_t num = 0,
218 std::size_t index = 0);
219
225 {
226 return CloneSprite(const_cast<gcSprite&>(spr));
227 }
228
231 std::vector<std::string> GetFolderAllSpriteName(std::string_view folder_name);
235 size_t GetSpriteSheetNum(std::string_view name);
239 SpriteID GetSpriteId(std::string_view name, size_t index);
248 std::string GetSpriteName(const SpriteID& spr_id);
249 std::string GetSpriteName(gcSprite& spr) { return GetSpriteName(spr->GetID()); }
260
267 gcAnimation CreateAnimation(std::string_view name, std::string_view state, unsigned dir = 0);
269
270 //------------------------Render--------------------------
271 gcSprite RenderSprite(std::string_view name, size_t index = 0);
273 gcAnimation RenderAnimation(std::string_view name, std::string_view state, unsigned dir = 0);
274
275 //------------------------Font--------------------------
281 size_t LoadFont(std::string_view path_name);
285 size_t GetFontSize();
291
301 float CreateSpriteChar(const CharInfo& info, gcSprite& spr, gcSprite& spr_outline);
302
303 float CreateSpriteChar(const CharInfo& info, gcSprite& spr)
304 {
305 gcSprite spr_outline;
306 return CreateSpriteChar(info, spr, spr_outline);
307 }
308
321 size_t font_id = 0, unsigned font_size = 16, const Color& col = ColorDef::WHITE,
322 unsigned font_outline = 0, const Color& col_outline = ColorDef::BLACK,
323 unsigned font_shadow = 0, const Color& col_shadow = ColorDef::BLACK);
324
326 {
327 gcText ret = CreateText(0, 16, ColorDef::WHITE);
328 ret->SetString("Default");
329 return ret;
330 }
331
334 gcText CloneText(gcText txt, bool copy_str = true)
335 {
336 gcText ret = CreateText(
337 txt->_fontId, txt->_fontSize[0], txt->_col[0],
338 txt->_fontSize[1], txt->_col[1],
339 txt->_fontSize[2], txt->_col[2]);
340 ret->GetCoor()->SetParent(txt->GetCoor()->GetParent());
341 ret->_layout = txt->_layout;
342 ret->_order = txt->_order;
343 ret->_bFormat = txt->_bFormat;
344 ret->SetBackground(txt.GetPointer());
345 if (copy_str)
346 ret->SetString(txt->GetString());
347 return ret;
348 }
349
354 Coor* GetRootCoor(size_t i);
355 //----------------------Color-----------------------------
363 void AddColor(std::string_view name, const Color& col);
364
372 Color GetColor(std::string_view name);
373
377 std::optional<Color> GetColorCheck(std::string_view name);
378
383 void LoadColorCsv(std::string_view name);
384
385
386 //-----------------------Other----------------------------
390 void SetOrderPickup(float order);
391 float GetOrderPickup() { return _orderPickup; }
392
393 void SetOrderPickupShow(bool show) { _bOrderPickupShow = show; }
394 bool GetOrderPickupShow() { return _bOrderPickupShow; }
395
399 void DeleteBinary(bool b_all, bool b_2d, bool b_3d);
404 FactoryImp* GetImp() { return _imp; }
405
407
409 {
410 _vecPickupSprite.push_back(spr);
411 }
413 {
414 _vecPickupText.push_back(txt);
415 }
417 {
418 SetOrderPickup(1.0f);
419 _vecPickupSprite.clear();
420 _vecPickupText.clear();
421 }
423private:
424 Factory();
425 ~Factory();
426
427 FactoryImp* _imp;
428
429 // 图像路径 name -> path
430 std::unordered_map<std::string, std::string, string_hash, std::equal_to<>> _mapPathImage;
431 std::string _strPathModel; //模型 根目录
432 std::string _strPathVoice; //音频 根目录
433 std::string _strPathBinary; //输出 根目录
434 std::string _strPathTemp; //临时 根目录
435 bool _bSetPath;
436 bool _bUseBinary; // 是否使用输出目录
437 bool _bOrderPickupShow; // 拾取检测调试绘图
438 std::size_t _idTemp; //临时文件使用id
439
440 float _orderPickup; // ui精灵拾取order
441
442 //颜色tag
443 std::unordered_map<std::string, Color> _hashTagColor;
444
445 // 拾取检测
446 std::vector<Sprite*> _vecPickupSprite;
447 std::vector<Text*> _vecPickupText;
448
449 // 动画信息
450 std::unordered_map<std::string, AnimationInfo, string_hash, std::equal_to<>> _mapAnimationInfo;
451
452 // Render接口绘制的资源(保留一帧)
453 std::vector<gcSprite> _vecSprite;
454 std::vector<gcText> _vecText;
455 std::vector<gcAnimation> _vecAnimation;
456
457 void _auto_render();
458 void _clear_render();
459
460 //init之前会调用一次
461 void _init_path();
462 void _init_defualt_res();//初始化默认资源
463 //init之后,程序结束均调用一次
464 void _save_cache();
465
466 // 加载精灵
467 void _load_sprite();
468 void _load_animation();
469 // 从缓存加载
470 void _load_sprite_binary();
471 // 加载子目录
472 void _load_sprite_sub(std::string_view path, std::string_view name, std::string_view path_out);
473
474 // 精灵加载方法
475 // 1.SetPath 设置默认的精灵图像路径
476 // 2.RegisterImagePath 设置额外的精灵图像路径
477 // 3.进入Start游戏循环后,遍历所有目录,生成到binary/2d,每一个目录单独一个文件夹
478 // 4.每一份都有 files.json、sprite.json 两个文件
479 // 5.files.json记录文件改动,若文件变化,则重新生成此目录到binary
480 // 6.sprite.json记录此目录的所有精灵
481 // 7.最后合并所有精灵到_sprGroup
482 // 8.发布版本只需要执行第1、2、7步
483
484};
485
486extern Factory* g_factory;
487
488}
2d坐标系
Coor * GetParent() const
void SetParent(Coor *coor)
资源工厂,加载、管理、创建各种资源
gcText CloneText(gcText txt, bool copy_str=true)
复制一个文本
SpriteID GetSpriteId(std::string_view name, size_t index)
返回精灵的id
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)
创建一个字符精灵
Color GetSpriteColor(gcSprite &spr)
gcSprite CreateSprite(const SpriteID &spr_id)
创建一个精灵
gcAnimation RenderAnimation(std::string_view name, std::string_view state, unsigned dir=0)
gcText RenderText()
Color GetSpriteColor(const SpriteID &spr_id)
返回精灵的默认颜色
std::string GetPathBinary2D()
返回2d资源缓存目录,例如"binary/2d/"
void SetOrderPickupShow(bool show)
size_t LoadFont(std::string_view path_name)
加载字体
void _render_pickup()
friend class Engine
Position GetSpriteAnchor(gcSprite &spr)
gcSprite CloneSprite(gcSprite &spr)
复制一个精灵
void SetOrderPickup(float order)
设置拾取ui的当前order
float GetOrderPickup()
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/")
设置 各种路径
void RegisterImagePath(std::string_view path, std::string_view name)
注册图像加载路径
Coor * GetRootCoor(size_t i)
返回根坐标系
void LoadModel(std::string_view path_name, std::string_view name="")
加载模型文件
gcSprite RenderSprite(std::string_view name, size_t index=0)
void AddColor(std::string_view name, const Color &col)
添加一个颜色
void _add_pickup_sprite(Sprite *spr)
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)
创建一个文本
size_t GetSpriteSheetNum(std::string_view name)
返回精灵的sheet有多少张
Camera * GetCamera()
返回相机(3d)
gcActor CreateActor(std::string_view name)
创建一个演员,通过指定模型文件
bool HasSprite(std::string_view name)
检测精灵是否存在
std::string GetPathImage()
返回图像目录
std::string GetSpriteName(const SpriteID &spr_id)
返回精灵的名字
std::vector< std::string > GetFolderAllSpriteName(std::string_view folder_name)
返回一个文件夹下的所有精灵
std::string GetPathVoice()
返回音效目录
gcAnimation CloneAnimation(gcAnimation ani)
Position GetSpriteAnchor(const SpriteID &spr_id)
返回精灵的默认锚点
gcAnimation CreateAnimation(std::string_view name, std::string_view state, unsigned dir=0)
创建精灵动画
CharImage * GetChar(const CharInfo &info)
获得字符图像
void _render_sprite(Sprite *spr)
gcSprite CloneSprite(const gcSprite &spr)
std::string GetPathTemp()
返回临时文件路径,例如"temp/"
bool GetOrderPickupShow()
Size GetSpriteTexSize(const SpriteID &spr_id)
返回精灵的纹理大小
gcText CreateTextDefault()
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)
创建多个精灵,从已加载的精灵表复制
std::string GetSpriteName(gcSprite &spr)
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
void _add_pickup_text(Text *txt)
bool RegisterSprite(std::string_view name, Image *img, const Position &anchor={})
注册一个精灵(动态)
gcSprite CreateSpriteRandom(std::string_view name)
创建一个精灵,随机选中下标
Color GetColor(std::string_view name)
返回一个颜色
T * GetPointer()
直接获取指针,注意生命周期
static size_t SizeT(size_t min, size_t max)
返回[min,max]区间的 size_t型整数
2d绘图精灵
SpriteID GetID() const
2d文本绘制对象
std::string GetString()
bool SetString(std::string_view str)
设置内容
Coor * GetCoor()
返回坐标系
void SetBackground(bool open, const Color &col=ColorDef::TEXT_BG)
设置背景精灵
3d绘图元素,翻译为角色
2d帧动画
帧动画配置
文件系统
字体解析结构
日志系统
#define log_err(...)
2d绘图元素
文本绘制
通用类型
constexpr Color WHITE
constexpr Color BLACK
Position2 Position
Factory * g_factory
constexpr std::string_view STR_PATH_2D
GC< Actor > gcActor
constexpr Size SIZE_SPRITE_DEFAULT
GC< Sprite > gcSprite
GC< Animation > gcAnimation
GC< Text > gcText
Array< float, 4 > Color
Vector2< uint32_t, unsigned > Image
PointU Size
void log_damage0(std::string_view str, const std::source_location location=std::source_location::current())
constexpr std::string_view STR_PATH_3D
字符图像数据
字符信息