mirror of
https://github.com/zhenyan121/SporeBG-Conid.git
synced 2026-04-09 22:06:09 +08:00
fix: memory access crash in select animation
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#include "TextureManager.h"
|
||||
#include <cmath>
|
||||
|
||||
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<int>(std::round(rect.x));
|
||||
int iy = static_cast<int>(std::round(rect.y));
|
||||
int iw = static_cast<int>(std::round(rect.w));
|
||||
int ih = static_cast<int>(std::round(rect.h));
|
||||
|
||||
// 分别计算字体名称和大小的哈希值
|
||||
size_t h1 = std::hash<float>{}(rect.x);
|
||||
size_t h2 = std::hash<float>{}(rect.y);
|
||||
size_t h3 = std::hash<float>{}(rect.w);
|
||||
size_t h4 = std::hash<float>{}(rect.h);
|
||||
size_t h1 = std::hash<int>{}(ix);
|
||||
size_t h2 = std::hash<int>{}(iy);
|
||||
size_t h3 = std::hash<int>{}(iw);
|
||||
size_t h4 = std::hash<int>{}(ih);
|
||||
|
||||
size_t h5 = std::hash<Uint8>{}(color.r);
|
||||
size_t h6 = std::hash<Uint8>{}(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;
|
||||
}
|
||||
|
||||
@@ -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:
|
||||
|
||||
|
||||
Reference in New Issue
Block a user