From b2f0190f6b2a227a21dd4909476171f7cc371a2d Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Sat, 19 Mar 2022 16:46:19 -0400 Subject: menu! --- src/renderer.cpp | 133 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 130 insertions(+), 3 deletions(-) (limited to 'src/renderer.cpp') diff --git a/src/renderer.cpp b/src/renderer.cpp index b90372e..1aa5cbf 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp @@ -16,8 +16,6 @@ Renderer::Renderer() GAME_HEIGHT, SDL_WINDOW_SHOWN)); - //SDL_SetWindowFullscreen(win_.get(), SDL_WINDOW_FULLSCREEN_DESKTOP); - if (!win_) { throw sdl_error(); @@ -111,6 +109,7 @@ Renderer::Renderer() loadTextureFromFile("../res/runninbloods.png", tileset_); loadTextureFromFile("../res/lamp.png", lamp_); loadTextureFromFile("../res/read_instruction.png", readInstruction_); + loadTextureFromFile("../res/menu.png", menuBg_); loadTextureFromFile("../res/title0.png", titles_[0]); SDL_QueryTexture(titles_[0].get(), nullptr, nullptr, &titleWidths_[0], &titleHeights_[0]); @@ -394,6 +393,20 @@ void Renderer::renderGame( SDL_RenderCopy(ren_.get(), canvas.get(), &zoomRect, nullptr); + texture_ptr overlays( + SDL_CreateTexture( + ren_.get(), + SDL_PIXELFORMAT_RGBA8888, + SDL_TEXTUREACCESS_TARGET, + GAME_WIDTH, + GAME_HEIGHT)); + + SDL_SetTextureBlendMode(overlays.get(), SDL_BLENDMODE_BLEND); + SDL_SetRenderTarget(ren_.get(), overlays.get()); + SDL_SetRenderDrawBlendMode(ren_.get(), SDL_BLENDMODE_BLEND); + SDL_SetRenderDrawColor(ren_.get(), 0, 0, 0, 0); + SDL_RenderClear(ren_.get()); + if (game.signInstructionState != SignInstructionState::Hidden) { int instOpacity = 255; if (game.signInstructionState == SignInstructionState::FadingIn) { @@ -444,7 +457,7 @@ void Renderer::renderGame( srcRect.w, srcRect.h }; - SDL_SetRenderTarget(ren_.get(), nullptr); + SDL_SetRenderTarget(ren_.get(), overlays.get()); SDL_RenderCopy(ren_.get(), messageLines_[lineIndex].renderedTex.get(), &srcRect, &destRect); //std::cout << line.charsRevealed << " (" << messageLines_[lineIndex].charIndexToWidth[line.charsRevealed] << "): " << messageLines_[lineIndex].line.substr(0,line.charsRevealed) << std::endl; } @@ -455,6 +468,34 @@ void Renderer::renderGame( } } + if (game.menu.menuState != MenuState::Closed) { + if (!menu_ || menuSelected_ != game.menu.cursor) { + renderMenu(game); + } + + SDL_Rect rect { + 0, 0, GAME_WIDTH, GAME_HEIGHT + }; + + int opacity = 128; + if (game.menu.menuState == MenuState::Opening) { + opacity = game.menu.menuDisplayProgress.getProgress(0, 128); + rect.y = game.menu.menuDisplayProgress.getProgress(-GAME_HEIGHT, 0); + } else if (game.menu.menuState == MenuState::Closing) { + opacity = game.menu.menuDisplayProgress.getProgress(128, 0); + rect.y = game.menu.menuDisplayProgress.getProgress(0, -GAME_HEIGHT); + } + + SDL_SetRenderTarget(ren_.get(), overlays.get()); + SDL_SetRenderDrawBlendMode(ren_.get(), SDL_BLENDMODE_BLEND); + SDL_SetRenderDrawColor(ren_.get(), 0, 0, 0, opacity); + SDL_RenderFillRect(ren_.get(), nullptr); + + SDL_RenderCopy(ren_.get(), menu_.get(), nullptr, &rect); + } + + SDL_SetRenderTarget(ren_.get(), nullptr); + SDL_RenderCopy(ren_.get(), overlays.get(), nullptr, nullptr); SDL_RenderPresent(ren_.get()); } @@ -550,3 +591,89 @@ void Renderer::renderMessageLine(MessageCache& line, const std::string& text, co SDL_RenderCopy(ren_.get(), lineTex.get(), nullptr, &rect); } +void Renderer::renderMenu(const Game& game) { + int maxTextWidth = 0; + for (const MenuItem& item : game.menu.items) { + int width = 0; + TTF_SizeText(font_.get(), item.text.c_str(), &width, nullptr); + if (width > maxTextWidth) { + maxTextWidth = width; + } + } + + menuWidth_ = maxTextWidth + MENU_PADDING * 2; + menuHeight_ = (TTF_FontHeight(font_.get()) + 1) * game.menu.items.size() - 1 + MENU_PADDING * 2; + + menu_.reset(SDL_CreateTexture( + ren_.get(), + SDL_PIXELFORMAT_RGBA8888, + SDL_TEXTUREACCESS_TARGET, + GAME_WIDTH, + GAME_HEIGHT)); + + SDL_SetRenderTarget(ren_.get(), menu_.get()); + SDL_SetTextureBlendMode(menu_.get(), SDL_BLENDMODE_BLEND); + SDL_SetRenderDrawBlendMode(ren_.get(), SDL_BLENDMODE_BLEND); + SDL_SetRenderDrawColor(ren_.get(), 0, 0, 0, 0); + SDL_RenderClear(ren_.get()); + + SDL_Color bgColor {.r = 55, .g = 55, .b = 87, .a = 255}; + SDL_Color fgColor {.r = 146, .g = 146, .b = 146, .a = 255}; + SDL_Color selectedColor {.r = 255, .g = 255, .b = 255, .a = 255}; + + /*{ + SDL_Rect drawRect { + (GAME_WIDTH - menuWidth_) / 2 - MENU_BORDER, + (GAME_HEIGHT - menuHeight_) / 2 - MENU_BORDER, + menuWidth_ + MENU_BORDER * 2, + menuHeight_ + MENU_BORDER * 2 + }; + + SDL_SetRenderDrawColor(ren_.get(), 200, 200, 200, 255); + SDL_RenderFillRect(ren_.get(), &drawRect); + } + + { + SDL_Rect drawRect { + (GAME_WIDTH - menuWidth_) / 2, + (GAME_HEIGHT - menuHeight_) / 2, + menuWidth_, + menuHeight_ + }; + + SDL_SetRenderDrawColor(ren_.get(), bgColor.r, bgColor.g, bgColor.b, 255); + SDL_RenderFillRect(ren_.get(), &drawRect); + + std::cout << "(" << drawRect.x << "," << drawRect.y << "),(" << drawRect.w << "," << drawRect.h << ")" << std::endl; + }*/ + + SDL_RenderCopy(ren_.get(), menuBg_.get(), nullptr, nullptr); + + for (int i=0; i < game.menu.items.size(); i++) { + const MenuItem& item = game.menu.items.at(i); + int width = 0; + TTF_SizeText(font_.get(), item.text.c_str(), &width, nullptr); + + surface_ptr lineSurf = surface_ptr(TTF_RenderText_Shaded(font_.get(), item.text.c_str(), game.menu.cursor == i ? selectedColor : fgColor, bgColor)); + texture_ptr lineTex = texture_ptr(SDL_CreateTextureFromSurface(ren_.get(), lineSurf.get())); + + SDL_Rect drawRect { + (GAME_WIDTH - width) / 2, + (GAME_HEIGHT - menuHeight_) / 2 + MENU_PADDING + (TTF_FontHeight(font_.get()) + 1) * i, + width, + TTF_FontHeight(font_.get()) + }; + SDL_RenderCopy(ren_.get(), lineTex.get(), nullptr, &drawRect); + } + + menuSelected_ = game.menu.cursor; +} + +void Renderer::toggleFullscreen() { + isFullscreen = !isFullscreen; + if (isFullscreen) { + SDL_SetWindowFullscreen(win_.get(), SDL_WINDOW_FULLSCREEN_DESKTOP); + } else { + SDL_SetWindowFullscreen(win_.get(), 0); + } +} -- cgit 1.4.1