change logictexture to 640x360

This commit is contained in:
2025-12-22 13:32:55 +08:00
parent 27801278a1
commit 473666f95c
5 changed files with 97 additions and 24 deletions

View File

@@ -35,7 +35,8 @@ bool WindowManager::Initialize(GameConfig& config) {
return false;
}
// 关键设置:启用像素模式
/* // 设置逻辑呈现模式,实现分辨率自适应[3](@ref)
SDL_SetRenderLogicalPresentation(m_renderer,

View File

@@ -69,7 +69,65 @@ void TextRenderer::renderText(const std::string& text, TextStyle style, int x, i
TextRenderer::CachedText TextRenderer::createAndCacheTexture(const std::string& text, TextStyle style) {
CachedText result = {nullptr, 0, 0, std::time(nullptr)};
/*
// 获取字体 - 需要大号字体用于高清渲染
const int TARGET_SCALE = 4; // 4倍超采样
int largeFontSize = style.fontSize * TARGET_SCALE;
TTF_Font* largeFont = m_fontManager->getFont(style.fontID, largeFontSize);
if (!largeFont) {
SDL_Log("错误:字体未找到 %s\n", style.fontID.c_str());
return result;
}
// 1. 创建高清离屏纹理(大尺寸)
int targetWidth = 0, targetHeight = 0;
// SDL3 使用 TTF_GetTextSize 替代 TTF_SizeText
if (TTF_GetTextSize(largeFont, text.c_str(), &targetWidth, &targetHeight) < 0) {
SDL_Log("错误:无法测量文本尺寸: %s\n", text.c_str());
return result;
}
// 计算高清纹理尺寸
int hiResWidth = targetWidth * TARGET_SCALE;
int hiResHeight = targetHeight * TARGET_SCALE;
// 创建高清渲染目标纹理
SDL_Texture* hiResTexture = SDL_CreateTexture(
m_renderer,
SDL_PIXELFORMAT_RGBA8888,
SDL_TEXTUREACCESS_TARGET,
hiResWidth,
hiResHeight
);
if (!hiResTexture) {
SDL_Log("错误:无法创建高清纹理\n");
return result;
}
// 设置高清纹理属性
SDL_SetTextureScaleMode(hiResTexture, SDL_SCALEMODE_NEAREST);
SDL_SetTextureBlendMode(hiResTexture, SDL_BLENDMODE_BLEND);
// 2. 渲染到大尺寸高清纹理
SDL_Texture* prevTarget = SDL_GetRenderTarget(m_renderer);
SDL_SetRenderTarget(m_renderer, hiResTexture);
// 用透明色清空高清纹理
SDL_SetRenderDrawColor(m_renderer, 0, 0, 0, 0);
SDL_RenderClear(m_renderer);
// 创建文字表面
SDL_Surface* surface = TTF_RenderText_Solid(largeFont, text.c_str(),text.length(), style.color);
if (!surface) {
SDL_Log("错误:无法创建文字表面 '%s'\n", text.c_str());
return result;
}
*/
// 获取字体
TTF_Font* font = m_fontManager->getFont(style.fontID, style.fontSize);
if (!font) {

View File

@@ -0,0 +1,26 @@
#pragma once
#include "ui/components/Button.h"
#include "ui/components/Label.h"
class UIWidgetFactory {
public:
static std::unique_ptr<Button> createStandardButton(const std::string& name, const std::string& text, int x, int y, std::function<void()> callback) {
auto button = std::make_unique<Button>();
button->setBackgroundColor({0, 150, 255, 255});
button->setBorder(2, {0, 0, 0, 255});
button->setRect(x, y, UI::ButtonSize * 6, UI::ButtonSize * 4);
button->setName(name);
button->setText(text, {"SourceHanSansSC-Regular.otf", UI::UI_NORMAL_FONT_SIZE, {255, 255, 255, 255}});
button->setCallback(callback);
return button;
}
static std::unique_ptr<Label> createStandardLabel(const std::string& name, const std::string& text, int x, int y) {
auto label = std::make_unique<Label>();
label->setRect(x, y, UI::ButtonSize * 4, UI::ButtonSize * 2);
label->setText(text, {"SourceHanSansSC-Regular.otf", UI::UI_NORMAL_FONT_SIZE, {0, 0, 0, 255}});
label->setName(name);
return label;
}
};

View File

@@ -1,5 +1,6 @@
#include "MainMenuUIManager.h"
#include "utils/CoordinateTools.h"
#include "ui/base/UIWidgetFactory.h"
MainMenuUIManager::MainMenuUIManager(SDL_Renderer* renderer,
TextRenderer* textRenderer,
SceneEventCallback eventCallback) {
@@ -13,28 +14,13 @@ MainMenuUIManager::~MainMenuUIManager() {
}
void MainMenuUIManager::init() {
auto button = std::make_unique<Button>();
auto startButton = UIWidgetFactory::createStandardButton(
"StartButton", "开始游戏", 0, 0,
[this]() { m_eventCallback("GameScene"); }
);
button->setBackgroundColor({0, 150, 255, 255});
button->setBorder(2, {0, 0, 0, 255});
button->setRect(0, 0, UI::ButtonSize * 4, UI::ButtonSize * 2);
button->setName("StartButton");
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->setRect(1200, 20, 200, 50);
label->setText("0 0", {"SourceHanSansSC-Regular.otf", 48, {0, 0, 0, 255}});
label->setName("MousePositionLabel");
m_buttons.emplace(startButton->getNameHash(), std::move(startButton));
auto label = UIWidgetFactory::createStandardLabel("MousePositionLabel", "0 0", 240, 0);
m_labels.emplace(label->getNameHash(), std::move(label));
}

View File

@@ -6,9 +6,11 @@ struct GameConfig {
/*
tileSize: 16x16 每个格子的逻辑大小(像素)
屏幕 = 20 × 11 tiles = 320x176 逻辑分辨率
640x360 逻辑分辨率窗口大小的1/2
画面更高清晰
*/
int logicalWidth = 320;
int logicalHeight = 180;
int logicalWidth = 640;
int logicalHeight = 360;
std::string windowTitle = "孢子棋";
bool vsync = true;
int scale = 2;