From 562599b8b47bb15fc8d0cec6a106675b5cfe15e7 Mon Sep 17 00:00:00 2001 From: zhenyan121 <3367366583@qq.com> Date: Thu, 15 Jan 2026 20:49:11 +0800 Subject: [PATCH] feat: add texture destory function --- src/game/GameSession.cpp | 7 ++++++- src/game/GameSession.h | 10 +++++++++- src/game/GameTypes.h | 5 +++++ src/graphics/game/BoardRenderer.cpp | 20 ++++++++++++++++++++ src/graphics/game/BoardRenderer.h | 3 +++ src/graphics/texture/TextureManager.cpp | 14 ++++++++++++++ src/graphics/texture/TextureManager.h | 2 ++ src/scenes/gameplay/GameScene.cpp | 7 ++++++- 8 files changed, 65 insertions(+), 3 deletions(-) diff --git a/src/game/GameSession.cpp b/src/game/GameSession.cpp index 2c22548..cbffe80 100644 --- a/src/game/GameSession.cpp +++ b/src/game/GameSession.cpp @@ -5,6 +5,7 @@ GameSession::GameSession() { m_board = std::make_unique(7, 7); + } @@ -61,6 +62,7 @@ bool GameSession::executeAction(int toRow, int toCol) { if (m_currentActionType == ActionType::GROW) { if (Rule::canGrow(m_board.get(), fromRow, fromCol, toRow, toCol, m_currentPlayer)) { m_board->placePieceAt(toRow, toCol, m_currentPlayer); + m_gamePieceEventCallback(GamePieceEvent::PLACE_PIECE, toRow, toCol); // 如果执行了操作就擦除 markComponentAsUsed(getOldComponentID(fromRow, fromCol)); return true; @@ -69,7 +71,9 @@ bool GameSession::executeAction(int toRow, int toCol) { if (m_currentActionType == ActionType::MOVE) { if (Rule::canMove(m_board.get(), fromRow, fromCol, toRow, toCol, m_currentPlayer)) { m_board->removePieceAt(fromRow, fromCol); + m_gamePieceEventCallback(GamePieceEvent::REMOVE_PIECE, fromRow, fromCol); m_board->removePieceAt(toRow, toCol); + m_gamePieceEventCallback(GamePieceEvent::REMOVE_PIECE, toRow, toCol); m_board->placePieceAt(toRow, toCol, m_currentPlayer); markComponentAsUsed(getOldComponentID(fromRow, fromCol)); @@ -79,8 +83,9 @@ bool GameSession::executeAction(int toRow, int toCol) { if (m_currentActionType == ActionType::SPORE) { if (Rule::canSpore(m_board.get(), fromRow, fromCol, toRow, toCol, m_currentPlayer)) { m_board->removePieceAt(fromRow, fromCol); + m_gamePieceEventCallback(GamePieceEvent::REMOVE_PIECE, fromRow, fromCol); m_board->placePieceAt(toRow, toCol, m_currentPlayer); - + m_gamePieceEventCallback(GamePieceEvent::PLACE_PIECE, toRow, toCol); markComponentAsUsed(getOldComponentID(fromRow, fromCol)); return true; } diff --git a/src/game/GameSession.h b/src/game/GameSession.h index 58d38fa..c11e9c0 100644 --- a/src/game/GameSession.h +++ b/src/game/GameSession.h @@ -3,14 +3,16 @@ #include #include #include - +#include #include "Board.h" class GameSession { + private: + using GamePieceEventCallback = std::function; std::unique_ptr m_board; PlayerID m_currentPlayer = PlayerID::P1; ActionType m_currentActionType = ActionType::GROW; @@ -23,6 +25,8 @@ private: GameState m_gameState = GameState::GAME_RUNING; + GamePieceEventCallback m_gamePieceEventCallback; + public: GameSession(); ~GameSession(); @@ -58,4 +62,8 @@ public: GameState getGameState() const; + void setGamePieceEventCallback(GamePieceEventCallback callback) { + m_gamePieceEventCallback = callback; + } + }; \ No newline at end of file diff --git a/src/game/GameTypes.h b/src/game/GameTypes.h index 672676b..7d22e10 100644 --- a/src/game/GameTypes.h +++ b/src/game/GameTypes.h @@ -26,4 +26,9 @@ enum class GameMode { VS_AI, // 玩家 vs AI ONLINE_PVP, // 网络对战 TUTORIAL_MODE // 教程(可视为特殊模式) +}; + +enum class GamePieceEvent { + REMOVE_PIECE, + PLACE_PIECE }; \ No newline at end of file diff --git a/src/graphics/game/BoardRenderer.cpp b/src/graphics/game/BoardRenderer.cpp index 4bc541c..f3a8604 100644 --- a/src/graphics/game/BoardRenderer.cpp +++ b/src/graphics/game/BoardRenderer.cpp @@ -282,4 +282,24 @@ void BoardRenderer::renderBlackOverlay() { // 恢复原来的混合模式 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; + } + } \ No newline at end of file diff --git a/src/graphics/game/BoardRenderer.h b/src/graphics/game/BoardRenderer.h index 331b9bd..a4b0ebf 100644 --- a/src/graphics/game/BoardRenderer.h +++ b/src/graphics/game/BoardRenderer.h @@ -6,6 +6,7 @@ #include #include #include "graphics/texture/TextureManager.h" +#include "game/GameTypes.h" struct PlayerColors { SDL_Color P1 = {255, 0, 0, 255}; // 红色 SDL_Color P2 = {0, 0, 255, 255}; // 蓝色 @@ -65,6 +66,8 @@ public: void setGameState(GameState state); void renderBlackOverlay(); + + void handleGamePieceEvent(GamePieceEvent event, int row, int col); }; diff --git a/src/graphics/texture/TextureManager.cpp b/src/graphics/texture/TextureManager.cpp index 76a31fa..219d2fc 100644 --- a/src/graphics/texture/TextureManager.cpp +++ b/src/graphics/texture/TextureManager.cpp @@ -81,3 +81,17 @@ SDL_Texture* TextureManager::getTexture(int x, int y) { 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; +} diff --git a/src/graphics/texture/TextureManager.h b/src/graphics/texture/TextureManager.h index 378142c..5893f64 100644 --- a/src/graphics/texture/TextureManager.h +++ b/src/graphics/texture/TextureManager.h @@ -16,6 +16,8 @@ public: SDL_Texture* getTexture(int x, int y); + bool destoryTexture(int x, int y); + private: SDL_Renderer* m_renderer = nullptr; diff --git a/src/scenes/gameplay/GameScene.cpp b/src/scenes/gameplay/GameScene.cpp index 4e8dc98..0ccddd4 100644 --- a/src/scenes/gameplay/GameScene.cpp +++ b/src/scenes/gameplay/GameScene.cpp @@ -71,7 +71,12 @@ void GameScene::onEnter(SDL_Renderer* renderer, int WIDTH, int HEIGHT, UIRendere 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); + } + ); }