commit 4f9f60ee
Add a small animation when switching tiles. Not that concincing though.
Changed files
| M | src/PuzzleSingleImageState.cpp before |
| M | src/PuzzleSingleImageState.hpp before |
diff --git a/src/PuzzleSingleImageState.cpp b/src/PuzzleSingleImageState.cpp
index b85d409..2438494 100644
--- a/src/PuzzleSingleImageState.cpp
+++ b/src/PuzzleSingleImageState.cpp
@@ -98,8 +98,20 @@ void PuzzleSingleImageState::Draw(SDL_Renderer* target) {
if (rotated_pieces[i] == 2 || rotated_pieces[i] == 3) {
flip |= SDL_FLIP_VERTICAL;
}
+
+ // Apply fade animation if this piece is animating
+ if (i == swapAnimatingPiece1 || i == swapAnimatingPiece2) {
+ Uint8 alpha = 255;
+ alpha = static_cast<Uint8>(255 * ((swapAnimationTime - swapAnimationDuration) / swapAnimationDuration));
+ SDL_SetTextureAlphaMod(this->pictureTex, alpha);
+ } else {
+ SDL_SetTextureAlphaMod(this->pictureTex, 255);
+ }
+
SDL_RenderCopyEx(target, this->pictureTex, &source, &destination, 0, nullptr, static_cast<SDL_RendererFlip>(flip) );
}
+ // Reset alpha to full after drawing all pieces
+ SDL_SetTextureAlphaMod(this->pictureTex, 255);
for (size_t i = 0; i < pieces_physical.size(); ++i) {
const SDL_Rect& piece = pieces_physical[i];
if (i == marked_piece) {
@@ -150,6 +162,17 @@ void PuzzleSingleImageState::Update() {
lastTime = currentTime;
confetti.Update(deltaTime);
+ // Update swap animation
+ if (swapAnimatingPiece1 != -1 && swapAnimatingPiece2 != -1) {
+ swapAnimationTime += deltaTime;
+ if (swapAnimationTime >= swapAnimationDuration) {
+ // Animation complete, reset
+ swapAnimatingPiece1 = -1;
+ swapAnimatingPiece2 = -1;
+ swapAnimationTime = 0.0f;
+ }
+ }
+
// If the mouse button is released, make bMouseUp equal true
if ( !(SDL_GetMouseState(nullptr, nullptr)&SDL_BUTTON(1)) ) {
globalData.mouseUp=true;
@@ -172,6 +195,10 @@ void PuzzleSingleImageState::Update() {
}
else {
if (i != marked_piece && marked_piece > -1 && marked_piece < pieces_physical.size()) {
+ // Start swap animation
+ swapAnimatingPiece1 = i;
+ swapAnimatingPiece2 = marked_piece;
+ swapAnimationTime = 0.0f;
std::swap(shuffeled_pieces[i], shuffeled_pieces[marked_piece]);
marked_piece = -1;
}
@@ -241,6 +268,7 @@ void PuzzleSingleImageState::LoadPictureFromFile(const std::string& filename, SD
this->pictureTex = SDL_CreateTextureFromSurface(renderer, bitmapSurface);
std::cerr << resized_image_logical_width << ", " << resized_image_logical_height << ", id: " << picture_id << ", file: " << filename << "\n";
SDL_FreeSurface(bitmapSurface);
+ SDL_SetTextureBlendMode(this->pictureTex, SDL_BLENDMODE_BLEND);
pieces_logical.clear();
SDL_Rect piece;
piece.x = 0;
diff --git a/src/PuzzleSingleImageState.hpp b/src/PuzzleSingleImageState.hpp
index 7ff97f0..5a2b8fd 100644
--- a/src/PuzzleSingleImageState.hpp
+++ b/src/PuzzleSingleImageState.hpp
@@ -62,6 +62,11 @@ private:
SDL_Texture* pictureTex = NULL;
int source_image_height = 1;
int source_image_width = 1;
+ // Swap animation
+ int swapAnimatingPiece1 = -1;
+ int swapAnimatingPiece2 = -1;
+ float swapAnimationTime = 0.0f;
+ const float swapAnimationDuration = 0.1f; // 100ms total
int resized_image_logical_height = 700;
int resized_image_logical_width = 1;
const int resized_image_logical_height_max = 700;