From d1ec962c788286d0eca331ac33b3dd1867618ddb Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Fri, 18 Mar 2022 23:04:04 -0400 Subject: fixed a lot of the zoom problems still looks weird while moving --- res/read_instruction.psd | Bin 0 -> 132202 bytes src/game.cpp | 15 ++++++++------ src/game.h | 4 +++- src/renderer.cpp | 53 ++++++++++++++++++++++++++++------------------- 4 files changed, 44 insertions(+), 28 deletions(-) create mode 100644 res/read_instruction.psd diff --git a/res/read_instruction.psd b/res/read_instruction.psd new file mode 100644 index 0000000..1c05291 Binary files /dev/null and b/res/read_instruction.psd differ diff --git a/src/game.cpp b/src/game.cpp index 08c96ee..8e4dac5 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -163,6 +163,8 @@ bool Game::movePlayer(int x, int y) moveProgress.start(1000/6); dirtyLighting = true; + std::cout << player_x << "," << player_y << std::endl; + int chunkX, chunkY, old_chunkX, old_chunkY; toChunkPos(player_x, player_y, chunkX, chunkY); toChunkPos(player_oldx, player_oldy, old_chunkX, old_chunkY); @@ -394,7 +396,7 @@ void Game::recalculateRender() { if (wallRenders.at(renderDesc) == TilesetIndex(21, 12) && renderDesc != 255) { //std::cout << renderDesc << std::endl; - std::cout << ((renderDesc & (1 << 7)) ? 'X' : 'O'); + /*std::cout << ((renderDesc & (1 << 7)) ? 'X' : 'O'); std::cout << ((renderDesc & (1 << 6)) ? 'X' : 'O'); std::cout << ((renderDesc & (1 << 5)) ? 'X' : 'O'); std::cout << std::endl; @@ -405,7 +407,7 @@ void Game::recalculateRender() { std::cout << ((renderDesc & (1 << 1)) ? 'X' : 'O'); std::cout << ((renderDesc & (1 << 2)) ? 'X' : 'O'); std::cout << ((renderDesc & (1 << 3)) ? 'X' : 'O'); - std::cout << std::endl; + std::cout << std::endl;*/ } } } @@ -562,8 +564,9 @@ void Game::processKickup() } void Game::loadMap() { - int newChunksHoriz = std::ceil(static_cast(curZoom) * ZOOM_X_FACTOR / CHUNK_WIDTH) + 4; - int newChunksVert = std::ceil(static_cast(curZoom) * ZOOM_Y_FACTOR / CHUNK_HEIGHT) + 4; + double zoomBasis = getZoomBasis(); + int newChunksHoriz = std::ceil(zoomBasis * ZOOM_X_FACTOR / CHUNK_WIDTH) + 4; + int newChunksVert = std::ceil(zoomBasis * ZOOM_Y_FACTOR / CHUNK_HEIGHT) + 4; int curPlayerChunkX, curPlayerChunkY; toChunkPos(player_x, player_y, curPlayerChunkX, curPlayerChunkY); @@ -584,14 +587,14 @@ void Game::loadMap() { void Game::setZoom(size_t zoom) { - if (zoom == curZoom) + if (zoom == curZoom || zooming) { return; } /*zoomProgress = 0; zoomLength = std::abs(static_cast(zoom - curZoom)) * TILE_WIDTH;*/ - zoomProgress.start(62 * 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; diff --git a/src/game.h b/src/game.h index c75d43b..6e676ae 100644 --- a/src/game.h +++ b/src/game.h @@ -82,12 +82,14 @@ public: int maxZoom = INIT_ZOOM; int curZoom = INIT_ZOOM; - int oldZoom; + int oldZoom = INIT_ZOOM; bool zooming = false; //int zoomProgress = 0; //int zoomLength; Interpolation zoomProgress; + double getZoomBasis() const { return zooming ? std::max(curZoom, oldZoom) : curZoom; } + Input keystate; bool firstInput = false; Input lastInput; diff --git a/src/renderer.cpp b/src/renderer.cpp index 10027dd..94be197 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp @@ -125,8 +125,8 @@ void Renderer::renderGame( const Game& game, bool drawDark) { - int windowTileWidth = game.curZoom * ZOOM_X_FACTOR + 2; - int windowTileHeight = game.curZoom * ZOOM_Y_FACTOR + 2; + int windowTileWidth = game.getZoomBasis() * ZOOM_X_FACTOR + 2; + int windowTileHeight = game.getZoomBasis() * ZOOM_Y_FACTOR + 2; texture_ptr canvas( SDL_CreateTexture( @@ -146,8 +146,8 @@ void Renderer::renderGame( SDL_SetRenderDrawColor(ren_.get(), rand() % 255, rand() % 255, rand() % 255, 255); SDL_RenderClear(ren_.get()); - int leftmost = game.player_x - game.curZoom * ZOOM_X_FACTOR / 2 - 1; - int topmost = game.player_y - game.curZoom * ZOOM_Y_FACTOR / 2 - 1; + int leftmost = game.player_x - game.getZoomBasis() * ZOOM_X_FACTOR / 2 - 1; + int topmost = game.player_y - game.getZoomBasis() * ZOOM_Y_FACTOR / 2 - 1; for (int y = topmost; y < topmost + windowTileHeight; y++) { for (int x = leftmost; x < leftmost + windowTileWidth; x++) @@ -332,31 +332,38 @@ void Renderer::renderGame( SDL_SetRenderTarget(ren_.get(), nullptr); - SDL_Rect zoomRect { - TILE_WIDTH, TILE_HEIGHT, (windowTileWidth - 2) * TILE_WIDTH, (windowTileHeight - 2) * TILE_HEIGHT - }; + SDL_Rect zoomRect; + zoomRect.w = game.curZoom * ZOOM_X_FACTOR * TILE_WIDTH; + zoomRect.h = game.curZoom * ZOOM_Y_FACTOR * TILE_HEIGHT; + if (!game.zooming) { + zoomRect.x = TILE_WIDTH; + zoomRect.y = TILE_HEIGHT; - if (game.moving) { - if (game.player_x > game.player_oldx) { - zoomRect.x = game.moveProgress.getProgress(0, TILE_WIDTH); - } else if (game.player_x < game.player_oldx) { - zoomRect.x = game.moveProgress.getProgress(2*TILE_WIDTH, TILE_WIDTH); - } + if (game.moving) { + if (game.player_x > game.player_oldx) { + zoomRect.x = game.moveProgress.getProgress(0, TILE_WIDTH); + } else if (game.player_x < game.player_oldx) { + zoomRect.x = game.moveProgress.getProgress(2*TILE_WIDTH, TILE_WIDTH); + } - if (game.player_y > game.player_oldy) { - zoomRect.y = game.moveProgress.getProgress(0, TILE_HEIGHT); - } else if (game.player_y < game.player_oldy) { - zoomRect.y = game.moveProgress.getProgress(TILE_HEIGHT*2, TILE_HEIGHT); + if (game.player_y > game.player_oldy) { + zoomRect.y = game.moveProgress.getProgress(0, TILE_HEIGHT); + } else if (game.player_y < game.player_oldy) { + zoomRect.y = game.moveProgress.getProgress(TILE_HEIGHT*2, TILE_HEIGHT); + } } - } + } else { + int curZoomDiff = game.getZoomBasis() - game.curZoom; + zoomRect.x = (curZoomDiff * ZOOM_X_FACTOR / 2 + 1) * TILE_WIDTH; + zoomRect.y = (curZoomDiff * ZOOM_Y_FACTOR / 2 + 1) * TILE_HEIGHT; - if (game.zooming) { int oldWidth = game.oldZoom * ZOOM_X_FACTOR * TILE_WIDTH; int oldHeight = game.oldZoom * ZOOM_Y_FACTOR * TILE_HEIGHT; + int oldZoomDiff = game.getZoomBasis() - game.oldZoom; SDL_Rect oldRect { - zoomRect.w / 2 - oldWidth / 2, - zoomRect.h / 2 - oldHeight / 2, + (oldZoomDiff * ZOOM_X_FACTOR / 2 + 1) * TILE_WIDTH, + (oldZoomDiff * ZOOM_Y_FACTOR / 2 + 1) * TILE_HEIGHT, oldWidth, oldHeight }; @@ -364,14 +371,18 @@ void Renderer::renderGame( if (game.moving) { if (game.player_x > game.player_oldx) { oldRect.x += game.moveProgress.getProgress(0, TILE_WIDTH); + zoomRect.x += game.moveProgress.getProgress(0, TILE_WIDTH); } else if (game.player_x < game.player_oldx) { oldRect.x -= game.moveProgress.getProgress(TILE_WIDTH, 0); + zoomRect.x -= game.moveProgress.getProgress(TILE_WIDTH, 0); } if (game.player_y > game.player_oldy) { oldRect.y += game.moveProgress.getProgress(0, TILE_HEIGHT); + zoomRect.y += game.moveProgress.getProgress(0, TILE_HEIGHT); } else if (game.player_y < game.player_oldy) { oldRect.y -= game.moveProgress.getProgress(TILE_HEIGHT, 0); + zoomRect.y -= game.moveProgress.getProgress(TILE_HEIGHT, 0); } } -- cgit 1.4.1