SwitchGame/source/EngineFrame/Actor/Debug_Actor.cpp

146 lines
4.4 KiB
C++

#include "Debug_Actor.h"
#include "EngineCore/Game.h"
#include <string>
Debug_Actor::Debug_Actor()
{
m_debugFont = TTF_OpenFont("Fonts/GasinamuNew.ttf", 16);
if (m_debugFont == nullptr)
{
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to load debug font: %s", TTF_GetError());
}
FPS_Text = new Text("Current FPS:", m_debugFont, SDL_Color{255, 255, 255, 255});
DT_Text = new Text("DeltaTime :", 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);
}
}
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 = "Current FPS: " + std::to_string(FPS);
FPS_Text->SetText(fpsText);
std::string dtText = "DeltaTime: " + std::to_string((int)(deltaTime * 1000));
DT_Text->SetText(dtText);
}
}
void Debug_Actor::Render(float deltaTime)
{
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(deltaTime);
}
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);
}
}