From 42495836c9ebe1c3914540043d2338eebd85dbab Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Sat, 13 Mar 2021 09:04:04 -0500 Subject: Rendered pause menu during animation Also, the game is not rendered at all if the pause menu is totally open, which makes sense because why are we going to the effort to render it and then just colour over it. --- src/renderer.cpp | 164 ++++++++++++++++++++++++++++++++++--------------------- src/renderer.h | 10 ++++ 2 files changed, 112 insertions(+), 62 deletions(-) diff --git a/src/renderer.cpp b/src/renderer.cpp index 81e6b9c..14eefea 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp @@ -124,9 +124,47 @@ void Renderer::renderSprite(const Sprite& sprite) { } void Renderer::render(Game& game) { - auto& effects = game.getSystem(); auto& menus = game.getSystem(); + texture_ptr canvas( + SDL_CreateTexture( + ren_.get(), + SDL_PIXELFORMAT_RGBA8888, + SDL_TEXTUREACCESS_TARGET, + CANVAS_WIDTH, + CANVAS_HEIGHT)); + + if (menus.getPauseAnimationProgress() < 1.0) { + renderGame(game); + + SDL_SetRenderTarget(ren_.get(), canvas.get()); + SDL_RenderCopy(ren_.get(), cameraTex_.get(), nullptr, nullptr); + } + + if (menus.getPauseAnimationProgress() > 0.0) { + renderMenu(game); + + SDL_SetRenderTarget(ren_.get(), canvas.get()); + if (menus.getPauseAnimationProgress() == 1.0) { + SDL_RenderCopy(ren_.get(), menuTex_.get(), nullptr, nullptr); + } else { + int barHeight = CANVAS_HEIGHT / 2 * menus.getPauseAnimationProgress(); + SDL_Rect topHalf { 0, 0, CANVAS_WIDTH, barHeight }; + SDL_Rect bottomHalf { 0, CANVAS_HEIGHT - barHeight, CANVAS_WIDTH, barHeight }; + + SDL_RenderCopy(ren_.get(), menuTex_.get(), &topHalf, &topHalf); + SDL_RenderCopy(ren_.get(), menuTex_.get(), &bottomHalf, &bottomHalf); + } + } + + SDL_SetRenderTarget(ren_.get(), nullptr); + SDL_RenderCopy(ren_.get(), canvas.get(), nullptr, nullptr); + SDL_RenderPresent(ren_.get()); +} + +void Renderer::renderGame(Game& game) { + auto& effects = game.getSystem(); + if (cachedMapName_ != game.getMap().getName()) { cachedMapName_ = game.getMap().getName(); renLowerLayer_ = renderMapLayer(game.getMap(), false); @@ -247,23 +285,25 @@ void Renderer::render(Game& game) { game.getSystem().getFieldOfView().h() }; - texture_ptr cameraTex( - SDL_CreateTexture( - ren_.get(), - SDL_PIXELFORMAT_RGBA8888, - SDL_TEXTUREACCESS_TARGET, - CANVAS_WIDTH, - CANVAS_HEIGHT)); + if (!cameraTex_) { + cameraTex_.reset( + SDL_CreateTexture( + ren_.get(), + SDL_PIXELFORMAT_RGBA8888, + SDL_TEXTUREACCESS_TARGET, + CANVAS_WIDTH, + CANVAS_HEIGHT)); - if (!cameraTex) { - throw sdl_error(); + if (!cameraTex_) { + throw sdl_error(); + } } - SDL_SetRenderTarget(ren_.get(), cameraTex.get()); + SDL_SetRenderTarget(ren_.get(), cameraTex_.get()); SDL_RenderCopy(ren_.get(), canvas.get(), &cameraField, nullptr); if (game.getSystem().getCutsceneBarsProgress() > 0) { - SDL_SetRenderTarget(ren_.get(), cameraTex.get()); + SDL_SetRenderTarget(ren_.get(), cameraTex_.get()); SDL_SetRenderDrawBlendMode(ren_.get(), SDL_BLENDMODE_NONE); SDL_SetRenderDrawColor(ren_.get(), 0, 0, 0, 255); @@ -284,7 +324,7 @@ void Renderer::render(Game& game) { { SDL_Rect destRect { 0, 111, 57, 13 }; - SDL_SetRenderTarget(ren_.get(), cameraTex.get()); + SDL_SetRenderTarget(ren_.get(), cameraTex_.get()); SDL_RenderCopy(ren_.get(), textures_.at(speakerHeaderTex_).get(), nullptr, &destRect); } @@ -299,7 +339,7 @@ void Renderer::render(Game& game) { MESSAGE_TEXT_WIDTH, game.getFont().getCharacterHeight() }; - SDL_SetRenderTarget(ren_.get(), cameraTex.get()); + SDL_SetRenderTarget(ren_.get(), cameraTex_.get()); SDL_RenderCopy(ren_.get(), speakerHeaderLine_.renderedTex.get(), nullptr, &destRect); } } @@ -323,7 +363,7 @@ void Renderer::render(Game& game) { srcRect.w, srcRect.h }; - SDL_SetRenderTarget(ren_.get(), cameraTex.get()); + SDL_SetRenderTarget(ren_.get(), cameraTex_.get()); SDL_RenderCopy(ren_.get(), messageLines_[lineIndex].renderedTex.get(), &srcRect, &destRect); } @@ -333,7 +373,7 @@ void Renderer::render(Game& game) { SDL_Rect srcRect { charLoc.x(), charLoc.y(), charSize.w(), charSize.h() }; SDL_Rect destRect { 13, 127 + 16 * lineIndex, charSize.w(), charSize.h() }; - SDL_SetRenderTarget(ren_.get(), cameraTex.get()); + SDL_SetRenderTarget(ren_.get(), cameraTex_.get()); SDL_RenderCopy(ren_.get(), textures_.at(game.getFont().getTextureId()).get(), &srcRect, &destRect); } } @@ -358,7 +398,7 @@ void Renderer::render(Game& game) { 8, 8 }; - SDL_SetRenderTarget(ren_.get(), cameraTex.get()); + SDL_SetRenderTarget(ren_.get(), cameraTex_.get()); SDL_RenderCopy(ren_.get(), textures_.at(choiceArrowTex_).get(), nullptr, &destRect); } else { if (advMsgArrowTex_ == -1) { @@ -371,7 +411,7 @@ void Renderer::render(Game& game) { 8, 8 }; - SDL_SetRenderTarget(ren_.get(), cameraTex.get()); + SDL_SetRenderTarget(ren_.get(), cameraTex_.get()); SDL_RenderCopy(ren_.get(), textures_.at(advMsgArrowTex_).get(), nullptr, &destRect); } } @@ -379,7 +419,7 @@ void Renderer::render(Game& game) { } if (effects.isScreenFaded()) { - SDL_SetRenderTarget(ren_.get(), cameraTex.get()); + SDL_SetRenderTarget(ren_.get(), cameraTex_.get()); SDL_SetRenderDrawBlendMode(ren_.get(), SDL_BLENDMODE_BLEND); SDL_SetRenderDrawColor(ren_.get(), 0, 0, 0, effects.getScreenFadeProgress() * 255); SDL_RenderFillRect(ren_.get(), nullptr); @@ -387,7 +427,7 @@ void Renderer::render(Game& game) { if (effects.isCircleTransitionActive()) { if (effects.getCircleTransitionProgress() == 1.0) { - SDL_SetRenderTarget(ren_.get(), cameraTex.get()); + SDL_SetRenderTarget(ren_.get(), cameraTex_.get()); SDL_SetRenderDrawBlendMode(ren_.get(), SDL_BLENDMODE_NONE); SDL_SetRenderDrawColor(ren_.get(), 0, 0, 0, 255); SDL_RenderClear(ren_.get()); @@ -427,13 +467,13 @@ void Renderer::render(Game& game) { copyFromCamera.h = CANVAS_HEIGHT; } - SDL_RenderCopy(ren_.get(), cameraTex.get(), ©FromCamera, ©FromCamera); + SDL_RenderCopy(ren_.get(), cameraTex_.get(), ©FromCamera, ©FromCamera); int pinholeTexId = loadImageFromFile("../res/pinhole.png"); SDL_SetTextureBlendMode(textures_[pinholeTexId].get(), SDL_BLENDMODE_MOD); SDL_RenderCopy(ren_.get(), textures_[pinholeTexId].get(), nullptr, &circleSize); - SDL_SetRenderTarget(ren_.get(), cameraTex.get()); + SDL_SetRenderTarget(ren_.get(), cameraTex_.get()); SDL_SetTextureBlendMode(circleEffectTex.get(), SDL_BLENDMODE_NONE); SDL_RenderCopy(ren_.get(), circleEffectTex.get(), nullptr, nullptr); } @@ -442,7 +482,7 @@ void Renderer::render(Game& game) { if (game.getSystem().isDebugConsoleOpen()) { // Not sure why I'm supposed to copy the cached texture to the screen // BEFORE rendering it, but we get flickering if we don't, so. - SDL_SetRenderTarget(ren_.get(), cameraTex.get()); + SDL_SetRenderTarget(ren_.get(), cameraTex_.get()); SDL_RenderCopy(ren_.get(), debugConsoleTex_.get(), nullptr, &debugDestRect_); if (!debugConsoleTex_ || cachedDebugText_ != game.getSystem().getDebugText()) { @@ -494,54 +534,54 @@ void Renderer::render(Game& game) { debugDestRect_ = SDL_Rect { 0, 0, CANVAS_WIDTH, height }; } } +} - if (menus.isMenuOpen()) { - SDL_SetRenderTarget(ren_.get(), cameraTex.get()); - SDL_SetRenderDrawBlendMode(ren_.get(), SDL_BLENDMODE_NONE); - SDL_SetRenderDrawColor(ren_.get(), 0, 0, 0, 255); - SDL_RenderFillRect(ren_.get(), nullptr); +void Renderer::renderMenu(Game& game) { + auto& menus = game.getSystem(); + + if (!menuTex_) { + menuTex_.reset(SDL_CreateTexture( + ren_.get(), + SDL_PIXELFORMAT_RGBA8888, + SDL_TEXTUREACCESS_TARGET, + CANVAS_WIDTH, + CANVAS_HEIGHT)); + + if (!menuTex_) { + throw sdl_error(); + } + } - const int lineHeight = 16; - int totalHeight = menus.getMenu().size() * lineHeight; + SDL_SetRenderTarget(ren_.get(), menuTex_.get()); + SDL_SetRenderDrawBlendMode(ren_.get(), SDL_BLENDMODE_NONE); + SDL_SetRenderDrawColor(ren_.get(), 0, 0, 0, 255); + SDL_RenderFillRect(ren_.get(), nullptr); - int index = 0; - for (const MenuItem& menuItem : menus.getMenu()) { - switch (menuItem.type) { - case MenuType::Command: { - MessageCache output; - renderMessageLine(output, menuItem.text, game); + const int lineHeight = 16; + int totalHeight = menus.getMenu().size() * lineHeight; - SDL_Rect dest { - (CANVAS_WIDTH - output.charIndexToWidth.back()) / 2, - (CANVAS_HEIGHT - totalHeight) / 2 + lineHeight * index, - MESSAGE_TEXT_WIDTH, - game.getFont().getCharacterHeight() - }; + int index = 0; + for (const MenuItem& menuItem : menus.getMenu()) { + switch (menuItem.type) { + case MenuType::Command: { + MessageCache output; + renderMessageLine(output, menuItem.text, game); + + SDL_Rect dest { + (CANVAS_WIDTH - output.charIndexToWidth.back()) / 2, + (CANVAS_HEIGHT - totalHeight) / 2 + lineHeight * index, + MESSAGE_TEXT_WIDTH, + game.getFont().getCharacterHeight() + }; - SDL_SetRenderTarget(ren_.get(), cameraTex.get()); - SDL_RenderCopy(ren_.get(), output.renderedTex.get(), nullptr, &dest); + SDL_SetRenderTarget(ren_.get(), menuTex_.get()); + SDL_RenderCopy(ren_.get(), output.renderedTex.get(), nullptr, &dest); - break; - } + break; } - index++; } - } else if (menus.getPauseAnimationProgress() > 0.0) { - SDL_SetRenderTarget(ren_.get(), cameraTex.get()); - SDL_SetRenderDrawBlendMode(ren_.get(), SDL_BLENDMODE_NONE); - SDL_SetRenderDrawColor(ren_.get(), 0, 0, 0, 255); - - int barHeight = CANVAS_HEIGHT / 2 * menus.getPauseAnimationProgress(); - SDL_Rect topHalf { 0, 0, CANVAS_WIDTH, barHeight }; - SDL_Rect bottomHalf { 0, CANVAS_HEIGHT - barHeight, CANVAS_WIDTH, barHeight }; - - SDL_RenderFillRect(ren_.get(), &topHalf); - SDL_RenderFillRect(ren_.get(), &bottomHalf); + index++; } - - SDL_SetRenderTarget(ren_.get(), nullptr); - SDL_RenderCopy(ren_.get(), cameraTex.get(), nullptr, nullptr); - SDL_RenderPresent(ren_.get()); } int Renderer::loadImageFromFile(std::string filename) { diff --git a/src/renderer.h b/src/renderer.h index 4c6ccc4..75bd9fe 100644 --- a/src/renderer.h +++ b/src/renderer.h @@ -134,6 +134,16 @@ private: std::vector textures_; std::map filenameToTexId_; + // Menu rendering + void renderMenu(Game& game); + + texture_ptr menuTex_; + + // Gameplay rendering + void renderGame(Game& game); + + texture_ptr cameraTex_; + // Sprite rendering void renderSprite(const Sprite& sprite); -- cgit 1.4.1