dl
dl_factory.h
浏览该文件的文档.
1
13#pragma once
14
15#ifndef DL_DISABLE_RENDER
16#include <unordered_map>
17#include <optional>
18
19#include "dl_type.h"
20#include "base/dl_color.h"
21#include "io/dl_log.h"
22#include "io/dl_file.h"
23#include "render/dl_sprite.h"
24#include "render/dl_actor.h"
25#include "render/dl_font.h"
26#include "render/dl_text.h"
28#include "render/dl_animation.h"
29
30namespace dl
31{
32
33constexpr Size SIZE_SPRITE_DEFAULT = { 32, 32 };
34constexpr std::string_view STR_PATH_2D = "2d/";
35constexpr std::string_view STR_PATH_3D = "3d/";
36
37class FactoryImp;
38class Camera;
40class Factory
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 _mapPathImage[""] = path_image;
62 _strPathModel = path_model;
63 _strPathBinary = path_binary;
64 _strPathTemp = path_temp;
65 }
66
69 void SetSpriteSampler(Sprite::Sampler sampler_normal, Sprite::Sampler sampler_ui)
70 {
71 _samplerNormal = sampler_normal;
72 _samplerUI = sampler_ui;
73 }
74
78 {
79 return is_ui ? _samplerUI : _samplerNormal;
80 }
81
85 {
86 _bUseBinary = true;
87 }
88
93 void RegisterImagePath(std::string_view path, std::string_view name)
94 {
95 if (_bSetPath)
96 {
97 log_err("请在调用SetPath之前添加图像路径:{},{}", path, name);
98 return;
99 }
100 auto iter = _mapPathImage.find(name);
101 if (iter != _mapPathImage.end())
102 {
103 log_err("重复添加图像路径:{},{}", path, name);
104 return;
105 }
106 _mapPathImage[std::string{ name }] = path;
107 }
108
113 void LoadModel(std::string_view path_name, std::string_view name = "");
114
115 std::vector<std::string> GetAllModel();
116
120 std::string GetPathImage()
121 {
122 return _mapPathImage[""];
123 }
124
127 std::string GetPathVoice()
128 {
129 return _strPathVoice;
130 }
131
134 std::string GetPathModel()
135 {
136 return _strPathModel;
137 }
138
141 std::string GetPathBinary()
142 {
143 return _strPathBinary;
144 }
145
148 std::string GetPathBinary3D()
149 {
150 return _strPathBinary + std::string{ STR_PATH_3D };
151 }
152
155 std::string GetPathBinary2D()
156 {
157 return _strPathBinary + std::string{ STR_PATH_2D };
158 }
159
162 std::string GetPathTemp()
163 {
164 return _strPathTemp;
165 }
166
170 std::string GetPathNameTemp(std::string_view str_fmt = "")
171 {
172 std::string ret = str_fmt.empty() ?
173 fmt::format("{}{}", _strPathTemp, _idTemp) :
174 fmt::format("{}{}.{}", _strPathTemp, _idTemp, str_fmt);
175 ++_idTemp;
176 return ret;
177 }
178
184 gcActor CreateActor(std::string_view name);
185
187 {
188 return CreateActor(":box");
189 }
190
195
201 size_t LoadSkybox(std::string_view path);
206 void SetSkybox(size_t id);
207
211
213 //-----------------------------Sprite------------------------------
217 [[nodiscard]]
219
232 std::string_view name,
233 std::size_t index = 0);
238 gcSprite CreateSprite(size_t rt_id);
243 gcSprite CreateSpriteRandom(std::string_view name)
244 {
245 // TODO:可优化性能
246 return CreateSprite(name, Random::SizeT(0, GetSpriteSheetNum(name) - 1));
247 }
248
256 bool RegisterSprite(std::string_view name, size_t index, Image* img, const Position& anchor = {});
265 bool UpdateSprite(std::string_view name, size_t index, Image* img, const Position& anchor = {});
269 bool HasSprite(std::string_view name);
276 std::vector<gcSprite> CreateSpriteMulti(
277 std::string_view name,
278 std::size_t num = 0,
279 std::size_t index = 0);
280
286 {
287 return CloneSprite(const_cast<gcSprite&>(spr));
288 }
289
292 std::vector<std::string> GetFolderAllSpriteName(std::string_view folder_name);
296 size_t GetSpriteSheetNum(std::string_view name);
301 SpriteID GetSpriteId(std::string_view name, size_t index);
306 bool GetSpriteId(std::string_view name, size_t index, SpriteID& id);
320 std::string GetSpriteName(const SpriteID& spr_id);
321 std::string GetSpriteName(gcSprite& spr) { return GetSpriteName(spr->GetID()); }
325 std::string GetSpriteNameIndex(const SpriteID& spr_id);
326 std::string GetSpriteNameIndex(gcSprite& spr) { return GetSpriteNameIndex(spr->GetID()); }
337
344 gcAnimation CreateAnimation(std::string_view name, std::string_view state, unsigned dir = 0);
346
347 //------------------------Render--------------------------
352 gcSprite RenderSprite(std::string_view name, size_t index = 0);
359 gcAnimation RenderAnimation(std::string_view name, std::string_view state, unsigned dir = 0);
360
361 //------------------------Font--------------------------
367 size_t LoadFont(std::string_view path_name);
371 size_t GetFontSize();
377
387 float CreateSpriteChar(const CharInfo& info, gcSprite& spr, gcSprite& spr_outline);
388
389 float CreateSpriteChar(const CharInfo& info, gcSprite& spr)
390 {
391 gcSprite spr_outline;
392 return CreateSpriteChar(info, spr, spr_outline);
393 }
394
407 size_t font_id = 0, unsigned font_size = 16, const Color& col = ColorDef::WHITE,
408 unsigned font_outline = 0, const Color& col_outline = ColorDef::BLACK,
409 unsigned font_shadow = 0, const Color& col_shadow = ColorDef::BLACK);
410
412 {
413 gcText ret = CreateText(0, 16, ColorDef::WHITE);
414 ret->SetString("Default");
415 return ret;
416 }
417
420 gcText CloneText(gcText txt, bool copy_str = true)
421 {
422 gcText ret = CreateText(
423 txt->_fontId, txt->_fontSize[0], txt->_col[0],
424 txt->_fontSize[1], txt->_col[1],
425 txt->_fontSize[2], txt->_col[2]);
426 ret->GetCoor()->SetParent(txt->GetCoor()->GetParent());
427 ret->_layout = txt->_layout;
428 ret->_order = txt->_order;
429 ret->_bFormat = txt->_bFormat;
430 ret->SetBackground(txt.GetPointer());
431 if (copy_str)
432 ret->SetString(txt->GetString());
433 return ret;
434 }
435
440 Coor* GetRootCoor(size_t i);
441 //----------------------Color-----------------------------
449 void AddColor(std::string_view name, const Color& col);
450
458 Color GetColor(std::string_view name);
459
463 std::optional<Color> GetColorCheck(std::string_view name);
464
469 void LoadColorJson(std::string_view path_name);
470
471
472 //-----------------------Other----------------------------
476 void SetOrderPickup(float order);
477 float GetOrderPickup() { return _orderPickup; }
478
479 void SetOrderPickupShow(bool show) { _bOrderPickupShow = show; }
480 bool GetOrderPickupShow() { return _bOrderPickupShow; }
481
485 void DeleteBinary(bool b_all, bool b_2d, bool b_3d);
490
492 bool CompressMir2Res(std::string_view path);
493
494 FactoryImp* GetImp() { return _imp; }
495
497
499 {
500 _vecPickupSprite.push_back(spr);
501 }
503 {
504 _vecPickupText.push_back(txt);
505 }
507 {
508 SetOrderPickup(1.0f);
509 _vecPickupSprite.clear();
510 _vecPickupText.clear();
511 }
513private:
514 Factory();
515 ~Factory();
516
517 FactoryImp* _imp;
518
519 // 图像路径 name -> path
520 std::unordered_map<std::string, std::string, string_hash, std::equal_to<>> _mapPathImage;
521 std::string _strPathModel; //模型 根目录
522 std::string _strPathVoice; //音频 根目录
523 std::string _strPathBinary; //输出 根目录
524 std::string _strPathTemp; //临时 根目录
525 bool _bSetPath;
526 bool _bUseBinary; // 是否使用输出目录
527 bool _bOrderPickupShow; // 拾取检测调试绘图
528 std::size_t _idTemp; //临时文件使用id
529
530 float _orderPickup; // ui精灵拾取order
531 Sprite::Sampler _samplerNormal; // 精灵默认的采样方法
532 Sprite::Sampler _samplerUI; // ui精灵默认的采用方法
533
534
535 //颜色tag
536 std::unordered_map<std::string, Color> _hashTagColor;
537
538 // 拾取检测
539 std::vector<Sprite*> _vecPickupSprite;
540 std::vector<Text*> _vecPickupText;
541
542 // 加载图像规则
543 File::SearchRule _ruleCheckImage;
544 File::SearchRule _ruleLoadImage;
545
546 // 动画信息
547 std::unordered_map<std::string, AnimationInfo, string_hash, std::equal_to<>> _mapAnimationInfo;
548
549 // Render接口绘制的资源(保留一帧)
550 std::vector<gcSprite> _vecSprite;
551 std::vector<gcText> _vecText;
552 std::vector<gcAnimation> _vecAnimation;
553 // 默认RenderTarget精灵
554 gcSprite _sprRenderTarget0;
555
556 void _auto_render();
557 void _clear_render();
558
559 void _render_rt();
560 void _resize();
561
562 //init之前会调用一次
563 void _init_path();
564 void _init_defualt_res();//初始化默认资源
565 //init之后,程序结束均调用一次
566 void _save_cache();
567
568 // 加载精灵
569 void _load_sprite();
570 void _load_animation();
571 // 从缓存加载
572 void _load_sprite_binary();
573 // 加载子目录
574 void _load_sprite_sub(std::string_view path, std::string_view name, std::string_view path_out);
575
576 // 精灵加载方法
577 // 1.SetPath 设置默认的精灵图像路径
578 // 2.RegisterImagePath 设置额外的精灵图像路径
579 // 3.进入Start游戏循环后,遍历所有目录,生成到binary/2d,每一个目录单独一个文件夹
580 // 4.每一份都有 files.json、sprite.json 两个文件
581 // 5.files.json记录文件改动,若文件变化,则重新生成此目录到binary
582 // 6.sprite.json记录此目录的所有精灵
583 // 7.最后合并所有精灵到_sprGroup
584 // 8.发布版本只需要执行第1、2、7步
585
586};
587
588extern Factory* g_factory;
589
590}
591#endif
2d坐标系
Coor * GetParent() const
void SetParent(Coor *coor)
资源工厂,加载、管理、创建各种资源
bool RegisterSprite(std::string_view name, size_t index, Image *img, const Position &anchor={})
注册一个精灵(动态)
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 GetSpriteNameIndex(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()
创建临时文本,并绘制
void LoadColorJson(std::string_view path_name)
从json文件加载颜色标签
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)
创建一个文本
Quad GetSpriteQuad(const SpriteID &spr_id)
返回精灵的默认quad
gcActor CreateActorDefault()
std::string GetSpriteNameIndex(const SpriteID &spr_id)
返回精灵的名字和下标,例如 btn,0
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)
返回精灵的默认锚点
void SetSpriteSampler(Sprite::Sampler sampler_normal, Sprite::Sampler sampler_ui)
设置精灵的默认采样模式
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/"
size_t LoadSkybox(std::string_view path)
加载天空盒
void UpdateDebugView()
bool GetOrderPickupShow()
Size GetSpriteTexSize(const SpriteID &spr_id)
返回精灵的纹理大小
gcText CreateTextDefault()
std::vector< std::string > GetAllModel()
bool UpdateSprite(std::string_view name, size_t index, Image *img, const Position &anchor={})
更新一个精灵
bool CompressMir2Res(std::string_view path)
打包传奇补丁包(TODO 有问题不要使用)
DebugView3D GetDebugView3D()
void SetReleaseMode()
设置发布模式,使用binary资源
void DeleteBinary(bool b_all, bool b_2d, bool b_3d)
删除输出目录内容
gcSprite RenderSpriteDefault()
void SetDebugView3D(DebugView3D type)
设置调试绘图(3d)
void SetSkybox(size_t id)
设置天空盒
Quad GetSpriteQuad(gcSprite &spr)
std::vector< gcSprite > CreateSpriteMulti(std::string_view name, std::size_t num=0, std::size_t index=0)
创建多个精灵,从已加载的精灵表复制
std::string GetSpriteName(gcSprite &spr)
Sprite::Sampler GetSpriteSampler(bool is_ui)
返回精灵的默认采样模式
gcSprite CreateSpriteDefault(const Color &col=ColorDef::WHITE)
创建默认精灵
bool GetSpriteId(std::string_view name, size_t index, SpriteID &id)
返回精灵的id
std::optional< Color > GetColorCheck(std::string_view name)
返回一个颜色,会检查
Size GetSpriteTexSize(gcSprite &spr)
void ExportTexture2D()
导出所有2d纹理
friend class System
void _add_pickup_text(Text *txt)
gcSprite CreateSpriteRandom(std::string_view name)
创建一个精灵,随机选中下标
gcSprite CreateSprite(size_t rt_id)
创建一个精灵
std::string GetPathModel()
返回模型目录
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
Array< Position, 4 > Quad
DebugView3D
调试绘图3d
PointU Size
void log_damage0(std::string_view str, DL_SOURCE_LOCATION)
constexpr std::string_view STR_PATH_3D
字符图像数据
字符信息
过滤规则,不兼容大小写