修改OpenGl渲染底层之前

This commit is contained in:
2025-10-20 20:50:12 +08:00
parent 1b011b9b68
commit 2b888aae5b
61 changed files with 1609 additions and 680 deletions

View File

@@ -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);
}
}

View File

@@ -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);
};