diff options
author | Kelly Rauchenberger <fefferburbia@gmail.com> | 2018-06-06 16:44:25 -0400 |
---|---|---|
committer | Kelly Rauchenberger <fefferburbia@gmail.com> | 2018-06-06 16:44:25 -0400 |
commit | 456122f5f0086ed0dbe1b784266b96e9624aa8e1 (patch) | |
tree | e67b3715b579164e92b9ce4c99d5ce676ae05596 | |
parent | d851eae8e7cb4192dcd7213dd1a8b60064156b15 (diff) | |
download | ether-456122f5f0086ed0dbe1b784266b96e9624aa8e1.tar.gz ether-456122f5f0086ed0dbe1b784266b96e9624aa8e1.tar.bz2 ether-456122f5f0086ed0dbe1b784266b96e9624aa8e1.zip |
zooming fixes
when zooming out, the view center stays the same (shifted by map edges). when zooming in, the view center becomes the player position at the start of zoom. the boundary of player movement is now also set to the target view area at start of zoom.
-rw-r--r-- | src/game.h | 11 | ||||
-rw-r--r-- | src/main.cpp | 47 | ||||
-rw-r--r-- | src/renderer.cpp | 41 | ||||
-rw-r--r-- | src/renderer.h | 2 |
4 files changed, 72 insertions, 29 deletions
diff --git a/src/game.h b/src/game.h index 6d8a64f..200b691 100644 --- a/src/game.h +++ b/src/game.h | |||
@@ -92,12 +92,19 @@ public: | |||
92 | int player_y = 0; | 92 | int player_y = 0; |
93 | bool renderPlayer = true; | 93 | bool renderPlayer = true; |
94 | 94 | ||
95 | int curZoom = INIT_ZOOM; | ||
96 | int maxZoom = INIT_ZOOM; | 95 | int maxZoom = INIT_ZOOM; |
97 | 96 | ||
97 | int curZoom = INIT_ZOOM; | ||
98 | int curBoundX = map.getLeft(); | ||
99 | int curBoundY = map.getTop(); | ||
100 | |||
98 | bool zooming = false; | 101 | bool zooming = false; |
99 | //size_t oldZoom; | ||
100 | int zoomProgress = 0; | 102 | int zoomProgress = 0; |
103 | int zoomLength; | ||
104 | int lastZoomTop; | ||
105 | int lastZoomLeft; | ||
106 | int lastZoomWidth; | ||
107 | int lastZoomHeight; | ||
101 | 108 | ||
102 | bool firstInput = false; | 109 | bool firstInput = false; |
103 | Input lastInput; | 110 | Input lastInput; |
diff --git a/src/main.cpp b/src/main.cpp index a0085cb..4b13051 100644 --- a/src/main.cpp +++ b/src/main.cpp | |||
@@ -81,7 +81,11 @@ void tick(Game& game, bool onlyDark = false) | |||
81 | 81 | ||
82 | bool movePlayer(Game& game, int x, int y) | 82 | bool movePlayer(Game& game, int x, int y) |
83 | { | 83 | { |
84 | if (game.map.inBounds(x, y) && game.map.at(x,y).tile == Tile::Floor) | 84 | if (x >= game.curBoundX && |
85 | y >= game.curBoundY && | ||
86 | x < game.curBoundX + game.curZoom * ZOOM_X_FACTOR && | ||
87 | y < game.curBoundY + game.curZoom * ZOOM_Y_FACTOR && | ||
88 | game.map.at(x,y).tile == Tile::Floor) | ||
85 | { | 89 | { |
86 | if (game.map.at(game.player_x, game.player_y).tile == Tile::Floor) | 90 | if (game.map.at(game.player_x, game.player_y).tile == Tile::Floor) |
87 | { | 91 | { |
@@ -366,11 +370,36 @@ void setZoom(Game& game, size_t zoom) | |||
366 | growMap(game, zoom); | 370 | growMap(game, zoom); |
367 | } | 371 | } |
368 | 372 | ||
369 | // TODO: don't think this works well with rapid zoom changes | 373 | std::tie( |
370 | game.zoomProgress += (zoom - game.curZoom) * TILE_WIDTH; | 374 | game.lastZoomLeft, |
371 | //game.oldZoom = game.curZoom; | 375 | game.lastZoomTop, |
376 | game.lastZoomWidth, | ||
377 | game.lastZoomHeight) = | ||
378 | Renderer::calculateZoomRect(game); | ||
379 | |||
380 | game.zoomProgress = 0; | ||
381 | game.zoomLength = | ||
382 | std::abs(static_cast<long>(zoom - game.curZoom)) * TILE_WIDTH; | ||
372 | game.curZoom = zoom; | 383 | game.curZoom = zoom; |
373 | game.zooming = true; | 384 | game.zooming = true; |
385 | |||
386 | game.curBoundX = game.player_x - zoom * ZOOM_X_FACTOR / 2; | ||
387 | if (game.curBoundX < game.map.getLeft()) | ||
388 | { | ||
389 | game.curBoundX = game.map.getLeft(); | ||
390 | } else if (game.curBoundX + zoom * ZOOM_X_FACTOR >= game.map.getRight()) | ||
391 | { | ||
392 | game.curBoundX = game.map.getRight() - zoom * ZOOM_X_FACTOR; | ||
393 | } | ||
394 | |||
395 | game.curBoundY = game.player_y - zoom * ZOOM_Y_FACTOR / 2; | ||
396 | if (game.curBoundY < game.map.getTop()) | ||
397 | { | ||
398 | game.curBoundY = game.map.getTop(); | ||
399 | } else if (game.curBoundY + zoom * ZOOM_Y_FACTOR >= game.map.getBottom()) | ||
400 | { | ||
401 | game.curBoundY = game.map.getBottom() - zoom * ZOOM_Y_FACTOR; | ||
402 | } | ||
374 | } | 403 | } |
375 | 404 | ||
376 | int main(int, char**) | 405 | int main(int, char**) |
@@ -691,15 +720,9 @@ int main(int, char**) | |||
691 | { | 720 | { |
692 | if (game.zooming) | 721 | if (game.zooming) |
693 | { | 722 | { |
694 | if (game.zoomProgress > 0) | 723 | game.zoomProgress++; |
695 | { | ||
696 | game.zoomProgress--; | ||
697 | } else if (game.zoomProgress < 0) | ||
698 | { | ||
699 | game.zoomProgress++; | ||
700 | } | ||
701 | 724 | ||
702 | if (game.zoomProgress == 0) | 725 | if (game.zoomProgress == game.zoomLength) |
703 | { | 726 | { |
704 | game.zooming = false; | 727 | game.zooming = false; |
705 | } | 728 | } |
diff --git a/src/renderer.cpp b/src/renderer.cpp index eddd11d..9d3af97 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp | |||
@@ -288,22 +288,33 @@ void Renderer::render( | |||
288 | 288 | ||
289 | SDL_SetRenderTarget(ren_.get(), nullptr); | 289 | SDL_SetRenderTarget(ren_.get(), nullptr); |
290 | 290 | ||
291 | if (!game.zooming) | 291 | SDL_Rect zoomRect; |
292 | |||
293 | std::tie(zoomRect.x, zoomRect.y, zoomRect.w, zoomRect.h) = | ||
294 | calculateZoomRect(game); | ||
295 | |||
296 | SDL_RenderCopy(ren_.get(), canvas.get(), &zoomRect, nullptr); | ||
297 | SDL_RenderPresent(ren_.get()); | ||
298 | } | ||
299 | |||
300 | std::tuple<int, int, int, int> Renderer::calculateZoomRect(const Game& game) | ||
301 | { | ||
302 | int x = game.map.getTrueX(game.curBoundX) * TILE_WIDTH; | ||
303 | int y = game.map.getTrueY(game.curBoundY) * TILE_HEIGHT; | ||
304 | int w = game.curZoom * TILE_WIDTH * ZOOM_X_FACTOR; | ||
305 | int h = game.curZoom * TILE_HEIGHT * ZOOM_Y_FACTOR; | ||
306 | |||
307 | if (game.zooming) | ||
292 | { | 308 | { |
293 | SDL_RenderCopy(ren_.get(), canvas.get(), nullptr, nullptr); | 309 | double interp = |
294 | } else { | 310 | static_cast<double>(game.zoomProgress) / |
295 | // TODO: zooming back in to the player | 311 | static_cast<double>(game.zoomLength); |
296 | SDL_Rect zoomRect { | 312 | |
297 | ((game.maxZoom - game.curZoom) * TILE_WIDTH + game.zoomProgress) | 313 | x = (x - game.lastZoomLeft) * interp + game.lastZoomLeft; |
298 | * ZOOM_X_FACTOR / 2, | 314 | y = (y - game.lastZoomTop) * interp + game.lastZoomTop; |
299 | ((game.maxZoom - game.curZoom) * TILE_HEIGHT + game.zoomProgress) | 315 | w = (w - game.lastZoomWidth) * interp + game.lastZoomWidth; |
300 | * ZOOM_Y_FACTOR / 2, | 316 | h = (h - game.lastZoomHeight) * interp + game.lastZoomHeight; |
301 | (game.curZoom * TILE_WIDTH - game.zoomProgress) * ZOOM_X_FACTOR, | ||
302 | (game.curZoom * TILE_HEIGHT - game.zoomProgress) * ZOOM_Y_FACTOR | ||
303 | }; | ||
304 | |||
305 | SDL_RenderCopy(ren_.get(), canvas.get(), &zoomRect, nullptr); | ||
306 | } | 317 | } |
307 | 318 | ||
308 | SDL_RenderPresent(ren_.get()); | 319 | return {x, y, w, h}; |
309 | } | 320 | } |
diff --git a/src/renderer.h b/src/renderer.h index 4aa27bb..a34b231 100644 --- a/src/renderer.h +++ b/src/renderer.h | |||
@@ -117,6 +117,8 @@ public: | |||
117 | const Game& game, | 117 | const Game& game, |
118 | bool drawDark = true); | 118 | bool drawDark = true); |
119 | 119 | ||
120 | static std::tuple<int, int, int, int> calculateZoomRect(const Game& game); | ||
121 | |||
120 | private: | 122 | private: |
121 | 123 | ||
122 | sdl_wrapper sdl_; | 124 | sdl_wrapper sdl_; |