修改OpenGl渲染底层之前
This commit is contained in:
@@ -3,7 +3,6 @@
|
||||
#include "EngineFrame/Component/Sprite.h"
|
||||
#include "EngineFrame/Actor/Actor.h"
|
||||
#include "EngineFrame/Component/Text.h"
|
||||
#include "EngineFrame/Actor/Debug_Actor.h"
|
||||
|
||||
Game::Game()
|
||||
{
|
||||
@@ -16,8 +15,6 @@ Game::~Game()
|
||||
|
||||
void Game::Init(std::function<void()> CallBack)
|
||||
{
|
||||
// 计算帧时间
|
||||
m_frameTime = 1000 / m_fps;
|
||||
|
||||
SDL_InitSubSystem(SDL_INIT_JOYSTICK);
|
||||
SDL_JoystickEventState(SDL_ENABLE);
|
||||
@@ -52,7 +49,7 @@ void Game::Init(std::function<void()> CallBack)
|
||||
}
|
||||
}
|
||||
// 创建渲染器
|
||||
m_renderer = SDL_CreateRenderer(m_window, -1, SDL_RENDERER_ACCELERATED);
|
||||
m_renderer = SDL_CreateRenderer(m_window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
|
||||
if (m_renderer == nullptr)
|
||||
{
|
||||
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "SDL could not Create Renderer! Error: %s\n", SDL_GetError());
|
||||
@@ -78,41 +75,41 @@ void Game::Init(std::function<void()> CallBack)
|
||||
m_isRunning = false;
|
||||
}
|
||||
// SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "0");
|
||||
// 构造调试对象
|
||||
m_DebugInfoActor = new Debug_Actor();
|
||||
CallBack();
|
||||
}
|
||||
|
||||
void Game::Run()
|
||||
{
|
||||
// 计算帧时间
|
||||
m_frameTime = 1000.0 / m_Settingfps;
|
||||
|
||||
while (m_isRunning)
|
||||
{
|
||||
u32 frameStart = SDL_GetTicks(); // 帧开始时间
|
||||
// 帧开始时间
|
||||
Uint64 frameStart = SDL_GetTicks64();
|
||||
SDL_Event m_event;
|
||||
HandleEvents(&m_event);
|
||||
Update(m_deltaTime); // 注意:这里使用的是上一帧的deltaTime(合理,见说明)
|
||||
Update(m_deltaTime);
|
||||
Render();
|
||||
|
||||
// 帧率统计(保持不变)
|
||||
m_frameCount++;
|
||||
u32 currentTime = SDL_GetTicks();
|
||||
// 帧率统计 每一秒钟的时候记录一次帧率
|
||||
m_frameCounter++;
|
||||
Uint64 currentTime = SDL_GetTicks64();
|
||||
if (currentTime - m_lastFpsPrintTime >= 1000)
|
||||
{
|
||||
u32 fps = m_frameCount;
|
||||
if (m_DebugInfoActor)
|
||||
m_DebugInfoActor->FPS = fps;
|
||||
m_fps = m_frameCounter;
|
||||
m_lastFpsPrintTime = currentTime;
|
||||
m_frameCount = 0;
|
||||
m_frameCounter = 0;
|
||||
}
|
||||
|
||||
// 计算实际总帧时间(关键修改)
|
||||
u32 diff = SDL_GetTicks() - frameStart; // 处理耗时
|
||||
Uint64 diff = SDL_GetTicks64() - frameStart; // 处理耗时
|
||||
if (diff < m_frameTime)
|
||||
{
|
||||
SDL_Delay(m_frameTime - diff); // 尝试延迟补全
|
||||
}
|
||||
// 延迟后,重新计算从帧开始到现在的总时间(包含可能的延迟误差)
|
||||
u32 actualFrameTime = SDL_GetTicks() - frameStart;
|
||||
Uint64 actualFrameTime = SDL_GetTicks64() - frameStart;
|
||||
m_deltaTime = actualFrameTime / 1000.0f; // 用实际总时间更新deltaTime
|
||||
}
|
||||
}
|
||||
@@ -156,26 +153,25 @@ void Game::Update(float deltaTime)
|
||||
m_scene->Update(deltaTime);
|
||||
if (m_uiScene != nullptr)
|
||||
m_uiScene->Update(deltaTime);
|
||||
|
||||
// 调试信息
|
||||
if (m_DebugInfoActor != nullptr)
|
||||
{
|
||||
m_DebugInfoActor->Update(deltaTime);
|
||||
m_DebugInfoActor->RenderCount = RenderCount;
|
||||
}
|
||||
|
||||
RenderCount = 0;
|
||||
}
|
||||
|
||||
void Game::Render()
|
||||
{
|
||||
// 绘制调用次数清0
|
||||
m_RenderCount = 0;
|
||||
|
||||
// 调用预渲染
|
||||
if (m_scene != nullptr)
|
||||
m_scene->PreRender();
|
||||
if (m_uiScene != nullptr)
|
||||
m_uiScene->PreRender();
|
||||
|
||||
// 清空渲染器后渲染
|
||||
SDL_RenderClear(m_renderer);
|
||||
if (m_scene != nullptr)
|
||||
m_scene->Render();
|
||||
if (m_uiScene != nullptr)
|
||||
m_uiScene->Render();
|
||||
if (m_DebugInfoActor != nullptr)
|
||||
m_DebugInfoActor->Render();
|
||||
SDL_RenderPresent(m_renderer);
|
||||
}
|
||||
|
||||
@@ -191,7 +187,6 @@ void Game::Clear()
|
||||
m_uiScene->Exit();
|
||||
}
|
||||
m_uiScene = nullptr;
|
||||
m_DebugInfoActor = nullptr;
|
||||
IMG_Quit();
|
||||
SDL_DestroyRenderer(m_renderer);
|
||||
SDL_DestroyWindow(m_window);
|
||||
|
||||
@@ -57,7 +57,10 @@ public:
|
||||
|
||||
SDL_Renderer *GetRenderer();
|
||||
|
||||
uint32_t RenderCount = 0;
|
||||
// 每秒帧率
|
||||
u32 m_fps;
|
||||
// 每帧绘制调用次数
|
||||
u32 m_RenderCount = 0;
|
||||
|
||||
private:
|
||||
// 构造函数和析构函数设为私有,防止外部创建和销毁
|
||||
@@ -78,18 +81,19 @@ private:
|
||||
|
||||
// 帧数
|
||||
#ifdef __SWITCH__
|
||||
int m_fps = 60;
|
||||
float m_Settingfps = 60.0;
|
||||
#else
|
||||
int m_fps = 2000;
|
||||
float m_Settingfps = 144.0;
|
||||
#endif
|
||||
u32 m_frameTime;
|
||||
float m_deltaTime;
|
||||
// 新增:帧率统计变量
|
||||
u32 m_frameCount; // 每秒内的帧数计数器
|
||||
u32 m_lastFpsPrintTime; // 上一次输出帧率的时间(毫秒,基于 SDL_GetTicks())
|
||||
// 单帧时间
|
||||
float m_frameTime = 0.f;
|
||||
// 每秒内的帧数计数器
|
||||
u32 m_frameCounter;
|
||||
// 上一次输出帧率的时间
|
||||
u32 m_lastFpsPrintTime;
|
||||
|
||||
// 调试信息Actor
|
||||
RefPtr<Debug_Actor> m_DebugInfoActor;
|
||||
// 帧间隔
|
||||
float m_deltaTime = 0.f;
|
||||
|
||||
public:
|
||||
// 屏幕宽高
|
||||
|
||||
@@ -1,155 +0,0 @@
|
||||
#include "Debug_Actor.h"
|
||||
#include "EngineCore/Game.h"
|
||||
#include <string>
|
||||
|
||||
Debug_Actor::Debug_Actor()
|
||||
{
|
||||
m_debugFont = TTF_OpenFont("Fonts/VonwaonBitmap-12px.ttf", 12);
|
||||
if (m_debugFont == nullptr)
|
||||
{
|
||||
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to load debug font: %s", TTF_GetError());
|
||||
}
|
||||
|
||||
FPS_Text = new Text("当 前 帧 数 :", m_debugFont, SDL_Color{255, 255, 255, 255});
|
||||
DT_Text = new Text("帧 时 间 :", m_debugFont, SDL_Color{255, 255, 255, 255});
|
||||
RC_Text = new Text("渲 染 调 用 次 数:", m_debugFont, SDL_Color{255, 255, 255, 255});
|
||||
if (FPS_Text != nullptr)
|
||||
{
|
||||
SDL_Point Pos{26, 26};
|
||||
FPS_Text->SetPos(Pos);
|
||||
this->AddComponent(FPS_Text);
|
||||
}
|
||||
if (DT_Text != nullptr)
|
||||
{
|
||||
SDL_Point Pos{26, 46};
|
||||
DT_Text->SetPos(Pos);
|
||||
this->AddComponent(DT_Text);
|
||||
}
|
||||
if (RC_Text != nullptr)
|
||||
{
|
||||
SDL_Point Pos{26, 66};
|
||||
RC_Text->SetPos(Pos);
|
||||
this->AddComponent(RC_Text);
|
||||
}
|
||||
}
|
||||
|
||||
Debug_Actor::~Debug_Actor()
|
||||
{
|
||||
|
||||
if (m_debugFont != nullptr)
|
||||
{
|
||||
TTF_CloseFont(m_debugFont);
|
||||
m_debugFont = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
void Debug_Actor::Update(float deltaTime)
|
||||
{
|
||||
Actor::Update(deltaTime);
|
||||
|
||||
if (FPS_Text != nullptr)
|
||||
{
|
||||
std::string fpsText = "当前帧数 : " + std::to_string(FPS);
|
||||
FPS_Text->SetText(fpsText);
|
||||
std::string dtText = "帧时间 : " + std::to_string((int)(deltaTime * 1000));
|
||||
DT_Text->SetText(dtText);
|
||||
std::string rcText = "渲染调用次数 : " + std::to_string(RenderCount);
|
||||
RC_Text->SetText(rcText);
|
||||
}
|
||||
}
|
||||
|
||||
void Debug_Actor::Render()
|
||||
{
|
||||
if (FPS_Text != nullptr)
|
||||
{
|
||||
SDL_Renderer *renderer = Game::GetInstance().GetRenderer();
|
||||
if (renderer != nullptr)
|
||||
{
|
||||
|
||||
SDL_Point textPos = FPS_Text->Pos;
|
||||
SDL_Point textSize = FPS_Text->Size;
|
||||
|
||||
int bgX = textPos.x - padding;
|
||||
int bgY = textPos.y - padding;
|
||||
int bgWidth = textSize.x + padding * 2;
|
||||
int bgHeight = textSize.y + padding * 2 * 4;
|
||||
|
||||
DrawRoundedRect(renderer, bgX, bgY, bgWidth, bgHeight, cornerRadius, bgColor);
|
||||
}
|
||||
}
|
||||
|
||||
Actor::Render();
|
||||
}
|
||||
|
||||
void Debug_Actor::DrawRoundedRect(SDL_Renderer *renderer, int x, int y, int w, int h, int radius, SDL_Color color)
|
||||
{
|
||||
if (renderer == nullptr)
|
||||
return;
|
||||
|
||||
SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, color.a);
|
||||
|
||||
SDL_RenderDrawLine(renderer, x + radius, y, x + w - radius, y);
|
||||
SDL_RenderDrawLine(renderer, x + radius, y + h, x + w - radius, y + h);
|
||||
SDL_RenderDrawLine(renderer, x, y + radius, x, y + h - radius);
|
||||
SDL_RenderDrawLine(renderer, x + w, y + radius, x + w, y + h - radius);
|
||||
|
||||
const int segments = 16;
|
||||
for (int i = 0; i < segments; i++)
|
||||
{
|
||||
|
||||
float t1 = static_cast<float>(i) / segments;
|
||||
float t2 = static_cast<float>(i + 1) / segments;
|
||||
|
||||
float angle1 = M_PI * (1.0f - t1);
|
||||
float angle2 = M_PI * (1.0f - t2);
|
||||
SDL_RenderDrawLineF(
|
||||
renderer,
|
||||
x + radius + cos(angle1) * radius, y + radius + sin(angle1) * radius,
|
||||
x + radius + cos(angle2) * radius, y + radius + sin(angle2) * radius);
|
||||
|
||||
angle1 = 2 * M_PI - t1 * M_PI_2;
|
||||
angle2 = 2 * M_PI - t2 * M_PI_2;
|
||||
SDL_RenderDrawLineF(
|
||||
renderer,
|
||||
x + w - radius + cos(angle1) * radius, y + radius + sin(angle1) * radius,
|
||||
x + w - radius + cos(angle2) * radius, y + radius + sin(angle2) * radius);
|
||||
|
||||
angle1 = t1 * M_PI_2;
|
||||
angle2 = t2 * M_PI_2;
|
||||
SDL_RenderDrawLineF(
|
||||
renderer,
|
||||
x + w - radius + cos(angle1) * radius, y + h - radius + sin(angle1) * radius,
|
||||
x + w - radius + cos(angle2) * radius, y + h - radius + sin(angle2) * radius);
|
||||
|
||||
angle1 = M_PI_2 + t1 * M_PI_2;
|
||||
angle2 = M_PI_2 + t2 * M_PI_2;
|
||||
SDL_RenderDrawLineF(
|
||||
renderer,
|
||||
x + radius + cos(angle1) * radius, y + h - radius + sin(angle1) * radius,
|
||||
x + radius + cos(angle2) * radius, y + h - radius + sin(angle2) * radius);
|
||||
}
|
||||
|
||||
for (int dy = 0; dy < h; dy++)
|
||||
{
|
||||
int leftIndent = 0;
|
||||
int rightIndent = 0;
|
||||
|
||||
if (dy < radius)
|
||||
{
|
||||
float ratio = 1.0f - static_cast<float>(dy) / radius;
|
||||
leftIndent = rightIndent = static_cast<int>(radius * (1.0f - sqrt(1.0f - ratio * ratio)));
|
||||
}
|
||||
|
||||
else if (dy > h - radius)
|
||||
{
|
||||
float ratio = static_cast<float>(dy - (h - radius)) / radius;
|
||||
leftIndent = rightIndent = static_cast<int>(radius * (1.0f - sqrt(1.0f - ratio * ratio)));
|
||||
}
|
||||
|
||||
SDL_RenderDrawLine(
|
||||
renderer,
|
||||
x + leftIndent, y + dy,
|
||||
x + w - rightIndent, y + dy);
|
||||
}
|
||||
}
|
||||
@@ -1,36 +0,0 @@
|
||||
#pragma once
|
||||
#include "EngineFrame/Actor/Actor.h"
|
||||
#include "EngineFrame/Component/Text.h"
|
||||
#include <SDL_ttf.h>
|
||||
#include <cstdint>
|
||||
|
||||
class Debug_Actor : public Actor
|
||||
{
|
||||
private:
|
||||
TTF_Font *m_debugFont;
|
||||
RefPtr<Text> FPS_Text;
|
||||
RefPtr<Text> DT_Text;
|
||||
RefPtr<Text> RC_Text;
|
||||
|
||||
public:
|
||||
Debug_Actor();
|
||||
~Debug_Actor() override;
|
||||
|
||||
void Update(float deltaTime) override;
|
||||
void Render() override;
|
||||
|
||||
uint32_t FPS = 0;
|
||||
uint32_t RenderCount = 0;
|
||||
|
||||
SDL_Color bgColor = {0, 0, 0, 90};
|
||||
|
||||
int cornerRadius = 4;
|
||||
int padding = 12;
|
||||
#ifdef __SWITCH__
|
||||
double M_PI = 3.14159265358979323846;
|
||||
double M_PI_2 = 1.57079632679489661923;
|
||||
#endif
|
||||
|
||||
private:
|
||||
void DrawRoundedRect(SDL_Renderer *renderer, int x, int y, int w, int h, int radius, SDL_Color color);
|
||||
};
|
||||
@@ -32,6 +32,18 @@ void BaseNode::Update(float 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()
|
||||
{
|
||||
// 如果有子节点并含有渲染标签,则渲染子节点
|
||||
@@ -150,6 +162,11 @@ void BaseNode::RemoveChild(RefPtr<BaseNode> child)
|
||||
m_BaseNodes.Remove(child);
|
||||
}
|
||||
|
||||
void BaseNode::RemoveAllChild()
|
||||
{
|
||||
m_BaseNodes.Clear();
|
||||
}
|
||||
|
||||
void BaseNode::OnAdded(BaseNode *node)
|
||||
{
|
||||
m_Parent = node;
|
||||
@@ -197,6 +214,11 @@ VecFPos BaseNode::GetPos()
|
||||
return this->transform.position;
|
||||
}
|
||||
|
||||
VecFPos BaseNode::GetWorldPos()
|
||||
{
|
||||
return this->transform.position + this->transformIter.position;
|
||||
}
|
||||
|
||||
void BaseNode::SetScale(VecFPos scale)
|
||||
{
|
||||
if (scale == this->transform.scale)
|
||||
@@ -261,6 +283,16 @@ bool BaseNode::GetVisible()
|
||||
return this->Visible;
|
||||
}
|
||||
|
||||
void BaseNode::SetAlpha(float alpha)
|
||||
{
|
||||
this->Alpha = alpha;
|
||||
}
|
||||
|
||||
float BaseNode::GetAlpha()
|
||||
{
|
||||
return this->Alpha;
|
||||
}
|
||||
|
||||
void BaseNode::SetIterationPos(VecFPos pos)
|
||||
{
|
||||
if (pos == this->transformIter.position)
|
||||
|
||||
@@ -35,6 +35,8 @@ public:
|
||||
VecFPos Anchor = {0.f, 0.f};
|
||||
// 大小
|
||||
VecSize Size = {0, 0};
|
||||
// 透明度
|
||||
float Alpha = 1.f;
|
||||
// 是否显示
|
||||
bool Visible = true;
|
||||
// 计算渲染信息Flag (为了保证每帧只计算一次)
|
||||
@@ -48,6 +50,7 @@ public:
|
||||
virtual void Init();
|
||||
virtual void HandleEvents(SDL_Event *e);
|
||||
virtual void Update(float deltaTime);
|
||||
virtual void PreRender();
|
||||
virtual void Render();
|
||||
virtual void Clear();
|
||||
|
||||
@@ -73,6 +76,8 @@ public:
|
||||
virtual void AddChild(RefPtr<BaseNode> child);
|
||||
// 移除子对象
|
||||
void RemoveChild(RefPtr<BaseNode> child);
|
||||
// 移除所有子对象
|
||||
void RemoveAllChild();
|
||||
// 被添加时
|
||||
virtual void OnAdded(BaseNode *node);
|
||||
// 设置迭代的二维仿射变换
|
||||
@@ -101,6 +106,8 @@ public:
|
||||
virtual void SetPos(VecFPos pos);
|
||||
// 获取坐标
|
||||
VecFPos GetPos();
|
||||
// 获取世界坐标
|
||||
VecFPos GetWorldPos();
|
||||
// 设置缩放
|
||||
virtual void SetScale(VecFPos scale);
|
||||
// 获取缩放
|
||||
@@ -121,4 +128,8 @@ public:
|
||||
virtual void SetVisible(bool visible);
|
||||
// 获取是否显示
|
||||
bool GetVisible();
|
||||
// 设置透明度
|
||||
virtual void SetAlpha(float alpha);
|
||||
// 获取透明度
|
||||
float GetAlpha();
|
||||
};
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#include "Asset/Asset_Script.h"
|
||||
#include "Tool/Math.h"
|
||||
#include "EngineFrame/Actor/Actor.h"
|
||||
#include "EngineCore/Game.h"
|
||||
|
||||
Animation::Animation()
|
||||
{
|
||||
@@ -66,20 +67,20 @@ void Animation::Init(std::string AniPath)
|
||||
// TODO 染色
|
||||
|
||||
// 判断是否有Als
|
||||
if (Asset_Script::GetInstance().GetFileInfo(AniPath + ".als"))
|
||||
{
|
||||
AniScriptParser::AlsInfo Info = AssetManager::GetInstance().StructAlsInfo(AniPath + ".als");
|
||||
if (Info.AniList.size() > 0)
|
||||
{
|
||||
std::string Dir = AniPath.substr(0, AniPath.find_last_of("/") + 1);
|
||||
for (auto &Ani : Info.AniList)
|
||||
{
|
||||
RefPtr<Animation> AlsAniObj = new Animation(Dir + Ani.second.path);
|
||||
AlsAniObj->SetRenderZOrder(Ani.second.layer[1]);
|
||||
AddChild(AlsAniObj);
|
||||
}
|
||||
}
|
||||
}
|
||||
// if (Asset_Script::GetInstance().GetFileInfo(AniPath + ".als"))
|
||||
// {
|
||||
// AniScriptParser::AlsInfo Info = AssetManager::GetInstance().StructAlsInfo(AniPath + ".als");
|
||||
// if (Info.AniList.size() > 0)
|
||||
// {
|
||||
// std::string Dir = AniPath.substr(0, AniPath.find_last_of("/") + 1);
|
||||
// for (auto &Ani : Info.AniList)
|
||||
// {
|
||||
// RefPtr<Animation> AlsAniObj = new Animation(Dir + Ani.second.path);
|
||||
// AlsAniObj->SetRenderZOrder(Ani.second.layer[1]);
|
||||
// AddChild(AlsAniObj);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
FlushFrame(0);
|
||||
}
|
||||
|
||||
@@ -134,7 +135,18 @@ void Animation::Render()
|
||||
{
|
||||
if (!Visible)
|
||||
return;
|
||||
Actor::Render();
|
||||
// 是否裁切
|
||||
if (CropFlag)
|
||||
{
|
||||
SDL_Renderer *renderer = Game::GetInstance().GetRenderer();
|
||||
SDL_RenderSetClipRect(renderer, &CropRect);
|
||||
Actor::Render();
|
||||
SDL_RenderSetClipRect(renderer, NULL);
|
||||
}
|
||||
else
|
||||
{
|
||||
Actor::Render();
|
||||
}
|
||||
}
|
||||
|
||||
void Animation::OnAdded(BaseNode *node)
|
||||
@@ -153,6 +165,7 @@ void Animation::SetVisible(bool visible)
|
||||
if (visible)
|
||||
{
|
||||
CurrentFrame->CalcRenderInfoLogic();
|
||||
CurrentFrame->CalcRenderInfo();
|
||||
}
|
||||
Actor::SetVisible(visible);
|
||||
}
|
||||
@@ -192,7 +205,6 @@ void Animation::FlushFrame(int Index)
|
||||
if (FlagBuf.count("IMAGE_RATE"))
|
||||
{
|
||||
VecFPos Rate = std::get<VecFPos>(FlagBuf["IMAGE_RATE"]);
|
||||
CurrentFrame->SetAnchor(VecFPos{0.5f, 0.5f});
|
||||
CurrentFrame->SetScale(VecFPos{Rate.x, Rate.y});
|
||||
}
|
||||
// 线性减淡
|
||||
@@ -233,7 +245,6 @@ void Animation::FlushFrame(int Index)
|
||||
}
|
||||
// Ani的大小同步为精灵帧对象的大小
|
||||
SetSize(CurrentFrame->GetSize());
|
||||
// 裁切 //TODO
|
||||
}
|
||||
|
||||
void Animation::Reset()
|
||||
@@ -317,3 +328,22 @@ void Animation::InterpolationLogic()
|
||||
CurrentFrame->SetRotation(OldAngleData + (NewAngleData - OldAngleData) * InterRate);
|
||||
}
|
||||
}
|
||||
|
||||
void Animation::SetClipRect(SDL_Rect clipRect)
|
||||
{
|
||||
CropFlag = true;
|
||||
CropRect = clipRect;
|
||||
}
|
||||
|
||||
void Animation::UnsetClipRect()
|
||||
{
|
||||
CropFlag = false;
|
||||
}
|
||||
|
||||
void Animation::SetShadow()
|
||||
{
|
||||
for (auto &Sp : SpriteArr)
|
||||
{
|
||||
Sp->SetShadow();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,12 +36,20 @@ public:
|
||||
void FlushFrame(int Index);
|
||||
void Reset();
|
||||
AniScriptParser::AniFrame GetCurrentFrameInfo();
|
||||
// 设置帧索引
|
||||
void SetFrameIndex(int Index);
|
||||
void InterpolationLogic();
|
||||
// TODO SetOutline
|
||||
// TODO SetDye
|
||||
// TODO SetCrop
|
||||
|
||||
// 设置裁切区域
|
||||
void SetClipRect(SDL_Rect clipRect);
|
||||
// 取消裁切区域
|
||||
void UnsetClipRect();
|
||||
// 设置阴影
|
||||
void SetShadow();
|
||||
|
||||
public:
|
||||
// Ani是否可用
|
||||
bool IsUsability = true;
|
||||
@@ -88,7 +96,9 @@ public:
|
||||
// // 整体染色
|
||||
// DyeAllFlag = false;
|
||||
// // 裁切数据
|
||||
// CropRect = null;
|
||||
SDL_Rect CropRect = {0, 0, 0, 0};
|
||||
// 裁切Flag
|
||||
bool CropFlag = false;
|
||||
|
||||
// 附加选项
|
||||
std::function<std::string(std::string, Animation::ReplaceData)> AdditionalOptions;
|
||||
|
||||
107
source/EngineFrame/Component/AnimationManager.cpp
Normal file
107
source/EngineFrame/Component/AnimationManager.cpp
Normal file
@@ -0,0 +1,107 @@
|
||||
#include "AnimationManager.h"
|
||||
#include "EngineCore/Game.h"
|
||||
|
||||
AnimationManager::AnimationManager()
|
||||
{
|
||||
SDL_Renderer *renderer = Game::GetInstance().GetRenderer();
|
||||
m_Texture = SDL_CreateTexture(
|
||||
renderer,
|
||||
SDL_PIXELFORMAT_ARGB8888,
|
||||
SDL_TEXTUREACCESS_TARGET,
|
||||
Game::GetInstance().Screen_W,
|
||||
Game::GetInstance().Screen_H);
|
||||
}
|
||||
|
||||
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()
|
||||
{
|
||||
SDL_Renderer *renderer = Game::GetInstance().GetRenderer();
|
||||
// 设定渲染目标为纹理
|
||||
SDL_SetRenderTarget(renderer, m_Texture);
|
||||
|
||||
SDL_RenderClear(renderer);
|
||||
// 渲染所有子对象
|
||||
Actor::Render();
|
||||
// 恢复渲染目标为默认窗口
|
||||
SDL_SetRenderTarget(renderer, nullptr);
|
||||
|
||||
SDL_Rect srcRect = {
|
||||
static_cast<int>(m_RenderRect.x), // 子对象包围盒左上角X(纹理中的位置)
|
||||
static_cast<int>(m_RenderRect.y), // 子对象包围盒左上角Y(纹理中的位置)
|
||||
static_cast<int>(m_RenderRect.w), // 包围盒宽度(有效区域宽度)
|
||||
static_cast<int>(m_RenderRect.h) // 包围盒高度(有效区域高度)
|
||||
};
|
||||
|
||||
// SDL_FRect renderToWindow = {
|
||||
// m_RenderRect.x, // 窗口上的X位置(可自定义)
|
||||
// m_RenderRect.y, // 窗口上的Y位置(可自定义)
|
||||
// m_RenderRect.w, // 渲染到窗口的宽度(与源区域等大)
|
||||
// m_RenderRect.h // 渲染到窗口的高度(与源区域等大)
|
||||
// };
|
||||
SDL_FRect renderToWindow = {
|
||||
0, 0, 1080, 720};
|
||||
|
||||
// SDL_Log("m_Texture rect: %f %f %f %f", renderToWindow.x, renderToWindow.y, renderToWindow.w, renderToWindow.h);
|
||||
// SDL_RenderCopyF(renderer, m_Texture, &srcRect, &renderToWindow);
|
||||
SDL_RenderCopyF(renderer, m_Texture, NULL, &renderToWindow);
|
||||
}
|
||||
|
||||
void AnimationManager::AddAnimation(RefPtr<Animation> ani)
|
||||
{
|
||||
this->AddChild(ani);
|
||||
}
|
||||
|
||||
void AnimationManager::Clear()
|
||||
{
|
||||
this->RemoveAllChild();
|
||||
}
|
||||
21
source/EngineFrame/Component/AnimationManager.h
Normal file
21
source/EngineFrame/Component/AnimationManager.h
Normal file
@@ -0,0 +1,21 @@
|
||||
#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();
|
||||
};
|
||||
@@ -9,12 +9,17 @@ class RenderBase : public Component
|
||||
public:
|
||||
struct RenderGuidanceInfo
|
||||
{
|
||||
SDL_Rect rect;
|
||||
SDL_FRect rect;
|
||||
// 旋转角度
|
||||
float rotation;
|
||||
// 翻转Flag
|
||||
SDL_RendererFlip flip = SDL_FLIP_NONE;
|
||||
VecPos AnchorPos;
|
||||
// 锚点坐标
|
||||
VecFPos AnchorPos;
|
||||
// 是否显示
|
||||
bool Visible = true;
|
||||
bool IsInScreen;
|
||||
// 是否在屏幕内
|
||||
bool IsInScreen = false;
|
||||
};
|
||||
|
||||
public:
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
#include "Sprite.h"
|
||||
#include "EngineCore/Game.h"
|
||||
#include "Text.h"
|
||||
Sprite::Sprite()
|
||||
|
||||
void Sprite::Init()
|
||||
{
|
||||
this->Size = m_texture->TextureSize;
|
||||
RenderBase::Init();
|
||||
}
|
||||
|
||||
Sprite::Sprite(std::string imgPath, int Index)
|
||||
@@ -30,27 +33,12 @@ RefPtr<Texture> Sprite::GetTexture()
|
||||
return m_texture;
|
||||
}
|
||||
|
||||
void Sprite::SetIterationPos(VecFPos pos)
|
||||
{
|
||||
RenderBase::SetIterationPos(pos);
|
||||
CalcRenderInfo();
|
||||
}
|
||||
|
||||
void Sprite::HandleEvents(SDL_Event *e)
|
||||
{
|
||||
}
|
||||
|
||||
void Sprite::Update(float deltaTime)
|
||||
{
|
||||
if (CalcRenderInfoFlag && Visible)
|
||||
CalcRenderInfoLogic();
|
||||
}
|
||||
|
||||
void Sprite::CalcRenderInfoLogic()
|
||||
{
|
||||
// 计算缩放因子和翻转状态
|
||||
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;
|
||||
@@ -63,46 +51,55 @@ void Sprite::CalcRenderInfoLogic()
|
||||
_RenderGuidanceInfo.flip = static_cast<SDL_RendererFlip>(SDL_FLIP_VERTICAL | _RenderGuidanceInfo.flip);
|
||||
|
||||
// 纹理数据里带的偏移数据 有这个偏移才能保证动画播放时视觉中心点在一个点上
|
||||
int texturePosX = flipX ? -(m_texture->TextureSize.width + m_texture->TexturePos.x) + SDL_abs(transform.position.x * 2) : m_texture->TexturePos.x;
|
||||
int texturePosY = flipY ? -(m_texture->TextureSize.height + m_texture->TexturePos.y) + SDL_abs(transform.position.y * 2) : m_texture->TexturePos.y;
|
||||
float texturePosX = flipX ? -(m_texture->TextureSize.width + m_texture->TexturePos.x) + SDL_abs(transform.position.x * 2) : m_texture->TexturePos.x;
|
||||
float texturePosY = flipY ? -(m_texture->TextureSize.height + m_texture->TexturePos.y) + SDL_abs(transform.position.y * 2) : m_texture->TexturePos.y;
|
||||
|
||||
// 先计算Img坐标与精灵坐标合成后的真实坐标
|
||||
int RealPosX = transform.position.x + texturePosX;
|
||||
int RealPosY = transform.position.y + texturePosY;
|
||||
float RealPosX = transform.position.x + texturePosX;
|
||||
float RealPosY = transform.position.y + texturePosY;
|
||||
|
||||
// 计算在世界中的位置
|
||||
int baseX = transformIter.position.x + RealPosX;
|
||||
int baseY = transformIter.position.y + RealPosY;
|
||||
float baseX = transformIter.position.x + RealPosX;
|
||||
float baseY = transformIter.position.y + RealPosY;
|
||||
|
||||
// 获取当前帧的原始尺寸
|
||||
int frameWidth = m_texture->TextureSize.width;
|
||||
int frameHeight = m_texture->TextureSize.height;
|
||||
int frameWidth = Size.width;
|
||||
int frameHeight = Size.height;
|
||||
|
||||
// 原始锚点偏移(基于帧尺寸)
|
||||
int origAnchorOffsetX = static_cast<int>(frameWidth * Anchor.x);
|
||||
int origAnchorOffsetY = static_cast<int>(frameHeight * Anchor.y);
|
||||
float origAnchorOffsetX = int(frameWidth * Anchor.x);
|
||||
float origAnchorOffsetY = int(frameHeight * Anchor.y);
|
||||
|
||||
// 缩放的绝对值
|
||||
float absScaleX = SDL_abs(scaleX);
|
||||
float absScaleY = SDL_abs(scaleY);
|
||||
float absScaleX = SDL_fabs(scaleX);
|
||||
float absScaleY = SDL_fabs(scaleY);
|
||||
// 缩放后的尺寸
|
||||
int scaledWidth = static_cast<int>(frameWidth * absScaleX);
|
||||
int scaledHeight = static_cast<int>(frameHeight * absScaleY);
|
||||
float scaledWidth = frameWidth * absScaleX;
|
||||
float scaledHeight = frameHeight * absScaleY;
|
||||
// 缩放后的锚点偏移
|
||||
int scaledAnchorOffsetX = static_cast<int>(origAnchorOffsetX * absScaleX);
|
||||
int scaledAnchorOffsetY = static_cast<int>(origAnchorOffsetY * absScaleY);
|
||||
float scaledAnchorOffsetX = int(origAnchorOffsetX * absScaleX);
|
||||
float scaledAnchorOffsetY = int(origAnchorOffsetY * absScaleY);
|
||||
|
||||
// 计算缩放后的锚点偏移与原锚点偏移的差值
|
||||
int scaleOffsetX = scaledAnchorOffsetX - origAnchorOffsetX;
|
||||
int scaleOffsetY = scaledAnchorOffsetY - origAnchorOffsetY;
|
||||
float scaleOffsetX = scaledAnchorOffsetX - origAnchorOffsetX;
|
||||
float scaleOffsetY = scaledAnchorOffsetY - origAnchorOffsetY;
|
||||
|
||||
// 最终位置计算:世界位置 + 缩放锚点差值 - 缩放后的锚点偏移 + 纹理数据里带的偏移数据 (计算出绘制点的左上角)
|
||||
int Xpos = baseX + scaleOffsetX;
|
||||
int Ypos = baseY + scaleOffsetY;
|
||||
float Xpos = baseX - scaleOffsetX;
|
||||
float Ypos = baseY - scaleOffsetY;
|
||||
|
||||
Xpos = (int)Xpos; // 强制转换为整数
|
||||
Ypos = (int)Ypos; // 强制转换为整数
|
||||
|
||||
// 更新渲染信息
|
||||
_RenderGuidanceInfo.rect = {Xpos, Ypos, scaledWidth, scaledHeight};
|
||||
|
||||
// 如果有阴影 计算阴影的渲染信息
|
||||
if (ShadowFlag)
|
||||
CalculateShadowData();
|
||||
_RenderGuidanceInfo.AnchorPos = {scaledAnchorOffsetX, scaledAnchorOffsetY};
|
||||
// 设置纹理透明度
|
||||
SDL_SetTextureAlphaMod(m_texture->GetTexture(), this->Alpha * 255);
|
||||
|
||||
// 屏幕内检测
|
||||
int screenWidth = Game::GetInstance().Screen_W;
|
||||
@@ -110,12 +107,29 @@ void Sprite::CalcRenderInfoLogic()
|
||||
bool isInScreen = (Xpos + scaledWidth >= 0 && Xpos <= screenWidth && Ypos + scaledHeight >= 0 && Ypos <= screenHeight);
|
||||
_RenderGuidanceInfo.IsInScreen = isInScreen;
|
||||
|
||||
this->Size = {scaledWidth, scaledHeight};
|
||||
// this->Size = {scaledWidth, scaledHeight};
|
||||
_RenderGuidanceInfo.rotation = transformIter.rotation + transform.rotation;
|
||||
|
||||
CalcRenderInfoFlag = false;
|
||||
}
|
||||
|
||||
void Sprite::SetClipRect(SDL_Rect clipRect)
|
||||
{
|
||||
CropFlag = true;
|
||||
CropRect = clipRect;
|
||||
}
|
||||
|
||||
void Sprite::UnsetClipRect()
|
||||
{
|
||||
CropFlag = false;
|
||||
}
|
||||
|
||||
void Sprite::PreRender()
|
||||
{
|
||||
if (CalcRenderInfoFlag && Visible)
|
||||
CalcRenderInfoLogic();
|
||||
}
|
||||
|
||||
void Sprite::Render()
|
||||
{
|
||||
if (!Visible)
|
||||
@@ -126,20 +140,27 @@ void Sprite::Render()
|
||||
|
||||
if (_RenderGuidanceInfo.IsInScreen && _RenderGuidanceInfo.Visible)
|
||||
{
|
||||
// 如果有阴影绘制阴影
|
||||
if (ShadowFlag)
|
||||
{
|
||||
std::array<int, 6> indices = {0, 1, 2, 1, 3, 2};
|
||||
SDL_RenderGeometry(renderer, m_texture->GetTexture(), vertices.data(), vertices.size(), indices.data(), indices.size());
|
||||
}
|
||||
|
||||
if (_RenderGuidanceInfo.rotation != 0.f || _RenderGuidanceInfo.flip != SDL_FLIP_NONE)
|
||||
{
|
||||
SDL_Point AnchorPos = _RenderGuidanceInfo.AnchorPos;
|
||||
SDL_RenderCopyEx(renderer, m_texture->GetTexture(), NULL, &_RenderGuidanceInfo.rect, _RenderGuidanceInfo.rotation, &AnchorPos, _RenderGuidanceInfo.flip);
|
||||
SDL_FPoint AnchorPos = _RenderGuidanceInfo.AnchorPos;
|
||||
SDL_RenderCopyExF(renderer, m_texture->GetTexture(), NULL, &_RenderGuidanceInfo.rect, _RenderGuidanceInfo.rotation, &AnchorPos, _RenderGuidanceInfo.flip);
|
||||
}
|
||||
else
|
||||
{
|
||||
SDL_RenderCopy(renderer, m_texture->GetTexture(), NULL, &_RenderGuidanceInfo.rect);
|
||||
SDL_RenderCopyF(renderer, m_texture->GetTexture(), NULL, &_RenderGuidanceInfo.rect);
|
||||
// // 设置绘制颜色
|
||||
// SDL_SetRenderDrawColor(renderer, 255, 0, 0, 128);
|
||||
// // 绘制填充矩形
|
||||
// SDL_RenderFillRect(renderer, &_RenderGuidanceInfo.rect);
|
||||
// SDL_RenderFillRectF(renderer, &_RenderGuidanceInfo.rect);
|
||||
}
|
||||
Game::GetInstance().RenderCount++;
|
||||
Game::GetInstance().m_RenderCount++;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -157,3 +178,62 @@ SDL_BlendMode Sprite::GetBlendMode()
|
||||
{
|
||||
return m_texture->GetBlendMode();
|
||||
}
|
||||
|
||||
void Sprite::SetShadow()
|
||||
{
|
||||
ShadowFlag = true;
|
||||
CalcRenderInfo();
|
||||
}
|
||||
|
||||
void Sprite::CalculateShadowData()
|
||||
{
|
||||
const float shearX = 1.3f;
|
||||
SDL_FRect guidance = _RenderGuidanceInfo.rect;
|
||||
float shadowW = guidance.w; // 影子宽度(建议与角色宽度一致)
|
||||
float shadowH = guidance.h * 0.33; // 影子高度(可根据需求调整,比如角色高度的1/4)
|
||||
|
||||
// 计算角色脚部中点坐标(底部中心,即需要重叠的锚点)
|
||||
float roleFootX = guidance.x + guidance.w / 2.0f + 3; // 角色X中心 + 位置偏移
|
||||
float roleFootY = guidance.y + guidance.h - 5; // 角色底部Y坐标(脚的位置)
|
||||
|
||||
// 计算错切后的顶点(局部坐标)
|
||||
vertices = {
|
||||
// 左上角(局部坐标)
|
||||
SDL_Vertex{{0.0f, 0.0f}, {0, 0, 0, 110}, {0.0f, 0.0f}},
|
||||
// 右上角(局部坐标)
|
||||
SDL_Vertex{{shadowW, 0.0f}, {0, 0, 0, 110}, {1.0f, 0.0f}},
|
||||
// 左下角(局部坐标)
|
||||
SDL_Vertex{{0.0f + shearX * shadowH, shadowH}, {0, 0, 0, 110}, {0.0f, 1.0f}},
|
||||
// 右下角(局部坐标)
|
||||
SDL_Vertex{{shadowW + shearX * shadowH, shadowH}, {0, 0, 0, 110}, {1.0f, 1.0f}}};
|
||||
|
||||
bool flipHorizontal = (_RenderGuidanceInfo.flip & SDL_FLIP_HORIZONTAL) != 0; // 检查水平翻转标志
|
||||
bool flipVertical = (_RenderGuidanceInfo.flip & SDL_FLIP_VERTICAL) != 0; // 检查垂直翻转标志
|
||||
// 应用纹理坐标翻转(核心逻辑)
|
||||
for (auto &v : vertices)
|
||||
{
|
||||
// 水平翻转:反转u坐标(0→1,1→0)
|
||||
if (flipHorizontal)
|
||||
{
|
||||
v.tex_coord.x = 1.0f - v.tex_coord.x;
|
||||
}
|
||||
// 垂直翻转:反转v坐标(0→1,1→0)
|
||||
if (flipVertical)
|
||||
{
|
||||
v.tex_coord.y = 1.0f - v.tex_coord.y;
|
||||
}
|
||||
}
|
||||
|
||||
// 计算影子底部中点并对齐角色脚部(与之前逻辑一致)
|
||||
float shadowFootX = (vertices[2].position.x + vertices[3].position.x) / 2.0f;
|
||||
float shadowFootY = shadowH;
|
||||
float offsetX = roleFootX - shadowFootX;
|
||||
float offsetY = roleFootY - shadowFootY;
|
||||
|
||||
// 应用位置偏移
|
||||
for (auto &v : vertices)
|
||||
{
|
||||
v.position.x += offsetX;
|
||||
v.position.y += offsetY;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,67 +11,47 @@ class Game;
|
||||
class Sprite : public RenderBase
|
||||
{
|
||||
protected:
|
||||
/* data */
|
||||
RefPtr<Texture> m_texture = nullptr;
|
||||
|
||||
public:
|
||||
/**
|
||||
* @brief Sprite类的默认构造函数
|
||||
*/
|
||||
Sprite(/* args */);
|
||||
/**
|
||||
* @brief Sprite类的带参数构造函数
|
||||
* @param imgPath 纹理图片路径
|
||||
* @param Index 索引值
|
||||
*/
|
||||
Sprite() = default;
|
||||
Sprite(std::string imgPath, int Index);
|
||||
/**
|
||||
* @brief Sprite类的带参数构造函数
|
||||
* @param imgPath 纹理图片文件路径
|
||||
*/
|
||||
Sprite(std::string PngPath);
|
||||
/**
|
||||
* @brief Sprite类的析构函数
|
||||
*/
|
||||
~Sprite();
|
||||
/**
|
||||
* @brief 处理事件
|
||||
* @param e SDL事件指针
|
||||
*/
|
||||
void HandleEvents(SDL_Event *e) override;
|
||||
/**
|
||||
* @brief 更新组件状态
|
||||
* @param deltaTime 时间增量
|
||||
*/
|
||||
void Update(float deltaTime) override;
|
||||
/**
|
||||
* @brief 渲染组件
|
||||
* @param deltaTime 时间增量
|
||||
*/
|
||||
void Render() override;
|
||||
/**
|
||||
* @brief 清理组件资源
|
||||
*/
|
||||
void PreRender() override;
|
||||
void Clear() override;
|
||||
void Init() override;
|
||||
|
||||
/**
|
||||
* @brief 获取纹理
|
||||
* @return SDL_Texture* 纹理指针
|
||||
*/
|
||||
RefPtr<Texture> GetTexture();
|
||||
|
||||
void SetIterationPos(VecFPos pos) override;
|
||||
|
||||
public:
|
||||
// 渲染信息
|
||||
RenderGuidanceInfo _RenderGuidanceInfo;
|
||||
// 裁切数据
|
||||
SDL_Rect CropRect = {0, 0, 0, 0};
|
||||
// 裁切Flag
|
||||
bool CropFlag = false;
|
||||
// 阴影数据
|
||||
std::array<SDL_Vertex, 4> vertices;
|
||||
// 阴影Flag
|
||||
bool ShadowFlag = false;
|
||||
std::string imgPath;
|
||||
int Index;
|
||||
|
||||
public:
|
||||
// 计算渲染信息
|
||||
void CalcRenderInfoLogic();
|
||||
// 设置裁切区域
|
||||
void SetClipRect(SDL_Rect clipRect);
|
||||
// 取消裁切区域
|
||||
void UnsetClipRect();
|
||||
// 设置混合模式
|
||||
void SetBlendMode(SDL_BlendMode blendMode);
|
||||
// 获取混合模式
|
||||
SDL_BlendMode GetBlendMode();
|
||||
// 设置阴影
|
||||
void SetShadow();
|
||||
// 计算阴影数据
|
||||
void CalculateShadowData();
|
||||
};
|
||||
|
||||
@@ -33,6 +33,8 @@ Texture::Texture(std::string PngPath)
|
||||
|
||||
Texture::Texture(std::string imgPath, int Index)
|
||||
{
|
||||
this->ImgPath = imgPath;
|
||||
this->Index = Index;
|
||||
Asset_ImagePack::IMG *Info = Asset_ImagePack::GetInstance().GetIMG(imgPath);
|
||||
if (Info->lpImgName == "sprite/interface/base.img")
|
||||
return;
|
||||
@@ -50,7 +52,7 @@ Texture::Texture(std::string imgPath, int Index)
|
||||
int pitch = Buf.Width * 4;
|
||||
SDL_UpdateTexture(m_texture, NULL, Buf.PNGdata, pitch);
|
||||
SDL_SetTextureBlendMode(m_texture, SDL_BLENDMODE_BLEND);
|
||||
SDL_SetTextureScaleMode(m_texture, SDL_ScaleModeBest);
|
||||
SDL_SetTextureScaleMode(m_texture, SDL_ScaleModeNearest);
|
||||
|
||||
this->TexturePos.x = Buf.Xpos;
|
||||
this->TexturePos.y = Buf.Ypos;
|
||||
@@ -70,6 +72,12 @@ SDL_Texture *Texture::GetTexture()
|
||||
return m_texture;
|
||||
}
|
||||
|
||||
RefPtr<Texture> Texture::GetTextureCopy()
|
||||
{
|
||||
RefPtr<Texture> nt = new Texture(this->ImgPath, this->Index);
|
||||
return nt;
|
||||
}
|
||||
|
||||
void Texture::SetBlendMode(SDL_BlendMode blendMode)
|
||||
{
|
||||
SDL_SetTextureBlendMode(m_texture, blendMode);
|
||||
|
||||
@@ -9,7 +9,8 @@ class Texture : public RefObject
|
||||
{
|
||||
private:
|
||||
SDL_Texture *m_texture = nullptr;
|
||||
|
||||
std::string ImgPath = "";
|
||||
int Index = 0;
|
||||
public:
|
||||
VecSize TextureSize = {0, 0}; // 纹理大小
|
||||
VecPos TexturePos = {0, 0}; // 纹理位置
|
||||
@@ -25,5 +26,8 @@ public:
|
||||
void SetBlendMode(SDL_BlendMode blendMode);
|
||||
// 获取混合模式
|
||||
SDL_BlendMode GetBlendMode();
|
||||
SDL_Texture *GetTexture(); // 获取纹理
|
||||
// 获取纹理
|
||||
SDL_Texture *GetTexture();
|
||||
// 获取一个拷贝的纹理
|
||||
RefPtr<Texture> GetTextureCopy();
|
||||
};
|
||||
|
||||
@@ -20,3 +20,8 @@ void Scene::Init()
|
||||
void Scene::Exit()
|
||||
{
|
||||
}
|
||||
|
||||
RefPtr<BaseNode> Scene::GetCamera()
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
@@ -6,5 +6,6 @@ public:
|
||||
Scene();
|
||||
virtual void Enter();
|
||||
virtual void Exit();
|
||||
virtual RefPtr<BaseNode> GetCamera();
|
||||
void Init();
|
||||
};
|
||||
@@ -84,6 +84,7 @@ typedef struct VecPos
|
||||
y -= other.y;
|
||||
return *this;
|
||||
}
|
||||
|
||||
} VecPos;
|
||||
|
||||
// 浮点数坐标向量
|
||||
@@ -120,6 +121,12 @@ typedef struct VecFPos
|
||||
return VecFPos(x * other.x, y * other.y);
|
||||
}
|
||||
|
||||
// 除法运算符重载:除以一个 float 值
|
||||
VecFPos operator/(float value) const
|
||||
{
|
||||
return VecFPos(x / value, y / value);
|
||||
}
|
||||
|
||||
// 等于运算符重载:判断两个 VecFPos 是否相等
|
||||
// 注意:浮点数比较需要考虑精度问题
|
||||
bool operator==(const VecFPos &other) const
|
||||
@@ -212,38 +219,19 @@ typedef struct VecPos3
|
||||
return x == other.x && y == other.y && z == other.z;
|
||||
}
|
||||
|
||||
bool operator!=(const VecPos3 &other) const
|
||||
{
|
||||
return x != other.x || y != other.y || z != other.z;
|
||||
}
|
||||
|
||||
} VecPos3;
|
||||
|
||||
typedef struct VecFPos3
|
||||
{
|
||||
float x;
|
||||
float y;
|
||||
float z;
|
||||
VecFPos3(float x_ = 0, float y_ = 0, float z_ = 0) : x(x_), y(y_), z(z_) {}
|
||||
|
||||
VecFPos3 operator+(const VecFPos3 &other) const
|
||||
{
|
||||
return VecFPos3(x + other.x, y + other.y, z + other.z);
|
||||
}
|
||||
|
||||
VecFPos3 operator-(const VecFPos3 &other) const
|
||||
{
|
||||
return VecFPos3(x - other.x, y - other.y, z - other.z);
|
||||
}
|
||||
|
||||
bool operator==(const VecFPos3 &other) const
|
||||
{
|
||||
return x == other.x && y == other.y && z == other.z;
|
||||
}
|
||||
|
||||
} VecFPos3;
|
||||
|
||||
typedef struct VecSpeed3
|
||||
{
|
||||
float x;
|
||||
float y;
|
||||
float z;
|
||||
VecSpeed3(float x_ = 0, float y_ = 0, float z_ = 0) : x(x_), y(y_), z(z_) {}
|
||||
int x;
|
||||
int y;
|
||||
int z;
|
||||
VecSpeed3(int x_ = 0, int y_ = 0, int z_ = 0) : x(x_), y(y_), z(z_) {}
|
||||
|
||||
VecSpeed3 operator+(const VecSpeed3 &other) const
|
||||
{
|
||||
|
||||
@@ -15,37 +15,6 @@ std::string Tool_toLowerCase(const std::string &str)
|
||||
{ return std::tolower(c); });
|
||||
return result;
|
||||
}
|
||||
// GBK 转 UTF-8(基于 SDL2 的 SDL_iconv)
|
||||
std::string Tool_Gbk2Utf8(const std::string &gbk_str)
|
||||
{
|
||||
iconv_t cd = iconv_open("UTF-8", "GBK");
|
||||
if (cd == (iconv_t)-1)
|
||||
{
|
||||
throw std::runtime_error("Failed to open iconv conversion descriptor");
|
||||
}
|
||||
|
||||
size_t in_bytes_left = gbk_str.size();
|
||||
char *in_buf = const_cast<char *>(gbk_str.data());
|
||||
|
||||
// 分配输出缓冲区(GBK转UTF-8后长度最多可能增长到原长的3倍)
|
||||
size_t out_bytes_left = gbk_str.size() * 3 + 1;
|
||||
std::string utf8_str(out_bytes_left, '\0');
|
||||
char *out_buf = &utf8_str[0];
|
||||
|
||||
// 执行转换
|
||||
size_t result = iconv(cd, &in_buf, &in_bytes_left, &out_buf, &out_bytes_left);
|
||||
iconv_close(cd);
|
||||
|
||||
if (result == (size_t)-1)
|
||||
{
|
||||
throw std::runtime_error("Conversion failed!");
|
||||
}
|
||||
|
||||
// 调整字符串大小以去除未使用的空间
|
||||
utf8_str.resize(utf8_str.size() - out_bytes_left);
|
||||
return utf8_str;
|
||||
}
|
||||
|
||||
std::string Tool_RegRealPath(const std::string &Path)
|
||||
{
|
||||
// 检查路径中是否包含"../"
|
||||
|
||||
@@ -5,8 +5,6 @@
|
||||
|
||||
std::string Tool_toLowerCase(const std::string &str);
|
||||
|
||||
std::string Tool_Gbk2Utf8(const std::string &gbk_str);
|
||||
|
||||
std::string Tool_RegRealPath(const std::string &Path);
|
||||
|
||||
std::string Tool_TruncatePath(const std::string &path);
|
||||
Reference in New Issue
Block a user