Files
dl_old/dl/render/dl_factory.h
T
2023-12-28 11:25:26 +08:00

340 lines
8.5 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* @file dl_factory.h
* @brief 资源工厂
*
*
* @version 1.1
* @author lveyou
* @date 21-08-06
*
* @note 精灵是不具名的,存在于精灵表的序列之中。而精灵表具名。
* @note 默认精灵与默认演员的隐含名字为“:”。
*/
#pragma once
#include <unordered_map>
#include <optional>
#include "dl_type.h"
#include "io/dl_log.h"
#include "io/dl_file.h"
#include "render/dl_sprite.h"
#include "render/dl_actor.h"
#include "render/dl_font.h"
#include "render/dl_text.h"
#include "base/dl_color.h"
namespace dl
{
constexpr Size SIZE_SPRITE_DEFAULT = { 32, 32 };
constexpr std::string_view STR_PATH_2D = "2d/";
constexpr std::string_view STR_PATH_3D = "3d/";
class FactoryImp;
class Camera;
/**
* @brief 资源工厂
* @info 加载、管理、创建各种资源
*/
class Factory
{
friend class System;
friend class Engine;
public:
/**
* @brief 设置 各种路径
* @note 请在System::Start之前设置。如果不调用,会使用默认值
*/
void SetPath(
std::string_view path_image,
std::string_view path_model,
std::string_view path_binary,
std::string_view path_temp)
{
if (_bSetPath)
{
log_damage0("只能设置一次,在调用Start之前!");
return;
}
_bSetPath = true;
_strPathImage = path_image;
_strPathModel = path_model;
_strPathBinary = path_binary;
_strPathTemp = path_temp;
}
/**
* @brief 加载模型文件
* @param[in] path_name 绝对路径 或 资源路径
* @param[in] name 自定义名称
*/
void LoadModel(std::string_view path_name, std::string_view name = "");
/**
* @brief 返回音效目录
*/
std::string GetPathVoice()
{
return _strPathVoice;
}
/**
* @brief 返回资源缓存目录,例如"binary/"
*/
std::string GetPathBinary()
{
return _strPathBinary;
}
/**
* @brief 返回3d资源缓存目录,例如"binary/3d/"
*/
std::string GetPathBinary3D()
{
return _strPathBinary + std::string{ STR_PATH_3D };
}
/**
* @brief 返回2d资源缓存目录,例如"binary/2d/"
*/
std::string GetPathBinary2D()
{
return _strPathBinary + std::string{ STR_PATH_2D };
}
/**
* @brief 返回临时文件路径,例如"temp/"
*/
std::string GetPathTemp()
{
return _strPathTemp;
}
/**
* @brief 返回临时文件名,例如"temp/1"
* @param[in] str_fmt 存在时添加后缀(额外添加.)
*/
std::string GetPathNameTemp(std::string_view str_fmt = "")
{
std::string ret = str_fmt.empty() ?
fmt::format("{}{}", _strPathTemp, _idTemp) :
fmt::format("{}{}.{}", _strPathTemp, _idTemp, str_fmt);
++_idTemp;
return ret;
}
/**
* @brief 创建一个演员,通过指定模型文件
* @param[in] name 名字
* @retval 失败返回默认演员,错误写入日志。
*/
gcActor CreateActor(std::string_view name);
/**
* @brief 返回相机(3d)
* @note 未设置前就有一个默认的
*/
Camera* GetCamera();
//-----------------------------Sprite------------------------------
/**
* @brief 创建默认精灵
*/
gcSprite CreateSpriteDefault(const Color& col = ColorDef::WHITE);
/**
* @brief 创建一个精灵
* @param[in] spr_id 精灵id
*/
gcSprite CreateSprite(const SpriteID& spr_id);
/**
* @brief 创建一个精灵
* @param[in] name 名字,例如“ui.btn0”
* @param[in] index 下标
* @retval 失败返回默认精灵,错误产生日志。
*/
gcSprite CreateSprite(
std::string_view name,
std::size_t index = 0);
/**
* @brief 注册一个精灵(动态)
* @param[in] name 命名
* @param[in] img 图像
* @note 注意,此操作是非常慢的
*/
bool RegisterSprite(std::string_view name, Image* img);
/**
* @brief 检测精灵是否存在
*/
bool HasSprite(std::string_view name);
/**
* @brief 创建多个精灵,从已加载的精灵表复制
* @param[in] name 名字,例如“ui.btn0”
* @param[in] num 数量,为0时表示全部
* @param[in] index 下标起始
*/
std::vector<gcSprite> CreateSpriteMulti(
std::string_view name,
std::size_t num = 0,
std::size_t index = 0);
/**
* @brief 复制一个精灵
*/
gcSprite CloneSprite(gcSprite& spr);
gcSprite CloneSprite(const gcSprite& spr)
{
return CloneSprite(const_cast<gcSprite&>(spr));
}
/**
* @brief 返回精灵的纹理大小
*/
Size GetSpriteTexSize(gcSprite& spr);
//------------------------Font--------------------------
/**
* @brief 加载字体
* @param[in] path_name 路径名称
* @note 0号为默认字体已被占用
*/
size_t LoadFont(std::string_view path_name);
/**
* @brief 返回加载的字体数量
*/
size_t GetFontSize();
/**
* @brief 获得字符图像
* @param[in] info 字符信息
*/
CharImage* GetChar(const CharInfo& info);
/**
* @brief 创建一个字符精灵
* @param[in] info 字符信息
* @param[out] spr 返回的精灵
* @param[out] spr_outline 返回的精灵(描边),如果描边大小为0,则不会返回
* @retval 横向偏移距离
*
* @note 在横向排列文字时,使用返回值进行偏移
*/
float CreateSpriteChar(const CharInfo& info, gcSprite& spr, gcSprite& spr_outline);
float CreateSpriteChar(const CharInfo& info, gcSprite& spr)
{
gcSprite spr_outline;
return CreateSpriteChar(info, spr, spr_outline);
}
/**
* @brief 创建一个文本
* @param[in] font_id 字体编号
* @param[in] font_size 字体大小(单位为像素,英文字母大约为一半大小)
* @param[in] col 颜色
* @param[in] font_outline 描边大小
* @param[in] col_outline 描边颜色
* @param[in] font_shadow 阴影偏移
* @param[in] col_shadow 阴影颜色
* @retval 失败返回nullptr
*/
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);
gcText CreateTextDefault()
{
gcText ret = CreateText(0, 16, ColorDef::WHITE);
ret->SetString("Default");
return ret;
}
/**
* @brief 复制一个文本
*/
gcText CloneText(gcText txt, bool copy_str = false)
{
gcText ret = CreateText(
txt->_fontId, txt->_fontSize[0], txt->_col[0],
txt->_fontSize[1], txt->_col[1],
txt->_fontSize[2], txt->_col[2]);
ret->GetCoor()->SetParent(txt->GetCoor()->GetParent());
ret->_layout = txt->_layout;
ret->_order = txt->_order;
ret->_bFormat = txt->_bFormat;
ret->SetBackground(txt.GetPointer());
if (copy_str)
ret->SetString(txt->GetString());
return ret;
}
//----------------------Color-----------------------------
/**
* @brief 添加一个颜色
* @param[in] name 名字
* @param[in] col 颜色值
*
* @note 例如 (bg, 0xff123456),在Text格式化中就可以使用标签{c_bg}修改颜色
*/
void AddColor(std::string_view name, const Color& col);
/**
* @brief 返回一个颜色
* @param[in] name 通过AddColor添加的颜色,如果是开头为0x,则返回对应十六进制颜色
* @retval 失败返回白色,并发出警告
*
* @note 如果传入16进制颜色,必须是完整的,比如0x00ff00ff
*/
Color GetColor(std::string_view name);
/**
* @brief 返回一个颜色,会检查
*/
std::optional<Color> GetColorCheck(std::string_view name);
/**
* @brief 从文件加载颜色标签
* @note 序列化的csv文件
*/
void LoadColorCsv(std::string_view name);
//-----------------------Other----------------------------
/**
* @brief 删除输出目录内容
*/
void DeleteBinary(bool b_all, bool b_2d, bool b_3d);
/**
* @brief 导出所有2d纹理
*/
void ExportTexture2D();
FactoryImp* GetImp() { return _imp; }
void _render_sprite(Sprite* spr);
private:
Factory();
~Factory();
FactoryImp* _imp;
std::string _strPathImage; //图像 根目录
std::string _strPathModel; //模型 根目录
std::string _strPathVoice; //音频 根目录
std::string _strPathBinary; //输出 根目录
std::string _strPathTemp; //临时 根目录
bool _bSetPath;
std::size_t _idTemp;//临时文件使用id
//颜色tag
std::unordered_map<std::string, Color> _hashTagColor;
//init之前会调用一次
void _init_path();
void _init_defualt_res();//初始化默认资源
//init之后,程序结束均调用一次
void _save_cache();
//加载精灵(根据_strPathImage和_strPathBinary)
//_init_path里调用
void _load_sprite();
};
extern Factory* g_factory;
}