feat: add support for parallel animations

This commit is contained in:
2026-02-07 19:13:22 +08:00
parent be083d75e9
commit 640d1a2108
4 changed files with 159 additions and 90 deletions

View File

@@ -42,68 +42,111 @@ void BoardRenderer::update(float deltaTime) {
} }
void BoardRenderer::updateGrowAnimation(float deltaTime) { void BoardRenderer::updateGrowAnimation(float deltaTime) {
m_growAnimation.currentTime += deltaTime;
if (m_growAnimation.currentTime > m_growAnimation.animationDuration) { for (auto growAnimation = m_pieceAnimation.grow.begin(); growAnimation != m_pieceAnimation.grow.end(); ) {
m_growAnimation.currentTime = m_growAnimation.animationDuration;
m_growAnimation.isAnimating = false; if (!growAnimation->isAnimating) {
std::cout << "BoardRenderer: grow animation play finished\n";
growAnimation = m_pieceAnimation.grow.erase(growAnimation);
continue;
}
growAnimation->currentTime += deltaTime;
if (growAnimation->currentTime > growAnimation->animationDuration) {
growAnimation->currentTime = growAnimation->animationDuration;
growAnimation->isAnimating = false;
}
growAnimation->renderColor = growAnimation->baseColor;
growAnimation->progress = growAnimation->currentTime / growAnimation->animationDuration;
growAnimation->renderColor.a = Tools::smoothMove(growAnimation->progress, 0, 255);
++growAnimation;
} }
m_growAnimation.renderColor = m_growAnimation.baseColor;
m_growAnimation.progress = m_growAnimation.currentTime / m_growAnimation.animationDuration;
m_growAnimation.renderColor.a = Tools::smoothMove(m_growAnimation.progress, 0, 255);
} }
void BoardRenderer::updateMoveAnimation(float deltaTime) { void BoardRenderer::updateMoveAnimation(float deltaTime) {
m_moveAnimation.currentTime += deltaTime;
if (m_moveAnimation.currentTime > m_moveAnimation.animationDuration) {
m_moveAnimation.currentTime = m_moveAnimation.animationDuration;
m_moveAnimation.isAnimating = false;
}
m_moveAnimation.progress = m_moveAnimation.currentTime / m_moveAnimation.animationDuration;
auto renderRect = m_moveAnimation.fromPieceRect; for (auto moveAnimation = m_pieceAnimation.move.begin(); moveAnimation != m_pieceAnimation.move.end(); ) {
renderRect.x += Tools::smoothMove(m_moveAnimation.progress, 0, m_moveAnimation.distanceCol);
renderRect.y += Tools::smoothMove(m_moveAnimation.progress, 0, m_moveAnimation.distanceRow); if (!moveAnimation->isAnimating) {
m_moveAnimation.renderRect = renderRect;
std::cout << "BoardRenderer: move animation play finished\n";
moveAnimation = m_pieceAnimation.move.erase(moveAnimation);
continue;
}
moveAnimation->currentTime += deltaTime;
if (moveAnimation->currentTime > moveAnimation->animationDuration) {
moveAnimation->currentTime = moveAnimation->animationDuration;
moveAnimation->isAnimating = false;
}
moveAnimation->progress = moveAnimation->currentTime / moveAnimation->animationDuration;
auto renderRect = moveAnimation->fromPieceRect;
renderRect.x += Tools::smoothMove(moveAnimation->progress, 0, moveAnimation->distanceCol);
renderRect.y += Tools::smoothMove(moveAnimation->progress, 0, moveAnimation->distanceRow);
moveAnimation->renderRect = renderRect;
++moveAnimation;
}
} }
void BoardRenderer::updateFightAnimation(float deltaTime) { void BoardRenderer::updateFightAnimation(float deltaTime) {
m_fightAnimation.currentTime += deltaTime; for (auto fightAnimation = m_pieceAnimation.fight.begin(); fightAnimation != m_pieceAnimation.fight.end(); ) {
if (m_fightAnimation.currentTime > m_fightAnimation.animationDuration) { if (!fightAnimation->isAnimating) {
m_fightAnimation.currentTime = m_fightAnimation.animationDuration;
m_fightAnimation.isAnimating = false; std::cout << "BoardRenderer: fight animation play finished\n";
fightAnimation = m_pieceAnimation.fight.erase(fightAnimation);
continue;
} }
m_fightAnimation.progress = m_fightAnimation.currentTime / m_fightAnimation.animationDuration;
auto renderRect = m_fightAnimation.fromPieceRect; fightAnimation->currentTime += deltaTime;
float cX = m_fightAnimation.distanceCol * 0.5f; if (fightAnimation->currentTime > fightAnimation->animationDuration) {
float cY = m_fightAnimation.distanceRow * 0.5f; fightAnimation->currentTime = fightAnimation->animationDuration;
renderRect.x = Tools::pingPongSpring(m_fightAnimation.progress, renderRect.x, cX); fightAnimation->isAnimating = false;
renderRect.y = Tools::pingPongSpring(m_fightAnimation.progress, renderRect.y, cY); }
m_fightAnimation.renderRect = renderRect; fightAnimation->progress = fightAnimation->currentTime / fightAnimation->animationDuration;
auto renderRect = fightAnimation->fromPieceRect;
float cX = fightAnimation->distanceCol * 0.5f;
float cY = fightAnimation->distanceRow * 0.5f;
renderRect.x = Tools::pingPongSpring(fightAnimation->progress, renderRect.x, cX);
renderRect.y = Tools::pingPongSpring(fightAnimation->progress, renderRect.y, cY);
fightAnimation->renderRect = renderRect;
++fightAnimation;
}
} }
void BoardRenderer::updateSelectedAnimation(float deltaTime) { void BoardRenderer::updateSelectedAnimation(float deltaTime) {
// 累加时间,限制不超过总时长 // 累加时间,限制不超过总时长
m_selectAnimation.currentTime += deltaTime; auto selectAnimation = m_pieceAnimation.select.begin();
if (m_selectAnimation.currentTime > m_selectAnimation.duration) { selectAnimation->currentTime += deltaTime;
m_selectAnimation.currentTime = 0; if (selectAnimation->currentTime > selectAnimation->duration) {
m_selectAnimation.isBigger = !m_selectAnimation.isBigger; selectAnimation->currentTime = 0;
selectAnimation->isBigger = !selectAnimation->isBigger;
} }
SDL_FRect renderRect = { SDL_FRect renderRect = {
m_selectAnimation.baseRect.x, selectAnimation->baseRect.x,
m_selectAnimation.baseRect.y, selectAnimation->baseRect.y,
m_selectAnimation.baseRect.w, selectAnimation->baseRect.w,
m_selectAnimation.baseRect.h selectAnimation->baseRect.h
}; };
float progess = m_selectAnimation.currentTime / m_selectAnimation.duration; float progess = selectAnimation->currentTime / selectAnimation->duration;
m_selectAnimation.rotatedAngel = 360 * static_cast<double>(progess); selectAnimation->rotatedAngel = 360 * static_cast<double>(progess);
float scale = 0.2 * progess; float scale = 0.2 * progess;
float baseW = m_selectAnimation.baseRect.w; float baseW = selectAnimation->baseRect.w;
float baseH = m_selectAnimation.baseRect.h; float baseH = selectAnimation->baseRect.h;
if (m_selectAnimation.isBigger) { if (selectAnimation->isBigger) {
renderRect.w = baseW * (1.0f + scale); renderRect.w = baseW * (1.0f + scale);
renderRect.h = baseH * (1.0f + scale); renderRect.h = baseH * (1.0f + scale);
} else { } else {
@@ -111,10 +154,10 @@ void BoardRenderer::updateSelectedAnimation(float deltaTime) {
renderRect.h = baseH * (1.2f - scale); renderRect.h = baseH * (1.2f - scale);
} }
// 居中缩放:重新计算 x, y 使中心点不变 // 居中缩放:重新计算 x, y 使中心点不变
renderRect.x = m_selectAnimation.baseRect.x + (baseW - renderRect.w) / 2.0f; renderRect.x = selectAnimation->baseRect.x + (baseW - renderRect.w) / 2.0f;
renderRect.y = m_selectAnimation.baseRect.y + (baseH - renderRect.h) / 2.0f; renderRect.y = selectAnimation->baseRect.y + (baseH - renderRect.h) / 2.0f;
m_selectAnimation.renderRect = renderRect; selectAnimation->renderRect = renderRect;
} }
void BoardRenderer::drawBackground() { void BoardRenderer::drawBackground() {
@@ -200,37 +243,45 @@ void BoardRenderer::drawPieceAt(int row, int col, std::optional<std::pair<int, i
auto texture = m_textureManager->createTextureFromRect(rect, color); auto texture = m_textureManager->createTextureFromRect(rect, color);
//SDL_FRect srect = {0, 0, rect.w, rect.h}; //SDL_FRect srect = {0, 0, rect.w, rect.h};
if (m_growAnimation.isAnimating && m_growAnimation.row == row && m_growAnimation.col == col) { for (const auto& growAnimation : m_pieceAnimation.grow) {
if (growAnimation.isAnimating && growAnimation.row == row && growAnimation.col == col) {
m_textureManager->destoryTexture(rect, color); m_textureManager->destoryTexture(rect, color);
m_growAnimation.baseColor = color;
auto texture = m_textureManager->createTextureFromRect(rect, m_growAnimation.renderColor); auto texture = m_textureManager->createTextureFromRect(rect, growAnimation.renderColor);
SDL_RenderTexture(m_renderer, texture, nullptr, &rect); SDL_RenderTexture(m_renderer, texture, nullptr, &rect);
return; return;
}
} }
if (isSelected) { if (isSelected) {
auto selectAnimation = m_pieceAnimation.select.begin();
SDL_RenderTextureRotated(m_renderer, texture, nullptr, &m_selectAnimation.renderRect, m_selectAnimation.rotatedAngel, nullptr, SDL_FLIP_NONE); SDL_RenderTextureRotated(m_renderer, texture, nullptr, &selectAnimation->renderRect, selectAnimation->rotatedAngel, nullptr, SDL_FLIP_NONE);
return; return;
} }
if (m_moveAnimation.isAnimating && col == m_moveAnimation.toCol && row == m_moveAnimation.toRow) { for (const auto& moveAnimation : m_pieceAnimation.move) {
//SDL_Log("rendering..\n"); if (moveAnimation.isAnimating && col == moveAnimation.toCol && row == moveAnimation.toRow) {
//SDL_Log("rendering..\n");
SDL_RenderTexture(m_renderer, texture, nullptr, &m_moveAnimation.renderRect); SDL_RenderTexture(m_renderer, texture, nullptr, &moveAnimation.renderRect);
return; return;
}
} }
if (m_fightAnimation.isAnimating && col == m_fightAnimation.fromCol && row == m_fightAnimation.fromRow) { for (const auto& fightAnimation : m_pieceAnimation.fight) {
if (fightAnimation.isAnimating && col == fightAnimation.fromCol && row == fightAnimation.fromRow) {
SDL_RenderTexture(m_renderer, texture, nullptr, &m_fightAnimation.renderRect); SDL_RenderTexture(m_renderer, texture, nullptr, &fightAnimation.renderRect);
return; return;
}
} }
SDL_RenderTexture(m_renderer, texture, nullptr, &rect); SDL_RenderTexture(m_renderer, texture, nullptr, &rect);
} }
@@ -249,7 +300,7 @@ void BoardRenderer::updateSelectedPiece(std::optional<std::pair<int, int>> selec
pieceRadius * 2, pieceRadius * 2,
pieceRadius * 2 pieceRadius * 2
}; };
m_selectAnimation = { SelectAnimation selectAnimation = {
true, true,
1.0f, 1.0f,
0.0f, 0.0f,
@@ -259,6 +310,10 @@ void BoardRenderer::updateSelectedPiece(std::optional<std::pair<int, int>> selec
baseRect, baseRect,
0.0f 0.0f
}; };
// 选择棋子的动画只需要一个
m_pieceAnimation.select.clear();
m_pieceAnimation.select.push_back(selectAnimation);
} }
@@ -428,8 +483,9 @@ void BoardRenderer::handleGamePieceEvent(GamePieceEvent event, int fromRow, int
case (GamePieceEvent::PLACE_PIECE): case (GamePieceEvent::PLACE_PIECE):
break; break;
case (GamePieceEvent::MOVE_PIECE): case (GamePieceEvent::MOVE_PIECE):
{
//SDL_Log("MovePPPPPP\n"); //SDL_Log("MovePPPPPP\n");
m_moveAnimation = { MoveAnimation moveAnimation = {
fromRow, fromCol, fromRow, fromCol,
toRow, toCol, toRow, toCol,
toY - fromY, toY - fromY,
@@ -440,18 +496,31 @@ void BoardRenderer::handleGamePieceEvent(GamePieceEvent event, int fromRow, int
1.0f 1.0f
}; };
m_pieceAnimation.move.push_back(moveAnimation);
break; break;
}
case (GamePieceEvent::GROW_PIECE): case (GamePieceEvent::GROW_PIECE):
m_growAnimation = { {
GrowAnimation growAnimation = {
toRow, toRow,
toCol, toCol,
0.0f, 0.0f,
true, true,
1.0f 1.0f
}; };
SDL_Color color = (piece->getPieceOwner() == PlayerID::P1) ?
m_colors.P1 : m_colors.P2;
growAnimation.baseColor = color;
m_pieceAnimation.grow.push_back(growAnimation);
break; break;
}
case (GamePieceEvent::FIGHT_PIECE): case (GamePieceEvent::FIGHT_PIECE):
m_fightAnimation = { {
FightAnimation fightAnimation = {
fromRow, fromCol, fromRow, fromCol,
toRow, toCol, toRow, toCol,
toY - fromY, toY - fromY,
@@ -461,7 +530,11 @@ void BoardRenderer::handleGamePieceEvent(GamePieceEvent event, int fromRow, int
rect, rect,
1.0f 1.0f
}; };
m_pieceAnimation.fight.push_back(fightAnimation);
break; break;
}
default: default:
break; break;
} }

View File

@@ -8,28 +8,23 @@
#include "graphics/texture/TextureManager.h" #include "graphics/texture/TextureManager.h"
#include "game/GameTypes.h" #include "game/GameTypes.h"
#include <glm/glm.hpp> #include <glm/glm.hpp>
#include <list>
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}; // 蓝色
SDL_Color selected = {255, 255, 0, 255}; // 黄色(选中状态) SDL_Color selected = {255, 255, 0, 255}; // 黄色(选中状态)
}; };
/*
enum class AnimationType { enum class AnimationType {
GROW, GROW,
MOVE, MOVE,
FIGHT, FIGHT,
SELECT SELECT
}; };
/*
struct PieceAnimation {
bool active = false;
float duration = 0.0f;
float currentTime = 0.0f;
SDL_FRect startRect, targetRect; // 或其他参数
AnimationType type;
};
*/ */
struct MoveAnimation { struct MoveAnimation {
int fromRow = -1; int fromRow = -1;
int fromCol = -1; int fromCol = -1;
@@ -82,6 +77,13 @@ struct SelectAnimation {
double rotatedAngel = 0.0f; double rotatedAngel = 0.0f;
}; };
struct PieceAnimation {
std::list<GrowAnimation> grow;
std::list<MoveAnimation> move;
std::list<FightAnimation> fight;
std::list<SelectAnimation> select;
};
class Board; class Board;
@@ -110,13 +112,7 @@ private:
std::optional<std::pair<int, int>> m_lastSelected = std::nullopt; std::optional<std::pair<int, int>> m_lastSelected = std::nullopt;
MoveAnimation m_moveAnimation; PieceAnimation m_pieceAnimation;
GrowAnimation m_growAnimation;
FightAnimation m_fightAnimation;
SelectAnimation m_selectAnimation;
public: public:
BoardRenderer(int WIDTH, int HEIGHT, SDL_Renderer* renderer, TextureManager* textureManager); BoardRenderer(int WIDTH, int HEIGHT, SDL_Renderer* renderer, TextureManager* textureManager);

View File

@@ -24,7 +24,7 @@ void TextureManager::cleanupAllTexture() {
SDL_Texture* TextureManager::createTextureFromRect(SDL_FRect& rect, SDL_Color& color) { SDL_Texture* TextureManager::createTextureFromRect(const SDL_FRect& rect, const SDL_Color& color) {
// 先在缓存中查找是否存在 // 先在缓存中查找是否存在
auto it = getTexture(rect, color); auto it = getTexture(rect, color);
@@ -62,7 +62,7 @@ SDL_Texture* TextureManager::createTextureFromRect(SDL_FRect& rect, SDL_Color& c
return newTexture; return newTexture;
} }
size_t TextureManager::makeHash(SDL_FRect& rect, SDL_Color& color) { size_t TextureManager::makeHash(const SDL_FRect& rect, const SDL_Color& color) {
// 分别计算字体名称和大小的哈希值 // 分别计算字体名称和大小的哈希值
size_t h1 = std::hash<float>{}(rect.x); size_t h1 = std::hash<float>{}(rect.x);
size_t h2 = std::hash<float>{}(rect.y); size_t h2 = std::hash<float>{}(rect.y);
@@ -79,7 +79,7 @@ size_t TextureManager::makeHash(SDL_FRect& rect, SDL_Color& color) {
return h1 ^ (h2 << 1) ^ (h3 << 2) ^ (h4 << 3) ^ (h5 << 4) ^ (h6 << 5) ^ (h7 << 6) ^ (h8 << 7); return h1 ^ (h2 << 1) ^ (h3 << 2) ^ (h4 << 3) ^ (h5 << 4) ^ (h6 << 5) ^ (h7 << 6) ^ (h8 << 7);
} }
SDL_Texture* TextureManager::getTexture(SDL_FRect& rect, SDL_Color& color) { SDL_Texture* TextureManager::getTexture(const SDL_FRect& rect, const SDL_Color& color) {
auto key = makeHash(rect, color); auto key = makeHash(rect, color);
auto it = m_cacheTexture.find(key); auto it = m_cacheTexture.find(key);

View File

@@ -10,11 +10,11 @@ public:
~TextureManager(); ~TextureManager();
SDL_Texture* createTextureFromRect(SDL_FRect& rect, SDL_Color& coler); SDL_Texture* createTextureFromRect(const SDL_FRect& rect, const SDL_Color& coler);
void cleanupAllTexture(); void cleanupAllTexture();
SDL_Texture* getTexture(SDL_FRect& rect, SDL_Color& color); SDL_Texture* getTexture(const SDL_FRect& rect, const SDL_Color& color);
bool destoryTexture(SDL_FRect& rect, SDL_Color& color); bool destoryTexture(SDL_FRect& rect, SDL_Color& color);
@@ -24,6 +24,6 @@ private:
std::unordered_map<size_t, SDL_Texture*> m_cacheTexture; std::unordered_map<size_t, SDL_Texture*> m_cacheTexture;
size_t makeHash(SDL_FRect& rect, SDL_Color& color); size_t makeHash(const SDL_FRect& rect, const SDL_Color& color);
}; };