diff --git a/src/graphics/game/BoardRenderer.cpp b/src/graphics/game/BoardRenderer.cpp index c7d3172..c69750c 100644 --- a/src/graphics/game/BoardRenderer.cpp +++ b/src/graphics/game/BoardRenderer.cpp @@ -18,7 +18,11 @@ BoardRenderer::BoardRenderer(int WIDTH, int HEIGHT, SDL_Renderer* renderer, Text BoardRenderer::~BoardRenderer() { - + m_pieceAnimation.grow.clear(); + m_pieceAnimation.move.clear(); + m_pieceAnimation.fight.clear(); + m_pieceAnimation.select.clear(); + std::cout << "BoardRenderer destory\n"; } bool BoardRenderer::initialize() { @@ -125,6 +129,11 @@ void BoardRenderer::updateFightAnimation(float deltaTime) { void BoardRenderer::updateSelectedAnimation(float deltaTime) { // 累加时间,限制不超过总时长 + + if (m_pieceAnimation.select.empty()) { + return; // 没有选中动画,直接退出 + } + auto selectAnimation = m_pieceAnimation.select.begin(); selectAnimation->currentTime += deltaTime; if (selectAnimation->currentTime > selectAnimation->duration) { diff --git a/src/graphics/texture/TextureManager.cpp b/src/graphics/texture/TextureManager.cpp index 2eb3f0c..159e1cb 100644 --- a/src/graphics/texture/TextureManager.cpp +++ b/src/graphics/texture/TextureManager.cpp @@ -1,4 +1,5 @@ #include "TextureManager.h" +#include TextureManager::TextureManager(SDL_Renderer* renderer) : m_renderer(renderer) @@ -27,9 +28,9 @@ void TextureManager::cleanupAllTexture() { SDL_Texture* TextureManager::createTextureFromRect(const SDL_FRect& rect, const SDL_Color& color) { // 先在缓存中查找是否存在 - auto it = getTexture(rect, color); - if(it) { - return it; + SDL_Texture* existing = getTexture(rect, color); + if(existing != nullptr) { + return existing; } if (!m_renderer) { SDL_Log("TextureManager renderer is null\n"); @@ -63,11 +64,18 @@ SDL_Texture* TextureManager::createTextureFromRect(const SDL_FRect& rect, const } size_t TextureManager::makeHash(const SDL_FRect& rect, const SDL_Color& color) { + + // 转为整数,避免浮点误差 + int ix = static_cast(std::round(rect.x)); + int iy = static_cast(std::round(rect.y)); + int iw = static_cast(std::round(rect.w)); + int ih = static_cast(std::round(rect.h)); + // 分别计算字体名称和大小的哈希值 - size_t h1 = std::hash{}(rect.x); - size_t h2 = std::hash{}(rect.y); - size_t h3 = std::hash{}(rect.w); - size_t h4 = std::hash{}(rect.h); + size_t h1 = std::hash{}(ix); + size_t h2 = std::hash{}(iy); + size_t h3 = std::hash{}(iw); + size_t h4 = std::hash{}(ih); size_t h5 = std::hash{}(color.r); size_t h6 = std::hash{}(color.g); @@ -91,8 +99,8 @@ SDL_Texture* TextureManager::getTexture(const SDL_FRect& rect, const SDL_Color& } } -bool TextureManager::destoryTexture(SDL_FRect& rect, SDL_Color& color) { - int key = makeHash(rect, color); +bool TextureManager::destoryTexture(const SDL_FRect& rect, const SDL_Color& color) { + size_t key = makeHash(rect, color); auto it = m_cacheTexture.find(key); if (it == m_cacheTexture.end()) { SDL_Log("can't find the texture\n"); @@ -100,7 +108,7 @@ bool TextureManager::destoryTexture(SDL_FRect& rect, SDL_Color& color) { } SDL_DestroyTexture(it->second); - m_cacheTexture.erase(key); + m_cacheTexture.erase(it); SDL_Log("TextureManager: destory texture sucessfully\n"); return true; } diff --git a/src/graphics/texture/TextureManager.h b/src/graphics/texture/TextureManager.h index 63d79c7..6583037 100644 --- a/src/graphics/texture/TextureManager.h +++ b/src/graphics/texture/TextureManager.h @@ -16,7 +16,7 @@ public: SDL_Texture* getTexture(const SDL_FRect& rect, const SDL_Color& color); - bool destoryTexture(SDL_FRect& rect, SDL_Color& color); + bool destoryTexture(const SDL_FRect& rect, const SDL_Color& color); private: