From 042da1570181dc80ee76860582f6a3c97641c14a Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Mon, 14 Mar 2022 08:32:39 -0400 Subject: fmod splash screen --- src/renderer.cpp | 103 +++++++++++++++++++++++++++++++------------------------ 1 file changed, 58 insertions(+), 45 deletions(-) (limited to 'src/renderer.cpp') diff --git a/src/renderer.cpp b/src/renderer.cpp index 90ce03f..9ef8bb9 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp @@ -31,17 +31,7 @@ Renderer::Renderer() } texture_ptr origFade; - { - surface_ptr pfs(IMG_Load("../res/lighting.png")); - if (!pfs) - { - throw img_error(); - } - - origFade = texture_ptr(SDL_CreateTextureFromSurface(ren_.get(), pfs.get())); - } - - SDL_SetTextureBlendMode(origFade.get(), SDL_BLENDMODE_BLEND); + loadTextureFromFile("../res/lighting.png", origFade); playerFade_ = texture_ptr( SDL_CreateTexture( @@ -113,41 +103,12 @@ Renderer::Renderer() SDL_SetRenderDrawColor(ren_.get(), 100, 100, 100, 255); SDL_RenderFillRect(ren_.get(), nullptr); - { - surface_ptr pfs(IMG_Load("../res/player.png")); - if (!pfs) - { - throw img_error(); - } - - playerSheet_ = texture_ptr(SDL_CreateTextureFromSurface(ren_.get(), pfs.get())); - } + loadTextureFromFile("../res/player.png", playerSheet_); + loadTextureFromFile("../res/runninbloods.png", tileset_); + loadTextureFromFile("../res/lamp.png", lamp_); - 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); - - { - surface_ptr pfs(IMG_Load("../res/lamp.png")); - if (!pfs) - { - throw img_error(); - } - - lamp_ = texture_ptr(SDL_CreateTextureFromSurface(ren_.get(), pfs.get())); - } - - SDL_SetTextureBlendMode(lamp_.get(), SDL_BLENDMODE_BLEND); + loadTextureFromFile("../res/title0.png", titles_[0]); + SDL_QueryTexture(titles_[0].get(), nullptr, nullptr, &titleWidths_[0], &titleHeights_[0]); } void Renderer::renderGame( @@ -354,6 +315,47 @@ void Renderer::renderGame( SDL_RenderPresent(ren_.get()); } +void Renderer::renderTitle(int num, double fade) { + texture_ptr canvas( + SDL_CreateTexture( + ren_.get(), + SDL_PIXELFORMAT_RGBA8888, + SDL_TEXTUREACCESS_TARGET, + GAME_WIDTH, + GAME_HEIGHT)); + + if (!canvas) + { + throw sdl_error(); + } + + SDL_SetRenderTarget(ren_.get(), canvas.get()); + SDL_SetRenderDrawBlendMode(ren_.get(), SDL_BLENDMODE_NONE); + SDL_SetRenderDrawColor(ren_.get(), 0, 0, 0, 255); + SDL_RenderClear(ren_.get()); + + if (fade > 0) { + SDL_Rect rect { + (GAME_WIDTH - titleWidths_[num]) / 2, + (GAME_HEIGHT - titleHeights_[num]) / 2, + titleWidths_[num], + titleHeights_[num] + }; + + SDL_RenderCopy(ren_.get(), titles_[num].get(), nullptr, &rect); + + if (fade < 1) { + SDL_SetRenderDrawBlendMode(ren_.get(), SDL_BLENDMODE_BLEND); + SDL_SetRenderDrawColor(ren_.get(), 0, 0, 0, (1.0 - fade) * 255); + SDL_RenderFillRect(ren_.get(), nullptr); + } + } + + SDL_SetRenderTarget(ren_.get(), nullptr); + SDL_RenderCopy(ren_.get(), canvas.get(), nullptr, nullptr); + SDL_RenderPresent(ren_.get()); +} + std::tuple Renderer::calculateZoomRect(const Game& game) { int x = game.map.getTrueX(game.curBoundX) * TILE_WIDTH; @@ -375,3 +377,14 @@ std::tuple Renderer::calculateZoomRect(const Game& game) return {x, y, w, h}; } + +void Renderer::loadTextureFromFile(std::string_view path, texture_ptr& texture) { + surface_ptr pfs(IMG_Load(path.data())); + if (!pfs) + { + throw img_error(); + } + + texture = texture_ptr(SDL_CreateTextureFromSurface(ren_.get(), pfs.get())); + SDL_SetTextureBlendMode(texture.get(), SDL_BLENDMODE_BLEND); +} -- cgit 1.4.1