From 4b6e5eb5d3e0733c239a7a231da91a9977c38246 Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Sat, 12 Mar 2022 14:12:42 -0500 Subject: dynamic wall and floor textures --- src/renderer.cpp | 80 ++++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 66 insertions(+), 14 deletions(-) (limited to 'src/renderer.cpp') diff --git a/src/renderer.cpp b/src/renderer.cpp index 0aaa14a..00b557a 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp @@ -122,6 +122,18 @@ Renderer::Renderer() } SDL_SetTextureBlendMode(playerSheet_.get(), SDL_BLENDMODE_BLEND); + + { + surface_ptr pfs(IMG_Load("../res/runninbloods.png")); + if (!pfs) + { + throw img_error(); + } + + tileset_ = texture_ptr(SDL_CreateTextureFromSurface(ren_.get(), pfs.get())); + } + + SDL_SetTextureBlendMode(tileset_.get(), SDL_BLENDMODE_BLEND); } void Renderer::render( @@ -151,12 +163,19 @@ void Renderer::render( for (int x = game.map.getLeft(); x < game.map.getRight(); x++) { bool draw = true; + bool drawColour = false; + SDL_Rect rect { + game.map.getTrueX(x) * TILE_WIDTH, + game.map.getTrueY(y) * TILE_HEIGHT, + TILE_WIDTH, + TILE_HEIGHT}; if (!game.map.at(x,y).lit) { if (drawDark) { SDL_SetRenderDrawColor(ren_.get(), 40, 40, 40, 255); + drawColour = true; } else { draw = false; } @@ -186,6 +205,7 @@ void Renderer::render( case Tile::Lamp: { SDL_SetRenderDrawColor(ren_.get(), 0, 255, 255, alpha); + drawColour = true; break; } } @@ -193,22 +213,47 @@ void Renderer::render( if (draw) { - SDL_Rect rect { - game.map.getTrueX(x) * TILE_WIDTH, - game.map.getTrueY(y) * TILE_HEIGHT, - TILE_WIDTH, - TILE_HEIGHT}; - - SDL_RenderFillRect(ren_.get(), &rect); + if (game.map.at(x,y).tile == Tile::Floor || game.map.at(x,y).tile == Tile::Dust) { + SDL_Rect tileRect {17 * 16, 15 * 16, 16, 16}; + SDL_RenderCopy(ren_.get(), tileset_.get(), &tileRect, &rect); + + if (game.map.at(x,y).renderId != -1) { + tileRect.x = game.map.at(x,y).renderId % 24 * 16; + tileRect.y = game.map.at(x,y).renderId / 24 * 16; + SDL_RenderCopy(ren_.get(), tileset_.get(), &tileRect, &rect); + } + } else if (game.map.at(x,y).tile == Tile::Wall) { + SDL_Rect tileRect { + game.map.at(x,y).renderId % 24 * 16, + game.map.at(x,y).renderId / 24 * 16, + 16, + 16}; + + SDL_RenderCopy(ren_.get(), tileset_.get(), &tileRect, &rect); + } - if ((game.player_x == x && game.player_y == y) && game.renderPlayer) - { - SDL_RenderCopy(ren_.get(), playerSheet_.get(), &game.playerAnim.getRenderRect(), &rect); + if (drawColour) { + SDL_RenderFillRect(ren_.get(), &rect); } } } } + if (game.renderPlayer) { + SDL_Rect rect { + game.map.getTrueX(game.player_x) * TILE_WIDTH, + game.map.getTrueY(game.player_y) * 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); + } + + SDL_RenderCopy(ren_.get(), playerSheet_.get(), &game.playerAnim.getRenderRect(), &rect); + } + texture_ptr mask( SDL_CreateTexture( ren_.get(), @@ -250,10 +295,17 @@ void Renderer::render( SDL_SetRenderDrawColor(ren_.get(), 0, 0, 0, 0); SDL_RenderClear(ren_.get()); - int fadeX = game.map.getTrueX(x) - game.map.at(x,y).lightRadius; - int fadeY = game.map.getTrueY(y) - game.map.at(x,y).lightRadius; - int fadeRight = game.map.getTrueX(x) + game.map.at(x,y).lightRadius; - int fadeBottom = game.map.getTrueY(y) + game.map.at(x,y).lightRadius; + int posToUseX = game.map.getTrueX(x); + int posToUseY = game.map.getTrueY(y); + 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); + } + + int fadeX = posToUseX - game.map.at(x,y).lightRadius; + int fadeY = posToUseY - game.map.at(x,y).lightRadius; + int fadeRight = posToUseX + game.map.at(x,y).lightRadius; + int fadeBottom = posToUseY + game.map.at(x,y).lightRadius; SDL_Rect fadeRect { fadeX * TILE_WIDTH, -- cgit 1.4.1