mirror of
https://github.com/zhenyan121/SporeBG-Conid.git
synced 2026-04-10 06:14:08 +08:00
Add draw piece function
This commit is contained in:
@@ -190,4 +190,12 @@ int GameSession::getOldComponentID(int row, int col) {
|
||||
auto it = m_oldPieceIDtoComponentID.find(pieceID);
|
||||
//for (auto [pieceID, y] : m_oldPieceIDtoComponentID)
|
||||
return (it != m_oldPieceIDtoComponentID.end()) ? it->second : -1;
|
||||
}
|
||||
}
|
||||
|
||||
std::optional<std::pair<int, int>> GameSession::getSelectedPiece() const {
|
||||
return m_seletedPiece;
|
||||
}
|
||||
|
||||
const Board* GameSession::getBoard() const {
|
||||
return m_board.get();
|
||||
}
|
||||
@@ -48,4 +48,8 @@ public:
|
||||
bool handleCoordinateInput(int row, int col);
|
||||
|
||||
int getOldComponentID(int row, int col);
|
||||
|
||||
std::optional<std::pair<int, int>> getSelectedPiece() const;
|
||||
|
||||
const Board* getBoard() const;
|
||||
};
|
||||
@@ -1,4 +1,5 @@
|
||||
#include "GameRenderer.h"
|
||||
#include "game/Board.h"
|
||||
#include <iostream>
|
||||
GameRenderer::GameRenderer(int WIDTH, int HEIGHT, SDL_Renderer* renderer) : m_Width(WIDTH), m_Height(HEIGHT), m_renderer(renderer) {
|
||||
m_cellSize = HEIGHT / m_boardRow;
|
||||
@@ -20,13 +21,10 @@ void GameRenderer::beginFrame() {
|
||||
return;
|
||||
}
|
||||
// 清屏为白色色背景
|
||||
SDL_SetRenderDrawColor(m_renderer, 0, 255, 0, 255);
|
||||
SDL_SetRenderDrawColor(m_renderer, 255, 255, 255, 255);
|
||||
SDL_RenderClear(m_renderer);
|
||||
//std::cout << "begin frame\n";
|
||||
// 临时测试:画个红框
|
||||
SDL_SetRenderDrawColor(m_renderer, 255, 0, 0, 255);
|
||||
SDL_FRect test{100, 100, 200, 200};
|
||||
SDL_RenderFillRect(m_renderer, &test);
|
||||
|
||||
}
|
||||
|
||||
void GameRenderer::endFrame() {
|
||||
@@ -36,6 +34,10 @@ void GameRenderer::endFrame() {
|
||||
}
|
||||
|
||||
|
||||
void GameRenderer::setBoard(const Board* board) {
|
||||
m_board = board;
|
||||
}
|
||||
|
||||
void GameRenderer::drawBackground() {
|
||||
|
||||
}
|
||||
@@ -56,21 +58,64 @@ for (int row = 0; row < area.rows; ++row) {
|
||||
static_cast<float>(area.cellSize)
|
||||
};
|
||||
|
||||
bool isLight = (row + col) % 2 == 0;
|
||||
//bool isLight = (row + col) % 2 == 0;
|
||||
SDL_SetRenderDrawColor(m_renderer,
|
||||
isLight ? 240 : 180,
|
||||
isLight ? 220 : 160,
|
||||
isLight ? 180 : 120,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
255);
|
||||
|
||||
// SDL3: RenderFillRect 接受 const SDL_FRect*
|
||||
SDL_RenderFillRect(m_renderer, &rect);
|
||||
SDL_RenderRect(m_renderer, &rect);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
void GameRenderer::drawPiece(std::optional<std::pair<int, int>> selectedPiece) {
|
||||
if (!m_board || !m_renderer) return;
|
||||
|
||||
auto area = getBoardArea();
|
||||
float pieceRadius = m_cellSize * m_pieceRadiusRatio / 2.0f;
|
||||
// 遍历所有格子
|
||||
for (int row = 0; row < m_boardRow; ++row) {
|
||||
for (int col = 0; col < m_boardCOL; ++col) {
|
||||
const Piece* piece = m_board->getPieceAt(row, col);
|
||||
if (!piece) continue; // 没有棋子则跳过
|
||||
|
||||
// 计算棋子中心位置
|
||||
float centerX = area.x + col * area.cellSize + area.cellSize / 2.0f;
|
||||
float centerY = area.y + row * area.cellSize + area.cellSize / 2.0f;
|
||||
|
||||
// 确定棋子颜色
|
||||
SDL_Color color;
|
||||
if (selectedPiece && selectedPiece->first == row && selectedPiece->second == col) {
|
||||
// 选中状态的棋子
|
||||
color = m_colors.selected;
|
||||
} else {
|
||||
// 根据玩家设置颜色
|
||||
color = (piece->getPieceOwner() == PlayerID::P1) ?
|
||||
m_colors.P1 : m_colors.P2;
|
||||
}
|
||||
|
||||
// 绘制棋子(圆形)
|
||||
SDL_SetRenderDrawColor(m_renderer, color.r, color.g, color.b, color.a);
|
||||
|
||||
// 绘制实心圆(使用多个同心圆来近似)
|
||||
for (float r = pieceRadius; r > 0; r -= 1.0f) {
|
||||
SDL_FRect rect = {
|
||||
centerX - r,
|
||||
centerY - r,
|
||||
r * 2.0f,
|
||||
r * 2.0f
|
||||
};
|
||||
SDL_RenderFillRect(m_renderer, &rect);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
BoardArea GameRenderer::getBoardArea() const {
|
||||
return {
|
||||
(m_Width - m_cellSize * m_boardCOL) / 2,
|
||||
|
||||
@@ -1,6 +1,18 @@
|
||||
#pragma once
|
||||
#include "utils/Config.h"
|
||||
#include <SDL3/SDL.h>
|
||||
#include <optional>
|
||||
#include <utility>
|
||||
|
||||
|
||||
struct PlayerColors {
|
||||
SDL_Color P1 = {255, 0, 0, 255}; // 红色
|
||||
SDL_Color P2 = {0, 0, 255, 255}; // 蓝色
|
||||
SDL_Color selected = {255, 255, 0, 255}; // 黄色(选中状态)
|
||||
};
|
||||
|
||||
|
||||
class Board;
|
||||
|
||||
class GameRenderer
|
||||
{
|
||||
@@ -11,6 +23,12 @@ private:
|
||||
int m_cellSize;
|
||||
int m_boardRow = 7;
|
||||
int m_boardCOL = 7;
|
||||
|
||||
const Board* m_board = nullptr;
|
||||
|
||||
// 棋子绘制相关
|
||||
float m_pieceRadiusRatio = 0.8f; // 棋子半径相对于格子大小的比例
|
||||
PlayerColors m_colors;
|
||||
public:
|
||||
GameRenderer(int WIDTH, int HEIGHT, SDL_Renderer* renderer);
|
||||
|
||||
@@ -23,13 +41,16 @@ public:
|
||||
void beginFrame();
|
||||
void endFrame();
|
||||
|
||||
|
||||
// 设置棋盘
|
||||
void setBoard(const Board* board);
|
||||
|
||||
// 渲染函数
|
||||
// 绘制背景
|
||||
void drawBackground();
|
||||
//绘制棋盘
|
||||
void drawBoard();
|
||||
// 绘制棋子
|
||||
void drawPiece(std::optional<std::pair<int, int>> selectedPiece = std::nullopt);
|
||||
|
||||
|
||||
BoardArea getBoardArea() const;
|
||||
|
||||
@@ -13,6 +13,8 @@ void GameScene::onEnter(SDL_Renderer* renderer, int WIDTH, int HEIGHT) {
|
||||
m_gameSession = std::make_unique<GameSession>();
|
||||
m_CoordinateConverter = std::make_unique<CoordinateConverter>(renderer);
|
||||
m_gameSession->initialize();
|
||||
|
||||
m_renderer->setBoard(m_gameSession->getBoard());
|
||||
}
|
||||
|
||||
void GameScene::update() {
|
||||
@@ -23,6 +25,7 @@ void GameScene::render() {
|
||||
m_renderer->beginFrame();
|
||||
m_renderer->drawBackground();
|
||||
m_renderer->drawBoard();
|
||||
m_renderer->drawPiece(m_gameSession->getSelectedPiece());
|
||||
m_renderer->endFrame();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user