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
70 {
71 _bUseBinary = true;
72 }
73
78 void RegisterImagePath(std::string_view path, std::string_view name)
79 {
80 if (_bSetPath)
81 {
82 log_err("请在调用SetPath之前添加图像路径:{},{}", path, name);
83 return;
84 }
85 auto iter = _mapPathImage.find(name);
86 if (iter != _mapPathImage.end())
87 {
88 log_err("重复添加图像路径:{},{}", path, name);
89 return;
90 }
91 _mapPathImage[std::string{ name }] = path;
92 }
93
98 void LoadModel(std::string_view path_name, std::string_view name = "");
99
103 std::string GetPathImage()
104 {
105 return _mapPathImage[""];
106 }
107
110 std::string GetPathVoice()
111 {
112 return _strPathVoice;
113 }
114
118 std::string GetPathBinary()
119 {
120 return _strPathBinary;
121 }
122
125 std::string GetPathBinary3D()
126 {
127 return _strPathBinary + std::string{ STR_PATH_3D };
128 }
129
132 std::string GetPathBinary2D()
133 {
134 return _strPathBinary + std::string{ STR_PATH_2D };
135 }
136
139 std::string GetPathTemp()
140 {
141 return _strPathTemp;
142 }
143
147 std::string GetPathNameTemp(std::string_view str_fmt = "")
148 {
149 std::string ret = str_fmt.empty() ?
150 fmt::format("{}{}", _strPathTemp, _idTemp) :
151 fmt::format("{}{}.{}", _strPathTemp, _idTemp, str_fmt);
152 ++_idTemp;
153 return ret;
154 }
155
161 gcActor CreateActor(std::string_view name);
162
168 //-----------------------------Sprite------------------------------
172 [[nodiscard]]
174
187 std::string_view name,
188 std::size_t index = 0);
193 gcSprite CreateSpriteRandom(std::string_view name)
194 {
195 // TODO:可优化性能
196 return CreateSprite(name, Random::SizeT(0, GetSpriteSheetNum(name) - 1));
197 }
198
206 bool RegisterSprite(std::string_view name, size_t index, Image* img, const Position& anchor = {});
210 bool HasSprite(std::string_view name);
217 std::vector<gcSprite> CreateSpriteMulti(
218 std::string_view name,
219 std::size_t num = 0,
220 std::size_t index = 0);
221
227 {
228 return CloneSprite(const_cast<gcSprite&>(spr));
229 }
230
233 std::vector<std::string> GetFolderAllSpriteName(std::string_view folder_name);
237 size_t GetSpriteSheetNum(std::string_view name);
241 SpriteID GetSpriteId(std::string_view name, size_t index);
250 std::string GetSpriteName(const SpriteID& spr_id);
251 std::string GetSpriteName(gcSprite& spr) { return GetSpriteName(spr->GetID()); }
262
269 gcAnimation CreateAnimation(std::string_view name, std::string_view state, unsigned dir = 0);
271
272 //------------------------Render--------------------------
277 gcSprite RenderSprite(std::string_view name, size_t index = 0);
284 gcAnimation RenderAnimation(std::string_view name, std::string_view state, unsigned dir = 0);
285
286 //------------------------Font--------------------------
292 size_t LoadFont(std::string_view path_name);
296 size_t GetFontSize();
302
312 float CreateSpriteChar(const CharInfo& info, gcSprite& spr, gcSprite& spr_outline);
313
314 float CreateSpriteChar(const CharInfo& info, gcSprite& spr)
315 {
316 gcSprite spr_outline;
317 return CreateSpriteChar(info, spr, spr_outline);
318 }
319
332 size_t font_id = 0, unsigned font_size = 16, const Color& col = ColorDef::WHITE,
333 unsigned font_outline = 0, const Color& col_outline = ColorDef::BLACK,
334 unsigned font_shadow = 0, const Color& col_shadow = ColorDef::BLACK);
335
337 {
338 gcText ret = CreateText(0, 16, ColorDef::WHITE);
339 ret->SetString("Default");
340 return ret;
341 }
342
345 gcText CloneText(gcText txt, bool copy_str = true)
346 {
347 gcText ret = CreateText(
348 txt->_fontId, txt->_fontSize[0], txt->_col[0],
349 txt->_fontSize[1], txt->_col[1],
350 txt->_fontSize[2], txt->_col[2]);
351 ret->GetCoor()->SetParent(txt->GetCoor()->GetParent());
352 ret->_layout = txt->_layout;
353 ret->_order = txt->_order;
354 ret->_bFormat = txt->_bFormat;
355 ret->SetBackground(txt.GetPointer());
356 if (copy_str)
357 ret->SetString(txt->GetString());
358 return ret;
359 }
360
365 Coor* GetRootCoor(size_t i);
366 //----------------------Color-----------------------------
374 void AddColor(std::string_view name, const Color& col);
375
383 Color GetColor(std::string_view name);
384
388 std::optional<Color> GetColorCheck(std::string_view name);
389
394 void LoadColorCsv(std::string_view name);
395
396
397 //-----------------------Other----------------------------
401 void SetOrderPickup(float order);
402 float GetOrderPickup() { return _orderPickup; }
403
404 void SetOrderPickupShow(bool show) { _bOrderPickupShow = show; }
405 bool GetOrderPickupShow() { return _bOrderPickupShow; }
406
410 void DeleteBinary(bool b_all, bool b_2d, bool b_3d);
415 FactoryImp* GetImp() { return _imp; }
416
418
420 {
421 _vecPickupSprite.push_back(spr);
422 }
424 {
425 _vecPickupText.push_back(txt);
426 }
428 {
429 SetOrderPickup(1.0f);
430 _vecPickupSprite.clear();
431 _vecPickupText.clear();
432 }
434private:
435 Factory();
436 ~Factory();
437
438 FactoryImp* _imp;
439
440 // 图像路径 name -> path
441 std::unordered_map<std::string, std::string, string_hash, std::equal_to<>> _mapPathImage;
442 std::string _strPathModel; //模型 根目录
443 std::string _strPathVoice; //音频 根目录
444 std::string _strPathBinary; //输出 根目录
445 std::string _strPathTemp; //临时 根目录
446 bool _bSetPath;
447 bool _bUseBinary; // 是否使用输出目录
448 bool _bOrderPickupShow; // 拾取检测调试绘图
449 std::size_t _idTemp; //临时文件使用id
450
451 float _orderPickup; // ui精灵拾取order
452
453 //颜色tag
454 std::unordered_map<std::string, Color> _hashTagColor;
455
456 // 拾取检测
457 std::vector<Sprite*> _vecPickupSprite;
458 std::vector<Text*> _vecPickupText;
459
460 // 动画信息
461 std::unordered_map<std::string, AnimationInfo, string_hash, std::equal_to<>> _mapAnimationInfo;
462
463 // Render接口绘制的资源(保留一帧)
464 std::vector<gcSprite> _vecSprite;
465 std::vector<gcText> _vecText;
466 std::vector<gcAnimation> _vecAnimation;
467
468 void _auto_render();
469 void _clear_render();
470
471 //init之前会调用一次
472 void _init_path();
473 void _init_defualt_res();//初始化默认资源
474 //init之后,程序结束均调用一次
475 void _save_cache();
476
477 // 加载精灵
478 void _load_sprite();
479 void _load_animation();
480 // 从缓存加载
481 void _load_sprite_binary();
482 // 加载子目录
483 void _load_sprite_sub(std::string_view path, std::string_view name, std::string_view path_out);
484
485 // 精灵加载方法
486 // 1.SetPath 设置默认的精灵图像路径
487 // 2.RegisterImagePath 设置额外的精灵图像路径
488 // 3.进入Start游戏循环后,遍历所有目录,生成到binary/2d,每一个目录单独一个文件夹
489 // 4.每一份都有 files.json、sprite.json 两个文件
490 // 5.files.json记录文件改动,若文件变化,则重新生成此目录到binary
491 // 6.sprite.json记录此目录的所有精灵
492 // 7.最后合并所有精灵到_sprGroup
493 // 8.发布版本只需要执行第1、2、7步
494
495};
496
497extern Factory* g_factory;
498
499}
500#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 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)
删除输出目录内容
gcSprite RenderSpriteDefault()
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)
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
字符图像数据
字符信息