From 7cd515fcfa1a5aac5605fcc63381033563fbf5d7 Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Mon, 14 Mar 2022 14:11:05 -0400 Subject: map is now "infinite" using chunks that dynamically load like in diamond&pearl game is also slow as shit now --- src/renderer.cpp | 83 ++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 53 insertions(+), 30 deletions(-) (limited to 'src/renderer.cpp') diff --git a/src/renderer.cpp b/src/renderer.cpp index 9ef8bb9..1f51e0a 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp @@ -12,7 +12,7 @@ Renderer::Renderer() GAME_HEIGHT, SDL_WINDOW_SHOWN)); - SDL_SetWindowFullscreen(win_.get(), SDL_WINDOW_FULLSCREEN_DESKTOP); + //SDL_SetWindowFullscreen(win_.get(), SDL_WINDOW_FULLSCREEN_DESKTOP); if (!win_) { @@ -115,13 +115,16 @@ void Renderer::renderGame( const Game& game, bool drawDark) { + int windowTileWidth = game.curZoom * ZOOM_X_FACTOR + 2; + int windowTileHeight = game.curZoom * ZOOM_Y_FACTOR + 2; + texture_ptr canvas( SDL_CreateTexture( ren_.get(), SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, - TILE_WIDTH * game.map.getWidth(), - TILE_HEIGHT * game.map.getHeight())); + windowTileWidth * TILE_WIDTH, + windowTileHeight * TILE_HEIGHT)); if (!canvas) { @@ -133,15 +136,17 @@ void Renderer::renderGame( SDL_SetRenderDrawColor(ren_.get(), rand() % 255, rand() % 255, rand() % 255, 255); SDL_RenderClear(ren_.get()); - for (int y = game.map.getTop(); y < game.map.getBottom(); y++) + int leftmost = game.player_x - game.curZoom * ZOOM_X_FACTOR / 2 - 1; + int topmost = game.player_y - game.curZoom * ZOOM_Y_FACTOR / 2 - 1; + for (int y = topmost; y < topmost + windowTileHeight; y++) { - for (int x = game.map.getLeft(); x < game.map.getRight(); x++) + for (int x = leftmost; x < leftmost + windowTileWidth; x++) { bool draw = true; bool drawColour = false; SDL_Rect rect { - game.map.getTrueX(x) * TILE_WIDTH, - game.map.getTrueY(y) * TILE_HEIGHT, + (x - leftmost) * TILE_WIDTH, + (y - topmost) * TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT}; @@ -190,14 +195,14 @@ void Renderer::renderGame( if (game.renderPlayer) { SDL_Rect rect { - game.map.getTrueX(game.player_x) * TILE_WIDTH, - game.map.getTrueY(game.player_y) * TILE_HEIGHT, + (game.player_x - leftmost) * TILE_WIDTH, + (game.player_y - topmost) * TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT}; if (game.moving) { - rect.x = game.moveProgress.getProgress(game.map.getTrueX(game.player_oldx) * TILE_WIDTH, rect.x); - rect.y = game.moveProgress.getProgress(game.map.getTrueY(game.player_oldy) * TILE_HEIGHT, rect.y); + rect.x = game.moveProgress.getProgress((game.player_oldx - leftmost) * TILE_WIDTH, rect.x); + rect.y = game.moveProgress.getProgress((game.player_oldy - topmost) * TILE_HEIGHT, rect.y); } SDL_RenderCopy(ren_.get(), playerSheet_.get(), &game.playerAnim.getRenderRect(), &rect); @@ -208,8 +213,8 @@ void Renderer::renderGame( ren_.get(), SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, - TILE_WIDTH * game.map.getWidth(), - TILE_HEIGHT * game.map.getHeight())); + windowTileWidth * TILE_WIDTH, + windowTileHeight * TILE_HEIGHT)); if (!mask) { @@ -220,9 +225,9 @@ void Renderer::renderGame( SDL_SetRenderDrawColor(ren_.get(), 0, 0, 0, 0); SDL_RenderClear(ren_.get()); - for (int y = game.map.getTop(); y < game.map.getBottom(); y++) + for (int y = topmost; y < topmost + windowTileHeight; y++) { - for (int x = game.map.getLeft(); x < game.map.getRight(); x++) + for (int x = leftmost; x < leftmost + windowTileWidth; x++) { if (game.map.at(x,y).lightType != Source::None) { @@ -231,8 +236,8 @@ void Renderer::renderGame( ren_.get(), SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, - TILE_WIDTH * game.map.getWidth(), - TILE_HEIGHT * game.map.getHeight())); + windowTileWidth * TILE_WIDTH, + windowTileHeight * TILE_HEIGHT)); if (!sourceMask) { @@ -244,11 +249,11 @@ void Renderer::renderGame( SDL_SetRenderDrawColor(ren_.get(), 0, 0, 0, 0); SDL_RenderClear(ren_.get()); - int posToUseX = game.map.getTrueX(x); - int posToUseY = game.map.getTrueY(y); + int posToUseX = x - leftmost; + int posToUseY = y - topmost; if (game.map.at(x,y).lightType == Source::Player && game.moving) { - posToUseX = game.moveProgress.getProgress(game.map.getTrueX(game.player_oldx), posToUseX); - posToUseY = game.moveProgress.getProgress(game.map.getTrueY(game.player_oldy), posToUseY); + posToUseX = game.moveProgress.getProgress((game.player_oldx) - leftmost, posToUseX); + posToUseY = game.moveProgress.getProgress((game.player_oldy) - topmost, posToUseY); } int fadeX = posToUseX - game.map.at(x,y).lightRadius; @@ -283,8 +288,8 @@ void Renderer::renderGame( if (!game.map.at(x,y).litTiles.count({sx, sy})) { SDL_Rect rect { - game.map.getTrueX(sx) * TILE_WIDTH, - game.map.getTrueY(sy) * TILE_HEIGHT, + (sx - leftmost) * TILE_WIDTH, + (sy - topmost) * TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT}; @@ -306,10 +311,28 @@ void Renderer::renderGame( SDL_SetRenderTarget(ren_.get(), nullptr); - SDL_Rect zoomRect; + // 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) { + zoomRect.x = game.moveProgress.getProgress(2*TILE_WIDTH, TILE_WIDTH); + } - std::tie(zoomRect.x, zoomRect.y, zoomRect.w, zoomRect.h) = - calculateZoomRect(game); + 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); + } + } SDL_RenderCopy(ren_.get(), canvas.get(), &zoomRect, nullptr); SDL_RenderPresent(ren_.get()); @@ -358,12 +381,12 @@ void Renderer::renderTitle(int num, double fade) { std::tuple Renderer::calculateZoomRect(const Game& game) { - int x = game.map.getTrueX(game.curBoundX) * TILE_WIDTH; - int y = game.map.getTrueY(game.curBoundY) * TILE_HEIGHT; int w = game.curZoom * TILE_WIDTH * ZOOM_X_FACTOR; int h = game.curZoom * TILE_HEIGHT * ZOOM_Y_FACTOR; + int x = (game.map.getTrueX(game.player_x) * TILE_WIDTH) - (w / 2); + int y = (game.map.getTrueY(game.player_y) * TILE_HEIGHT) - (h / 2); - if (game.zooming) + /*if (game.zooming) { double interp = static_cast(game.zoomProgress) / @@ -373,7 +396,7 @@ std::tuple Renderer::calculateZoomRect(const Game& game) y = (y - game.lastZoomTop) * interp + game.lastZoomTop; w = (w - game.lastZoomWidth) * interp + game.lastZoomWidth; h = (h - game.lastZoomHeight) * interp + game.lastZoomHeight; - } + }*/ return {x, y, w, h}; } -- cgit 1.4.1