推
This commit is contained in:
306
source/Asset/Asset_Audio.hpp
Normal file
306
source/Asset/Asset_Audio.hpp
Normal file
@@ -0,0 +1,306 @@
|
||||
#pragma once
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
#include <fstream>
|
||||
#include <stdexcept>
|
||||
#include "rapidxml/rapidxml.hpp"
|
||||
|
||||
class AudioConfigReader
|
||||
{
|
||||
public:
|
||||
// --------------------------
|
||||
// 1. 结构体定义:放在类内public区,外部可通过 AudioConfigReader::SoundEffect 访问
|
||||
// --------------------------
|
||||
struct SoundEffect
|
||||
{
|
||||
std::string file_path; // 文件路径(FILE属性)
|
||||
int volume_adjust = 100; // 音量调整(默认100)
|
||||
int priority = 0; // 优先级(默认0)
|
||||
};
|
||||
|
||||
struct RandomItem
|
||||
{
|
||||
std::string tag; // 关联的音效ID(TAG属性)
|
||||
int prob = 0; // 概率(PROB属性)
|
||||
};
|
||||
|
||||
struct RandomSoundGroup
|
||||
{
|
||||
std::vector<RandomItem> items; // 组内所有项
|
||||
int delay = 0; // 延迟(DELAY属性)
|
||||
int loop_delay = 0; // 循环延迟
|
||||
int loop_delay_range = 0; // 循环延迟范围
|
||||
};
|
||||
|
||||
struct Music
|
||||
{
|
||||
std::string file_path; // 文件路径(FILE属性)
|
||||
int loop_delay = 0; // 循环延迟(LOOP_DELAY属性)
|
||||
};
|
||||
|
||||
public:
|
||||
// 构造函数:初始化解析器
|
||||
AudioConfigReader() = default;
|
||||
|
||||
// 析构函数:清理资源
|
||||
~AudioConfigReader() = default;
|
||||
|
||||
// 核心函数:解析XML文件(返回是否成功)
|
||||
bool LoadFromFile(const std::string &xml_path)
|
||||
{
|
||||
// 1. 读取XML文件内容
|
||||
std::ifstream file(xml_path, std::ios::binary);
|
||||
if (!file.is_open())
|
||||
{
|
||||
throw std::runtime_error("无法打开XML文件: " + xml_path);
|
||||
}
|
||||
std::string xml_content((std::istreambuf_iterator<char>(file)),
|
||||
std::istreambuf_iterator<char>());
|
||||
file.close();
|
||||
|
||||
// 2. 解析XML
|
||||
rapidxml::xml_document<> doc;
|
||||
try
|
||||
{
|
||||
doc.parse<rapidxml::parse_default>(&xml_content[0]);
|
||||
}
|
||||
catch (const rapidxml::parse_error &e)
|
||||
{
|
||||
throw std::runtime_error("XML解析错误: " + std::string(e.what()) +
|
||||
"(位置: " + std::string(e.where<char>()) + ")");
|
||||
}
|
||||
|
||||
// 3. 获取根节点
|
||||
rapidxml::xml_node<> *root = doc.first_node("AudioTagDatabase");
|
||||
if (!root)
|
||||
{
|
||||
throw std::runtime_error("XML根节点错误: 找不到AudioTagDatabase");
|
||||
}
|
||||
|
||||
// 4. 解析所有节点
|
||||
ParseEffects(root); // 解析<EFFECT>
|
||||
ParseRandomGroups(root); // 解析<RANDOM>
|
||||
ParseMusics(root); // 解析<MUSIC>
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// ------------------------------
|
||||
// 接口1:获取音效配置(<EFFECT>)
|
||||
// ------------------------------
|
||||
bool HasEffect(const std::string &effect_id) const
|
||||
{
|
||||
return m_effects.find(effect_id) != m_effects.end();
|
||||
}
|
||||
|
||||
const SoundEffect &GetEffect(const std::string &effect_id) const
|
||||
{
|
||||
auto it = m_effects.find(effect_id);
|
||||
if (it == m_effects.end())
|
||||
{
|
||||
throw std::out_of_range("音效ID不存在: " + effect_id);
|
||||
}
|
||||
return it->second;
|
||||
}
|
||||
|
||||
// 获取所有音效ID列表
|
||||
std::vector<std::string> GetAllEffectIds() const
|
||||
{
|
||||
return GetAllKeys(m_effects);
|
||||
}
|
||||
|
||||
// ------------------------------
|
||||
// 接口2:获取随机音效组配置(<RANDOM>)
|
||||
// ------------------------------
|
||||
bool HasRandomGroup(const std::string &group_id) const
|
||||
{
|
||||
return m_random_groups.find(group_id) != m_random_groups.end();
|
||||
}
|
||||
|
||||
const RandomSoundGroup &GetRandomGroup(const std::string &group_id) const
|
||||
{
|
||||
auto it = m_random_groups.find(group_id);
|
||||
if (it == m_random_groups.end())
|
||||
{
|
||||
throw std::out_of_range("随机组ID不存在: " + group_id);
|
||||
}
|
||||
return it->second;
|
||||
}
|
||||
|
||||
// 获取所有随机组ID列表
|
||||
std::vector<std::string> GetAllRandomGroupIds() const
|
||||
{
|
||||
return GetAllKeys(m_random_groups);
|
||||
}
|
||||
|
||||
// ------------------------------
|
||||
// 接口3:获取背景音乐配置(<MUSIC>)
|
||||
// ------------------------------
|
||||
bool HasMusic(const std::string &music_id) const
|
||||
{
|
||||
return m_musics.find(music_id) != m_musics.end();
|
||||
}
|
||||
|
||||
const Music &GetMusic(const std::string &music_id) const
|
||||
{
|
||||
auto it = m_musics.find(music_id);
|
||||
if (it == m_musics.end())
|
||||
{
|
||||
throw std::out_of_range("背景音乐ID不存在: " + music_id);
|
||||
}
|
||||
return it->second;
|
||||
}
|
||||
|
||||
// 获取所有背景音乐ID列表
|
||||
std::vector<std::string> GetAllMusicIds() const
|
||||
{
|
||||
return GetAllKeys(m_musics);
|
||||
}
|
||||
|
||||
private:
|
||||
// 辅助函数:解析<EFFECT>节点
|
||||
void ParseEffects(rapidxml::xml_node<> *root)
|
||||
{
|
||||
for (rapidxml::xml_node<> *node = root->first_node("EFFECT");
|
||||
node; node = node->next_sibling("EFFECT"))
|
||||
{
|
||||
|
||||
// 必选属性:ID和FILE
|
||||
rapidxml::xml_attribute<> *id_attr = node->first_attribute("ID");
|
||||
rapidxml::xml_attribute<> *file_attr = node->first_attribute("FILE");
|
||||
if (!id_attr || !file_attr)
|
||||
{
|
||||
continue; // 跳过无效节点
|
||||
}
|
||||
|
||||
SoundEffect effect;
|
||||
effect.file_path = NormalizePath(file_attr->value()); // 统一路径分隔符
|
||||
|
||||
// 可选属性:VOLUME_ADJUST
|
||||
if (auto attr = node->first_attribute("VOLUME_ADJUST"))
|
||||
{
|
||||
effect.volume_adjust = std::stoi(attr->value());
|
||||
}
|
||||
|
||||
// 可选属性:PRIORITY
|
||||
if (auto attr = node->first_attribute("PRIORITY"))
|
||||
{
|
||||
effect.priority = std::stoi(attr->value());
|
||||
}
|
||||
|
||||
m_effects[id_attr->value()] = effect;
|
||||
}
|
||||
}
|
||||
|
||||
// 辅助函数:解析<RANDOM>节点
|
||||
void ParseRandomGroups(rapidxml::xml_node<> *root)
|
||||
{
|
||||
for (rapidxml::xml_node<> *node = root->first_node("RANDOM");
|
||||
node; node = node->next_sibling("RANDOM"))
|
||||
{
|
||||
|
||||
// 必选属性:ID
|
||||
rapidxml::xml_attribute<> *id_attr = node->first_attribute("ID");
|
||||
if (!id_attr)
|
||||
{
|
||||
continue; // 跳过无效节点
|
||||
}
|
||||
|
||||
RandomSoundGroup group;
|
||||
|
||||
// 可选属性:延迟相关
|
||||
if (auto attr = node->first_attribute("DELAY"))
|
||||
{
|
||||
group.delay = std::stoi(attr->value());
|
||||
}
|
||||
if (auto attr = node->first_attribute("LOOP_DELAY"))
|
||||
{
|
||||
group.loop_delay = std::stoi(attr->value());
|
||||
}
|
||||
if (auto attr = node->first_attribute("LOOP_DELAY_RANGE"))
|
||||
{
|
||||
group.loop_delay_range = std::stoi(attr->value());
|
||||
}
|
||||
|
||||
// 解析子节点<ITEM>
|
||||
for (rapidxml::xml_node<> *item_node = node->first_node("ITEM");
|
||||
item_node; item_node = item_node->next_sibling("ITEM"))
|
||||
{
|
||||
|
||||
rapidxml::xml_attribute<> *tag_attr = item_node->first_attribute("TAG");
|
||||
rapidxml::xml_attribute<> *prob_attr = item_node->first_attribute("PROB");
|
||||
if (!tag_attr || !prob_attr)
|
||||
{
|
||||
continue; // 跳过无效项
|
||||
}
|
||||
|
||||
group.items.push_back({tag_attr->value(),
|
||||
std::stoi(prob_attr->value())});
|
||||
}
|
||||
|
||||
m_random_groups[id_attr->value()] = group;
|
||||
}
|
||||
}
|
||||
|
||||
// 辅助函数:解析<MUSIC>节点
|
||||
void ParseMusics(rapidxml::xml_node<> *root)
|
||||
{
|
||||
for (rapidxml::xml_node<> *node = root->first_node("MUSIC");
|
||||
node; node = node->next_sibling("MUSIC"))
|
||||
{
|
||||
|
||||
// 必选属性:ID和FILE
|
||||
rapidxml::xml_attribute<> *id_attr = node->first_attribute("ID");
|
||||
rapidxml::xml_attribute<> *file_attr = node->first_attribute("FILE");
|
||||
if (!id_attr || !file_attr)
|
||||
{
|
||||
continue; // 跳过无效节点
|
||||
}
|
||||
|
||||
Music music;
|
||||
music.file_path = NormalizePath(file_attr->value()); // 统一路径分隔符
|
||||
|
||||
// 可选属性:LOOP_DELAY
|
||||
if (auto attr = node->first_attribute("LOOP_DELAY"))
|
||||
{
|
||||
music.loop_delay = std::stoi(attr->value());
|
||||
}
|
||||
|
||||
m_musics[id_attr->value()] = music;
|
||||
}
|
||||
}
|
||||
|
||||
// 辅助函数:统一路径分隔符(将\转为/,兼容跨平台)
|
||||
std::string NormalizePath(const std::string &path)
|
||||
{
|
||||
std::string normalized = path;
|
||||
for (char &c : normalized)
|
||||
{
|
||||
if (c == '\\')
|
||||
{
|
||||
c = '/';
|
||||
}
|
||||
}
|
||||
return normalized;
|
||||
}
|
||||
|
||||
// 辅助函数:获取哈希表中所有key
|
||||
template <typename Map>
|
||||
std::vector<std::string> GetAllKeys(const Map &map) const
|
||||
{
|
||||
std::vector<std::string> keys;
|
||||
keys.reserve(map.size());
|
||||
for (const auto &pair : map)
|
||||
{
|
||||
keys.push_back(pair.first);
|
||||
}
|
||||
return keys;
|
||||
}
|
||||
|
||||
private:
|
||||
// 存储解析后的数据
|
||||
std::unordered_map<std::string, SoundEffect> m_effects; // 音效映射(ID -> 音效)
|
||||
std::unordered_map<std::string, RandomSoundGroup> m_random_groups; // 随机组映射(ID -> 随机组)
|
||||
std::unordered_map<std::string, Music> m_musics; // 背景音乐映射(ID -> 音乐)
|
||||
};
|
||||
233
source/Asset/Asset_SoundPack.cpp
Normal file
233
source/Asset/Asset_SoundPack.cpp
Normal file
@@ -0,0 +1,233 @@
|
||||
#include "Asset_SoundPack.h"
|
||||
Asset_SoundPack::Asset_SoundPack()
|
||||
{
|
||||
int device = -1; // 使用默认设备
|
||||
unsigned long freq = 44100; // 采样频率,PCM常用的采样率之一,可以按需调整
|
||||
|
||||
// 构造NPK信息
|
||||
InitNpkList();
|
||||
// 解析audio.xml
|
||||
XmlConfig.LoadFromFile("audio.xml");
|
||||
|
||||
// 初始化SDL_mixer,支持OGG格式
|
||||
// 44100: 采样率, MIX_DEFAULT_FORMAT: 音频格式, 2: 声道数(立体声), 4096: 缓冲区大小
|
||||
if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 8192) < 0)
|
||||
{
|
||||
SDL_LogError(0, "SDL_mixer初始化失败! Mix_Error: %s\n", Mix_GetError());
|
||||
}
|
||||
if (Mix_Init(MIX_INIT_MP3 | MIX_INIT_OGG) < 0)
|
||||
{
|
||||
SDL_LogError(0, "Mix_Init failed: %s\n", Mix_GetError());
|
||||
}
|
||||
// 分配通道
|
||||
ChannelCount = Mix_AllocateChannels(64);
|
||||
SetAllChannelVolume(0);
|
||||
}
|
||||
|
||||
Asset_SoundPack::~Asset_SoundPack()
|
||||
{
|
||||
}
|
||||
|
||||
void Asset_SoundPack::InitNpkList()
|
||||
{
|
||||
DIR *dir;
|
||||
struct dirent *ent;
|
||||
std::string path = "SoundPacks/";
|
||||
dir = opendir(path.c_str());
|
||||
|
||||
while ((ent = readdir(dir)))
|
||||
{
|
||||
// 跳过.和..目录
|
||||
if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0)
|
||||
continue;
|
||||
std::string RealPath = path + ent->d_name;
|
||||
Ifstream_NPK Fs;
|
||||
Fs.open(RealPath.c_str(), std::ios::in | std::ios::binary);
|
||||
if (Fs.is_open())
|
||||
{
|
||||
std::string Header = Fs.ReadString();
|
||||
// 如果是NPK
|
||||
if (Header == "NeoplePack_Bill")
|
||||
{
|
||||
// 读取img数量
|
||||
int ImageCount = Fs.ReadInt();
|
||||
|
||||
for (int i = 0; i < ImageCount; ++i)
|
||||
{
|
||||
// 直接读取当前项的偏移、长度和路径
|
||||
auto offset = Fs.ReadUnsignedInt();
|
||||
auto length = Fs.ReadUnsignedInt();
|
||||
auto path = Fs.ReadInfo();
|
||||
|
||||
// 构造 WAV 并插入映射表
|
||||
lp_arr[path] = WAV{
|
||||
offset, // offset
|
||||
length, // size
|
||||
path, // lpWavName(直接复用读取的path)
|
||||
RealPath, // lpBelongsFile
|
||||
i // img_index
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
closedir(dir);
|
||||
}
|
||||
|
||||
void Asset_SoundPack::Reclaims()
|
||||
{
|
||||
}
|
||||
|
||||
Mix_Chunk *Asset_SoundPack::GetOgg(const std::string &lpWavName)
|
||||
{
|
||||
|
||||
if (map_sound.find(lpWavName) != map_sound.end())
|
||||
{
|
||||
map_sound[lpWavName].LastUseTime = SDL_GetTicks64();
|
||||
return map_sound[lpWavName].lpMusic;
|
||||
}
|
||||
// 在NPK映射表中找到
|
||||
if (lp_arr.find(lpWavName) != lp_arr.end())
|
||||
{
|
||||
WAV &wav = lp_arr[lpWavName];
|
||||
if (wav.lpBelongsFile.empty())
|
||||
return nullptr;
|
||||
Ifstream_NPK Fs;
|
||||
Fs.open(wav.lpBelongsFile.c_str(), std::ios::in | std::ios::binary);
|
||||
if (Fs.is_open())
|
||||
{
|
||||
Fs.seekg(wav.offset, std::ios::beg);
|
||||
auto buffer = Fs.ReadCustomSize(wav.size);
|
||||
SDL_RWops *rw = SDL_RWFromConstMem(buffer, wav.size);
|
||||
if (!rw)
|
||||
{
|
||||
SDL_Log("创建内存流失败: %s\n", SDL_GetError());
|
||||
Mix_CloseAudio();
|
||||
SDL_Quit();
|
||||
return nullptr;
|
||||
}
|
||||
Mix_Chunk *music = Mix_LoadWAV_RW(rw, 0);
|
||||
if (!music)
|
||||
{
|
||||
SDL_Log("加载OGG失败: %s\n", Mix_GetError());
|
||||
SDL_RWclose(rw); // 手动关闭流
|
||||
Mix_CloseAudio();
|
||||
SDL_Quit();
|
||||
return nullptr;
|
||||
}
|
||||
map_sound[lpWavName].lpMusic = music;
|
||||
map_sound[lpWavName].LastUseTime = SDL_GetTicks64();
|
||||
return music;
|
||||
}
|
||||
}
|
||||
// 去Music文件夹中寻找
|
||||
else
|
||||
{
|
||||
Mix_Chunk *music = Mix_LoadWAV(lpWavName.c_str());
|
||||
if (!music)
|
||||
{
|
||||
SDL_Log("加载OGG失败: %s\n", Mix_GetError());
|
||||
Mix_CloseAudio();
|
||||
SDL_Quit();
|
||||
return nullptr;
|
||||
}
|
||||
map_sound[lpWavName].lpMusic = music;
|
||||
map_sound[lpWavName].LastUseTime = SDL_GetTicks64();
|
||||
return music;
|
||||
}
|
||||
}
|
||||
|
||||
void Asset_SoundPack::PlayAudio(const std::string Key)
|
||||
{
|
||||
std::string RealKey = Key;
|
||||
// 先判断是不是随机组 如果是随机组去里面调出真正要播放的Key
|
||||
if (XmlConfig.HasRandomGroup(Key))
|
||||
{
|
||||
auto Group = XmlConfig.GetRandomGroup(Key);
|
||||
int RandNum = rand() % 100;
|
||||
for (auto &item : Group.items)
|
||||
{
|
||||
if (RandNum <= item.prob)
|
||||
{
|
||||
RealKey = item.tag;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 如果是特效音频
|
||||
if (XmlConfig.HasEffect(Key))
|
||||
{
|
||||
std::string oggPath = XmlConfig.GetEffect(Key).file_path;
|
||||
int volume = XmlConfig.GetEffect(Key).volume_adjust;
|
||||
PlayEffect(oggPath, volume);
|
||||
}
|
||||
// 如果是背景音乐
|
||||
else if (XmlConfig.HasMusic(Key))
|
||||
{
|
||||
std::string oggPath = XmlConfig.GetMusic(Key).file_path;
|
||||
PlayMusic(Key, oggPath);
|
||||
}
|
||||
else
|
||||
{
|
||||
SDL_LogError(0, "找不到音频: %s\n", Key.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
void Asset_SoundPack::StopAudio(const std::string Key)
|
||||
{
|
||||
StopChannel(backgroud_music_map[Key]);
|
||||
backgroud_music_map.erase(Key);
|
||||
}
|
||||
|
||||
void Asset_SoundPack::PlayMusic(const std::string Key, const std::string oggpath)
|
||||
{
|
||||
auto ogg = GetOgg(oggpath);
|
||||
if (ogg)
|
||||
{
|
||||
int channel = FindFreeChannelInRange(0, 4);
|
||||
backgroud_music_map[Key] = Mix_PlayChannel(channel, ogg, -1);
|
||||
}
|
||||
}
|
||||
|
||||
void Asset_SoundPack::PlayEffect(const std::string Key, int Volume)
|
||||
{
|
||||
auto ogg = GetOgg(Key);
|
||||
if (ogg)
|
||||
{
|
||||
int channel = FindFreeChannelInRange(4, ChannelCount - 1);
|
||||
Mix_VolumeChunk(ogg, Volume);
|
||||
Mix_PlayChannel(channel, ogg, 0);
|
||||
}
|
||||
}
|
||||
|
||||
void Asset_SoundPack::StopChannel(int Channel)
|
||||
{
|
||||
Mix_HaltChannel(Channel); // 停止指定通道的音效
|
||||
}
|
||||
|
||||
void Asset_SoundPack::SetAllChannelVolume(int Volume)
|
||||
{
|
||||
Mix_Volume(-1, Volume);
|
||||
}
|
||||
|
||||
std::map<std::string, int> Asset_SoundPack::GetBackgroudMusicVector()
|
||||
{
|
||||
return backgroud_music_map;
|
||||
}
|
||||
|
||||
int Asset_SoundPack::FindFreeChannelInRange(int startChannel, int endChannel)
|
||||
{
|
||||
// 遍历通道(若总通道数小于32,则只检查到最后一个通道)
|
||||
for (int channel = startChannel; channel <= endChannel; channel++)
|
||||
{
|
||||
// 检查通道是否空闲(Mix_Playing返回0表示空闲)
|
||||
if (Mix_Playing(channel) == 0)
|
||||
{
|
||||
return channel; // 返回第一个空闲通道
|
||||
}
|
||||
}
|
||||
|
||||
// 若所有通道都在使用中
|
||||
return -1;
|
||||
}
|
||||
88
source/Asset/Asset_SoundPack.h
Normal file
88
source/Asset/Asset_SoundPack.h
Normal file
@@ -0,0 +1,88 @@
|
||||
#pragma once
|
||||
#include <string>
|
||||
#include <dirent.h>
|
||||
#include <map>
|
||||
#include <stdio.h>
|
||||
#include <SDL.h>
|
||||
#include <SDL_mixer.h>
|
||||
#include "Tool/Ifstream_NPK.h"
|
||||
#include "Asset/Asset_Audio.hpp"
|
||||
|
||||
class Asset_SoundPack
|
||||
{
|
||||
public:
|
||||
struct WAV
|
||||
{
|
||||
unsigned int offset; // 偏移
|
||||
unsigned int size; // 大小
|
||||
std::string lpWavName; // img文件的路径
|
||||
std::string lpBelongsFile; // 这个img属于哪个npk文件
|
||||
int img_index; // img文件在npk文件里的序号
|
||||
};
|
||||
|
||||
struct NpkInfo
|
||||
{
|
||||
int Offset;
|
||||
int Length;
|
||||
std::string Path;
|
||||
};
|
||||
|
||||
struct MusicInfo
|
||||
{
|
||||
/**上次使用时间 */
|
||||
Uint64 LastUseTime;
|
||||
/**音频指针 */
|
||||
Mix_Chunk *lpMusic;
|
||||
};
|
||||
|
||||
public:
|
||||
Asset_SoundPack(const Asset_SoundPack &) = delete;
|
||||
Asset_SoundPack &operator=(const Asset_SoundPack &) = delete;
|
||||
Asset_SoundPack(Asset_SoundPack &&) = delete;
|
||||
Asset_SoundPack &operator=(Asset_SoundPack &&) = delete;
|
||||
// 全局访问点
|
||||
static Asset_SoundPack &GetInstance()
|
||||
{
|
||||
static Asset_SoundPack instance; // 局部静态变量,保证只初始化一次
|
||||
return instance;
|
||||
}
|
||||
|
||||
/**音频NPK信息表 */
|
||||
std::map<std::string, WAV> lp_arr;
|
||||
/**音频储存表 */
|
||||
std::map<std::string, MusicInfo> map_sound;
|
||||
/**audio信息表 */
|
||||
AudioConfigReader XmlConfig;
|
||||
/**正在播放的背景音乐表 */
|
||||
std::map<std::string, int> backgroud_music_map;
|
||||
/**总通道数 */
|
||||
int ChannelCount;
|
||||
|
||||
public:
|
||||
Asset_SoundPack(/* args */);
|
||||
~Asset_SoundPack();
|
||||
|
||||
void InitNpkList();
|
||||
|
||||
void Reclaims();
|
||||
|
||||
Mix_Chunk *GetOgg(const std::string &lpWavName);
|
||||
|
||||
/**播放音频 */
|
||||
void PlayAudio(const std::string Key);
|
||||
/**停止音频 */
|
||||
void StopAudio(const std::string Key);
|
||||
|
||||
void PlayMusic(const std::string Key, const std::string oggpath);
|
||||
void PlayEffect(const std::string Key, int Volume);
|
||||
|
||||
/**停止通道播放音频 */
|
||||
void StopChannel(int Channel);
|
||||
/**设置所有通道音量 */
|
||||
void SetAllChannelVolume(int Volume);
|
||||
/**获取正在播放的背景音乐 */
|
||||
std::map<std::string, int> GetBackgroudMusicVector();
|
||||
|
||||
/**获取空闲的通道 */
|
||||
int FindFreeChannelInRange(int Start, int End);
|
||||
};
|
||||
2596
source/Asset/rapidxml/rapidxml.hpp
Normal file
2596
source/Asset/rapidxml/rapidxml.hpp
Normal file
File diff suppressed because it is too large
Load Diff
174
source/Asset/rapidxml/rapidxml_iterators.hpp
Normal file
174
source/Asset/rapidxml/rapidxml_iterators.hpp
Normal file
@@ -0,0 +1,174 @@
|
||||
#ifndef RAPIDXML_ITERATORS_HPP_INCLUDED
|
||||
#define RAPIDXML_ITERATORS_HPP_INCLUDED
|
||||
|
||||
// Copyright (C) 2006, 2009 Marcin Kalicinski
|
||||
// Version 1.13
|
||||
// Revision $DateTime: 2009/05/13 01:46:17 $
|
||||
//! \file rapidxml_iterators.hpp This file contains rapidxml iterators
|
||||
|
||||
#include "rapidxml.hpp"
|
||||
|
||||
namespace rapidxml
|
||||
{
|
||||
|
||||
//! Iterator of child nodes of xml_node
|
||||
template<class Ch>
|
||||
class node_iterator
|
||||
{
|
||||
|
||||
public:
|
||||
|
||||
typedef typename xml_node<Ch> value_type;
|
||||
typedef typename xml_node<Ch> &reference;
|
||||
typedef typename xml_node<Ch> *pointer;
|
||||
typedef std::ptrdiff_t difference_type;
|
||||
typedef std::bidirectional_iterator_tag iterator_category;
|
||||
|
||||
node_iterator()
|
||||
: m_node(0)
|
||||
{
|
||||
}
|
||||
|
||||
node_iterator(xml_node<Ch> *node)
|
||||
: m_node(node->first_node())
|
||||
{
|
||||
}
|
||||
|
||||
reference operator *() const
|
||||
{
|
||||
assert(m_node);
|
||||
return *m_node;
|
||||
}
|
||||
|
||||
pointer operator->() const
|
||||
{
|
||||
assert(m_node);
|
||||
return m_node;
|
||||
}
|
||||
|
||||
node_iterator& operator++()
|
||||
{
|
||||
assert(m_node);
|
||||
m_node = m_node->next_sibling();
|
||||
return *this;
|
||||
}
|
||||
|
||||
node_iterator operator++(int)
|
||||
{
|
||||
node_iterator tmp = *this;
|
||||
++this;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
node_iterator& operator--()
|
||||
{
|
||||
assert(m_node && m_node->previous_sibling());
|
||||
m_node = m_node->previous_sibling();
|
||||
return *this;
|
||||
}
|
||||
|
||||
node_iterator operator--(int)
|
||||
{
|
||||
node_iterator tmp = *this;
|
||||
++this;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
bool operator ==(const node_iterator<Ch> &rhs)
|
||||
{
|
||||
return m_node == rhs.m_node;
|
||||
}
|
||||
|
||||
bool operator !=(const node_iterator<Ch> &rhs)
|
||||
{
|
||||
return m_node != rhs.m_node;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
xml_node<Ch> *m_node;
|
||||
|
||||
};
|
||||
|
||||
//! Iterator of child attributes of xml_node
|
||||
template<class Ch>
|
||||
class attribute_iterator
|
||||
{
|
||||
|
||||
public:
|
||||
|
||||
typedef typename xml_attribute<Ch> value_type;
|
||||
typedef typename xml_attribute<Ch> &reference;
|
||||
typedef typename xml_attribute<Ch> *pointer;
|
||||
typedef std::ptrdiff_t difference_type;
|
||||
typedef std::bidirectional_iterator_tag iterator_category;
|
||||
|
||||
attribute_iterator()
|
||||
: m_attribute(0)
|
||||
{
|
||||
}
|
||||
|
||||
attribute_iterator(xml_node<Ch> *node)
|
||||
: m_attribute(node->first_attribute())
|
||||
{
|
||||
}
|
||||
|
||||
reference operator *() const
|
||||
{
|
||||
assert(m_attribute);
|
||||
return *m_attribute;
|
||||
}
|
||||
|
||||
pointer operator->() const
|
||||
{
|
||||
assert(m_attribute);
|
||||
return m_attribute;
|
||||
}
|
||||
|
||||
attribute_iterator& operator++()
|
||||
{
|
||||
assert(m_attribute);
|
||||
m_attribute = m_attribute->next_attribute();
|
||||
return *this;
|
||||
}
|
||||
|
||||
attribute_iterator operator++(int)
|
||||
{
|
||||
attribute_iterator tmp = *this;
|
||||
++this;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
attribute_iterator& operator--()
|
||||
{
|
||||
assert(m_attribute && m_attribute->previous_attribute());
|
||||
m_attribute = m_attribute->previous_attribute();
|
||||
return *this;
|
||||
}
|
||||
|
||||
attribute_iterator operator--(int)
|
||||
{
|
||||
attribute_iterator tmp = *this;
|
||||
++this;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
bool operator ==(const attribute_iterator<Ch> &rhs)
|
||||
{
|
||||
return m_attribute == rhs.m_attribute;
|
||||
}
|
||||
|
||||
bool operator !=(const attribute_iterator<Ch> &rhs)
|
||||
{
|
||||
return m_attribute != rhs.m_attribute;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
xml_attribute<Ch> *m_attribute;
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
421
source/Asset/rapidxml/rapidxml_print.hpp
Normal file
421
source/Asset/rapidxml/rapidxml_print.hpp
Normal file
@@ -0,0 +1,421 @@
|
||||
#ifndef RAPIDXML_PRINT_HPP_INCLUDED
|
||||
#define RAPIDXML_PRINT_HPP_INCLUDED
|
||||
|
||||
// Copyright (C) 2006, 2009 Marcin Kalicinski
|
||||
// Version 1.13
|
||||
// Revision $DateTime: 2009/05/13 01:46:17 $
|
||||
//! \file rapidxml_print.hpp This file contains rapidxml printer implementation
|
||||
|
||||
#include "rapidxml.hpp"
|
||||
|
||||
// Only include streams if not disabled
|
||||
#ifndef RAPIDXML_NO_STREAMS
|
||||
#include <ostream>
|
||||
#include <iterator>
|
||||
#endif
|
||||
|
||||
namespace rapidxml
|
||||
{
|
||||
|
||||
///////////////////////////////////////////////////////////////////////
|
||||
// Printing flags
|
||||
|
||||
const int print_no_indenting = 0x1; //!< Printer flag instructing the printer to suppress indenting of XML. See print() function.
|
||||
|
||||
///////////////////////////////////////////////////////////////////////
|
||||
// Internal
|
||||
|
||||
//! \cond internal
|
||||
namespace internal
|
||||
{
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// Internal character operations
|
||||
|
||||
// Copy characters from given range to given output iterator
|
||||
template<class OutIt, class Ch>
|
||||
inline OutIt copy_chars(const Ch *begin, const Ch *end, OutIt out)
|
||||
{
|
||||
while (begin != end)
|
||||
*out++ = *begin++;
|
||||
return out;
|
||||
}
|
||||
|
||||
// Copy characters from given range to given output iterator and expand
|
||||
// characters into references (< > ' " &)
|
||||
template<class OutIt, class Ch>
|
||||
inline OutIt copy_and_expand_chars(const Ch *begin, const Ch *end, Ch noexpand, OutIt out)
|
||||
{
|
||||
while (begin != end)
|
||||
{
|
||||
if (*begin == noexpand)
|
||||
{
|
||||
*out++ = *begin; // No expansion, copy character
|
||||
}
|
||||
else
|
||||
{
|
||||
switch (*begin)
|
||||
{
|
||||
case Ch('<'):
|
||||
*out++ = Ch('&'); *out++ = Ch('l'); *out++ = Ch('t'); *out++ = Ch(';');
|
||||
break;
|
||||
case Ch('>'):
|
||||
*out++ = Ch('&'); *out++ = Ch('g'); *out++ = Ch('t'); *out++ = Ch(';');
|
||||
break;
|
||||
case Ch('\''):
|
||||
*out++ = Ch('&'); *out++ = Ch('a'); *out++ = Ch('p'); *out++ = Ch('o'); *out++ = Ch('s'); *out++ = Ch(';');
|
||||
break;
|
||||
case Ch('"'):
|
||||
*out++ = Ch('&'); *out++ = Ch('q'); *out++ = Ch('u'); *out++ = Ch('o'); *out++ = Ch('t'); *out++ = Ch(';');
|
||||
break;
|
||||
case Ch('&'):
|
||||
*out++ = Ch('&'); *out++ = Ch('a'); *out++ = Ch('m'); *out++ = Ch('p'); *out++ = Ch(';');
|
||||
break;
|
||||
default:
|
||||
*out++ = *begin; // No expansion, copy character
|
||||
}
|
||||
}
|
||||
++begin; // Step to next character
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
// Fill given output iterator with repetitions of the same character
|
||||
template<class OutIt, class Ch>
|
||||
inline OutIt fill_chars(OutIt out, int n, Ch ch)
|
||||
{
|
||||
for (int i = 0; i < n; ++i)
|
||||
*out++ = ch;
|
||||
return out;
|
||||
}
|
||||
|
||||
// Find character
|
||||
template<class Ch, Ch ch>
|
||||
inline bool find_char(const Ch *begin, const Ch *end)
|
||||
{
|
||||
while (begin != end)
|
||||
if (*begin++ == ch)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// Internal printing operations
|
||||
|
||||
// Print node
|
||||
template<class OutIt, class Ch>
|
||||
inline OutIt print_node(OutIt out, const xml_node<Ch> *node, int flags, int indent)
|
||||
{
|
||||
// Print proper node type
|
||||
switch (node->type())
|
||||
{
|
||||
|
||||
// Document
|
||||
case node_document:
|
||||
out = print_children(out, node, flags, indent);
|
||||
break;
|
||||
|
||||
// Element
|
||||
case node_element:
|
||||
out = print_element_node(out, node, flags, indent);
|
||||
break;
|
||||
|
||||
// Data
|
||||
case node_data:
|
||||
out = print_data_node(out, node, flags, indent);
|
||||
break;
|
||||
|
||||
// CDATA
|
||||
case node_cdata:
|
||||
out = print_cdata_node(out, node, flags, indent);
|
||||
break;
|
||||
|
||||
// Declaration
|
||||
case node_declaration:
|
||||
out = print_declaration_node(out, node, flags, indent);
|
||||
break;
|
||||
|
||||
// Comment
|
||||
case node_comment:
|
||||
out = print_comment_node(out, node, flags, indent);
|
||||
break;
|
||||
|
||||
// Doctype
|
||||
case node_doctype:
|
||||
out = print_doctype_node(out, node, flags, indent);
|
||||
break;
|
||||
|
||||
// Pi
|
||||
case node_pi:
|
||||
out = print_pi_node(out, node, flags, indent);
|
||||
break;
|
||||
|
||||
// Unknown
|
||||
default:
|
||||
assert(0);
|
||||
break;
|
||||
}
|
||||
|
||||
// If indenting not disabled, add line break after node
|
||||
if (!(flags & print_no_indenting))
|
||||
*out = Ch('\n'), ++out;
|
||||
|
||||
// Return modified iterator
|
||||
return out;
|
||||
}
|
||||
|
||||
// Print children of the node
|
||||
template<class OutIt, class Ch>
|
||||
inline OutIt print_children(OutIt out, const xml_node<Ch> *node, int flags, int indent)
|
||||
{
|
||||
for (xml_node<Ch> *child = node->first_node(); child; child = child->next_sibling())
|
||||
out = print_node(out, child, flags, indent);
|
||||
return out;
|
||||
}
|
||||
|
||||
// Print attributes of the node
|
||||
template<class OutIt, class Ch>
|
||||
inline OutIt print_attributes(OutIt out, const xml_node<Ch> *node, int flags)
|
||||
{
|
||||
for (xml_attribute<Ch> *attribute = node->first_attribute(); attribute; attribute = attribute->next_attribute())
|
||||
{
|
||||
if (attribute->name() && attribute->value())
|
||||
{
|
||||
// Print attribute name
|
||||
*out = Ch(' '), ++out;
|
||||
out = copy_chars(attribute->name(), attribute->name() + attribute->name_size(), out);
|
||||
*out = Ch('='), ++out;
|
||||
// Print attribute value using appropriate quote type
|
||||
if (find_char<Ch, Ch('"')>(attribute->value(), attribute->value() + attribute->value_size()))
|
||||
{
|
||||
*out = Ch('\''), ++out;
|
||||
out = copy_and_expand_chars(attribute->value(), attribute->value() + attribute->value_size(), Ch('"'), out);
|
||||
*out = Ch('\''), ++out;
|
||||
}
|
||||
else
|
||||
{
|
||||
*out = Ch('"'), ++out;
|
||||
out = copy_and_expand_chars(attribute->value(), attribute->value() + attribute->value_size(), Ch('\''), out);
|
||||
*out = Ch('"'), ++out;
|
||||
}
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
// Print data node
|
||||
template<class OutIt, class Ch>
|
||||
inline OutIt print_data_node(OutIt out, const xml_node<Ch> *node, int flags, int indent)
|
||||
{
|
||||
assert(node->type() == node_data);
|
||||
if (!(flags & print_no_indenting))
|
||||
out = fill_chars(out, indent, Ch('\t'));
|
||||
out = copy_and_expand_chars(node->value(), node->value() + node->value_size(), Ch(0), out);
|
||||
return out;
|
||||
}
|
||||
|
||||
// Print data node
|
||||
template<class OutIt, class Ch>
|
||||
inline OutIt print_cdata_node(OutIt out, const xml_node<Ch> *node, int flags, int indent)
|
||||
{
|
||||
assert(node->type() == node_cdata);
|
||||
if (!(flags & print_no_indenting))
|
||||
out = fill_chars(out, indent, Ch('\t'));
|
||||
*out = Ch('<'); ++out;
|
||||
*out = Ch('!'); ++out;
|
||||
*out = Ch('['); ++out;
|
||||
*out = Ch('C'); ++out;
|
||||
*out = Ch('D'); ++out;
|
||||
*out = Ch('A'); ++out;
|
||||
*out = Ch('T'); ++out;
|
||||
*out = Ch('A'); ++out;
|
||||
*out = Ch('['); ++out;
|
||||
out = copy_chars(node->value(), node->value() + node->value_size(), out);
|
||||
*out = Ch(']'); ++out;
|
||||
*out = Ch(']'); ++out;
|
||||
*out = Ch('>'); ++out;
|
||||
return out;
|
||||
}
|
||||
|
||||
// Print element node
|
||||
template<class OutIt, class Ch>
|
||||
inline OutIt print_element_node(OutIt out, const xml_node<Ch> *node, int flags, int indent)
|
||||
{
|
||||
assert(node->type() == node_element);
|
||||
|
||||
// Print element name and attributes, if any
|
||||
if (!(flags & print_no_indenting))
|
||||
out = fill_chars(out, indent, Ch('\t'));
|
||||
*out = Ch('<'), ++out;
|
||||
out = copy_chars(node->name(), node->name() + node->name_size(), out);
|
||||
out = print_attributes(out, node, flags);
|
||||
|
||||
// If node is childless
|
||||
if (node->value_size() == 0 && !node->first_node())
|
||||
{
|
||||
// Print childless node tag ending
|
||||
*out = Ch('/'), ++out;
|
||||
*out = Ch('>'), ++out;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Print normal node tag ending
|
||||
*out = Ch('>'), ++out;
|
||||
|
||||
// Test if node contains a single data node only (and no other nodes)
|
||||
xml_node<Ch> *child = node->first_node();
|
||||
if (!child)
|
||||
{
|
||||
// If node has no children, only print its value without indenting
|
||||
out = copy_and_expand_chars(node->value(), node->value() + node->value_size(), Ch(0), out);
|
||||
}
|
||||
else if (child->next_sibling() == 0 && child->type() == node_data)
|
||||
{
|
||||
// If node has a sole data child, only print its value without indenting
|
||||
out = copy_and_expand_chars(child->value(), child->value() + child->value_size(), Ch(0), out);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Print all children with full indenting
|
||||
if (!(flags & print_no_indenting))
|
||||
*out = Ch('\n'), ++out;
|
||||
out = print_children(out, node, flags, indent + 1);
|
||||
if (!(flags & print_no_indenting))
|
||||
out = fill_chars(out, indent, Ch('\t'));
|
||||
}
|
||||
|
||||
// Print node end
|
||||
*out = Ch('<'), ++out;
|
||||
*out = Ch('/'), ++out;
|
||||
out = copy_chars(node->name(), node->name() + node->name_size(), out);
|
||||
*out = Ch('>'), ++out;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
// Print declaration node
|
||||
template<class OutIt, class Ch>
|
||||
inline OutIt print_declaration_node(OutIt out, const xml_node<Ch> *node, int flags, int indent)
|
||||
{
|
||||
// Print declaration start
|
||||
if (!(flags & print_no_indenting))
|
||||
out = fill_chars(out, indent, Ch('\t'));
|
||||
*out = Ch('<'), ++out;
|
||||
*out = Ch('?'), ++out;
|
||||
*out = Ch('x'), ++out;
|
||||
*out = Ch('m'), ++out;
|
||||
*out = Ch('l'), ++out;
|
||||
|
||||
// Print attributes
|
||||
out = print_attributes(out, node, flags);
|
||||
|
||||
// Print declaration end
|
||||
*out = Ch('?'), ++out;
|
||||
*out = Ch('>'), ++out;
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
// Print comment node
|
||||
template<class OutIt, class Ch>
|
||||
inline OutIt print_comment_node(OutIt out, const xml_node<Ch> *node, int flags, int indent)
|
||||
{
|
||||
assert(node->type() == node_comment);
|
||||
if (!(flags & print_no_indenting))
|
||||
out = fill_chars(out, indent, Ch('\t'));
|
||||
*out = Ch('<'), ++out;
|
||||
*out = Ch('!'), ++out;
|
||||
*out = Ch('-'), ++out;
|
||||
*out = Ch('-'), ++out;
|
||||
out = copy_chars(node->value(), node->value() + node->value_size(), out);
|
||||
*out = Ch('-'), ++out;
|
||||
*out = Ch('-'), ++out;
|
||||
*out = Ch('>'), ++out;
|
||||
return out;
|
||||
}
|
||||
|
||||
// Print doctype node
|
||||
template<class OutIt, class Ch>
|
||||
inline OutIt print_doctype_node(OutIt out, const xml_node<Ch> *node, int flags, int indent)
|
||||
{
|
||||
assert(node->type() == node_doctype);
|
||||
if (!(flags & print_no_indenting))
|
||||
out = fill_chars(out, indent, Ch('\t'));
|
||||
*out = Ch('<'), ++out;
|
||||
*out = Ch('!'), ++out;
|
||||
*out = Ch('D'), ++out;
|
||||
*out = Ch('O'), ++out;
|
||||
*out = Ch('C'), ++out;
|
||||
*out = Ch('T'), ++out;
|
||||
*out = Ch('Y'), ++out;
|
||||
*out = Ch('P'), ++out;
|
||||
*out = Ch('E'), ++out;
|
||||
*out = Ch(' '), ++out;
|
||||
out = copy_chars(node->value(), node->value() + node->value_size(), out);
|
||||
*out = Ch('>'), ++out;
|
||||
return out;
|
||||
}
|
||||
|
||||
// Print pi node
|
||||
template<class OutIt, class Ch>
|
||||
inline OutIt print_pi_node(OutIt out, const xml_node<Ch> *node, int flags, int indent)
|
||||
{
|
||||
assert(node->type() == node_pi);
|
||||
if (!(flags & print_no_indenting))
|
||||
out = fill_chars(out, indent, Ch('\t'));
|
||||
*out = Ch('<'), ++out;
|
||||
*out = Ch('?'), ++out;
|
||||
out = copy_chars(node->name(), node->name() + node->name_size(), out);
|
||||
*out = Ch(' '), ++out;
|
||||
out = copy_chars(node->value(), node->value() + node->value_size(), out);
|
||||
*out = Ch('?'), ++out;
|
||||
*out = Ch('>'), ++out;
|
||||
return out;
|
||||
}
|
||||
|
||||
}
|
||||
//! \endcond
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// Printing
|
||||
|
||||
//! Prints XML to given output iterator.
|
||||
//! \param out Output iterator to print to.
|
||||
//! \param node Node to be printed. Pass xml_document to print entire document.
|
||||
//! \param flags Flags controlling how XML is printed.
|
||||
//! \return Output iterator pointing to position immediately after last character of printed text.
|
||||
template<class OutIt, class Ch>
|
||||
inline OutIt print(OutIt out, const xml_node<Ch> &node, int flags = 0)
|
||||
{
|
||||
return internal::print_node(out, &node, flags, 0);
|
||||
}
|
||||
|
||||
#ifndef RAPIDXML_NO_STREAMS
|
||||
|
||||
//! Prints XML to given output stream.
|
||||
//! \param out Output stream to print to.
|
||||
//! \param node Node to be printed. Pass xml_document to print entire document.
|
||||
//! \param flags Flags controlling how XML is printed.
|
||||
//! \return Output stream.
|
||||
template<class Ch>
|
||||
inline std::basic_ostream<Ch> &print(std::basic_ostream<Ch> &out, const xml_node<Ch> &node, int flags = 0)
|
||||
{
|
||||
print(std::ostream_iterator<Ch>(out), node, flags);
|
||||
return out;
|
||||
}
|
||||
|
||||
//! Prints formatted XML to given output stream. Uses default printing flags. Use print() function to customize printing process.
|
||||
//! \param out Output stream to print to.
|
||||
//! \param node Node to be printed.
|
||||
//! \return Output stream.
|
||||
template<class Ch>
|
||||
inline std::basic_ostream<Ch> &operator <<(std::basic_ostream<Ch> &out, const xml_node<Ch> &node)
|
||||
{
|
||||
return print(out, node);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
122
source/Asset/rapidxml/rapidxml_utils.hpp
Normal file
122
source/Asset/rapidxml/rapidxml_utils.hpp
Normal file
@@ -0,0 +1,122 @@
|
||||
#ifndef RAPIDXML_UTILS_HPP_INCLUDED
|
||||
#define RAPIDXML_UTILS_HPP_INCLUDED
|
||||
|
||||
// Copyright (C) 2006, 2009 Marcin Kalicinski
|
||||
// Version 1.13
|
||||
// Revision $DateTime: 2009/05/13 01:46:17 $
|
||||
//! \file rapidxml_utils.hpp This file contains high-level rapidxml utilities that can be useful
|
||||
//! in certain simple scenarios. They should probably not be used if maximizing performance is the main objective.
|
||||
|
||||
#include "rapidxml.hpp"
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <fstream>
|
||||
#include <stdexcept>
|
||||
|
||||
namespace rapidxml
|
||||
{
|
||||
|
||||
//! Represents data loaded from a file
|
||||
template<class Ch = char>
|
||||
class file
|
||||
{
|
||||
|
||||
public:
|
||||
|
||||
//! Loads file into the memory. Data will be automatically destroyed by the destructor.
|
||||
//! \param filename Filename to load.
|
||||
file(const char *filename)
|
||||
{
|
||||
using namespace std;
|
||||
|
||||
// Open stream
|
||||
basic_ifstream<Ch> stream(filename, ios::binary);
|
||||
if (!stream)
|
||||
throw runtime_error(string("cannot open file ") + filename);
|
||||
stream.unsetf(ios::skipws);
|
||||
|
||||
// Determine stream size
|
||||
stream.seekg(0, ios::end);
|
||||
size_t size = stream.tellg();
|
||||
stream.seekg(0);
|
||||
|
||||
// Load data and add terminating 0
|
||||
m_data.resize(size + 1);
|
||||
stream.read(&m_data.front(), static_cast<streamsize>(size));
|
||||
m_data[size] = 0;
|
||||
}
|
||||
|
||||
//! Loads file into the memory. Data will be automatically destroyed by the destructor
|
||||
//! \param stream Stream to load from
|
||||
file(std::basic_istream<Ch> &stream)
|
||||
{
|
||||
using namespace std;
|
||||
|
||||
// Load data and add terminating 0
|
||||
stream.unsetf(ios::skipws);
|
||||
m_data.assign(istreambuf_iterator<Ch>(stream), istreambuf_iterator<Ch>());
|
||||
if (stream.fail() || stream.bad())
|
||||
throw runtime_error("error reading stream");
|
||||
m_data.push_back(0);
|
||||
}
|
||||
|
||||
//! Gets file data.
|
||||
//! \return Pointer to data of file.
|
||||
Ch *data()
|
||||
{
|
||||
return &m_data.front();
|
||||
}
|
||||
|
||||
//! Gets file data.
|
||||
//! \return Pointer to data of file.
|
||||
const Ch *data() const
|
||||
{
|
||||
return &m_data.front();
|
||||
}
|
||||
|
||||
//! Gets file data size.
|
||||
//! \return Size of file data, in characters.
|
||||
std::size_t size() const
|
||||
{
|
||||
return m_data.size();
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
std::vector<Ch> m_data; // File data
|
||||
|
||||
};
|
||||
|
||||
//! Counts children of node. Time complexity is O(n).
|
||||
//! \return Number of children of node
|
||||
template<class Ch>
|
||||
inline std::size_t count_children(xml_node<Ch> *node)
|
||||
{
|
||||
xml_node<Ch> *child = node->first_node();
|
||||
std::size_t count = 0;
|
||||
while (child)
|
||||
{
|
||||
++count;
|
||||
child = child->next_sibling();
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
//! Counts attributes of node. Time complexity is O(n).
|
||||
//! \return Number of attributes of node
|
||||
template<class Ch>
|
||||
inline std::size_t count_attributes(xml_node<Ch> *node)
|
||||
{
|
||||
xml_attribute<Ch> *attr = node->first_attribute();
|
||||
std::size_t count = 0;
|
||||
while (attr)
|
||||
{
|
||||
++count;
|
||||
attr = attr->next_attribute();
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -3,6 +3,7 @@
|
||||
#include "EngineFrame/Component/Sprite.h"
|
||||
#include "EngineFrame/Base/Actor.h"
|
||||
#include "EngineFrame/Component/Text.h"
|
||||
#include "Asset/Asset_SoundPack.h"
|
||||
|
||||
Game::Game()
|
||||
{
|
||||
@@ -67,13 +68,6 @@ void Game::Init(std::function<void()> CallBack)
|
||||
|
||||
IMG_Init(IMG_INIT_PNG);
|
||||
|
||||
// 初始化SDL_mixer,支持OGG格式
|
||||
// 44100: 采样率, MIX_DEFAULT_FORMAT: 音频格式, 2: 声道数(立体声), 4096: 缓冲区大小
|
||||
if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 4096) < 0)
|
||||
{
|
||||
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "SDL_mixer初始化失败! Mix_Error: %s\n", Mix_GetError());
|
||||
m_isRunning = false;
|
||||
}
|
||||
|
||||
// 初始化 TTF
|
||||
if (TTF_Init() == -1)
|
||||
@@ -157,9 +151,16 @@ void Game::HandleEvents(SDL_Event *e)
|
||||
|
||||
void Game::Update(float deltaTime)
|
||||
{
|
||||
//调用松鼠的更新
|
||||
// 内存回收机制
|
||||
m_lastMemoryPrintTime += deltaTime;
|
||||
if (m_lastMemoryPrintTime >= m_memoryPrintInterval)
|
||||
{
|
||||
m_lastMemoryPrintTime = 0;
|
||||
MemoryReclaims();
|
||||
}
|
||||
// 调用松鼠的更新
|
||||
SquirrelEx::GetInstance().Update(deltaTime);
|
||||
if (m_scene != nullptr)
|
||||
if (m_scene != nullptr)
|
||||
m_scene->Update(deltaTime);
|
||||
if (m_uiScene != nullptr)
|
||||
m_uiScene->Update(deltaTime);
|
||||
@@ -205,6 +206,12 @@ void Game::Clear()
|
||||
SDL_Quit();
|
||||
}
|
||||
|
||||
void Game::MemoryReclaims()
|
||||
{
|
||||
// 音频内存回收
|
||||
Asset_SoundPack::GetInstance().Reclaims();
|
||||
}
|
||||
|
||||
void Game::ChangeScene(RefPtr<Scene> scene)
|
||||
{
|
||||
if (m_scene != nullptr)
|
||||
@@ -225,6 +232,11 @@ void Game::ChangeUIScene(RefPtr<Scene> scene)
|
||||
m_uiScene->Enter();
|
||||
}
|
||||
|
||||
RefPtr<Scene> Game::GetScene()
|
||||
{
|
||||
return m_scene;
|
||||
}
|
||||
|
||||
RenderManager *Game::GetRenderer()
|
||||
{
|
||||
return m_renderer;
|
||||
|
||||
@@ -51,10 +51,14 @@ public:
|
||||
void Render();
|
||||
void Clear();
|
||||
|
||||
void MemoryReclaims();
|
||||
|
||||
// 切换场景
|
||||
void ChangeScene(RefPtr<Scene> scene);
|
||||
// 设定UI层场景对象
|
||||
void ChangeUIScene(RefPtr<Scene> scene);
|
||||
// 获取当前游戏层场景
|
||||
RefPtr<Scene> GetScene();
|
||||
|
||||
RenderManager *GetRenderer();
|
||||
|
||||
@@ -79,7 +83,7 @@ private:
|
||||
#ifdef __SWITCH__
|
||||
float m_Settingfps = 5000.0;
|
||||
#else
|
||||
float m_Settingfps = 5000.0;
|
||||
float m_Settingfps = 144.0;
|
||||
#endif
|
||||
// 单帧时间
|
||||
float m_frameTime = 0.f;
|
||||
@@ -88,6 +92,10 @@ private:
|
||||
// 每秒内的帧数计数器
|
||||
u32 m_frameCounter;
|
||||
u32 m_lastFpsPrintTime;
|
||||
// 内存回收计时器
|
||||
u32 m_lastMemoryPrintTime = 0;
|
||||
// 内存回收间隔
|
||||
u32 m_memoryPrintInterval = 5000;
|
||||
|
||||
public:
|
||||
// 屏幕宽高
|
||||
|
||||
@@ -1,45 +1,41 @@
|
||||
#include "Actor.h"
|
||||
#include "EngineFrame/Scene/Scene.h"
|
||||
#include "EngineCore/Game.h"
|
||||
#include "EngineFrame/Component/RenderBase.h"
|
||||
#include "EngineFrame/Render/RenderManager.h"
|
||||
#include <algorithm>
|
||||
|
||||
Actor::Actor()
|
||||
{
|
||||
Init();
|
||||
}
|
||||
|
||||
void Actor::Init()
|
||||
{
|
||||
addTag(Tag::ACTOR);
|
||||
addTag(Tag::RENDER);
|
||||
addTag(Tag::TRANSFORM);
|
||||
addTag(Tag::UPDATE);
|
||||
}
|
||||
|
||||
void Actor::Update(float deltaTime)
|
||||
void Actor::OnAdded(Actor *node)
|
||||
{
|
||||
if (!Visible)
|
||||
return;
|
||||
BaseNode::Update(deltaTime);
|
||||
|
||||
}
|
||||
|
||||
void Actor::SetBlendMode(LE_BlEND_MODE mode)
|
||||
{
|
||||
this->_BlendMode = mode;
|
||||
}
|
||||
|
||||
LE_BlEND_MODE Actor::GetBlendMode()
|
||||
{
|
||||
return this->_BlendMode;
|
||||
}
|
||||
|
||||
void Actor::Render()
|
||||
{
|
||||
if (!Visible)
|
||||
return;
|
||||
// 如果有裁切视口
|
||||
if (this->_CropViewportFlag)
|
||||
{
|
||||
RenderManager *renderer = Game::GetInstance().GetRenderer();
|
||||
renderer->SetClipRect(&this->_CropViewport);
|
||||
BaseNode::Render();
|
||||
Node::Render();
|
||||
renderer->CloseClipRect();
|
||||
}
|
||||
else
|
||||
{
|
||||
BaseNode::Render();
|
||||
Node::Render();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
#pragma once
|
||||
#include "EngineFrame/Base/BaseNode.h"
|
||||
#include "EngineFrame/Base/Node.h"
|
||||
#include "Tool/IntrusiveList.hpp"
|
||||
class Scene;
|
||||
/**
|
||||
@@ -7,24 +7,31 @@ class Scene;
|
||||
*
|
||||
* Actor类是一个基础的游戏对象类,可以添加到场景中
|
||||
*/
|
||||
class Actor : public BaseNode
|
||||
class Actor : public Node
|
||||
{
|
||||
private:
|
||||
//裁切视口Flag
|
||||
protected:
|
||||
/**裁切视口Flag */
|
||||
bool _CropViewportFlag = false;
|
||||
//裁切视口
|
||||
/**裁切视口 */
|
||||
SDL_Rect _CropViewport = {0, 0, 0, 0};
|
||||
/**混合模式 */
|
||||
LE_BlEND_MODE _BlendMode = NONE;
|
||||
|
||||
public:
|
||||
Actor();
|
||||
|
||||
// 初始化
|
||||
virtual void Init();
|
||||
// 被添加时
|
||||
virtual void OnAdded(Actor *node);
|
||||
// 设置混合模式
|
||||
void SetBlendMode(LE_BlEND_MODE mode);
|
||||
// 获取混合模式
|
||||
LE_BlEND_MODE GetBlendMode();
|
||||
public:
|
||||
void Init() override;
|
||||
void Update(float deltaTime) override;
|
||||
/**重载渲染函数 */
|
||||
void Render() override;
|
||||
|
||||
// 设置裁切视口(放在Actor里 他与他的子对象都会被裁切)
|
||||
/**设置裁切视口(放在Actor里 他与他的子对象都会被裁切) */
|
||||
void SetCropViewport(SDL_Rect CropViewport);
|
||||
// 获取裁切视口
|
||||
/**获取裁切视口 */
|
||||
SDL_Rect GetCropViewport();
|
||||
};
|
||||
|
||||
@@ -1,368 +0,0 @@
|
||||
#include "BaseNode.h"
|
||||
#include "EngineCore/Game.h"
|
||||
BaseNode::BaseNode()
|
||||
{
|
||||
Game::GetInstance().m_nodeCount++;
|
||||
}
|
||||
|
||||
BaseNode::~BaseNode()
|
||||
{
|
||||
Game::GetInstance().m_nodeCount--;
|
||||
}
|
||||
|
||||
void BaseNode::Init()
|
||||
{
|
||||
}
|
||||
void BaseNode::HandleEvents(SDL_Event *e)
|
||||
{
|
||||
// 如果有子节点并含有事件标签,则处理事件
|
||||
RefPtr<BaseNode> child = m_BaseNodes.GetFirst();
|
||||
while (child)
|
||||
{
|
||||
if (child->hasTag(Tag::ACTOR) || child->hasTag(Tag::HANDEL_EVENT))
|
||||
child->HandleEvents(e);
|
||||
child = child->GetNext();
|
||||
}
|
||||
}
|
||||
|
||||
void BaseNode::Update(float deltaTime)
|
||||
{
|
||||
// 如果有子节点并含有刷新标签,则更新子节点
|
||||
RefPtr<BaseNode> child = m_BaseNodes.GetFirst();
|
||||
while (child)
|
||||
{
|
||||
if (child->hasTag(Tag::UPDATE))
|
||||
child->Update(deltaTime);
|
||||
child = child->GetNext();
|
||||
}
|
||||
// 如果有回调函数,则调用回调函数
|
||||
if (cb_update_.size() > 0)
|
||||
{
|
||||
for (auto &cb : cb_update_)
|
||||
{
|
||||
cb.second(deltaTime);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void BaseNode::PreRender()
|
||||
{
|
||||
// 如果有子节点并含有渲染标签,则渲染子节点
|
||||
RefPtr<BaseNode> child = m_BaseNodes.GetFirst();
|
||||
while (child)
|
||||
{
|
||||
if (child->hasTag(Tag::RENDER))
|
||||
child->PreRender();
|
||||
child = child->GetNext();
|
||||
}
|
||||
}
|
||||
void BaseNode::Render()
|
||||
{
|
||||
// 如果有子节点并含有渲染标签,则渲染子节点
|
||||
RefPtr<BaseNode> child = m_BaseNodes.GetFirst();
|
||||
while (child)
|
||||
{
|
||||
if (child->hasTag(Tag::RENDER))
|
||||
child->Render();
|
||||
child = child->GetNext();
|
||||
}
|
||||
}
|
||||
|
||||
void BaseNode::Clear()
|
||||
{
|
||||
}
|
||||
|
||||
void BaseNode::SetCallbackOnUpdate(std::string Key, const UpdateCallback &cb)
|
||||
{
|
||||
cb_update_[Key] = cb;
|
||||
}
|
||||
|
||||
void BaseNode::RemoveCallbackOnUpdate(std::string Key)
|
||||
{
|
||||
cb_update_.erase(Key);
|
||||
}
|
||||
|
||||
void BaseNode::SetChildIterationTransform()
|
||||
{
|
||||
Transform n_transform;
|
||||
n_transform.position = transform.position + transformIter.position;
|
||||
n_transform.scale = transform.scale * transformIter.scale;
|
||||
n_transform.rotation = transform.rotation + transformIter.rotation;
|
||||
|
||||
// 如果有子节点并含有transform标签,则设置其位置
|
||||
RefPtr<BaseNode> child = m_BaseNodes.GetFirst();
|
||||
while (child)
|
||||
{
|
||||
if (child->hasTag(Tag::TRANSFORM))
|
||||
child->SetIterationTransform(n_transform);
|
||||
child = child->GetNext();
|
||||
}
|
||||
}
|
||||
|
||||
void BaseNode::CalcRenderInfo()
|
||||
{
|
||||
}
|
||||
|
||||
void BaseNode::SetName(std::string name)
|
||||
{
|
||||
m_Name = name;
|
||||
}
|
||||
|
||||
std::string BaseNode::GetName()
|
||||
{
|
||||
return m_Name;
|
||||
}
|
||||
|
||||
int BaseNode::GetRenderZOrder()
|
||||
{
|
||||
return m_RenderZOrder;
|
||||
}
|
||||
|
||||
void BaseNode::SetRenderZOrder(int zOrder)
|
||||
{
|
||||
m_RenderZOrder = zOrder;
|
||||
Reorder();
|
||||
}
|
||||
|
||||
void BaseNode::Reorder()
|
||||
{
|
||||
if (m_Parent)
|
||||
{
|
||||
RefPtr<BaseNode> me = this;
|
||||
m_Parent->m_BaseNodes.Remove(me);
|
||||
RefPtr<BaseNode> sibling = m_Parent->m_BaseNodes.GetLast();
|
||||
if (sibling && sibling->GetRenderZOrder() > m_RenderZOrder)
|
||||
{
|
||||
sibling = sibling->GetPrev();
|
||||
while (sibling)
|
||||
{
|
||||
if (sibling->GetRenderZOrder() <= m_RenderZOrder)
|
||||
break;
|
||||
sibling = sibling->GetPrev();
|
||||
}
|
||||
}
|
||||
if (sibling)
|
||||
{
|
||||
m_Parent->m_BaseNodes.InsertAfter(me, sibling);
|
||||
}
|
||||
else
|
||||
{
|
||||
m_Parent->m_BaseNodes.PushFront(me);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void BaseNode::AddChild(RefPtr<BaseNode> child)
|
||||
{
|
||||
m_BaseNodes.PushBack(child);
|
||||
child->OnAdded(this);
|
||||
// 如果是需要渲染的子对象
|
||||
if (child->hasTag(Tag::RENDER))
|
||||
{
|
||||
// 重新排序
|
||||
child->Reorder();
|
||||
// 如果组件有transform标签,则设置其位置
|
||||
if (child->hasTag(Tag::TRANSFORM))
|
||||
{
|
||||
Transform n_transform;
|
||||
n_transform.position = transform.position + transformIter.position;
|
||||
n_transform.scale = transform.scale * transformIter.scale;
|
||||
n_transform.rotation = transform.rotation + transformIter.rotation;
|
||||
child->SetIterationTransform(n_transform);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void BaseNode::RemoveChild(RefPtr<BaseNode> child)
|
||||
{
|
||||
child->m_Parent = nullptr;
|
||||
m_BaseNodes.Remove(child);
|
||||
}
|
||||
|
||||
void BaseNode::RemoveAllChild()
|
||||
{
|
||||
m_BaseNodes.Clear();
|
||||
}
|
||||
|
||||
void BaseNode::OnAdded(BaseNode *node)
|
||||
{
|
||||
m_Parent = node;
|
||||
}
|
||||
|
||||
void BaseNode::SetIterationTransform(Transform n_transform)
|
||||
{
|
||||
if (n_transform == transformIter)
|
||||
return;
|
||||
transformIter = n_transform;
|
||||
CalcRenderInfo();
|
||||
SetChildIterationTransform();
|
||||
}
|
||||
|
||||
Transform BaseNode::GetIterationTransform()
|
||||
{
|
||||
return transformIter;
|
||||
}
|
||||
|
||||
void BaseNode::SetTransform(Transform n_transform)
|
||||
{
|
||||
if (n_transform == transform)
|
||||
return;
|
||||
transform = n_transform;
|
||||
CalcRenderInfo();
|
||||
SetChildIterationTransform();
|
||||
}
|
||||
|
||||
Transform BaseNode::GetTransform()
|
||||
{
|
||||
return transform;
|
||||
}
|
||||
|
||||
void BaseNode::SetPos(Vec2 pos)
|
||||
{
|
||||
if (pos == this->transform.position)
|
||||
return;
|
||||
this->transform.position = pos;
|
||||
CalcRenderInfo();
|
||||
SetChildIterationTransform();
|
||||
}
|
||||
|
||||
Vec2 BaseNode::GetPos()
|
||||
{
|
||||
return this->transform.position;
|
||||
}
|
||||
|
||||
Vec2 BaseNode::GetWorldPos()
|
||||
{
|
||||
return this->transform.position + this->transformIter.position;
|
||||
}
|
||||
|
||||
void BaseNode::SetScale(Vec2 scale)
|
||||
{
|
||||
if (scale == this->transform.scale)
|
||||
return;
|
||||
this->transform.scale = scale;
|
||||
CalcRenderInfo();
|
||||
SetChildIterationTransform();
|
||||
}
|
||||
|
||||
Vec2 BaseNode::GetScale()
|
||||
{
|
||||
return this->transform.scale;
|
||||
}
|
||||
|
||||
void BaseNode::SetRotation(float angle)
|
||||
{
|
||||
if (angle == this->transform.rotation)
|
||||
return;
|
||||
this->transform.rotation = angle;
|
||||
CalcRenderInfo();
|
||||
SetChildIterationTransform();
|
||||
}
|
||||
|
||||
float BaseNode::GetRotation()
|
||||
{
|
||||
return this->transform.rotation;
|
||||
}
|
||||
|
||||
void BaseNode::SetAnchor(Vec2 anchor)
|
||||
{
|
||||
if (anchor == this->Anchor)
|
||||
return;
|
||||
Anchor.x = anchor.x;
|
||||
Anchor.y = anchor.y;
|
||||
}
|
||||
|
||||
Vec2 BaseNode::GetAnchor()
|
||||
{
|
||||
Vec2 P;
|
||||
P.x = Anchor.x;
|
||||
P.y = Anchor.y;
|
||||
return P;
|
||||
}
|
||||
|
||||
void BaseNode::SetSize(VecSize size)
|
||||
{
|
||||
this->Size = size;
|
||||
}
|
||||
|
||||
VecSize BaseNode::GetSize()
|
||||
{
|
||||
return this->Size;
|
||||
}
|
||||
|
||||
void BaseNode::SetVisible(bool visible)
|
||||
{
|
||||
this->Visible = visible;
|
||||
}
|
||||
|
||||
bool BaseNode::GetVisible()
|
||||
{
|
||||
return this->Visible;
|
||||
}
|
||||
|
||||
void BaseNode::SetAlpha(float alpha)
|
||||
{
|
||||
this->Alpha = alpha;
|
||||
}
|
||||
|
||||
BaseNode *BaseNode::GetParent()
|
||||
{
|
||||
return this->m_Parent;
|
||||
}
|
||||
|
||||
float BaseNode::GetAlpha()
|
||||
{
|
||||
return this->Alpha;
|
||||
}
|
||||
|
||||
void BaseNode::SetIterationPos(Vec2 pos)
|
||||
{
|
||||
if (pos == this->transformIter.position)
|
||||
return;
|
||||
this->transformIter.position = pos;
|
||||
CalcRenderInfo();
|
||||
SetChildIterationTransform();
|
||||
}
|
||||
|
||||
Vec2 BaseNode::GetIterationPos()
|
||||
{
|
||||
return this->transformIter.position;
|
||||
}
|
||||
|
||||
void BaseNode::SetIterationScale(Vec2 scale)
|
||||
{
|
||||
if (scale == this->transformIter.scale)
|
||||
return;
|
||||
this->transformIter.scale = scale;
|
||||
CalcRenderInfo();
|
||||
SetChildIterationTransform();
|
||||
}
|
||||
|
||||
Vec2 BaseNode::GetIterationScale()
|
||||
{
|
||||
return this->transformIter.scale;
|
||||
}
|
||||
|
||||
void BaseNode::SetIterationRotation(float angle)
|
||||
{
|
||||
if (angle == this->transformIter.rotation)
|
||||
return;
|
||||
this->transformIter.rotation = angle;
|
||||
CalcRenderInfo();
|
||||
SetChildIterationTransform();
|
||||
}
|
||||
|
||||
float BaseNode::GetIterationRotation()
|
||||
{
|
||||
return this->transformIter.rotation;
|
||||
}
|
||||
|
||||
void BaseNode::SetBlendMode(LE_BlEND_MODE mode)
|
||||
{
|
||||
this->_BlendMode = mode;
|
||||
}
|
||||
|
||||
LE_BlEND_MODE BaseNode::GetBlendMode()
|
||||
{
|
||||
return this->_BlendMode;
|
||||
}
|
||||
@@ -1,147 +0,0 @@
|
||||
#pragma once
|
||||
#include <functional>
|
||||
#include <SDL.h>
|
||||
#include <string>
|
||||
#include "Tool/RefObject.h"
|
||||
#include "Tool/RefPtr.h"
|
||||
#include "Tool/IntrusiveList.hpp"
|
||||
#include "Tool/TagGed.h"
|
||||
#include "math/Transform.hpp"
|
||||
#include "math/Math.h"
|
||||
using namespace ember;
|
||||
class BaseNode : public RefObject, public TagGed, protected IntrusiveListValue<RefPtr<BaseNode>>
|
||||
{
|
||||
|
||||
public:
|
||||
typedef std::function<void(float deltaTime)> UpdateCallback;
|
||||
|
||||
using IntrusiveListValue<RefPtr<BaseNode>>::GetNext;
|
||||
using IntrusiveListValue<RefPtr<BaseNode>>::GetPrev;
|
||||
|
||||
public:
|
||||
// 更新时的回调函数
|
||||
std::map<std::string ,UpdateCallback> cb_update_;
|
||||
// 节点名称
|
||||
std::string m_Name;
|
||||
// 子节点列表
|
||||
IntrusiveList<RefPtr<BaseNode>> m_BaseNodes;
|
||||
// 指向父对象的指针
|
||||
BaseNode *m_Parent = nullptr;
|
||||
// 渲染层级
|
||||
int m_RenderZOrder = 0;
|
||||
// 二维仿射变换
|
||||
Transform transform;
|
||||
// 迭代的二维仿射变换
|
||||
Transform transformIter;
|
||||
// 锚点
|
||||
Vec2 Anchor = {0.f, 0.f};
|
||||
// 大小
|
||||
VecSize Size = {0, 0};
|
||||
// 透明度
|
||||
float Alpha = 1.f;
|
||||
// 混合模式
|
||||
LE_BlEND_MODE _BlendMode = NONE;
|
||||
// 是否显示
|
||||
bool Visible = true;
|
||||
// 计算渲染信息Flag (为了保证每帧只计算一次)
|
||||
bool CalcRenderInfoFlag = true;
|
||||
|
||||
public:
|
||||
BaseNode(/* args */);
|
||||
~BaseNode();
|
||||
|
||||
public:
|
||||
virtual void Init();
|
||||
virtual void HandleEvents(SDL_Event *e);
|
||||
virtual void Update(float deltaTime);
|
||||
virtual void PreRender();
|
||||
virtual void Render();
|
||||
virtual void Clear();
|
||||
|
||||
/// \~chinese
|
||||
/// @brief 设置更新时的回调函数
|
||||
void SetCallbackOnUpdate(std::string Key,const UpdateCallback &cb);
|
||||
void RemoveCallbackOnUpdate(std::string Key);
|
||||
|
||||
void SetChildIterationTransform();
|
||||
|
||||
// 计算渲染信息
|
||||
virtual void CalcRenderInfo();
|
||||
// 设置名字
|
||||
void SetName(std::string name);
|
||||
// 获取名字
|
||||
std::string GetName();
|
||||
// 设置渲染层级
|
||||
int GetRenderZOrder();
|
||||
// 获取渲染层级
|
||||
void SetRenderZOrder(int zOrder);
|
||||
// 重新排序
|
||||
void Reorder();
|
||||
// 添加子对象
|
||||
virtual void AddChild(RefPtr<BaseNode> child);
|
||||
// 移除子对象
|
||||
void RemoveChild(RefPtr<BaseNode> child);
|
||||
// 移除所有子对象
|
||||
void RemoveAllChild();
|
||||
// 被添加时
|
||||
virtual void OnAdded(BaseNode *node);
|
||||
// 设置迭代的二维仿射变换
|
||||
void SetIterationTransform(Transform transform);
|
||||
// 获取迭代的二维仿射变换
|
||||
Transform GetIterationTransform();
|
||||
// 设置二维仿射变换
|
||||
void SetTransform(Transform transform);
|
||||
// 获取二维仿射变换
|
||||
Transform GetTransform();
|
||||
|
||||
// 设置迭代的坐标
|
||||
virtual void SetIterationPos(Vec2 pos);
|
||||
// 获取迭代的坐标
|
||||
Vec2 GetIterationPos();
|
||||
// 设置迭代的缩放
|
||||
virtual void SetIterationScale(Vec2 scale);
|
||||
// 获取迭代的缩放
|
||||
Vec2 GetIterationScale();
|
||||
// 设置迭代的旋转角度
|
||||
virtual void SetIterationRotation(float angle);
|
||||
// 获取迭代的旋转角度
|
||||
float GetIterationRotation();
|
||||
|
||||
// 设置坐标
|
||||
virtual void SetPos(Vec2 pos);
|
||||
// 获取坐标
|
||||
Vec2 GetPos();
|
||||
// 获取世界坐标
|
||||
Vec2 GetWorldPos();
|
||||
// 设置缩放
|
||||
virtual void SetScale(Vec2 scale);
|
||||
// 获取缩放
|
||||
Vec2 GetScale();
|
||||
// 设置旋转角度
|
||||
virtual void SetRotation(float angle);
|
||||
// 获取旋转角度
|
||||
float GetRotation();
|
||||
// 设置中心点
|
||||
virtual void SetAnchor(Vec2 anchor);
|
||||
// 获取中心点
|
||||
Vec2 GetAnchor();
|
||||
// 设置大小
|
||||
virtual void SetSize(VecSize size);
|
||||
// 获取大小
|
||||
VecSize GetSize();
|
||||
// 设置是否显示
|
||||
virtual void SetVisible(bool visible);
|
||||
// 获取是否显示
|
||||
bool GetVisible();
|
||||
// 设置透明度
|
||||
virtual void SetAlpha(float alpha);
|
||||
// 获取透明度
|
||||
float GetAlpha();
|
||||
// 设置混合模式
|
||||
void SetBlendMode(LE_BlEND_MODE mode);
|
||||
// 获取混合模式
|
||||
LE_BlEND_MODE GetBlendMode();
|
||||
|
||||
//获取父对象
|
||||
BaseNode *GetParent();
|
||||
};
|
||||
@@ -5,11 +5,18 @@ void Node::Init()
|
||||
}
|
||||
void Node::HandleEvents(SDL_Event *e)
|
||||
{
|
||||
RefPtr<Node> child = children_.GetFirst();
|
||||
while (child)
|
||||
{
|
||||
child->HandleEvents(e);
|
||||
child = child->GetNext();
|
||||
}
|
||||
}
|
||||
inline void Node::Update(float deltaTime)
|
||||
{
|
||||
if (children_.IsEmpty())
|
||||
{
|
||||
UpdateSelf(deltaTime);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -34,26 +41,118 @@ inline void Node::Update(float deltaTime)
|
||||
}
|
||||
inline void Node::PreRender()
|
||||
{
|
||||
if (!visible_)
|
||||
return;
|
||||
UpdateTransform();
|
||||
UpdateOpacity();
|
||||
|
||||
if (children_.IsEmpty())
|
||||
{
|
||||
if (CheckVisibility())
|
||||
{
|
||||
OnPreRender();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
RefPtr<Node> child = children_.GetFirst();
|
||||
while (child)
|
||||
{
|
||||
if (child->GetZOrder() >= 0)
|
||||
break;
|
||||
|
||||
child->PreRender();
|
||||
child = child->GetNext();
|
||||
}
|
||||
|
||||
if (CheckVisibility())
|
||||
{
|
||||
OnPreRender();
|
||||
}
|
||||
|
||||
while (child)
|
||||
{
|
||||
child->PreRender();
|
||||
child = child->GetNext();
|
||||
}
|
||||
}
|
||||
}
|
||||
inline void Node::Render()
|
||||
{
|
||||
if (!visible_)
|
||||
return;
|
||||
if (children_.IsEmpty())
|
||||
{
|
||||
if (visible_in_rt_)
|
||||
{
|
||||
PrepareToRender();
|
||||
OnRender();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
RefPtr<Node> child = children_.GetFirst();
|
||||
while (child)
|
||||
{
|
||||
if (child->GetZOrder() >= 0)
|
||||
break;
|
||||
|
||||
child->Render();
|
||||
child = child->GetNext();
|
||||
}
|
||||
|
||||
if (visible_in_rt_)
|
||||
{
|
||||
PrepareToRender();
|
||||
OnRender();
|
||||
}
|
||||
|
||||
while (child)
|
||||
{
|
||||
child->Render();
|
||||
child = child->GetNext();
|
||||
}
|
||||
}
|
||||
}
|
||||
void Node::Clear()
|
||||
{
|
||||
}
|
||||
|
||||
void Node::OnPreRender()
|
||||
{
|
||||
}
|
||||
|
||||
void Node::OnRender()
|
||||
{
|
||||
}
|
||||
|
||||
void Node::OnUpdate(float deltaTime)
|
||||
{
|
||||
}
|
||||
|
||||
void Node::UpdateSelf(float deltaTime)
|
||||
{
|
||||
// 如果有回调函数,则调用回调函数
|
||||
if (cb_update_.size() > 0)
|
||||
if (!update_pausing_)
|
||||
{
|
||||
for (auto &cb : cb_update_)
|
||||
OnUpdate(deltaTime);
|
||||
|
||||
// 如果有回调函数,则调用回调函数
|
||||
if (cb_update_.size() > 0)
|
||||
{
|
||||
cb.second(deltaTime);
|
||||
for (auto &cb : cb_update_)
|
||||
{
|
||||
cb.second(deltaTime);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Node::PrepareToRender()
|
||||
{
|
||||
Game::GetInstance().GetRenderer()->SetMatrix(render_matrix_);
|
||||
Game::GetInstance().GetRenderer()->SetOpacity(displayed_opacity_);
|
||||
}
|
||||
|
||||
void Node::RemoveFromParent()
|
||||
{
|
||||
if (parent_)
|
||||
@@ -109,7 +208,7 @@ void Node::ShowBorder(bool show)
|
||||
show_border_ = show;
|
||||
}
|
||||
|
||||
Node::Node() : visible_(true), update_pausing_(false), show_border_(false), parent_(nullptr), anchor_(0.0f, 0.0f), z_order_(0), opacity_(1.f), name_("Node")
|
||||
Node::Node() : visible_(true), update_pausing_(false), show_border_(false), parent_(nullptr), anchor_(0.0f, 0.0f), z_order_(0), opacity_(1.f), name_("Node"), displayed_opacity_(1.f), cascade_opacity_(true)
|
||||
{
|
||||
Game::GetInstance().m_nodeCount++;
|
||||
}
|
||||
@@ -146,6 +245,8 @@ void Node::UpdateTransform() const
|
||||
transform_matrix_ *= parent_->transform_matrix_;
|
||||
}
|
||||
|
||||
GenerateRenderMatrix();
|
||||
|
||||
for (const auto &child : children_)
|
||||
child->dirty_flag_.Set(DirtyFlag::DirtyTransform);
|
||||
}
|
||||
@@ -165,6 +266,26 @@ void Node::UpdateTransformUpwards() const
|
||||
UpdateTransform();
|
||||
}
|
||||
|
||||
void Node::UpdateOpacity()
|
||||
{
|
||||
if (!dirty_flag_.Has(DirtyFlag::DirtyOpacity))
|
||||
return;
|
||||
|
||||
dirty_flag_.Unset(DirtyFlag::DirtyOpacity);
|
||||
|
||||
if (parent_ && parent_->IsCascadeOpacityEnabled())
|
||||
{
|
||||
displayed_opacity_ = opacity_ * parent_->displayed_opacity_;
|
||||
}
|
||||
else
|
||||
{
|
||||
displayed_opacity_ = opacity_;
|
||||
}
|
||||
|
||||
for (const auto &child : children_)
|
||||
child->dirty_flag_.Set(DirtyFlag::DirtyOpacity);
|
||||
}
|
||||
|
||||
const Matrix3x2 &Node::GetTransformMatrix() const
|
||||
{
|
||||
UpdateTransformUpwards();
|
||||
@@ -188,6 +309,20 @@ const Matrix3x2 &Node::GetTransformMatrixToParent() const
|
||||
return transform_matrix_to_parent_;
|
||||
}
|
||||
|
||||
void Node::GenerateRenderMatrix() const
|
||||
{
|
||||
// 构造OpenGl渲染矩阵
|
||||
render_matrix_ = glm::mat4(1.0f);
|
||||
// 填充旋转+缩放分量(2D 变换核心,对应 4x4 矩阵左上 2x2 区域)
|
||||
render_matrix_[0][0] = transform_matrix_[0]; // _11 → x轴缩放+旋转
|
||||
render_matrix_[0][1] = transform_matrix_[1]; // _12 → 影响y轴方向的旋转分量
|
||||
render_matrix_[1][0] = transform_matrix_[2]; // _21 → 影响x轴方向的旋转分量
|
||||
render_matrix_[1][1] = transform_matrix_[3]; // _22 → y轴缩放+旋转
|
||||
// 填充平移分量(对应 4x4 矩阵最后一行前两列,z轴平移为0)
|
||||
render_matrix_[3][0] = transform_matrix_[4]; // _31 → x方向平移(世界坐标)
|
||||
render_matrix_[3][1] = transform_matrix_[5]; // _32 → y方向平移(世界坐标)
|
||||
}
|
||||
|
||||
void Node::Reorder()
|
||||
{
|
||||
if (parent_)
|
||||
@@ -220,287 +355,24 @@ void Node::Reorder()
|
||||
}
|
||||
}
|
||||
|
||||
inline void Node::RemoveAllChildren()
|
||||
bool Node::CheckVisibility() const
|
||||
{
|
||||
RefPtr<Node> next;
|
||||
for (RefPtr<Node> child = children_.GetFirst(); child; child = next)
|
||||
if (dirty_flag_.Has(DirtyFlag::DirtyVisibility))
|
||||
{
|
||||
next = child->GetNext();
|
||||
RemoveChild(child);
|
||||
}
|
||||
}
|
||||
|
||||
inline bool Node::IsVisible() const
|
||||
{
|
||||
return visible_;
|
||||
}
|
||||
|
||||
inline int Node::GetZOrder() const
|
||||
{
|
||||
return z_order_;
|
||||
}
|
||||
|
||||
inline glm::vec2 Node::GetPosition() const
|
||||
{
|
||||
return transform_.position;
|
||||
}
|
||||
|
||||
inline float Node::GetPositionX() const
|
||||
{
|
||||
return GetPosition().x;
|
||||
}
|
||||
|
||||
inline float Node::GetPositionY() const
|
||||
{
|
||||
return GetPosition().y;
|
||||
}
|
||||
|
||||
inline VecSize Node::GetSize() const
|
||||
{
|
||||
return size_;
|
||||
}
|
||||
|
||||
inline float Node::GetWidth() const
|
||||
{
|
||||
return GetSize().width;
|
||||
}
|
||||
|
||||
inline float Node::GetHeight() const
|
||||
{
|
||||
return GetSize().height;
|
||||
}
|
||||
|
||||
inline float Node::GetScaledWidth() const
|
||||
{
|
||||
return GetWidth() * GetScaleX();
|
||||
}
|
||||
|
||||
inline float Node::GetScaledHeight() const
|
||||
{
|
||||
return GetHeight() * GetScaleY();
|
||||
}
|
||||
|
||||
inline VecSize Node::GetScaledSize() const
|
||||
{
|
||||
return VecSize{GetScaledWidth(), GetScaledHeight()};
|
||||
}
|
||||
|
||||
inline glm::vec2 Node::GetAnchor() const
|
||||
{
|
||||
return anchor_;
|
||||
}
|
||||
|
||||
inline float Node::GetAnchorX() const
|
||||
{
|
||||
return GetAnchor().x;
|
||||
}
|
||||
|
||||
inline float Node::GetAnchorY() const
|
||||
{
|
||||
return GetAnchor().y;
|
||||
}
|
||||
|
||||
inline float Node::GetOpacity() const
|
||||
{
|
||||
return opacity_;
|
||||
}
|
||||
|
||||
inline float Node::GetRotation() const
|
||||
{
|
||||
return transform_.rotation;
|
||||
}
|
||||
|
||||
inline glm::vec2 Node::GetScale() const
|
||||
{
|
||||
return transform_.scale;
|
||||
}
|
||||
|
||||
inline float Node::GetScaleX() const
|
||||
{
|
||||
return GetScale().x;
|
||||
}
|
||||
|
||||
inline float Node::GetScaleY() const
|
||||
{
|
||||
return GetScale().y;
|
||||
}
|
||||
|
||||
inline glm::vec2 Node::GetSkew() const
|
||||
{
|
||||
return transform_.skew;
|
||||
}
|
||||
|
||||
inline float Node::GetSkewX() const
|
||||
{
|
||||
return GetSkew().x;
|
||||
}
|
||||
|
||||
inline float Node::GetSkewY() const
|
||||
{
|
||||
return GetSkew().y;
|
||||
}
|
||||
|
||||
inline Y_Transform Node::GetTransform() const
|
||||
{
|
||||
return transform_;
|
||||
}
|
||||
|
||||
inline Node *Node::GetParent() const
|
||||
{
|
||||
return parent_;
|
||||
}
|
||||
|
||||
inline void Node::SetVisible(bool val)
|
||||
{
|
||||
visible_ = val;
|
||||
}
|
||||
|
||||
inline void Node::SetName(std::string name)
|
||||
{
|
||||
name_ = name;
|
||||
}
|
||||
|
||||
inline void Node::SetPosition(const glm::vec2 &pos)
|
||||
{
|
||||
if (transform_.position == pos)
|
||||
return;
|
||||
|
||||
transform_.position = pos;
|
||||
dirty_flag_.Set(DirtyFlag::DirtyTransform);
|
||||
}
|
||||
|
||||
inline void Node::SetPosition(float x, float y)
|
||||
{
|
||||
this->SetPosition(glm::vec2(x, y));
|
||||
}
|
||||
|
||||
inline void Node::SetPositionX(float x)
|
||||
{
|
||||
this->SetPosition(glm::vec2(x, GetPosition().y));
|
||||
}
|
||||
|
||||
inline void Node::SetPositionY(float y)
|
||||
{
|
||||
this->SetPosition(glm::vec2(GetPosition().x, y));
|
||||
}
|
||||
|
||||
inline void Node::MoveTo(const glm::vec2 &p)
|
||||
{
|
||||
this->SetPosition(p);
|
||||
}
|
||||
|
||||
inline void Node::MoveTo(float x, float y)
|
||||
{
|
||||
this->SetPosition(glm::vec2(x, y));
|
||||
}
|
||||
|
||||
inline void Node::MoveBy(const glm::vec2 &trans)
|
||||
{
|
||||
this->SetPosition(transform_.position.x + trans.x, transform_.position.y + trans.y);
|
||||
}
|
||||
|
||||
inline void Node::MoveBy(float trans_x, float trans_y)
|
||||
{
|
||||
this->MoveBy(glm::vec2(trans_x, trans_y));
|
||||
}
|
||||
|
||||
inline void Node::SetScale(const glm::vec2 &scale)
|
||||
{
|
||||
if (transform_.scale == scale)
|
||||
return;
|
||||
|
||||
transform_.scale = scale;
|
||||
dirty_flag_.Set(DirtyFlag::DirtyTransform);
|
||||
}
|
||||
|
||||
inline void Node::SetScale(float scalex, float scaley)
|
||||
{
|
||||
this->SetScale(glm::vec2(scalex, scaley));
|
||||
}
|
||||
|
||||
inline void Node::SetSkew(const glm::vec2 &skew)
|
||||
{
|
||||
if (transform_.skew == skew)
|
||||
return;
|
||||
|
||||
transform_.skew = skew;
|
||||
dirty_flag_.Set(DirtyFlag::DirtyTransform);
|
||||
}
|
||||
|
||||
inline void Node::SetSkew(float skewx, float skewy)
|
||||
{
|
||||
this->SetSkew(glm::vec2(skewx, skewy));
|
||||
}
|
||||
|
||||
inline void Node::SetRotation(float rotation)
|
||||
{
|
||||
if (transform_.rotation == rotation)
|
||||
return;
|
||||
|
||||
transform_.rotation = rotation;
|
||||
dirty_flag_.Set(DirtyFlag::DirtyTransform);
|
||||
}
|
||||
|
||||
inline void Node::SetAnchor(const glm::vec2 &anchor)
|
||||
{
|
||||
if (anchor_ == anchor)
|
||||
return;
|
||||
|
||||
anchor_ = anchor;
|
||||
dirty_flag_.Set(DirtyFlag::DirtyTransform);
|
||||
}
|
||||
|
||||
inline void Node::SetAnchor(float anchorx, float anchory)
|
||||
{
|
||||
this->SetAnchor(glm::vec2(anchorx, anchory));
|
||||
}
|
||||
|
||||
inline void Node::SetSize(const VecSize &size)
|
||||
{
|
||||
if (size_ == size)
|
||||
return;
|
||||
|
||||
size_ = size;
|
||||
dirty_flag_.Set(DirtyFlag::DirtyTransform);
|
||||
}
|
||||
|
||||
inline void Node::SetSize(float width, float height)
|
||||
{
|
||||
this->SetSize(VecSize{width, height});
|
||||
}
|
||||
|
||||
inline void Node::SetWidth(float width)
|
||||
{
|
||||
this->SetSize(width, GetHeight());
|
||||
}
|
||||
|
||||
inline void Node::SetHeight(float height)
|
||||
{
|
||||
this->SetSize(GetWidth(), height);
|
||||
}
|
||||
|
||||
inline void Node::SetOpacity(float opacity)
|
||||
{
|
||||
if (opacity_ == opacity)
|
||||
return;
|
||||
|
||||
opacity_ = std::min(std::max(opacity, 0.f), 1.f);
|
||||
dirty_flag_.Set(DirtyFlag::DirtyOpacity);
|
||||
}
|
||||
|
||||
inline void Node::SetTransform(const Y_Transform &transform)
|
||||
{
|
||||
transform_ = transform;
|
||||
dirty_flag_.Set(DirtyFlag::DirtyTransform);
|
||||
}
|
||||
|
||||
inline void Node::SetZOrder(int zorder)
|
||||
{
|
||||
if (z_order_ != zorder)
|
||||
{
|
||||
z_order_ = zorder;
|
||||
Reorder();
|
||||
dirty_flag_.Unset(DirtyFlag::DirtyVisibility);
|
||||
|
||||
if (size_.width == 0.f && size_.height == 0.f)
|
||||
{
|
||||
visible_in_rt_ = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
// TODO
|
||||
// visible_in_rt_ = ctx.CheckVisibility(GetBounds(), transform_matrix_ /* GetTransformMatrix() */);
|
||||
visible_in_rt_ = true;
|
||||
}
|
||||
}
|
||||
return visible_in_rt_;
|
||||
}
|
||||
|
||||
void Node::AddChild(RefPtr<Node> child)
|
||||
@@ -532,7 +404,7 @@ const Node::NodeList &Node::GetAllChildren() const
|
||||
return children_;
|
||||
}
|
||||
|
||||
inline void Node::RemoveChild(RefPtr<Node> child)
|
||||
void Node::RemoveChild(RefPtr<Node> child)
|
||||
{
|
||||
if (children_.IsEmpty())
|
||||
return;
|
||||
|
||||
@@ -16,13 +16,19 @@ public:
|
||||
using IntrusiveListValue<RefPtr<Node>>::GetNext;
|
||||
using IntrusiveListValue<RefPtr<Node>>::GetPrev;
|
||||
|
||||
private:
|
||||
protected:
|
||||
/**渲染矩阵 */
|
||||
mutable glm::mat4 render_matrix_;
|
||||
/**变换属性 */
|
||||
Y_Transform transform_;
|
||||
/**变换矩阵 */
|
||||
mutable Matrix3x2 transform_matrix_;
|
||||
mutable Matrix3x2 transform_matrix_inverse_;
|
||||
mutable Matrix3x2 transform_matrix_to_parent_;
|
||||
|
||||
private:
|
||||
|
||||
|
||||
/**更新时的回调函数 */
|
||||
std::map<std::string, UpdateCallback> cb_update_;
|
||||
/**锚点 */
|
||||
@@ -33,6 +39,8 @@ private:
|
||||
VecSize size_;
|
||||
/**透明度 */
|
||||
float opacity_;
|
||||
/**显示透明度 */
|
||||
float displayed_opacity_;
|
||||
|
||||
/**是否可见 */
|
||||
bool visible_;
|
||||
@@ -40,6 +48,10 @@ private:
|
||||
bool update_pausing_;
|
||||
/**是否显示边界 */
|
||||
bool show_border_;
|
||||
/**是否在渲染区域中 */
|
||||
mutable bool visible_in_rt_;
|
||||
/**联级透明度 */
|
||||
bool cascade_opacity_;
|
||||
/**名称 */
|
||||
std::string name_;
|
||||
|
||||
@@ -58,14 +70,22 @@ public:
|
||||
virtual void Render();
|
||||
virtual void Clear();
|
||||
|
||||
void UpdateSelf(float dt);
|
||||
virtual void OnPreRender();
|
||||
virtual void OnRender();
|
||||
virtual void OnUpdate(float deltaTime);
|
||||
|
||||
void UpdateSelf(float dt);
|
||||
void PrepareToRender();
|
||||
|
||||
public:
|
||||
/// \~chinese
|
||||
/// @brief 获取显示状态
|
||||
bool IsVisible() const;
|
||||
|
||||
/// \~chinese
|
||||
/// @brief 是否启用级联透明度
|
||||
bool IsCascadeOpacityEnabled() const;
|
||||
|
||||
/// \~chinese
|
||||
/// @brief 获取 Z 轴顺序
|
||||
int GetZOrder() const;
|
||||
@@ -160,12 +180,18 @@ public:
|
||||
|
||||
/// \~chinese
|
||||
/// @brief 设置角色是否可见
|
||||
void SetVisible(bool val);
|
||||
virtual void SetVisible(bool val);
|
||||
|
||||
virtual void OnSetPosition();
|
||||
|
||||
/// \~chinese
|
||||
/// @brief 设置名称
|
||||
void SetName(std::string name);
|
||||
|
||||
/// \~chinese
|
||||
/// @brief 获取名称
|
||||
std::string GetName() const;
|
||||
|
||||
/// \~chinese
|
||||
/// @brief 设置坐标
|
||||
void SetPosition(const glm::vec2 &pos);
|
||||
@@ -246,6 +272,10 @@ public:
|
||||
/// @brief 设置透明度,默认为 1.0, 范围 [0, 1]
|
||||
void SetOpacity(float opacity);
|
||||
|
||||
/// \~chinese
|
||||
/// @brief 启用或禁用级联透明度
|
||||
void SetCascadeOpacityEnabled(bool enabled);
|
||||
|
||||
/// \~chinese
|
||||
/// @brief 设置二维仿射变换
|
||||
void SetTransform(const Y_Transform &transform);
|
||||
@@ -328,6 +358,10 @@ public:
|
||||
/// @details 对于节点树 A->B(dirty)->C->D,当对 D 执行 UpdateTransformUpwards 时会对 B、C、D 从上到下依次更新
|
||||
void UpdateTransformUpwards() const;
|
||||
|
||||
/// \~chinese
|
||||
/// @brief 更新自己和所有子角色的透明度
|
||||
void UpdateOpacity();
|
||||
|
||||
/// \~chinese
|
||||
/// @brief 获取二维变换矩阵
|
||||
const Matrix3x2 &GetTransformMatrix() const;
|
||||
@@ -340,10 +374,18 @@ public:
|
||||
/// @brief 获取变换到父角色的二维变换矩阵
|
||||
const Matrix3x2 &GetTransformMatrixToParent() const;
|
||||
|
||||
/// \~chinese
|
||||
/// @brief 生成用于OpenGL渲染的矩阵
|
||||
virtual void GenerateRenderMatrix() const;
|
||||
|
||||
/// \~chinese
|
||||
/// @brief 将所有子角色按Z轴顺序排序
|
||||
void Reorder();
|
||||
|
||||
/// \~chinese
|
||||
/// @brief 检查是否在渲染上下文的视区内
|
||||
virtual bool CheckVisibility() const;
|
||||
|
||||
enum DirtyFlag : uint8_t
|
||||
{
|
||||
Clean = 0,
|
||||
@@ -353,3 +395,310 @@ public:
|
||||
DirtyVisibility = 1 << 3
|
||||
};
|
||||
};
|
||||
|
||||
inline void Node::RemoveAllChildren()
|
||||
{
|
||||
RefPtr<Node> next;
|
||||
for (RefPtr<Node> child = children_.GetFirst(); child; child = next)
|
||||
{
|
||||
next = child->GetNext();
|
||||
RemoveChild(child);
|
||||
}
|
||||
}
|
||||
|
||||
inline bool Node::IsVisible() const
|
||||
{
|
||||
return visible_;
|
||||
}
|
||||
|
||||
inline bool Node::IsCascadeOpacityEnabled() const
|
||||
{
|
||||
return cascade_opacity_;
|
||||
}
|
||||
|
||||
inline int Node::GetZOrder() const
|
||||
{
|
||||
return z_order_;
|
||||
}
|
||||
|
||||
inline glm::vec2 Node::GetPosition() const
|
||||
{
|
||||
return transform_.position;
|
||||
}
|
||||
|
||||
inline float Node::GetPositionX() const
|
||||
{
|
||||
return GetPosition().x;
|
||||
}
|
||||
|
||||
inline float Node::GetPositionY() const
|
||||
{
|
||||
return GetPosition().y;
|
||||
}
|
||||
|
||||
inline VecSize Node::GetSize() const
|
||||
{
|
||||
return size_;
|
||||
}
|
||||
|
||||
inline float Node::GetWidth() const
|
||||
{
|
||||
return GetSize().width;
|
||||
}
|
||||
|
||||
inline float Node::GetHeight() const
|
||||
{
|
||||
return GetSize().height;
|
||||
}
|
||||
|
||||
inline float Node::GetScaledWidth() const
|
||||
{
|
||||
return GetWidth() * GetScaleX();
|
||||
}
|
||||
|
||||
inline float Node::GetScaledHeight() const
|
||||
{
|
||||
return GetHeight() * GetScaleY();
|
||||
}
|
||||
|
||||
inline VecSize Node::GetScaledSize() const
|
||||
{
|
||||
return VecSize{GetScaledWidth(), GetScaledHeight()};
|
||||
}
|
||||
|
||||
inline glm::vec2 Node::GetAnchor() const
|
||||
{
|
||||
return anchor_;
|
||||
}
|
||||
|
||||
inline float Node::GetAnchorX() const
|
||||
{
|
||||
return GetAnchor().x;
|
||||
}
|
||||
|
||||
inline float Node::GetAnchorY() const
|
||||
{
|
||||
return GetAnchor().y;
|
||||
}
|
||||
|
||||
inline float Node::GetOpacity() const
|
||||
{
|
||||
return opacity_;
|
||||
}
|
||||
|
||||
inline float Node::GetRotation() const
|
||||
{
|
||||
return transform_.rotation;
|
||||
}
|
||||
|
||||
inline glm::vec2 Node::GetScale() const
|
||||
{
|
||||
return transform_.scale;
|
||||
}
|
||||
|
||||
inline float Node::GetScaleX() const
|
||||
{
|
||||
return GetScale().x;
|
||||
}
|
||||
|
||||
inline float Node::GetScaleY() const
|
||||
{
|
||||
return GetScale().y;
|
||||
}
|
||||
|
||||
inline glm::vec2 Node::GetSkew() const
|
||||
{
|
||||
return transform_.skew;
|
||||
}
|
||||
|
||||
inline float Node::GetSkewX() const
|
||||
{
|
||||
return GetSkew().x;
|
||||
}
|
||||
|
||||
inline float Node::GetSkewY() const
|
||||
{
|
||||
return GetSkew().y;
|
||||
}
|
||||
|
||||
inline Y_Transform Node::GetTransform() const
|
||||
{
|
||||
return transform_;
|
||||
}
|
||||
|
||||
inline Node *Node::GetParent() const
|
||||
{
|
||||
return parent_;
|
||||
}
|
||||
|
||||
inline void Node::SetVisible(bool val)
|
||||
{
|
||||
visible_ = val;
|
||||
}
|
||||
|
||||
inline void Node::OnSetPosition()
|
||||
{
|
||||
}
|
||||
|
||||
inline void Node::SetName(std::string name)
|
||||
{
|
||||
name_ = name;
|
||||
}
|
||||
|
||||
inline std::string Node::GetName() const
|
||||
{
|
||||
return name_;
|
||||
}
|
||||
|
||||
inline void Node::SetPosition(const glm::vec2 &pos)
|
||||
{
|
||||
if (transform_.position == pos)
|
||||
return;
|
||||
|
||||
transform_.position = pos;
|
||||
dirty_flag_.Set(DirtyFlag::DirtyTransform);
|
||||
OnSetPosition();
|
||||
}
|
||||
|
||||
inline void Node::SetPosition(float x, float y)
|
||||
{
|
||||
this->SetPosition(glm::vec2(x, y));
|
||||
}
|
||||
|
||||
inline void Node::SetPositionX(float x)
|
||||
{
|
||||
this->SetPosition(glm::vec2(x, GetPosition().y));
|
||||
}
|
||||
|
||||
inline void Node::SetPositionY(float y)
|
||||
{
|
||||
this->SetPosition(glm::vec2(GetPosition().x, y));
|
||||
}
|
||||
|
||||
inline void Node::MoveTo(const glm::vec2 &p)
|
||||
{
|
||||
this->SetPosition(p);
|
||||
}
|
||||
|
||||
inline void Node::MoveTo(float x, float y)
|
||||
{
|
||||
this->SetPosition(glm::vec2(x, y));
|
||||
}
|
||||
|
||||
inline void Node::MoveBy(const glm::vec2 &trans)
|
||||
{
|
||||
this->SetPosition(transform_.position.x + trans.x, transform_.position.y + trans.y);
|
||||
}
|
||||
|
||||
inline void Node::MoveBy(float trans_x, float trans_y)
|
||||
{
|
||||
this->MoveBy(glm::vec2(trans_x, trans_y));
|
||||
}
|
||||
|
||||
inline void Node::SetScale(const glm::vec2 &scale)
|
||||
{
|
||||
if (transform_.scale == scale)
|
||||
return;
|
||||
|
||||
transform_.scale = scale;
|
||||
dirty_flag_.Set(DirtyFlag::DirtyTransform);
|
||||
}
|
||||
|
||||
inline void Node::SetScale(float scalex, float scaley)
|
||||
{
|
||||
this->SetScale(glm::vec2(scalex, scaley));
|
||||
}
|
||||
|
||||
inline void Node::SetSkew(const glm::vec2 &skew)
|
||||
{
|
||||
if (transform_.skew == skew)
|
||||
return;
|
||||
|
||||
transform_.skew = skew;
|
||||
dirty_flag_.Set(DirtyFlag::DirtyTransform);
|
||||
}
|
||||
|
||||
inline void Node::SetSkew(float skewx, float skewy)
|
||||
{
|
||||
this->SetSkew(glm::vec2(skewx, skewy));
|
||||
}
|
||||
|
||||
inline void Node::SetRotation(float rotation)
|
||||
{
|
||||
if (transform_.rotation == rotation)
|
||||
return;
|
||||
|
||||
transform_.rotation = rotation;
|
||||
dirty_flag_.Set(DirtyFlag::DirtyTransform);
|
||||
}
|
||||
|
||||
inline void Node::SetAnchor(const glm::vec2 &anchor)
|
||||
{
|
||||
if (anchor_ == anchor)
|
||||
return;
|
||||
|
||||
anchor_ = anchor;
|
||||
dirty_flag_.Set(DirtyFlag::DirtyTransform);
|
||||
}
|
||||
|
||||
inline void Node::SetAnchor(float anchorx, float anchory)
|
||||
{
|
||||
this->SetAnchor(glm::vec2(anchorx, anchory));
|
||||
}
|
||||
|
||||
inline void Node::SetSize(const VecSize &size)
|
||||
{
|
||||
if (size_ == size)
|
||||
return;
|
||||
|
||||
size_ = size;
|
||||
dirty_flag_.Set(DirtyFlag::DirtyTransform);
|
||||
}
|
||||
|
||||
inline void Node::SetSize(float width, float height)
|
||||
{
|
||||
this->SetSize(VecSize{width, height});
|
||||
}
|
||||
|
||||
inline void Node::SetWidth(float width)
|
||||
{
|
||||
this->SetSize(width, GetHeight());
|
||||
}
|
||||
|
||||
inline void Node::SetHeight(float height)
|
||||
{
|
||||
this->SetSize(GetWidth(), height);
|
||||
}
|
||||
|
||||
inline void Node::SetOpacity(float opacity)
|
||||
{
|
||||
if (opacity_ == opacity)
|
||||
return;
|
||||
|
||||
opacity_ = std::min(std::max(opacity, 0.f), 1.f);
|
||||
dirty_flag_.Set(DirtyFlag::DirtyOpacity);
|
||||
}
|
||||
|
||||
inline void Node::SetCascadeOpacityEnabled(bool enabled)
|
||||
{
|
||||
if (cascade_opacity_ == enabled)
|
||||
return;
|
||||
|
||||
cascade_opacity_ = enabled;
|
||||
dirty_flag_.Set(DirtyFlag::DirtyOpacity);
|
||||
}
|
||||
|
||||
inline void Node::SetTransform(const Y_Transform &transform)
|
||||
{
|
||||
transform_ = transform;
|
||||
dirty_flag_.Set(DirtyFlag::DirtyTransform);
|
||||
}
|
||||
|
||||
inline void Node::SetZOrder(int zorder)
|
||||
{
|
||||
if (z_order_ != zorder)
|
||||
{
|
||||
z_order_ = zorder;
|
||||
Reorder();
|
||||
}
|
||||
}
|
||||
@@ -27,8 +27,6 @@ Animation::~Animation()
|
||||
|
||||
void Animation::Init(std::string AniPath)
|
||||
{
|
||||
Actor::Init();
|
||||
|
||||
AniScriptParser::AniInfo Info = AssetManager::GetInstance().StructAniInfo(AniPath);
|
||||
this->AniPath = AniPath;
|
||||
this->AnimationFlag = Info.Flag;
|
||||
@@ -45,17 +43,19 @@ void Animation::Init(std::string AniPath)
|
||||
FrameObj.Img_Path = AdditionalOptions(FrameObj.Img_Path, this->AdditionalOptionsData);
|
||||
}
|
||||
SpriteObj = new Sprite(FrameObj.Img_Path, FrameObj.Img_Index);
|
||||
SpriteObj->SetAnchor(Vec2(0.5f, 0.5f));
|
||||
SpriteObj->SetPos(Vec2(FrameObj.Img_Pos.x, FrameObj.Img_Pos.y));
|
||||
SpriteObj->SetPosition(FrameObj.Img_Pos.x, FrameObj.Img_Pos.y);
|
||||
SpriteObj->SetVisible(false);
|
||||
}
|
||||
else
|
||||
{
|
||||
SpriteObj = new Sprite();
|
||||
SpriteObj->SetVisible(false);
|
||||
SDL_Log("Animation::Init() SpriteObj is nullptr");
|
||||
}
|
||||
SpriteArr.push_back(SpriteObj);
|
||||
//记录最大帧宽高
|
||||
auto SpriteSize = SpriteObj->GetSize();
|
||||
if(this->MaxSize.width < SpriteSize.width)this->MaxSize.width = SpriteSize.width;
|
||||
if(this->MaxSize.height < SpriteSize.height)this->MaxSize.height = SpriteSize.height;
|
||||
AddChild(SpriteObj);
|
||||
}
|
||||
|
||||
@@ -76,7 +76,7 @@ void Animation::Init(std::string AniPath)
|
||||
for (auto &Ani : Info.AniList)
|
||||
{
|
||||
RefPtr<Animation> AlsAniObj = new Animation(Dir + Ani.second.path);
|
||||
AlsAniObj->SetRenderZOrder(Ani.second.layer[1]);
|
||||
AlsAniObj->SetZOrder(Ani.second.layer[1]);
|
||||
AddChild(AlsAniObj);
|
||||
}
|
||||
}
|
||||
@@ -84,20 +84,16 @@ void Animation::Init(std::string AniPath)
|
||||
FlushFrame(0);
|
||||
}
|
||||
|
||||
void Animation::HandleEvents(SDL_Event *e)
|
||||
{
|
||||
}
|
||||
|
||||
void Animation::Update(float deltaTime)
|
||||
void Animation::OnUpdate(float deltaTime)
|
||||
{
|
||||
// 可用性检查
|
||||
if (IsUsability && Visible)
|
||||
if (IsUsability && IsVisible())
|
||||
{
|
||||
float dt_ms = deltaTime * 1000.0f;
|
||||
// 累加当前帧时间
|
||||
CurrentIndexT += dt_ms;
|
||||
// 插值模式判断
|
||||
InterpolationLogic();
|
||||
// InterpolationLogic();
|
||||
|
||||
// 循环处理:只要当前时间超过帧延迟,就切换帧(支持一次跳过多帧)
|
||||
while (CurrentIndexT >= NextFrameDelay)
|
||||
@@ -127,32 +123,26 @@ void Animation::Update(float deltaTime)
|
||||
}
|
||||
}
|
||||
}
|
||||
Actor::Update(deltaTime);
|
||||
}
|
||||
else
|
||||
{
|
||||
for (auto &Sp : SpriteArr)
|
||||
{
|
||||
Sp->SetVisible(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Animation::Render()
|
||||
void Animation::OnAdded(Actor *node)
|
||||
{
|
||||
Actor::Render();
|
||||
}
|
||||
|
||||
void Animation::OnAdded(BaseNode *node)
|
||||
{
|
||||
Actor::OnAdded(node);
|
||||
FlushFrame(0);
|
||||
}
|
||||
|
||||
void Animation::Clear()
|
||||
{
|
||||
}
|
||||
|
||||
void Animation::SetVisible(bool visible)
|
||||
{
|
||||
// 设置启用的时候要更新一下当前帧的渲染信息 避免因为延迟造成的坐标闪烁
|
||||
if (visible)
|
||||
{
|
||||
CurrentFrame->CalcRenderInfoLogic();
|
||||
CurrentFrame->CalcRenderInfo();
|
||||
CurrentFrame->SetVisible(true);
|
||||
}
|
||||
Actor::SetVisible(visible);
|
||||
}
|
||||
@@ -192,7 +182,8 @@ void Animation::FlushFrame(int Index)
|
||||
if (FlagBuf.count("IMAGE_RATE"))
|
||||
{
|
||||
Vec2 Rate = std::get<Vec2>(FlagBuf["IMAGE_RATE"]);
|
||||
CurrentFrame->SetScale(Vec2{Rate.x, Rate.y});
|
||||
CurrentFrame->SetScale(Rate.x, Rate.y);
|
||||
CurrentFrame->SetPosition(FrameInfo.Img_Pos.x * Rate.x, FrameInfo.Img_Pos.y * Rate.y);
|
||||
}
|
||||
// 线性减淡
|
||||
if (FlagBuf.count("GRAPHIC_EFFECT_LINEARDODGE"))
|
||||
@@ -202,7 +193,7 @@ void Animation::FlushFrame(int Index)
|
||||
// 旋转
|
||||
if (FlagBuf.count("IMAGE_ROTATE"))
|
||||
{
|
||||
CurrentFrame->SetAnchor(Vec2{0.5f, 0.5f});
|
||||
// CurrentFrame->SetAnchor(0.5f, 0.5f);
|
||||
CurrentFrame->SetRotation(std::get<float>(FlagBuf["IMAGE_ROTATE"]));
|
||||
}
|
||||
// 染色
|
||||
@@ -277,10 +268,10 @@ void Animation::InterpolationLogic()
|
||||
}
|
||||
// 坐标
|
||||
{
|
||||
Vec2 PosData = {
|
||||
glm::vec2 PosData = {
|
||||
(OldData.Img_Pos.x + (NewData.Img_Pos.x - OldData.Img_Pos.x) * InterRate),
|
||||
(OldData.Img_Pos.y + (NewData.Img_Pos.y - OldData.Img_Pos.y) * InterRate)};
|
||||
CurrentFrame->SetPos(PosData);
|
||||
CurrentFrame->SetPosition(PosData);
|
||||
}
|
||||
// 缩放
|
||||
{
|
||||
@@ -294,10 +285,9 @@ void Animation::InterpolationLogic()
|
||||
{
|
||||
NewRateData = std::get<Vec2>(NewData.Flag["IMAGE_RATE"]);
|
||||
}
|
||||
Vec2 RateData = {
|
||||
glm::vec2 RateData = {
|
||||
OldRateData.x + (NewRateData.x - OldRateData.x) * InterRate,
|
||||
OldRateData.y + (NewRateData.y - OldRateData.y) * InterRate};
|
||||
CurrentFrame->SetAnchor(Vec2{0.5f, 0.5f});
|
||||
CurrentFrame->SetScale(RateData);
|
||||
}
|
||||
// 旋转
|
||||
@@ -315,3 +305,8 @@ void Animation::InterpolationLogic()
|
||||
CurrentFrame->SetRotation(OldAngleData + (NewAngleData - OldAngleData) * InterRate);
|
||||
}
|
||||
}
|
||||
|
||||
VecSize Animation::GetMaxSize()
|
||||
{
|
||||
return MaxSize;
|
||||
}
|
||||
|
||||
@@ -24,24 +24,26 @@ public:
|
||||
Animation(std::string AniPath, std::function<std::string(std::string, Animation::ReplaceData)> AdditionalOptions, Animation::ReplaceData);
|
||||
~Animation();
|
||||
|
||||
/**初始化 */
|
||||
void Init(std::string AniPath);
|
||||
void HandleEvents(SDL_Event *e) override;
|
||||
void Update(float deltaTime) override;
|
||||
void Render() override;
|
||||
void OnAdded(BaseNode *node) override;
|
||||
void Clear() override;
|
||||
|
||||
void OnUpdate(float deltaTime) override;
|
||||
void OnAdded(Actor *node) override;
|
||||
void SetVisible(bool visible) override;
|
||||
|
||||
public:
|
||||
/**刷新帧 */
|
||||
void FlushFrame(int Index);
|
||||
/**重置Ani */
|
||||
void Reset();
|
||||
/**获取当前帧信息 */
|
||||
AniScriptParser::AniFrame GetCurrentFrameInfo();
|
||||
// 设置帧索引
|
||||
void SetFrameIndex(int Index);
|
||||
void InterpolationLogic();
|
||||
// TODO SetOutline
|
||||
// TODO SetDye
|
||||
// TODO SetCrop
|
||||
|
||||
/**获取最大尺寸 */
|
||||
VecSize GetMaxSize();
|
||||
|
||||
public:
|
||||
// Ani是否可用
|
||||
@@ -76,20 +78,11 @@ public:
|
||||
std::string AniPath;
|
||||
// 是否描边
|
||||
bool IsOutline = false;
|
||||
// // 描边颜色
|
||||
// OutlineColor = null;
|
||||
// // 描边对象List
|
||||
// OutlineList = null;
|
||||
// // 当前描边对象
|
||||
// CurrentOutline = null;
|
||||
// // 染色颜色
|
||||
// DyeColor = null;
|
||||
// // 染色帧List
|
||||
// DyeFrameList = null;
|
||||
// // 整体染色
|
||||
// DyeAllFlag = false;
|
||||
|
||||
// 附加选项
|
||||
std::function<std::string(std::string, Animation::ReplaceData)> AdditionalOptions;
|
||||
Animation::ReplaceData AdditionalOptionsData;
|
||||
/**最大尺寸
|
||||
* 所有子精灵最大的尺寸
|
||||
*/
|
||||
VecSize MaxSize = {0, 0};
|
||||
};
|
||||
|
||||
@@ -1,72 +0,0 @@
|
||||
#include "AnimationManager.h"
|
||||
#include "EngineCore/Game.h"
|
||||
|
||||
AnimationManager::AnimationManager()
|
||||
{
|
||||
}
|
||||
|
||||
void AnimationManager::PreRender()
|
||||
{
|
||||
Actor::PreRender();
|
||||
// 标记是否是第一个子对象(用于初始化min/max值)
|
||||
bool isFirst = true;
|
||||
float minX = 0.0f, minY = 0.0f; // 所有子对象的最小X、Y坐标
|
||||
float maxX = 0.0f, maxY = 0.0f; // 所有子对象的最大X、Y坐标(右下角)
|
||||
|
||||
RefPtr<BaseNode> child = m_BaseNodes.GetFirst();
|
||||
while (child)
|
||||
{
|
||||
// 转换为Animation对象
|
||||
Animation *ani = static_cast<Animation *>(child.Get());
|
||||
|
||||
float width = ani->CurrentFrame->_RenderGuidanceInfo.rect.w;
|
||||
float height = ani->CurrentFrame->_RenderGuidanceInfo.rect.h;
|
||||
float x = ani->CurrentFrame->_RenderGuidanceInfo.rect.x;
|
||||
float y = ani->CurrentFrame->_RenderGuidanceInfo.rect.y;
|
||||
|
||||
// 计算子对象的右下角坐标(左上角 + 宽高)
|
||||
float currentMaxX = x + width;
|
||||
float currentMaxY = y + height;
|
||||
// 初始化或更新min/max值
|
||||
if (isFirst)
|
||||
{
|
||||
minX = x;
|
||||
minY = y;
|
||||
maxX = currentMaxX;
|
||||
maxY = currentMaxY;
|
||||
isFirst = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
minX = std::min(minX, x);
|
||||
minY = std::min(minY, y);
|
||||
maxX = std::max(maxX, currentMaxX);
|
||||
maxY = std::max(maxY, currentMaxY);
|
||||
}
|
||||
|
||||
child = child->GetNext();
|
||||
}
|
||||
// 计算最终的包围盒矩形
|
||||
if (!isFirst) // 至少有一个子对象时才计算
|
||||
{
|
||||
m_RenderRect.x = minX; // 矩形左上角X
|
||||
m_RenderRect.y = minY; // 矩形左上角Y
|
||||
m_RenderRect.w = maxX - minX; // 矩形宽度(右下角X - 左上角X)
|
||||
m_RenderRect.h = maxY - minY; // 矩形高度(右下角Y - 左上角Y)
|
||||
}
|
||||
}
|
||||
|
||||
void AnimationManager::Render()
|
||||
{
|
||||
Actor::Render();
|
||||
}
|
||||
|
||||
void AnimationManager::AddAnimation(RefPtr<Animation> ani)
|
||||
{
|
||||
this->AddChild(ani);
|
||||
}
|
||||
|
||||
void AnimationManager::Clear()
|
||||
{
|
||||
this->RemoveAllChild();
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
#pragma once
|
||||
#include "EngineFrame/Component/Animation.h"
|
||||
#include <vector>
|
||||
class AnimationManager : public Actor
|
||||
{
|
||||
private:
|
||||
SDL_Texture *m_Texture = nullptr;
|
||||
SDL_FRect m_RenderRect = {0, 0, 0, 0};
|
||||
|
||||
public:
|
||||
AnimationManager();
|
||||
// void Update(float deltaTime) override;
|
||||
void PreRender() override;
|
||||
void Render() override;
|
||||
|
||||
public:
|
||||
// 添加Ani
|
||||
void AddAnimation(RefPtr<Animation> ani);
|
||||
// 清空Ani
|
||||
void Clear();
|
||||
};
|
||||
132
source/EngineFrame/Component/AnimationMap.cpp
Normal file
132
source/EngineFrame/Component/AnimationMap.cpp
Normal file
@@ -0,0 +1,132 @@
|
||||
#include "AnimationMap.h"
|
||||
#include "EngineCore/Game.h"
|
||||
#include "EngineFrame/Render/RenderManager.h"
|
||||
AnimationMap::~AnimationMap()
|
||||
{
|
||||
}
|
||||
|
||||
void AnimationMap::CompleteConstruction()
|
||||
{
|
||||
m_size = {800, 600};
|
||||
// 遍历自己的所持有的所有Ani 取最大的尺寸
|
||||
auto &ac = this->GetAllChildren();
|
||||
RefPtr<Node> child = ac.GetFirst();
|
||||
while (child)
|
||||
{
|
||||
Animation *anim = static_cast<Animation *>(child.Get());
|
||||
auto size = anim->GetMaxSize();
|
||||
if (size.width > m_size.width)
|
||||
m_size.width = size.width;
|
||||
if (size.height > m_size.height)
|
||||
m_size.height = size.height;
|
||||
child = child->GetNext();
|
||||
}
|
||||
|
||||
SetSize(m_size);
|
||||
// 创建指定大小的纹理
|
||||
m_texture = new Texture();
|
||||
m_texture->Init(m_size);
|
||||
|
||||
// 构造FBO
|
||||
glGenFramebuffers(1, &m_fbo);
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, m_fbo);
|
||||
// 将目标纹理附加到FBO的颜色附着点
|
||||
glFramebufferTexture2D(
|
||||
GL_FRAMEBUFFER, // 帧缓冲类型
|
||||
GL_COLOR_ATTACHMENT0, // 颜色附着点(可多个,这里用第一个)
|
||||
GL_TEXTURE_2D, // 纹理类型
|
||||
m_texture->getID(), // 目标纹理ID
|
||||
0 // 多级渐远纹理级别
|
||||
);
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, 0); // 解绑FBO
|
||||
}
|
||||
|
||||
void AnimationMap::AddAnimation(RefPtr<Animation> animation)
|
||||
{
|
||||
this->AddChild(animation);
|
||||
}
|
||||
|
||||
void AnimationMap::Reset()
|
||||
{
|
||||
auto &ac = this->GetAllChildren();
|
||||
RefPtr<Node> child = ac.GetFirst();
|
||||
while (child)
|
||||
{
|
||||
Animation *anim = static_cast<Animation *>(child.Get());
|
||||
anim->Reset();
|
||||
child = child->GetNext();
|
||||
}
|
||||
}
|
||||
|
||||
void AnimationMap::Render()
|
||||
{
|
||||
Actor::PreRender();
|
||||
Actor::PrepareToRender();
|
||||
Actor::OnRender();
|
||||
}
|
||||
|
||||
void AnimationMap::OnRender()
|
||||
{
|
||||
RenderManager *renderer = Game::GetInstance().GetRenderer();
|
||||
renderer->DrawTexture(m_texture);
|
||||
}
|
||||
|
||||
void AnimationMap::OnUpdate(float deltaTime)
|
||||
{
|
||||
Actor::OnUpdate(deltaTime);
|
||||
|
||||
RenderManager *renderer = Game::GetInstance().GetRenderer();
|
||||
// 绑定FBO
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, m_fbo);
|
||||
// 清屏
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
// 保存原始的正交矩阵 设置纹理对应的正交矩阵 和 视口
|
||||
Oom = renderer->GetOrthoMatrix();
|
||||
renderer->SetOrthoMatrix(glm::ortho(0.0f, (float)m_size.width, 0.0f, (float)m_size.height, -1.0f, 1.0f));
|
||||
Oviewport = renderer->GetViewport();
|
||||
renderer->SetViewport({0, 0, m_size.width, m_size.height});
|
||||
// 以上步骤将渲染目标调整至AniMap的纹理上
|
||||
|
||||
SDL_Rect buf;
|
||||
buf.x = 0;
|
||||
buf.y = 0;
|
||||
buf.w = m_size.width;
|
||||
buf.h = m_size.height;
|
||||
glm::vec4 red = {0.0f, 0.0f, 1.0f, 0.4f}; // RGBA:红色,不透明基础色
|
||||
Game::GetInstance().GetRenderer()->DrawRect(&buf, red);
|
||||
|
||||
auto &ac = this->GetAllChildren();
|
||||
RefPtr<Node> child = ac.GetFirst();
|
||||
while (child)
|
||||
{
|
||||
child->Render();
|
||||
child = child->GetNext();
|
||||
}
|
||||
|
||||
// 渲染完毕后,将渲染目标恢复为默认
|
||||
// 解绑FBO
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||
// 恢复原始的正交矩阵 和 视口
|
||||
renderer->SetOrthoMatrix(Oom);
|
||||
renderer->SetViewport(Oviewport);
|
||||
|
||||
Actor::OnUpdate(deltaTime);
|
||||
}
|
||||
|
||||
// void AnimationMap::SetVisible(bool visible)
|
||||
// {
|
||||
// if (this->IsVisible() == visible)
|
||||
// return;
|
||||
// Actor::SetVisible(visible);
|
||||
// auto ac = this->GetAllChildren();
|
||||
// RefPtr<Node> next;
|
||||
// for (RefPtr<Node> child = ac.GetFirst(); child; child = next)
|
||||
// {
|
||||
// next = child->GetNext();
|
||||
// child->SetVisible(visible);
|
||||
// }
|
||||
// }
|
||||
|
||||
AnimationMap::AnimationMap()
|
||||
{
|
||||
}
|
||||
36
source/EngineFrame/Component/AnimationMap.h
Normal file
36
source/EngineFrame/Component/AnimationMap.h
Normal file
@@ -0,0 +1,36 @@
|
||||
#pragma once
|
||||
#include "EngineFrame/Base/Actor.h"
|
||||
#include "Animation.h"
|
||||
class AnimationMap : public Actor
|
||||
{
|
||||
private:
|
||||
/**纹理 */
|
||||
RefPtr<Texture> m_texture = nullptr;
|
||||
/**FBO */
|
||||
GLuint m_fbo = 0;
|
||||
/**大小 */
|
||||
VecSize m_size;
|
||||
/**原始的正交矩阵 */
|
||||
glm::mat4 Oom;
|
||||
/**原始的视口 */
|
||||
SDL_Rect Oviewport;
|
||||
|
||||
public:
|
||||
AnimationMap(/* args */);
|
||||
~AnimationMap();
|
||||
|
||||
/**完成构造
|
||||
* 必须调用完成构造以创建足够大小的画布和各项初始化
|
||||
*/
|
||||
void CompleteConstruction();
|
||||
/**添加动画 */
|
||||
void AddAnimation(RefPtr<Animation> animation);
|
||||
|
||||
/**重置动画组 */
|
||||
void Reset();
|
||||
|
||||
void Render() override;
|
||||
void OnRender() override;
|
||||
void OnUpdate(float deltaTime) override;
|
||||
// void SetVisible(bool visible) override;
|
||||
};
|
||||
@@ -1,16 +1,29 @@
|
||||
#include "Canvas.h"
|
||||
#include "EngineCore/Game.h"
|
||||
#include "EngineFrame/Render/RenderManager.h"
|
||||
Canvas::Canvas()
|
||||
{
|
||||
|
||||
}
|
||||
Canvas::Canvas(VecSize size)
|
||||
{
|
||||
Init(size);
|
||||
}
|
||||
|
||||
Canvas::Canvas(int width, int height)
|
||||
{
|
||||
Init({width, height});
|
||||
}
|
||||
|
||||
void Canvas::Init(VecSize size)
|
||||
{
|
||||
m_size = size;
|
||||
m_renderRect = {0, 0, size.width, size.height};
|
||||
m_rect = {0, 0, (float)size.width, (float)size.height};
|
||||
SetSize(size);
|
||||
// 创建指定大小的纹理
|
||||
m_texture = new Texture();
|
||||
m_texture->Init(VecSize(Game::GetInstance().Screen_W, Game::GetInstance().Screen_H));
|
||||
|
||||
Actor::Init();
|
||||
m_texture->Init(m_size);
|
||||
|
||||
// 构造FBO
|
||||
glGenFramebuffers(1, &m_fbo);
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, m_fbo);
|
||||
// 将目标纹理附加到FBO的颜色附着点
|
||||
@@ -24,92 +37,86 @@ Canvas::Canvas(VecSize size)
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, 0); // 解绑FBO
|
||||
}
|
||||
|
||||
void Canvas::PreRender()
|
||||
{
|
||||
m_rect.x = GetWorldPos().x;
|
||||
m_rect.y = GetWorldPos().y;
|
||||
}
|
||||
|
||||
void Canvas::Render()
|
||||
void Canvas::BeginDraw()
|
||||
{
|
||||
RenderManager *renderer = Game::GetInstance().GetRenderer();
|
||||
SDL_FPoint AnchorPos = {0, 0};
|
||||
if (m_dirty)
|
||||
{
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, m_fbo); // 绑定FBO
|
||||
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
for (auto &info : m_drawQueue)
|
||||
{
|
||||
renderer->DrawTexture(m_imgMap[info.texture], nullptr, &info.rect, 0.f, &AnchorPos, SDL_FLIP_NONE);
|
||||
}
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, 0); // 解绑FBO
|
||||
m_dirty = false;
|
||||
}
|
||||
// 绑定FBO
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, m_fbo);
|
||||
// 清屏
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
// 保存原始的正交矩阵 设置纹理对应的正交矩阵 和 视口
|
||||
Oom = renderer->GetOrthoMatrix();
|
||||
renderer->SetOrthoMatrix(glm::ortho(0.0f, (float)m_size.width, 0.0f, (float)m_size.height, -1.0f, 1.0f));
|
||||
Oviewport = renderer->GetViewport();
|
||||
renderer->SetViewport({0, 0, m_size.width, m_size.height});
|
||||
|
||||
renderer->SetCurrentShaderProgram("flip_y");
|
||||
// 混合
|
||||
if (this->_BlendMode != NONE)
|
||||
Blend();
|
||||
renderer->DrawTexture(m_texture, &m_renderRect, &m_rect, 0.f, &AnchorPos, SDL_FLIP_NONE);
|
||||
// 还原混合
|
||||
if (this->_BlendMode != NONE)
|
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||
renderer->SetCurrentShaderProgram("normal");
|
||||
}
|
||||
|
||||
void Canvas::DrawImg(std::string img, int index, Vec2 pos)
|
||||
void Canvas::EndDraw()
|
||||
{
|
||||
m_dirty = true;
|
||||
ImgKey key = {img, index};
|
||||
DrawInfo info;
|
||||
info.rect.x = pos.x;
|
||||
info.rect.y = pos.y;
|
||||
info.texture = key;
|
||||
|
||||
if (m_imgMap.find(key) == m_imgMap.end())
|
||||
{
|
||||
RefPtr<Texture> tex = new Texture();
|
||||
tex->Init(img, index);
|
||||
m_imgMap[key] = tex;
|
||||
}
|
||||
|
||||
info.rect.w = m_imgMap[key]->getSize().width;
|
||||
info.rect.h = m_imgMap[key]->getSize().height;
|
||||
|
||||
m_drawQueue.push_back(info);
|
||||
RenderManager *renderer = Game::GetInstance().GetRenderer();
|
||||
// 解绑FBO
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||
// 恢复原始的正交矩阵 和 视口
|
||||
renderer->SetOrthoMatrix(Oom);
|
||||
renderer->SetViewport(Oviewport);
|
||||
}
|
||||
|
||||
void Canvas::DrawImg(std::string img, int index, SDL_FRect rect)
|
||||
void Canvas::DrawActor(RefPtr<Actor> actor)
|
||||
{
|
||||
m_dirty = true;
|
||||
ImgKey key = {img, index};
|
||||
DrawInfo info;
|
||||
info.rect = rect;
|
||||
info.texture = key;
|
||||
|
||||
if (m_imgMap.find(key) == m_imgMap.end())
|
||||
{
|
||||
RefPtr<Texture> tex = new Texture();
|
||||
tex->Init(img, index);
|
||||
m_imgMap[key] = tex;
|
||||
}
|
||||
|
||||
m_drawQueue.push_back(info);
|
||||
// 计算矩阵
|
||||
actor->PreRender();
|
||||
// 设定矩阵并渲染(Render中会调用OnRender)
|
||||
actor->Render();
|
||||
}
|
||||
|
||||
void Canvas::Clear()
|
||||
{
|
||||
m_drawQueue.clear();
|
||||
m_dirty = true;
|
||||
m_texture = new Texture();
|
||||
m_texture->Init(m_size);
|
||||
}
|
||||
|
||||
void Canvas::Blend()
|
||||
void Canvas::OnRender()
|
||||
{
|
||||
switch (this->_BlendMode)
|
||||
if (!m_texture || !IsVisible())
|
||||
return;
|
||||
|
||||
RenderManager *renderer = Game::GetInstance().GetRenderer();
|
||||
if (this->_BlendMode != NONE)
|
||||
{
|
||||
case LINEARDODGE:
|
||||
glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE, GL_ZERO, GL_ONE);
|
||||
break;
|
||||
renderer->SetBlendMode(this->_BlendMode);
|
||||
}
|
||||
renderer->DrawTexture(m_texture);
|
||||
if (this->_BlendMode != NONE)
|
||||
{
|
||||
renderer->SetBlendMode(NONE);
|
||||
}
|
||||
}
|
||||
|
||||
// void Canvas::Render()
|
||||
// {
|
||||
// RenderManager *renderer = Game::GetInstance().GetRenderer();
|
||||
// SDL_FPoint AnchorPos = {0, 0};
|
||||
// if (m_dirty)
|
||||
// {
|
||||
// glBindFramebuffer(GL_FRAMEBUFFER, m_fbo); // 绑定FBO
|
||||
// glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
|
||||
// glClear(GL_COLOR_BUFFER_BIT);
|
||||
// for (auto &info : m_drawQueue)
|
||||
// {
|
||||
// renderer->DrawTexture(m_imgMap[info.texture], nullptr, &info.rect, 0.f, &AnchorPos, SDL_FLIP_NONE);
|
||||
// }
|
||||
// glBindFramebuffer(GL_FRAMEBUFFER, 0); // 解绑FBO
|
||||
// m_dirty = false;
|
||||
// }
|
||||
|
||||
// renderer->SetCurrentShaderProgram("flip_y");
|
||||
// // 混合
|
||||
// if (this->_BlendMode != NONE)
|
||||
// Blend();
|
||||
// renderer->DrawTexture(m_texture, &m_renderRect, &m_rect, 0.f, &AnchorPos, SDL_FLIP_NONE);
|
||||
// // 还原混合
|
||||
// if (this->_BlendMode != NONE)
|
||||
// glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||
// renderer->SetCurrentShaderProgram("normal");
|
||||
// }
|
||||
|
||||
@@ -18,37 +18,37 @@ class Canvas : public Actor
|
||||
return index < other.index; // 字符串相同则比较index
|
||||
}
|
||||
};
|
||||
struct DrawInfo
|
||||
{
|
||||
ImgKey texture;
|
||||
SDL_FRect rect = {0, 0, 0, 0};
|
||||
};
|
||||
|
||||
private:
|
||||
/**纹理 */
|
||||
RefPtr<Texture> m_texture = nullptr;
|
||||
/**大小 */
|
||||
VecSize m_size;
|
||||
/**渲染大小 */
|
||||
SDL_Rect m_renderRect;
|
||||
SDL_FRect m_rect;
|
||||
/**脏标记 */
|
||||
bool m_dirty = true;
|
||||
/**FBO */
|
||||
GLuint m_fbo = 0;
|
||||
/**绘制纹理集 */
|
||||
std::map<ImgKey, RefPtr<Texture>> m_imgMap;
|
||||
/**绘制队列 */
|
||||
std::vector<DrawInfo> m_drawQueue;
|
||||
|
||||
/**原始的正交矩阵 */
|
||||
glm::mat4 Oom;
|
||||
/**原始的视口 */
|
||||
SDL_Rect Oviewport;
|
||||
|
||||
public:
|
||||
Canvas();
|
||||
Canvas(VecSize size);
|
||||
Canvas(int width, int height);
|
||||
|
||||
void PreRender() override;
|
||||
void Render() override;
|
||||
void Blend();
|
||||
/**初始化 */
|
||||
void Init(VecSize size);
|
||||
|
||||
void DrawImg(std::string img, int index, Vec2 pos);
|
||||
void DrawImg(std::string img, int index, SDL_FRect rect);
|
||||
/**开始绘制 */
|
||||
void BeginDraw();
|
||||
/**结束绘制 */
|
||||
void EndDraw();
|
||||
/**绘制角色 */
|
||||
void DrawActor(RefPtr<Actor> actor);
|
||||
/**清空画布 */
|
||||
void Clear();
|
||||
|
||||
/**重载OnRender函数 */
|
||||
void OnRender() override;
|
||||
};
|
||||
|
||||
@@ -20,7 +20,7 @@ private:
|
||||
// 数字字符纹理缓存('0'-'9'和'-')
|
||||
std::unordered_map<char, RefPtr<Texture>> m_digitTextures;
|
||||
// 当前数字的字符精灵列表
|
||||
std::vector<Sprite *> m_digitSprites;
|
||||
std::vector<Sprite *> m_digitSprites;
|
||||
|
||||
// 预加载0-9和负号的纹理
|
||||
void PreloadDigits();
|
||||
|
||||
@@ -1,63 +0,0 @@
|
||||
#include "RenderBase.h"
|
||||
#include "EngineFrame/Base/Actor.h"
|
||||
|
||||
|
||||
void RenderBase::CalcRenderInfo()
|
||||
{
|
||||
this->CalcRenderInfoFlag = true;
|
||||
}
|
||||
|
||||
void RenderBase::Init()
|
||||
{
|
||||
// 计算渲染信息
|
||||
CalcRenderInfo();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void RenderBase::SetIterationPos(Vec2 pos)
|
||||
{
|
||||
Actor::SetIterationPos(pos);
|
||||
CalcRenderInfo(); // 更新渲染信息
|
||||
}
|
||||
|
||||
void RenderBase::SetIterationScale(Vec2 scale)
|
||||
{
|
||||
Actor::SetIterationScale(scale);
|
||||
CalcRenderInfo(); // 更新渲染信息
|
||||
}
|
||||
|
||||
void RenderBase::SetIterationRotation(float angle)
|
||||
{
|
||||
if (!Visible)
|
||||
return;
|
||||
Actor::SetIterationRotation(angle);
|
||||
CalcRenderInfo(); // 更新渲染信息
|
||||
}
|
||||
|
||||
void RenderBase::SetPos(Vec2 pos)
|
||||
{
|
||||
Actor::SetPos(pos);
|
||||
|
||||
CalcRenderInfo(); // 更新渲染信息
|
||||
}
|
||||
|
||||
void RenderBase::SetScale(Vec2 scale)
|
||||
{
|
||||
Actor::SetScale(scale);
|
||||
CalcRenderInfo(); // 更新渲染信息
|
||||
}
|
||||
|
||||
void RenderBase::SetRotation(float angle)
|
||||
{
|
||||
Actor::SetRotation(angle);
|
||||
CalcRenderInfo(); // 更新渲染信息
|
||||
}
|
||||
|
||||
void RenderBase::SetAnchor(Vec2 anchor)
|
||||
{
|
||||
Actor::SetAnchor(anchor);
|
||||
CalcRenderInfo(); // 更新渲染信息
|
||||
}
|
||||
@@ -1,45 +0,0 @@
|
||||
#pragma once
|
||||
#include "EngineFrame/Base/Actor.h"
|
||||
|
||||
class RenderBase : public Actor
|
||||
{
|
||||
|
||||
public:
|
||||
struct RenderGuidanceInfo
|
||||
{
|
||||
SDL_FRect rect;
|
||||
// 旋转角度
|
||||
float rotation;
|
||||
// 翻转Flag
|
||||
SDL_RendererFlip flip = SDL_FLIP_NONE;
|
||||
// 锚点坐标
|
||||
Vec2 AnchorPos;
|
||||
// 是否显示
|
||||
bool Visible = true;
|
||||
// 是否在屏幕内
|
||||
bool IsInScreen = false;
|
||||
};
|
||||
|
||||
public:
|
||||
void Init() override;
|
||||
|
||||
public:
|
||||
// 设置迭代的坐标
|
||||
void SetIterationPos(Vec2 pos) override;
|
||||
// 设置迭代的缩放
|
||||
void SetIterationScale(Vec2 scale) override;
|
||||
// 设置迭代的旋转角度
|
||||
void SetIterationRotation(float angle) override;
|
||||
|
||||
// 设置坐标
|
||||
void SetPos(Vec2 pos) override;
|
||||
// 设置缩放
|
||||
void SetScale(Vec2 scale) override;
|
||||
// 设置旋转角度
|
||||
void SetRotation(float angle) override;
|
||||
// 设置中心点
|
||||
void SetAnchor(Vec2 anchor) override;
|
||||
|
||||
// 计算渲染信息
|
||||
void CalcRenderInfo() override;
|
||||
};
|
||||
@@ -3,17 +3,31 @@
|
||||
#include "Text.h"
|
||||
#include "EngineFrame/Render/RenderManager.h"
|
||||
|
||||
void Sprite::GenerateRenderMatrix() const
|
||||
{
|
||||
Matrix3x2 RenderBuffer = Matrix3x2::Translation(glm::vec2(m_texture->getPos().x, m_texture->getPos().y)) * GetTransformMatrix();
|
||||
|
||||
// 构造OpenGl渲染矩阵
|
||||
render_matrix_ = glm::mat4(1.0f);
|
||||
// 填充旋转+缩放分量(2D 变换核心,对应 4x4 矩阵左上 2x2 区域)
|
||||
render_matrix_[0][0] = RenderBuffer[0]; // _11 → x轴缩放+旋转
|
||||
render_matrix_[0][1] = RenderBuffer[1]; // _12 → 影响y轴方向的旋转分量
|
||||
render_matrix_[1][0] = RenderBuffer[2]; // _21 → 影响x轴方向的旋转分量
|
||||
render_matrix_[1][1] = RenderBuffer[3]; // _22 → y轴缩放+旋转
|
||||
// 填充平移分量(对应 4x4 矩阵最后一行前两列,z轴平移为0)
|
||||
render_matrix_[3][0] = RenderBuffer[4]; // _31 → x方向平移(世界坐标)
|
||||
render_matrix_[3][1] = RenderBuffer[5]; // _32 → y方向平移(世界坐标)
|
||||
}
|
||||
|
||||
void Sprite::Init()
|
||||
{
|
||||
this->Size = m_texture->getSize();
|
||||
RenderBase::Init();
|
||||
SetSize(m_texture->getSize());
|
||||
}
|
||||
|
||||
void Sprite::SetTexture(RefPtr<Texture> texture)
|
||||
{
|
||||
m_texture = texture;
|
||||
Init();
|
||||
CalcRenderInfoLogic(); // 第一次计算
|
||||
}
|
||||
|
||||
Sprite::Sprite(std::string imgPath, int Index)
|
||||
@@ -23,7 +37,6 @@ Sprite::Sprite(std::string imgPath, int Index)
|
||||
m_texture = new Texture();
|
||||
m_texture->Init(imgPath, Index);
|
||||
Init();
|
||||
CalcRenderInfoLogic(); // 第一次计算
|
||||
}
|
||||
|
||||
Sprite::Sprite(std::string PngPath)
|
||||
@@ -31,7 +44,6 @@ Sprite::Sprite(std::string PngPath)
|
||||
m_texture = new Texture();
|
||||
m_texture->Init(PngPath);
|
||||
Init();
|
||||
CalcRenderInfoLogic(); // 第一次计算
|
||||
}
|
||||
|
||||
Sprite::~Sprite()
|
||||
@@ -43,140 +55,19 @@ RefPtr<Texture> Sprite::GetTexture()
|
||||
return m_texture;
|
||||
}
|
||||
|
||||
void Sprite::CalcRenderInfoLogic()
|
||||
void Sprite::OnRender()
|
||||
{
|
||||
// 获取至在最终的父对象检查是否显示
|
||||
BaseNode *parentNode = this->GetParent();
|
||||
while (parentNode != nullptr)
|
||||
{
|
||||
if (parentNode->Visible == false)
|
||||
{
|
||||
_RenderGuidanceInfo.Visible = false;
|
||||
return;
|
||||
}
|
||||
parentNode = parentNode->GetParent();
|
||||
}
|
||||
_RenderGuidanceInfo.Visible = true;
|
||||
|
||||
// 计算缩放因子和翻转状态
|
||||
float scaleX = transformIter.scale.x * transform.scale.x;
|
||||
float scaleY = transformIter.scale.y * transform.scale.y;
|
||||
|
||||
// X轴和Y轴上是否翻转的标志
|
||||
bool flipX = scaleX < 0;
|
||||
bool flipY = scaleY < 0;
|
||||
|
||||
_RenderGuidanceInfo.flip = SDL_FLIP_NONE;
|
||||
// 更新翻转状态
|
||||
if (flipX)
|
||||
_RenderGuidanceInfo.flip = static_cast<SDL_RendererFlip>(SDL_FLIP_HORIZONTAL | _RenderGuidanceInfo.flip);
|
||||
if (flipY)
|
||||
_RenderGuidanceInfo.flip = static_cast<SDL_RendererFlip>(SDL_FLIP_VERTICAL | _RenderGuidanceInfo.flip);
|
||||
|
||||
// 纹理数据里带的偏移数据 有这个偏移才能保证动画播放时视觉中心点在一个点上
|
||||
auto T_Size = m_texture->getSize();
|
||||
auto T_Pos = m_texture->getPos();
|
||||
float texturePosX = flipX ? -((float)T_Size.width + (float)T_Pos.x) + SDL_fabsf(transform.position.x * 2.0f) : (float)T_Pos.x;
|
||||
float texturePosY = flipY ? -((float)T_Size.height + (float)T_Pos.y) + SDL_fabsf(transform.position.y * 2.0f) : (float)T_Pos.y;
|
||||
|
||||
// 先计算Img坐标与精灵坐标合成后的真实坐标
|
||||
float RealPosX = transform.position.x + texturePosX;
|
||||
float RealPosY = transform.position.y + texturePosY;
|
||||
|
||||
// 计算在世界中的位置
|
||||
float baseX = transformIter.position.x + RealPosX;
|
||||
float baseY = transformIter.position.y + RealPosY;
|
||||
|
||||
// 获取当前帧的原始尺寸
|
||||
float frameWidth = (float)Size.width;
|
||||
float frameHeight = (float)Size.height;
|
||||
|
||||
// 原始锚点偏移(基于帧尺寸)
|
||||
float origAnchorOffsetX = frameWidth * Anchor.x;
|
||||
float origAnchorOffsetY = frameHeight * Anchor.y;
|
||||
|
||||
// 缩放的绝对值
|
||||
float absScaleX = SDL_fabsf(scaleX);
|
||||
float absScaleY = SDL_fabsf(scaleY);
|
||||
// 缩放后的尺寸
|
||||
float scaledWidth = frameWidth * absScaleX;
|
||||
float scaledHeight = frameHeight * absScaleY;
|
||||
// 缩放后的锚点偏移
|
||||
float scaledAnchorOffsetX = origAnchorOffsetX * absScaleX;
|
||||
float scaledAnchorOffsetY = origAnchorOffsetY * absScaleY;
|
||||
|
||||
// 计算缩放后的锚点偏移与原锚点偏移的差值
|
||||
float scaleOffsetX = scaledAnchorOffsetX - origAnchorOffsetX;
|
||||
float scaleOffsetY = scaledAnchorOffsetY - origAnchorOffsetY;
|
||||
|
||||
// 最终位置计算:世界位置 + 缩放锚点差值 - 缩放后的锚点偏移 + 纹理数据里带的偏移数据 (计算出绘制点的左上角)
|
||||
float Xpos = baseX - scaleOffsetX;
|
||||
float Ypos = baseY - scaleOffsetY;
|
||||
|
||||
// 更新渲染信息
|
||||
_RenderGuidanceInfo.rect = {Xpos, Ypos, scaledWidth, scaledHeight};
|
||||
_RenderGuidanceInfo.AnchorPos = {scaledAnchorOffsetX, scaledAnchorOffsetY};
|
||||
// 设置纹理透明度
|
||||
m_texture->setAlpha(this->Alpha);
|
||||
|
||||
// 屏幕内检测
|
||||
float screenWidth = (float)Game::GetInstance().Screen_W;
|
||||
float screenHeight = (float)Game::GetInstance().Screen_H;
|
||||
bool isInScreen = (Xpos + scaledWidth >= 0 && Xpos <= screenWidth && Ypos + scaledHeight >= 0 && Ypos <= screenHeight);
|
||||
_RenderGuidanceInfo.IsInScreen = isInScreen;
|
||||
|
||||
// this->Size = {scaledWidth, scaledHeight};
|
||||
_RenderGuidanceInfo.rotation = transformIter.rotation + transform.rotation;
|
||||
|
||||
CalcRenderInfoFlag = false;
|
||||
}
|
||||
|
||||
void Sprite::PreRender()
|
||||
{
|
||||
if (CalcRenderInfoFlag && Visible)
|
||||
CalcRenderInfoLogic();
|
||||
}
|
||||
|
||||
void Sprite::Render()
|
||||
{
|
||||
if (!Visible)
|
||||
return;
|
||||
if (!m_texture)
|
||||
if (!m_texture || !IsVisible())
|
||||
return;
|
||||
|
||||
RenderManager *renderer = Game::GetInstance().GetRenderer();
|
||||
|
||||
if (_RenderGuidanceInfo.IsInScreen && _RenderGuidanceInfo.Visible)
|
||||
if (this->_BlendMode != NONE)
|
||||
{
|
||||
SDL_FPoint AnchorPos;
|
||||
AnchorPos.x = _RenderGuidanceInfo.AnchorPos.x;
|
||||
AnchorPos.y = _RenderGuidanceInfo.AnchorPos.y;
|
||||
|
||||
// 混合
|
||||
if (this->_BlendMode != NONE)
|
||||
Blend();
|
||||
|
||||
SDL_Rect srcrect = {0, 0, this->Size.width, this->Size.height};
|
||||
renderer->DrawTexture(m_texture, &srcrect, &_RenderGuidanceInfo.rect, _RenderGuidanceInfo.rotation, &AnchorPos, _RenderGuidanceInfo.flip);
|
||||
|
||||
// 还原混合
|
||||
if (this->_BlendMode != NONE)
|
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||
}
|
||||
}
|
||||
|
||||
void Sprite::Clear()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
|
||||
void Sprite::Blend()
|
||||
{
|
||||
switch (this->_BlendMode)
|
||||
{
|
||||
case LINEARDODGE:
|
||||
glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE, GL_ZERO, GL_ONE);
|
||||
break;
|
||||
renderer->SetBlendMode(this->_BlendMode);
|
||||
}
|
||||
renderer->DrawTexture(m_texture);
|
||||
if (this->_BlendMode != NONE)
|
||||
{
|
||||
renderer->SetBlendMode(NONE);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
#pragma once
|
||||
#include <string>
|
||||
#include "Asset/Asset_ImagePack.h"
|
||||
#include "EngineFrame/Component/RenderBase.h"
|
||||
#include "EngineFrame/Render/Texture.h"
|
||||
#include "EngineFrame/Base/Actor.h"
|
||||
|
||||
class Game;
|
||||
/**
|
||||
* @brief Sprite类,继承自Component类,用于表示游戏中的精灵组件
|
||||
*/
|
||||
class Sprite : public RenderBase
|
||||
class Sprite : public Actor
|
||||
{
|
||||
protected:
|
||||
RefPtr<Texture> m_texture = nullptr;
|
||||
@@ -18,24 +18,18 @@ public:
|
||||
Sprite(std::string imgPath, int Index);
|
||||
Sprite(std::string PngPath);
|
||||
~Sprite();
|
||||
void Render() override;
|
||||
void PreRender() override;
|
||||
void Clear() override;
|
||||
void Init() override;
|
||||
void SetTexture(RefPtr<Texture> texture);
|
||||
|
||||
void SetTexture(RefPtr<Texture> texture);
|
||||
RefPtr<Texture> GetTexture();
|
||||
|
||||
public:
|
||||
// 渲染信息
|
||||
RenderGuidanceInfo _RenderGuidanceInfo;
|
||||
/**重载生成渲染矩阵函数 */
|
||||
void GenerateRenderMatrix() const override;
|
||||
/**重载初始化函数 */
|
||||
void Init() override;
|
||||
/**重载渲染函数 */
|
||||
void OnRender() override;
|
||||
|
||||
public:
|
||||
std::string imgPath;
|
||||
int Index;
|
||||
|
||||
public:
|
||||
// 计算渲染信息
|
||||
void CalcRenderInfoLogic();
|
||||
// 混合
|
||||
void Blend();
|
||||
};
|
||||
|
||||
@@ -22,7 +22,7 @@ RenderManager::RenderManager(SDL_Window *window)
|
||||
int width, height;
|
||||
SDL_GetWindowSize(window, &width, &height);
|
||||
// 设置视口
|
||||
glViewport(0, 0, width, height);
|
||||
SetViewport(SDL_Rect{0, 0, width, height});
|
||||
// 设置窗口尺寸
|
||||
_windowWidth = width;
|
||||
_windowHeight = height;
|
||||
@@ -33,7 +33,7 @@ RenderManager::RenderManager(SDL_Window *window)
|
||||
_UIOrthoMatrix = glm::ortho(0.0f, (float)width, (float)height, 0.0f, -1.0f, 1.0f);
|
||||
|
||||
// 设置清屏颜色
|
||||
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
||||
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
|
||||
// 启用混合模式
|
||||
glEnable(GL_BLEND);
|
||||
|
||||
@@ -44,6 +44,8 @@ RenderManager::RenderManager(SDL_Window *window)
|
||||
InitShaderProgram();
|
||||
// 初始化绘制2D纹理缓冲程序
|
||||
Init2DTextureProgram();
|
||||
// 初始化绘制矩形缓冲程序
|
||||
InitRectProgram();
|
||||
|
||||
// 设定默认使用的着色器程序和缓冲程序
|
||||
SetCurrentShaderProgram("normal");
|
||||
@@ -79,17 +81,17 @@ void RenderManager::InitShaderProgram()
|
||||
{
|
||||
if (!groupData.is_object())
|
||||
{
|
||||
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "着色器组 %s 格式错误,跳过\n", groupName.c_str());
|
||||
SDL_LogError(0, "着色器组 %s 格式错误,跳过\n", groupName.c_str());
|
||||
continue;
|
||||
}
|
||||
if (!groupData.contains("vertex") || !groupData["vertex"].is_string())
|
||||
{
|
||||
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "组 %s 缺少 vertex 路径,跳过\n", groupName.c_str());
|
||||
SDL_LogError(0, "组 %s 缺少 vertex 路径,跳过\n", groupName.c_str());
|
||||
continue;
|
||||
}
|
||||
if (!groupData.contains("fragment") || !groupData["fragment"].is_string())
|
||||
{
|
||||
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "组 %s 缺少 fragment 路径,跳过\n", groupName.c_str());
|
||||
SDL_LogError(0, "组 %s 缺少 fragment 路径,跳过\n", groupName.c_str());
|
||||
continue;
|
||||
}
|
||||
std::string vertPath = groupData["vertex"].get<std::string>();
|
||||
@@ -99,7 +101,7 @@ void RenderManager::InitShaderProgram()
|
||||
GLuint fragmentShader = CompileShader(GL_FRAGMENT_SHADER, fragPath);
|
||||
if (vertexShader == 0 || fragmentShader == 0)
|
||||
{
|
||||
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "组 %s 着色器编译失败,跳过\n", groupName.c_str());
|
||||
SDL_LogError(0, "组 %s 着色器编译失败,跳过\n", groupName.c_str());
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -113,7 +115,7 @@ void RenderManager::InitShaderProgram()
|
||||
{
|
||||
char infoLog[512];
|
||||
glGetProgramInfoLog(program, 512, nullptr, infoLog);
|
||||
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "组 %s 着色器链接失败,失败原因: %s\n", groupName.c_str(), infoLog);
|
||||
SDL_LogError(0, "组 %s 着色器链接失败,失败原因: %s\n", groupName.c_str(), infoLog);
|
||||
}
|
||||
|
||||
_shaderProgramMap[groupName] = program;
|
||||
@@ -142,10 +144,10 @@ void RenderManager::Init2DTextureProgram()
|
||||
glBindVertexArray(VAO);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_DYNAMIC_DRAW);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STREAM_DRAW);
|
||||
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_DYNAMIC_DRAW);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STREAM_DRAW);
|
||||
|
||||
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void *)0);
|
||||
glEnableVertexAttribArray(0);
|
||||
@@ -158,126 +160,71 @@ void RenderManager::Init2DTextureProgram()
|
||||
glUseProgram(_currentShaderProgram);
|
||||
|
||||
// 获取uniform变量位置
|
||||
GLuint _uModelLoc = glGetUniformLocation(_currentShaderProgram, "uModel");
|
||||
GLuint _uViewProjLoc = glGetUniformLocation(_currentShaderProgram, "uViewProj");
|
||||
GLuint _uTextureLoc = glGetUniformLocation(_currentShaderProgram, "uTexture");
|
||||
if (_uModelLoc == -1 || _uViewProjLoc == -1 || _uTextureLoc == -1)
|
||||
GLint _uModelLoc = glGetUniformLocation(_currentShaderProgram, "uModel");
|
||||
GLint _uViewProjLoc = glGetUniformLocation(_currentShaderProgram, "uViewProj");
|
||||
GLint _uTextureLoc = glGetUniformLocation(_currentShaderProgram, "uTexture");
|
||||
GLint _uOpacityLoc = glGetUniformLocation(_currentShaderProgram, "uOpacity");
|
||||
|
||||
if (_uModelLoc == -1 || _uViewProjLoc == -1 || _uTextureLoc == -1 || _uOpacityLoc == -1)
|
||||
{
|
||||
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "无法获取着色器 uniform 变量位置\n");
|
||||
SDL_LogError(0, "无法获取着色器 uniform 变量位置\n");
|
||||
}
|
||||
std::vector<GLuint> uniformLocs = {_uModelLoc, _uViewProjLoc, _uTextureLoc};
|
||||
std::vector<GLint> uniformLocs = {_uModelLoc, _uViewProjLoc, _uTextureLoc, _uOpacityLoc};
|
||||
|
||||
DrawLogicFunc drawFunc = [this](
|
||||
RefPtr<Texture> textureObj, const SDL_Rect *srcrect, const SDL_FRect *dstrect, const double angle, const SDL_FPoint *center, const SDL_RendererFlip flip, void *userdata)
|
||||
{
|
||||
GLuint texture = textureObj->getID();
|
||||
if (!texture || !dstrect)
|
||||
return;
|
||||
|
||||
glUseProgram(_currentShaderProgram);
|
||||
|
||||
glm::mat4 model = glm::mat4(1.0f);
|
||||
|
||||
// 平移
|
||||
model = glm::translate(model, glm::vec3(dstrect->x, dstrect->y, 0.0f));
|
||||
|
||||
// 计算旋转中心
|
||||
float centerX = center ? center->x : dstrect->w * 0.5f;
|
||||
float centerY = center ? center->y : dstrect->h * 0.5f;
|
||||
|
||||
// 旋转中心平移
|
||||
model = glm::translate(model, glm::vec3(centerX, centerY, 0.0f));
|
||||
|
||||
// 旋转
|
||||
if (angle != 0.0)
|
||||
{
|
||||
model = glm::rotate(model, glm::radians(static_cast<float>(angle)), glm::vec3(0.0f, 0.0f, 1.0f));
|
||||
}
|
||||
|
||||
// 旋转中心平移
|
||||
model = glm::translate(model, glm::vec3(-centerX, -centerY, 0.0f));
|
||||
|
||||
// 缩放和翻转
|
||||
float scaleX = dstrect->w;
|
||||
float scaleY = dstrect->h;
|
||||
|
||||
// 应用翻转(通过负缩放实现)
|
||||
if (flip & SDL_FLIP_HORIZONTAL)
|
||||
scaleX = -scaleX;
|
||||
if (flip & SDL_FLIP_VERTICAL)
|
||||
scaleY = -scaleY;
|
||||
|
||||
model = glm::scale(model, glm::vec3(scaleX, scaleY, 1.0f));
|
||||
|
||||
// 翻转后的位置补偿
|
||||
if (flip & SDL_FLIP_HORIZONTAL)
|
||||
{
|
||||
model = glm::translate(model, glm::vec3(-1.0f, 0.0f, 0.0f));
|
||||
}
|
||||
if (flip & SDL_FLIP_VERTICAL)
|
||||
{
|
||||
model = glm::translate(model, glm::vec3(0.0f, -1.0f, 0.0f));
|
||||
}
|
||||
|
||||
// 设置着色器 uniforms
|
||||
glUniformMatrix4fv(_currentRenderParams.UnimLocs[0], 1, GL_FALSE, glm::value_ptr(model));
|
||||
glUniformMatrix4fv(_currentRenderParams.UnimLocs[1], 1, GL_FALSE, glm::value_ptr(_OrthoMatrix));
|
||||
|
||||
// 绑定纹理并设置采样器
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindTexture(GL_TEXTURE_2D, texture);
|
||||
glUniform1i(_currentRenderParams.UnimLocs[2], 0);
|
||||
|
||||
// 处理纹理裁剪
|
||||
float texWidth = (float)textureObj->getSize().width;
|
||||
float texHeight = (float)textureObj->getSize().height;
|
||||
|
||||
// 计算纹理坐标
|
||||
float minu, minv, maxu, maxv;
|
||||
if (srcrect)
|
||||
{
|
||||
minu = (float)srcrect->x / texWidth;
|
||||
minv = (float)srcrect->y / texHeight;
|
||||
maxu = (float)(srcrect->x + srcrect->w) / texWidth;
|
||||
maxv = (float)(srcrect->y + srcrect->h) / texHeight;
|
||||
}
|
||||
else
|
||||
{
|
||||
minu = 0.0f;
|
||||
minv = 0.0f;
|
||||
maxu = 1.0f;
|
||||
maxv = 1.0f;
|
||||
}
|
||||
|
||||
// 计算顶点位置(单位矩形,通过模型变换进行缩放和平移)
|
||||
float minx = 0.0f;
|
||||
float miny = 0.0f;
|
||||
float maxx = 1.0f;
|
||||
float maxy = 1.0f;
|
||||
|
||||
// 创建顶点数据(位置 + 纹理坐标)
|
||||
float vertices[] = {
|
||||
// 位置 // 纹理坐标
|
||||
minx, miny, minu, minv, // 左上
|
||||
maxx, miny, maxu, minv, // 右上
|
||||
maxx, maxy, maxu, maxv, // 右下
|
||||
minx, maxy, minu, maxv // 左下
|
||||
};
|
||||
|
||||
// 更新VBO数据
|
||||
glBindBuffer(GL_ARRAY_BUFFER, _currentRenderParams.VBO);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_DYNAMIC_DRAW);
|
||||
|
||||
// 绘制纹理
|
||||
glBindVertexArray(_currentRenderParams.VAO);
|
||||
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
|
||||
glBindVertexArray(0);
|
||||
};
|
||||
|
||||
GL_RenderParams bufferObject = {VAO, VBO, EBO, uniformLocs, drawFunc};
|
||||
GL_RenderParams bufferObject = {VAO, VBO, EBO, uniformLocs, nullptr};
|
||||
AddBufferObject("2DTexture", bufferObject);
|
||||
}
|
||||
|
||||
void RenderManager::InitRectProgram()
|
||||
{
|
||||
// 单位矩形顶点(仅位置,左下角(0,0),右上角(1,1))
|
||||
float vertices[] = {
|
||||
0.0f, 0.0f, // 左下
|
||||
1.0f, 0.0f, // 右下
|
||||
1.0f, 1.0f, // 右上
|
||||
0.0f, 1.0f // 左上
|
||||
};
|
||||
unsigned int indices[] = {0, 1, 2, 2, 3, 0}; // 固定索引
|
||||
|
||||
GLuint VAO, VBO, EBO;
|
||||
glGenVertexArrays(1, &VAO);
|
||||
glGenBuffers(1, &VBO);
|
||||
glGenBuffers(1, &EBO);
|
||||
|
||||
// 绑定并上传数据(GL_STATIC_DRAW:数据仅初始化一次,后续不修改)
|
||||
glBindVertexArray(VAO);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); // 静态数据
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW); // 静态数据
|
||||
|
||||
// 配置顶点属性(仅位置,2个float,步长2*sizeof(float))
|
||||
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void *)0);
|
||||
glEnableVertexAttribArray(0);
|
||||
|
||||
// 解绑(恢复默认状态)
|
||||
glBindVertexArray(0);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
||||
|
||||
// 获取矩形着色器的Uniform位置(需提前准备矩形专用着色器)
|
||||
SetCurrentShaderProgram("rect"); // 假设着色器配置中“rect”为矩形专用着色器
|
||||
glUseProgram(_currentShaderProgram);
|
||||
GLint uModelLoc = glGetUniformLocation(_currentShaderProgram, "uModel");
|
||||
GLint uViewProjLoc = glGetUniformLocation(_currentShaderProgram, "uViewProj");
|
||||
GLint uColorLoc = glGetUniformLocation(_currentShaderProgram, "uColor");
|
||||
if (uModelLoc == -1 || uViewProjLoc == -1 || uColorLoc == -1)
|
||||
{
|
||||
SDL_LogError(0, "矩形着色器Uniform变量获取失败\n uModelLoc: %d, uViewProjLoc: %d, uColorLoc: %d\n", uModelLoc, uViewProjLoc, uColorLoc);
|
||||
return;
|
||||
}
|
||||
std::vector<GLint> uniformLocs = {uModelLoc, uViewProjLoc, uColorLoc};
|
||||
|
||||
// 将矩形渲染参数加入管理器
|
||||
GL_RenderParams rectBuffer = {VAO, VBO, EBO, uniformLocs, nullptr};
|
||||
AddBufferObject("Rect", rectBuffer);
|
||||
}
|
||||
|
||||
void RenderManager::SetCurrentShaderProgram(std::string name)
|
||||
{
|
||||
if (_shaderProgramMap.find(name) != _shaderProgramMap.end())
|
||||
@@ -285,7 +232,7 @@ void RenderManager::SetCurrentShaderProgram(std::string name)
|
||||
this->_currentShaderProgram = _shaderProgramMap[name];
|
||||
}
|
||||
else
|
||||
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "找不到着色器程序: %s\n", name.c_str());
|
||||
SDL_LogError(0, "找不到着色器程序: %s\n", name.c_str());
|
||||
}
|
||||
|
||||
void RenderManager::SetCurrentBufferObject(std::string name)
|
||||
@@ -294,8 +241,8 @@ void RenderManager::SetCurrentBufferObject(std::string name)
|
||||
{
|
||||
this->_currentRenderParams = _RenderParamsMap[name];
|
||||
}
|
||||
else
|
||||
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "找不到渲染参数: %s\n", name.c_str());
|
||||
// else
|
||||
// SDL_LogError(0, "找不到渲染参数: %s\n", name.c_str());
|
||||
}
|
||||
|
||||
void RenderManager::ClearScreen()
|
||||
@@ -320,13 +267,147 @@ void RenderManager::CloseClipRect()
|
||||
glDisable(GL_SCISSOR_TEST);
|
||||
}
|
||||
|
||||
void RenderManager::DrawTexture(RefPtr<Texture> textureObj, const SDL_Rect *srcrect, const SDL_FRect *dstrect, const double angle, const SDL_FPoint *center, const SDL_RendererFlip flip, void *userdata)
|
||||
void RenderManager::SetOpacity(float opacity)
|
||||
{
|
||||
_frameRenderCount++;
|
||||
if (_currentRenderParams.DrawFunc != nullptr)
|
||||
_currentRenderParams.DrawFunc(textureObj, srcrect, dstrect, angle, center, flip, userdata);
|
||||
this->opacity_ = opacity;
|
||||
}
|
||||
|
||||
float RenderManager::GetOpacity()
|
||||
{
|
||||
return this->opacity_;
|
||||
}
|
||||
|
||||
void RenderManager::SetMatrix(glm::mat4 matrix)
|
||||
{
|
||||
this->_currentMatrix = matrix;
|
||||
}
|
||||
|
||||
glm::mat4 RenderManager::GetMatrix()
|
||||
{
|
||||
return this->_currentMatrix;
|
||||
}
|
||||
|
||||
void RenderManager::SetBlendMode(LE_BlEND_MODE blendMode)
|
||||
{
|
||||
switch (blendMode)
|
||||
{
|
||||
/**常规混合 */
|
||||
case NONE:
|
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||
break;
|
||||
/**线性减淡 */
|
||||
case LINEARDODGE:
|
||||
glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE, GL_ZERO, GL_ONE);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void RenderManager::DrawTexture(RefPtr<Texture> textureObj)
|
||||
{
|
||||
_frameRenderCount++; // 统计渲染次数
|
||||
glUseProgram(_currentShaderProgram);
|
||||
|
||||
GLuint texture = textureObj->getID();
|
||||
float texWidth = (float)textureObj->getSize().width;
|
||||
float texHeight = (float)textureObj->getSize().height;
|
||||
|
||||
glm::mat4 modelMat = _currentMatrix;
|
||||
|
||||
modelMat = glm::scale(modelMat, glm::vec3(texWidth, texHeight, 1.0f));
|
||||
|
||||
// 设置着色器 uniforms
|
||||
glUniformMatrix4fv(_currentRenderParams.UnimLocs[0], 1, GL_FALSE, glm::value_ptr(modelMat));
|
||||
glUniformMatrix4fv(_currentRenderParams.UnimLocs[1], 1, GL_FALSE, glm::value_ptr(_OrthoMatrix));
|
||||
|
||||
// 绑定纹理并设置采样器
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindTexture(GL_TEXTURE_2D, texture);
|
||||
glUniform1i(_currentRenderParams.UnimLocs[2], 0);
|
||||
glUniform1f(_currentRenderParams.UnimLocs[3], opacity_);
|
||||
|
||||
// 计算纹理坐标
|
||||
float minu, minv, maxu, maxv;
|
||||
if (false)
|
||||
{
|
||||
// minu = (float)srcrect->x / texWidth;
|
||||
// minv = (float)srcrect->y / texHeight;
|
||||
// maxu = (float)(srcrect->x + srcrect->w) / texWidth;
|
||||
// maxv = (float)(srcrect->y + srcrect->h) / texHeight;
|
||||
}
|
||||
else
|
||||
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "找不到渲染逻辑函数\n");
|
||||
{
|
||||
minu = 0.0f;
|
||||
minv = 0.0f;
|
||||
maxu = 1.0f;
|
||||
maxv = 1.0f;
|
||||
}
|
||||
|
||||
// 计算顶点位置(单位矩形,通过模型变换进行缩放和平移)
|
||||
float minx = 0.0f;
|
||||
float miny = 0.0f;
|
||||
float maxx = 1.0f;
|
||||
float maxy = 1.0f;
|
||||
|
||||
// 创建顶点数据(位置 + 纹理坐标)
|
||||
float vertices[] = {
|
||||
// 位置 // 纹理坐标
|
||||
minx, miny, minu, minv, // 左上
|
||||
maxx, miny, maxu, minv, // 右上
|
||||
maxx, maxy, maxu, maxv, // 右下
|
||||
minx, maxy, minu, maxv // 左下
|
||||
};
|
||||
|
||||
// 更新VBO数据
|
||||
glBindBuffer(GL_ARRAY_BUFFER, _currentRenderParams.VBO);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STREAM_DRAW);
|
||||
|
||||
// 绘制纹理
|
||||
glBindVertexArray(_currentRenderParams.VAO);
|
||||
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
|
||||
glBindVertexArray(0);
|
||||
glBindTexture(GL_TEXTURE_2D, 0); // 解绑纹理
|
||||
}
|
||||
|
||||
void RenderManager::DrawRect(const SDL_Rect *rect, glm::vec4 color)
|
||||
{
|
||||
// 1. 合法性检查(补充VAO/EBO存在性检查)
|
||||
if (!rect || _currentShaderProgram == 0 || _currentRenderParams.VAO == 0 || _currentRenderParams.EBO == 0)
|
||||
{
|
||||
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "DrawRect:参数无效或缓冲未初始化\n");
|
||||
return;
|
||||
}
|
||||
SetCurrentShaderProgram("rect");
|
||||
SetCurrentBufferObject("Rect");
|
||||
_frameRenderCount++;
|
||||
|
||||
// 绑定VAO和EBO(GPU获取顶点数据和格式的关键)
|
||||
glBindVertexArray(_currentRenderParams.VAO);
|
||||
|
||||
// 3. 计算模型矩阵(通过缩放+平移,将单位矩形变换到目标位置和大小)
|
||||
glm::mat4 model = glm::mat4(1.0f);
|
||||
// 先平移到rect的左上角(OpenGL正交投影下Y轴向下,与SDL_Rect一致)
|
||||
model = glm::translate(model, glm::vec3((float)rect->x, (float)rect->y, 0.0f));
|
||||
// 再缩放到rect的宽高
|
||||
model = glm::scale(model, glm::vec3((float)rect->w, (float)rect->h, 1.0f));
|
||||
|
||||
// 4. 激活着色器并设置Uniform
|
||||
glUseProgram(_currentShaderProgram);
|
||||
// 4.1 模型矩阵(传递变换后的矩阵)
|
||||
glUniformMatrix4fv(_currentRenderParams.UnimLocs[0], 1, GL_FALSE, glm::value_ptr(model));
|
||||
// 4.2 投影矩阵(复用当前正交矩阵)
|
||||
glUniformMatrix4fv(_currentRenderParams.UnimLocs[1], 1, GL_FALSE, glm::value_ptr(_OrthoMatrix));
|
||||
// 4.3 颜色
|
||||
glUniform4fv(_currentRenderParams.UnimLocs[2], 1, glm::value_ptr(color));
|
||||
|
||||
// 5. 绘制矩形(6个索引对应2个三角形)
|
||||
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
|
||||
|
||||
// 6. 解绑资源(避免影响后续绘制)
|
||||
glBindVertexArray(0);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
||||
|
||||
SetCurrentShaderProgram("normal");
|
||||
SetCurrentBufferObject("2DTexture");
|
||||
}
|
||||
|
||||
void RenderManager::SetOrthoMatrixType(int type)
|
||||
@@ -347,6 +428,17 @@ void RenderManager::SetOrthoMatrix(glm::mat4 matrix)
|
||||
_OrthoMatrix = matrix;
|
||||
}
|
||||
|
||||
void RenderManager::SetViewport(SDL_Rect viewport)
|
||||
{
|
||||
_viewport = viewport;
|
||||
glViewport(viewport.x, viewport.y, viewport.w, viewport.h);
|
||||
}
|
||||
|
||||
SDL_Rect RenderManager::GetViewport()
|
||||
{
|
||||
return _viewport;
|
||||
}
|
||||
|
||||
GLuint RenderManager::CompileShader(GLenum type, std::string Path)
|
||||
{
|
||||
std::ifstream Source("shader/" + Path);
|
||||
|
||||
@@ -22,7 +22,7 @@ public:
|
||||
GLuint VAO;
|
||||
GLuint VBO;
|
||||
GLuint EBO;
|
||||
std::vector<GLuint> UnimLocs;
|
||||
std::vector<GLint> UnimLocs;
|
||||
DrawLogicFunc DrawFunc = nullptr;
|
||||
};
|
||||
|
||||
@@ -37,6 +37,8 @@ private:
|
||||
glm::mat4 _UIOrthoMatrix;
|
||||
// 渲染的正交投影矩阵
|
||||
glm::mat4 _OrthoMatrix;
|
||||
/**视口 */
|
||||
SDL_Rect _viewport;
|
||||
|
||||
// 渲染器上下文
|
||||
SDL_GLContext _ctx;
|
||||
@@ -50,40 +52,64 @@ private:
|
||||
// 当前使用渲染参数
|
||||
GL_RenderParams _currentRenderParams;
|
||||
|
||||
// 渲染透明度
|
||||
float opacity_ = 1.0f;
|
||||
// 渲染矩阵
|
||||
glm::mat4 _currentMatrix;
|
||||
|
||||
public:
|
||||
// 单帧渲染调用次数
|
||||
int _frameRenderCount = 0;
|
||||
|
||||
public:
|
||||
RenderManager(SDL_Window *window);
|
||||
~RenderManager();
|
||||
|
||||
// 初始化着色器程序
|
||||
/**初始化着色器程序 */
|
||||
void InitShaderProgram();
|
||||
// 初始化绘制2D纹理缓冲程序
|
||||
/**初始化绘制2D纹理缓冲程序 */
|
||||
void Init2DTextureProgram();
|
||||
// 设定当前使用的着色器程序
|
||||
/**初始化绘制矩形缓冲程序 */
|
||||
void InitRectProgram();
|
||||
/**设定当前使用的着色器程序 */
|
||||
void SetCurrentShaderProgram(std::string name);
|
||||
// 设定当前使用的缓冲对象
|
||||
/**设定当前使用的缓冲对象 */
|
||||
void SetCurrentBufferObject(std::string name);
|
||||
|
||||
// 清空屏幕
|
||||
/**清空屏幕 */
|
||||
void ClearScreen();
|
||||
// 交换缓冲区
|
||||
/**交换缓冲区 */
|
||||
void SwapBuffer();
|
||||
// 设置裁切区域
|
||||
/**设置裁切区域 */
|
||||
void SetClipRect(const SDL_Rect *rect);
|
||||
// 关闭裁切区域
|
||||
/**关闭裁切区域 */
|
||||
void CloseClipRect();
|
||||
// 绘制纹理
|
||||
void DrawTexture(RefPtr<Texture> texture, const SDL_Rect *srcrect, const SDL_FRect *dstrect, const double angle, const SDL_FPoint *center, const SDL_RendererFlip flip, void *userdata = nullptr);
|
||||
// 设置正交投影矩阵类型
|
||||
/**设置渲染透明度 */
|
||||
void SetOpacity(float opacity);
|
||||
/**获取渲染透明度 */
|
||||
float GetOpacity();
|
||||
/**设置渲染矩阵 */
|
||||
void SetMatrix(glm::mat4 matrix);
|
||||
/**获取渲染矩阵 */
|
||||
glm::mat4 GetMatrix();
|
||||
/**设置混合模式 */
|
||||
void SetBlendMode(LE_BlEND_MODE blendMode);
|
||||
|
||||
/**绘制纹理 */
|
||||
void DrawTexture(RefPtr<Texture> texture);
|
||||
/**绘制矩形 */
|
||||
void DrawRect(const SDL_Rect *rect, glm::vec4 color);
|
||||
|
||||
/**设置正交投影矩阵类型 */
|
||||
void SetOrthoMatrixType(int type);
|
||||
// 获取渲染的正交投影矩阵
|
||||
/**获取渲染的正交投影矩阵 */
|
||||
glm::mat4 GetOrthoMatrix();
|
||||
// 设置渲染的正交投影矩阵
|
||||
/**设置渲染的正交投影矩阵 */
|
||||
void SetOrthoMatrix(glm::mat4 matrix);
|
||||
|
||||
|
||||
/**设置视口 */
|
||||
void SetViewport(SDL_Rect viewport);
|
||||
/**获取视口 */
|
||||
SDL_Rect GetViewport();
|
||||
|
||||
private:
|
||||
// 编译着色器
|
||||
|
||||
@@ -13,15 +13,14 @@ void Scene::Enter()
|
||||
|
||||
void Scene::Init()
|
||||
{
|
||||
BaseNode::Init();
|
||||
addTag(Tag::SCENE);
|
||||
|
||||
}
|
||||
|
||||
void Scene::Exit()
|
||||
{
|
||||
}
|
||||
|
||||
RefPtr<BaseNode> Scene::GetCamera()
|
||||
RefPtr<Node> Scene::GetCamera()
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
#pragma once
|
||||
#include "EngineFrame/Base/BaseNode.h"
|
||||
class Scene : public BaseNode
|
||||
#include "EngineFrame/Base/Node.h"
|
||||
class Scene : public Node
|
||||
{
|
||||
public:
|
||||
Scene();
|
||||
virtual void Enter();
|
||||
virtual void Exit();
|
||||
virtual RefPtr<BaseNode> GetCamera();
|
||||
virtual RefPtr<Node> GetCamera();
|
||||
void Init();
|
||||
};
|
||||
@@ -31,6 +31,18 @@ int Ifstream_NPK::ReadInt()
|
||||
return Count;
|
||||
}
|
||||
|
||||
unsigned int Ifstream_NPK::ReadUnsignedInt()
|
||||
{
|
||||
char *CountBuffer = new char[4];
|
||||
for (int i = 0; i < 4; i++)
|
||||
{
|
||||
this->get(CountBuffer[i]);
|
||||
}
|
||||
unsigned int Count = *(unsigned int *)CountBuffer;
|
||||
delete[] CountBuffer;
|
||||
return Count;
|
||||
}
|
||||
|
||||
// 读字符串
|
||||
std::string Ifstream_NPK::ReadString()
|
||||
{
|
||||
@@ -60,7 +72,7 @@ std::string Ifstream_NPK::ReadInfo()
|
||||
}
|
||||
|
||||
// 读LONG
|
||||
int Ifstream_NPK::ReadLong()
|
||||
long Ifstream_NPK::ReadLong()
|
||||
{
|
||||
char *CountBuffer = new char[8];
|
||||
for (int i = 0; i < 8; i++)
|
||||
@@ -72,6 +84,18 @@ int Ifstream_NPK::ReadLong()
|
||||
return Count;
|
||||
}
|
||||
|
||||
unsigned long Ifstream_NPK::ReadUnsignedLong()
|
||||
{
|
||||
char *CountBuffer = new char[8];
|
||||
for (int i = 0; i < 8; i++)
|
||||
{
|
||||
this->get(CountBuffer[i]);
|
||||
}
|
||||
unsigned long Count = *(unsigned long *)CountBuffer;
|
||||
delete[] CountBuffer;
|
||||
return Count;
|
||||
}
|
||||
|
||||
// 读指定长度数据
|
||||
BYTE *Ifstream_NPK::ReadCustomSize(int Size)
|
||||
{
|
||||
|
||||
@@ -22,6 +22,9 @@ public:
|
||||
// 读整数
|
||||
int ReadInt();
|
||||
|
||||
// 读无符号整数
|
||||
unsigned int ReadUnsignedInt();
|
||||
|
||||
// 读字符串
|
||||
std::string ReadString();
|
||||
|
||||
@@ -29,7 +32,9 @@ public:
|
||||
std::string ReadInfo();
|
||||
|
||||
// 读LONG
|
||||
int ReadLong();
|
||||
long ReadLong();
|
||||
// 读取无符号long
|
||||
unsigned long ReadUnsignedLong();
|
||||
|
||||
// 读指定长度数据
|
||||
BYTE *ReadCustomSize(int Size);
|
||||
|
||||
Reference in New Issue
Block a user