From 5ab72bf802e7a79d6eacded4e985740324c4faa7 Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Tue, 15 Mar 2022 09:54:34 -0400 Subject: started adding zooming anim, looks weird esp when moving --- src/game.cpp | 14 +++++++++++--- src/game.h | 8 +++++--- src/renderer.cpp | 36 +++++++++++++++++++++++++++++++----- 3 files changed, 47 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/game.cpp b/src/game.cpp index beabc55..fc57acc 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -523,8 +523,10 @@ void Game::setZoom(size_t zoom) return; } - zoomProgress = 0; - zoomLength = std::abs(static_cast(zoom - curZoom)) * TILE_WIDTH; + /*zoomProgress = 0; + zoomLength = std::abs(static_cast(zoom - curZoom)) * TILE_WIDTH;*/ + zoomProgress.start(62 * std::abs(static_cast(zoom - curZoom)) * TILE_WIDTH); + oldZoom = curZoom; curZoom = zoom; zooming = true; loadMap(); @@ -824,7 +826,7 @@ void Game::update(size_t frameTime) { recalculateRender(); } - zoomTimer.accumulate(frameTime); + /*zoomTimer.accumulate(frameTime); while (zoomTimer.step()) { if (zooming) @@ -836,6 +838,12 @@ void Game::update(size_t frameTime) { zooming = false; } } + }*/ + if (zooming) { + zoomProgress.tick(frameTime); + if (zoomProgress.isComplete()) { + zooming = false; + } } playerAnim.update(frameTime); diff --git a/src/game.h b/src/game.h index 40ace3d..93ca81a 100644 --- a/src/game.h +++ b/src/game.h @@ -78,9 +78,11 @@ public: int maxZoom = INIT_ZOOM; int curZoom = INIT_ZOOM; + int oldZoom; bool zooming = false; - int zoomProgress = 0; - int zoomLength; + //int zoomProgress = 0; + //int zoomLength; + Interpolation zoomProgress; Input keystate; bool firstInput = false; @@ -95,7 +97,7 @@ public: Timer inputTimer = {50}; Timer losePopLampTimer = {800}; Timer losePopPlayerTimer = {3000}; - Timer zoomTimer = {62}; + //Timer zoomTimer = {62}; private: diff --git a/src/renderer.cpp b/src/renderer.cpp index 21c4746..befad8a 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp @@ -325,16 +325,11 @@ void Renderer::renderGame( SDL_SetRenderTarget(ren_.get(), nullptr); - // TODO: this is just moving interp. we also need zoom SDL_Rect zoomRect { TILE_WIDTH, TILE_HEIGHT, (windowTileWidth - 2) * TILE_WIDTH, (windowTileHeight - 2) * TILE_HEIGHT }; if (game.moving) { - double interp = - static_cast(game.zoomProgress) / - static_cast(game.zoomLength); - if (game.player_x > game.player_oldx) { zoomRect.x = game.moveProgress.getProgress(0, TILE_WIDTH); } else if (game.player_x < game.player_oldx) { @@ -348,6 +343,37 @@ void Renderer::renderGame( } } + if (game.zooming) { + int oldWidth = game.oldZoom * ZOOM_X_FACTOR * TILE_WIDTH; + int oldHeight = game.oldZoom * ZOOM_Y_FACTOR * TILE_HEIGHT; + + SDL_Rect oldRect { + zoomRect.w / 2 - oldWidth / 2, + zoomRect.h / 2 - oldHeight / 2, + oldWidth, + oldHeight + }; + + if (game.moving) { + if (game.player_x > game.player_oldx) { + oldRect.x += game.moveProgress.getProgress(0, TILE_WIDTH); + } else if (game.player_x < game.player_oldx) { + oldRect.x -= game.moveProgress.getProgress(TILE_WIDTH, 0); + } + + if (game.player_y > game.player_oldy) { + oldRect.y += game.moveProgress.getProgress(0, TILE_HEIGHT); + } else if (game.player_y < game.player_oldy) { + oldRect.y -= game.moveProgress.getProgress(TILE_HEIGHT, 0); + } + } + + zoomRect.x = game.zoomProgress.getProgress(oldRect.x, zoomRect.x); + zoomRect.y = game.zoomProgress.getProgress(oldRect.y, zoomRect.y); + zoomRect.w = game.zoomProgress.getProgress(oldRect.w, zoomRect.w); + zoomRect.h = game.zoomProgress.getProgress(oldRect.h, zoomRect.h); + } + SDL_RenderCopy(ren_.get(), canvas.get(), &zoomRect, nullptr); SDL_RenderPresent(ren_.get()); } -- cgit 1.4.1