Added a lots of the file to write the pixel game but due to some problem ,i dicide to change sdl3 to sfml,a more modern lib

This commit is contained in:
2025-12-21 14:58:33 +08:00
parent 96b005e2ef
commit 86dce63e74
15 changed files with 443 additions and 51 deletions

View File

@@ -34,9 +34,13 @@ public:
* @brief 设置组件位置
* @param x X坐标屏幕像素
* @param y Y坐标屏幕像素
* @param w 宽度(屏幕像素)
* @param h 高度(屏幕像素)
*/
virtual void setPosition(int x, int y) {
virtual void setRect(int x, int y, int w, int h) {
m_rect.w = static_cast<float>(w);
m_rect.h = static_cast<float>(h);
m_rect.x = static_cast<float>(x);
m_rect.y = static_cast<float>(y);
}

View File

@@ -1,24 +1,25 @@
#include "Button.h"
#include "graphics/font/TextRenderer.h"
//#include "graphics/font/TextRenderer.h"
Button::Button()
{
}
Button::Button(TextRenderer* textRenderer) : m_textRenderer(textRenderer) {
}
Button::Button(
const std::string& text,
TextStyle style,
int x,
int y,
TextRenderer* textRenderer,
int w,
int h,
//TextRenderer* textRenderer,
SDL_Color backgroundColor,
int borderThickness,
SDL_Color borderColor
) : m_textRenderer(textRenderer)
) //: m_textRenderer(textRenderer)
{
m_rect.x = static_cast<float>(x);
m_rect.y = static_cast<float>(y);
@@ -29,12 +30,14 @@ Button::Button(
m_buttonData.borderColor = borderColor;
// 如果提供了 TextRenderer则立即测量文本并更新控件尺寸
if (m_textRenderer) {
/*if (m_textRenderer) {
auto [w, h] = m_textRenderer->getTextSize(text, style);
m_rect.w = static_cast<float>(w);
m_rect.h = static_cast<float>(h);
m_buttonData.rect = m_rect;
}
}*/
m_rect.w = static_cast<float>(w);
m_rect.h = static_cast<float>(h);
}
@@ -43,24 +46,24 @@ void Button::setText(const std::string& text, TextStyle style) {
m_buttonData.textstytle = style;
// 如果提供了 TextRenderer则立即测量文本并更新控件尺寸
if (m_textRenderer) {
/*if (m_textRenderer) {
auto [w, h] = m_textRenderer->getTextSize(text, style);
m_rect.w = static_cast<float>(w);
m_rect.h = static_cast<float>(h);
m_buttonData.rect = m_rect;
}
}*/
}
void Button::setText(const std::string& text) {
m_buttonData.text = text;
// 如果提供了 TextRenderer则立即测量文本并更新控件尺寸
if (m_textRenderer) {
/*if (m_textRenderer) {
auto [w, h] = m_textRenderer->getTextSize(text, m_buttonData.textstytle);
m_rect.w = static_cast<float>(w);
m_rect.h = static_cast<float>(h);
m_buttonData.rect = m_rect;
}
}*/
}
void Button::setBackgroundColor(SDL_Color normal) {

View File

@@ -1,6 +1,7 @@
#pragma once
#include "ui/base/UIRenderData.h"
#include "ui/base/UIComponent.h"
#include "utils/Config.h"
#include <memory>
// 前向声明,避免在头文件包含过多实现细节
@@ -10,15 +11,15 @@ class Button : public UIComponent{
public:
// 默认构造(不进行自动测量)
Button();
// 可以传入 TextRenderer 指针以便在 setText 时立即计算文字尺寸并更新 rect
explicit Button(TextRenderer* textRenderer);
explicit Button(
const std::string& text,
TextStyle style = {"SourceHanSansSC-Regular.otf", 48, {0, 0, 0, 255}},
int x = 0,
int y = 0,
TextRenderer* textRenderer = nullptr,
int w = UI::ButtonSize,
int h = UI::ButtonSize,
//TextRenderer* textRenderer = nullptr,
SDL_Color backgroundColor = {200, 200, 200, 255},
int borderThickness = 0,
SDL_Color borderColor = {0, 0, 0, 255}
@@ -78,7 +79,7 @@ private:
std::function<void()> m_callback;
ButtonData m_buttonData;
// 用于在 setText 时测量文本尺寸(非拥有)
TextRenderer* m_textRenderer = nullptr;
//TextRenderer* m_textRenderer = nullptr;
};

View File

@@ -126,10 +126,10 @@ void GameUIManager::updateGameState(GameState state) {
void GameUIManager::setupUIComponents() {
// 这里可以添加更多的UI组件初始化逻辑
auto button = std::make_unique<Button>(m_textRenderer);
auto button = std::make_unique<Button>();
button->setBackgroundColor({255, 100, 0, 255});
button->setBorder(2, {0, 0, 0, 255});
button->setPosition(20, 20);
button->setRect(20, 20, 200, 100);
button->setEnabled(true);
button->setVisible(true);
button->setText("Please Choose", {"SourceHanSansSC-Regular.otf", 48, {0, 0, 0, 255}});
@@ -137,18 +137,16 @@ void GameUIManager::setupUIComponents() {
m_buttons.emplace(button->getNameHash(), std::move(button));
auto label = std::make_unique<Label>();
label->setPosition(1200, 20);
label->setRect(1200, 20, 200, 50);
label->setText("0 0", {"SourceHanSansSC-Regular.otf", 48, {0, 0, 0, 255}});
label->setName("MousePositionLabel");
m_labels.emplace(label->getNameHash(), std::move(label));
auto restartButton = std::make_unique<Button>(
"Restart",
(TextStyle){"SourceHanSansSC-Regular.otf", 48, {0, 0, 0, 255}},
(TextStyle){"unifont.otf", 48, {0, 0, 0, 255}},
700,
500,
m_textRenderer,
(SDL_Color){100, 255, 100, 255}
500
);
restartButton->setCallback([this](){

View File

@@ -13,19 +13,26 @@ MainMenuUIManager::~MainMenuUIManager() {
}
void MainMenuUIManager::init() {
auto button = std::make_unique<Button>(m_textRenderer);
auto button = std::make_unique<Button>();
button->setBackgroundColor({0, 150, 255, 255});
button->setBorder(2, {0, 0, 0, 255});
button->setPosition(650, 430);
button->setRect(0, 0, UI::ButtonSize * 4, UI::ButtonSize * 2);
button->setName("StartButton");
button->setText("Start Game", {"SourceHanSansSC-Regular.otf", 64, {255, 255, 255, 255}});
button->setText("Start Game", {"SourceHanSansSC-Regular.otf", UI::UI_NORMAL_FONT_SIZE, {255, 255, 255, 255}});
button->setCallback([this](){
SDL_Log("Start Game button clicked!");
m_eventCallback("GameScene");
});
m_buttons.emplace(button->getNameHash(), std::move(button));
auto label = std::make_unique<Label>();
label->setPosition(1200, 20);
label->setRect(1200, 20, 200, 50);
label->setText("0 0", {"SourceHanSansSC-Regular.otf", 48, {0, 0, 0, 255}});
label->setName("MousePositionLabel");
m_labels.emplace(label->getNameHash(), std::move(label));