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
100 std::vector<std::string> GetAllModel();
101
105 std::string GetPathImage()
106 {
107 return _mapPathImage[""];
108 }
109
112 std::string GetPathVoice()
113 {
114 return _strPathVoice;
115 }
116
119 std::string GetPathModel()
120 {
121 return _strPathModel;
122 }
123
126 std::string GetPathBinary()
127 {
128 return _strPathBinary;
129 }
130
133 std::string GetPathBinary3D()
134 {
135 return _strPathBinary + std::string{ STR_PATH_3D };
136 }
137
140 std::string GetPathBinary2D()
141 {
142 return _strPathBinary + std::string{ STR_PATH_2D };
143 }
144
147 std::string GetPathTemp()
148 {
149 return _strPathTemp;
150 }
151
155 std::string GetPathNameTemp(std::string_view str_fmt = "")
156 {
157 std::string ret = str_fmt.empty() ?
158 fmt::format("{}{}", _strPathTemp, _idTemp) :
159 fmt::format("{}{}.{}", _strPathTemp, _idTemp, str_fmt);
160 ++_idTemp;
161 return ret;
162 }
163
169 gcActor CreateActor(std::string_view name);
170
172 {
173 return CreateActor(":box");
174 }
175
180
186 size_t LoadSkybox(std::string_view path);
191 void SetSkybox(size_t id);
192
196
198 //-----------------------------Sprite------------------------------
202 [[nodiscard]]
204
217 std::string_view name,
218 std::size_t index = 0);
223 gcSprite CreateSpriteRandom(std::string_view name)
224 {
225 // TODO:可优化性能
226 return CreateSprite(name, Random::SizeT(0, GetSpriteSheetNum(name) - 1));
227 }
228
236 bool RegisterSprite(std::string_view name, size_t index, Image* img, const Position& anchor = {});
240 bool HasSprite(std::string_view name);
247 std::vector<gcSprite> CreateSpriteMulti(
248 std::string_view name,
249 std::size_t num = 0,
250 std::size_t index = 0);
251
257 {
258 return CloneSprite(const_cast<gcSprite&>(spr));
259 }
260
263 std::vector<std::string> GetFolderAllSpriteName(std::string_view folder_name);
267 size_t GetSpriteSheetNum(std::string_view name);
271 SpriteID GetSpriteId(std::string_view name, size_t index);
280 std::string GetSpriteName(const SpriteID& spr_id);
281 std::string GetSpriteName(gcSprite& spr) { return GetSpriteName(spr->GetID()); }
292
299 gcAnimation CreateAnimation(std::string_view name, std::string_view state, unsigned dir = 0);
301
302 //------------------------Render--------------------------
307 gcSprite RenderSprite(std::string_view name, size_t index = 0);
314 gcAnimation RenderAnimation(std::string_view name, std::string_view state, unsigned dir = 0);
315
316 //------------------------Font--------------------------
322 size_t LoadFont(std::string_view path_name);
326 size_t GetFontSize();
332
342 float CreateSpriteChar(const CharInfo& info, gcSprite& spr, gcSprite& spr_outline);
343
344 float CreateSpriteChar(const CharInfo& info, gcSprite& spr)
345 {
346 gcSprite spr_outline;
347 return CreateSpriteChar(info, spr, spr_outline);
348 }
349
362 size_t font_id = 0, unsigned font_size = 16, const Color& col = ColorDef::WHITE,
363 unsigned font_outline = 0, const Color& col_outline = ColorDef::BLACK,
364 unsigned font_shadow = 0, const Color& col_shadow = ColorDef::BLACK);
365
367 {
368 gcText ret = CreateText(0, 16, ColorDef::WHITE);
369 ret->SetString("Default");
370 return ret;
371 }
372
375 gcText CloneText(gcText txt, bool copy_str = true)
376 {
377 gcText ret = CreateText(
378 txt->_fontId, txt->_fontSize[0], txt->_col[0],
379 txt->_fontSize[1], txt->_col[1],
380 txt->_fontSize[2], txt->_col[2]);
381 ret->GetCoor()->SetParent(txt->GetCoor()->GetParent());
382 ret->_layout = txt->_layout;
383 ret->_order = txt->_order;
384 ret->_bFormat = txt->_bFormat;
385 ret->SetBackground(txt.GetPointer());
386 if (copy_str)
387 ret->SetString(txt->GetString());
388 return ret;
389 }
390
395 Coor* GetRootCoor(size_t i);
396 //----------------------Color-----------------------------
404 void AddColor(std::string_view name, const Color& col);
405
413 Color GetColor(std::string_view name);
414
418 std::optional<Color> GetColorCheck(std::string_view name);
419
424 void LoadColorCsv(std::string_view name);
425
426
427 //-----------------------Other----------------------------
431 void SetOrderPickup(float order);
432 float GetOrderPickup() { return _orderPickup; }
433
434 void SetOrderPickupShow(bool show) { _bOrderPickupShow = show; }
435 bool GetOrderPickupShow() { return _bOrderPickupShow; }
436
440 void DeleteBinary(bool b_all, bool b_2d, bool b_3d);
445 FactoryImp* GetImp() { return _imp; }
446
448
450 {
451 _vecPickupSprite.push_back(spr);
452 }
454 {
455 _vecPickupText.push_back(txt);
456 }
458 {
459 SetOrderPickup(1.0f);
460 _vecPickupSprite.clear();
461 _vecPickupText.clear();
462 }
464private:
465 Factory();
466 ~Factory();
467
468 FactoryImp* _imp;
469
470 // 图像路径 name -> path
471 std::unordered_map<std::string, std::string, string_hash, std::equal_to<>> _mapPathImage;
472 std::string _strPathModel; //模型 根目录
473 std::string _strPathVoice; //音频 根目录
474 std::string _strPathBinary; //输出 根目录
475 std::string _strPathTemp; //临时 根目录
476 bool _bSetPath;
477 bool _bUseBinary; // 是否使用输出目录
478 bool _bOrderPickupShow; // 拾取检测调试绘图
479 std::size_t _idTemp; //临时文件使用id
480
481 float _orderPickup; // ui精灵拾取order
482
483 //颜色tag
484 std::unordered_map<std::string, Color> _hashTagColor;
485
486 // 拾取检测
487 std::vector<Sprite*> _vecPickupSprite;
488 std::vector<Text*> _vecPickupText;
489
490 // 动画信息
491 std::unordered_map<std::string, AnimationInfo, string_hash, std::equal_to<>> _mapAnimationInfo;
492
493 // Render接口绘制的资源(保留一帧)
494 std::vector<gcSprite> _vecSprite;
495 std::vector<gcText> _vecText;
496 std::vector<gcAnimation> _vecAnimation;
497
498 void _auto_render();
499 void _clear_render();
500
501 //init之前会调用一次
502 void _init_path();
503 void _init_defualt_res();//初始化默认资源
504 //init之后,程序结束均调用一次
505 void _save_cache();
506
507 // 加载精灵
508 void _load_sprite();
509 void _load_animation();
510 // 从缓存加载
511 void _load_sprite_binary();
512 // 加载子目录
513 void _load_sprite_sub(std::string_view path, std::string_view name, std::string_view path_out);
514
515 // 精灵加载方法
516 // 1.SetPath 设置默认的精灵图像路径
517 // 2.RegisterImagePath 设置额外的精灵图像路径
518 // 3.进入Start游戏循环后,遍历所有目录,生成到binary/2d,每一个目录单独一个文件夹
519 // 4.每一份都有 files.json、sprite.json 两个文件
520 // 5.files.json记录文件改动,若文件变化,则重新生成此目录到binary
521 // 6.sprite.json记录此目录的所有精灵
522 // 7.最后合并所有精灵到_sprGroup
523 // 8.发布版本只需要执行第1、2、7步
524
525};
526
527extern Factory* g_factory;
528
529}
530#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)
创建一个文本
gcActor CreateActorDefault()
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/"
size_t LoadSkybox(std::string_view path)
加载天空盒
void UpdateDebugView()
bool GetOrderPickupShow()
Size GetSpriteTexSize(const SpriteID &spr_id)
返回精灵的纹理大小
gcText CreateTextDefault()
std::vector< std::string > GetAllModel()
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)
设置天空盒
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)
创建一个精灵,随机选中下标
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
DebugView3D
调试绘图3d
PointU Size
void log_damage0(std::string_view str, DL_SOURCE_LOCATION)
constexpr std::string_view STR_PATH_3D
字符图像数据
字符信息