mirror of
https://github.com/zhenyan121/SporeBG-Conid.git
synced 2026-04-10 06:14:08 +08:00
feat: add texture destory function
This commit is contained in:
@@ -5,6 +5,7 @@
|
|||||||
GameSession::GameSession()
|
GameSession::GameSession()
|
||||||
{
|
{
|
||||||
m_board = std::make_unique<Board>(7, 7);
|
m_board = std::make_unique<Board>(7, 7);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -61,6 +62,7 @@ bool GameSession::executeAction(int toRow, int toCol) {
|
|||||||
if (m_currentActionType == ActionType::GROW) {
|
if (m_currentActionType == ActionType::GROW) {
|
||||||
if (Rule::canGrow(m_board.get(), fromRow, fromCol, toRow, toCol, m_currentPlayer)) {
|
if (Rule::canGrow(m_board.get(), fromRow, fromCol, toRow, toCol, m_currentPlayer)) {
|
||||||
m_board->placePieceAt(toRow, toCol, m_currentPlayer);
|
m_board->placePieceAt(toRow, toCol, m_currentPlayer);
|
||||||
|
m_gamePieceEventCallback(GamePieceEvent::PLACE_PIECE, toRow, toCol);
|
||||||
// 如果执行了操作就擦除
|
// 如果执行了操作就擦除
|
||||||
markComponentAsUsed(getOldComponentID(fromRow, fromCol));
|
markComponentAsUsed(getOldComponentID(fromRow, fromCol));
|
||||||
return true;
|
return true;
|
||||||
@@ -69,7 +71,9 @@ bool GameSession::executeAction(int toRow, int toCol) {
|
|||||||
if (m_currentActionType == ActionType::MOVE) {
|
if (m_currentActionType == ActionType::MOVE) {
|
||||||
if (Rule::canMove(m_board.get(), fromRow, fromCol, toRow, toCol, m_currentPlayer)) {
|
if (Rule::canMove(m_board.get(), fromRow, fromCol, toRow, toCol, m_currentPlayer)) {
|
||||||
m_board->removePieceAt(fromRow, fromCol);
|
m_board->removePieceAt(fromRow, fromCol);
|
||||||
|
m_gamePieceEventCallback(GamePieceEvent::REMOVE_PIECE, fromRow, fromCol);
|
||||||
m_board->removePieceAt(toRow, toCol);
|
m_board->removePieceAt(toRow, toCol);
|
||||||
|
m_gamePieceEventCallback(GamePieceEvent::REMOVE_PIECE, toRow, toCol);
|
||||||
m_board->placePieceAt(toRow, toCol, m_currentPlayer);
|
m_board->placePieceAt(toRow, toCol, m_currentPlayer);
|
||||||
|
|
||||||
markComponentAsUsed(getOldComponentID(fromRow, fromCol));
|
markComponentAsUsed(getOldComponentID(fromRow, fromCol));
|
||||||
@@ -79,8 +83,9 @@ bool GameSession::executeAction(int toRow, int toCol) {
|
|||||||
if (m_currentActionType == ActionType::SPORE) {
|
if (m_currentActionType == ActionType::SPORE) {
|
||||||
if (Rule::canSpore(m_board.get(), fromRow, fromCol, toRow, toCol, m_currentPlayer)) {
|
if (Rule::canSpore(m_board.get(), fromRow, fromCol, toRow, toCol, m_currentPlayer)) {
|
||||||
m_board->removePieceAt(fromRow, fromCol);
|
m_board->removePieceAt(fromRow, fromCol);
|
||||||
|
m_gamePieceEventCallback(GamePieceEvent::REMOVE_PIECE, fromRow, fromCol);
|
||||||
m_board->placePieceAt(toRow, toCol, m_currentPlayer);
|
m_board->placePieceAt(toRow, toCol, m_currentPlayer);
|
||||||
|
m_gamePieceEventCallback(GamePieceEvent::PLACE_PIECE, toRow, toCol);
|
||||||
markComponentAsUsed(getOldComponentID(fromRow, fromCol));
|
markComponentAsUsed(getOldComponentID(fromRow, fromCol));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,14 +3,16 @@
|
|||||||
#include <optional>
|
#include <optional>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <functional>
|
||||||
#include "Board.h"
|
#include "Board.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class GameSession {
|
class GameSession {
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
using GamePieceEventCallback = std::function<void(GamePieceEvent, int x, int y)>;
|
||||||
std::unique_ptr<Board> m_board;
|
std::unique_ptr<Board> m_board;
|
||||||
PlayerID m_currentPlayer = PlayerID::P1;
|
PlayerID m_currentPlayer = PlayerID::P1;
|
||||||
ActionType m_currentActionType = ActionType::GROW;
|
ActionType m_currentActionType = ActionType::GROW;
|
||||||
@@ -23,6 +25,8 @@ private:
|
|||||||
|
|
||||||
GameState m_gameState = GameState::GAME_RUNING;
|
GameState m_gameState = GameState::GAME_RUNING;
|
||||||
|
|
||||||
|
GamePieceEventCallback m_gamePieceEventCallback;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
GameSession();
|
GameSession();
|
||||||
~GameSession();
|
~GameSession();
|
||||||
@@ -58,4 +62,8 @@ public:
|
|||||||
|
|
||||||
GameState getGameState() const;
|
GameState getGameState() const;
|
||||||
|
|
||||||
|
void setGamePieceEventCallback(GamePieceEventCallback callback) {
|
||||||
|
m_gamePieceEventCallback = callback;
|
||||||
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
@@ -26,4 +26,9 @@ enum class GameMode {
|
|||||||
VS_AI, // 玩家 vs AI
|
VS_AI, // 玩家 vs AI
|
||||||
ONLINE_PVP, // 网络对战
|
ONLINE_PVP, // 网络对战
|
||||||
TUTORIAL_MODE // 教程(可视为特殊模式)
|
TUTORIAL_MODE // 教程(可视为特殊模式)
|
||||||
|
};
|
||||||
|
|
||||||
|
enum class GamePieceEvent {
|
||||||
|
REMOVE_PIECE,
|
||||||
|
PLACE_PIECE
|
||||||
};
|
};
|
||||||
@@ -282,4 +282,24 @@ void BoardRenderer::renderBlackOverlay() {
|
|||||||
|
|
||||||
// 恢复原来的混合模式
|
// 恢复原来的混合模式
|
||||||
SDL_SetRenderDrawBlendMode(m_renderer, SDL_BLENDMODE_NONE);
|
SDL_SetRenderDrawBlendMode(m_renderer, SDL_BLENDMODE_NONE);
|
||||||
|
}
|
||||||
|
|
||||||
|
void BoardRenderer::handleGamePieceEvent(GamePieceEvent event, int row, int col) {
|
||||||
|
auto area = getBoardArea();
|
||||||
|
float x = area.x + col * area.cellSize;
|
||||||
|
float y = area.y + row * area.cellSize;
|
||||||
|
float pieceRadius = m_cellSize * m_pieceRadiusRatio / 2.0f;
|
||||||
|
switch (event) {
|
||||||
|
case (GamePieceEvent::REMOVE_PIECE):
|
||||||
|
//SDL_Log("BoardRenderer: try to destory texture\n");
|
||||||
|
m_textureManager->destoryTexture(
|
||||||
|
x + (area.cellSize - pieceRadius * 2) / 2.0f,
|
||||||
|
y + (area.cellSize - pieceRadius * 2) / 2.0f);
|
||||||
|
break;
|
||||||
|
case (GamePieceEvent::PLACE_PIECE):
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -6,6 +6,7 @@
|
|||||||
#include <utility>
|
#include <utility>
|
||||||
#include <unordered_set>
|
#include <unordered_set>
|
||||||
#include "graphics/texture/TextureManager.h"
|
#include "graphics/texture/TextureManager.h"
|
||||||
|
#include "game/GameTypes.h"
|
||||||
struct PlayerColors {
|
struct PlayerColors {
|
||||||
SDL_Color P1 = {255, 0, 0, 255}; // 红色
|
SDL_Color P1 = {255, 0, 0, 255}; // 红色
|
||||||
SDL_Color P2 = {0, 0, 255, 255}; // 蓝色
|
SDL_Color P2 = {0, 0, 255, 255}; // 蓝色
|
||||||
@@ -65,6 +66,8 @@ public:
|
|||||||
void setGameState(GameState state);
|
void setGameState(GameState state);
|
||||||
|
|
||||||
void renderBlackOverlay();
|
void renderBlackOverlay();
|
||||||
|
|
||||||
|
void handleGamePieceEvent(GamePieceEvent event, int row, int col);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -81,3 +81,17 @@ SDL_Texture* TextureManager::getTexture(int x, int y) {
|
|||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool TextureManager::destoryTexture(int x, int y) {
|
||||||
|
int key = makeHash(x, y);
|
||||||
|
auto it = m_cacheTexture.find(key);
|
||||||
|
if (it == m_cacheTexture.end()) {
|
||||||
|
SDL_Log("can't find the texture\n");
|
||||||
|
return false;
|
||||||
|
|
||||||
|
}
|
||||||
|
SDL_DestroyTexture(it->second);
|
||||||
|
m_cacheTexture.erase(key);
|
||||||
|
SDL_Log("TextureManager: destory texture sucessfully\n");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|||||||
@@ -16,6 +16,8 @@ public:
|
|||||||
|
|
||||||
SDL_Texture* getTexture(int x, int y);
|
SDL_Texture* getTexture(int x, int y);
|
||||||
|
|
||||||
|
bool destoryTexture(int x, int y);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
SDL_Renderer* m_renderer = nullptr;
|
SDL_Renderer* m_renderer = nullptr;
|
||||||
|
|||||||
@@ -71,7 +71,12 @@ void GameScene::onEnter(SDL_Renderer* renderer, int WIDTH, int HEIGHT, UIRendere
|
|||||||
|
|
||||||
m_boardRenderer->setBoard(m_gameSession->getBoard());
|
m_boardRenderer->setBoard(m_gameSession->getBoard());
|
||||||
|
|
||||||
|
m_gameSession->setGamePieceEventCallback(
|
||||||
|
[this](GamePieceEvent evnet, int row, int col) {
|
||||||
|
std::cout << "GameScene: recevie the event piece at " << row << " " << col << "\n";
|
||||||
|
m_boardRenderer->handleGamePieceEvent(evnet, row, col);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user