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/consts.h | 14 ++++++++ src/game.cpp | 1 + src/game.h | 10 +----- src/main.cpp | 69 +++++++++++++++++++++++++++++++++++-- src/renderer.cpp | 103 +++++++++++++++++++++++++++++++------------------------ src/renderer.h | 11 ++++++ 6 files changed, 151 insertions(+), 57 deletions(-) create mode 100644 src/consts.h (limited to 'src') diff --git a/src/consts.h b/src/consts.h new file mode 100644 index 0000000..141838b --- /dev/null +++ b/src/consts.h @@ -0,0 +1,14 @@ +#ifndef CONSTS_H_152EBF56 +#define CONSTS_H_152EBF56 + +constexpr int GAME_WIDTH = 1280;//640*2; +constexpr int GAME_HEIGHT = 720;//480*2; +constexpr int TILE_WIDTH = 8*2; +constexpr int TILE_HEIGHT = TILE_WIDTH; +constexpr int INIT_ZOOM = 5; +constexpr int ZOOM_X_FACTOR = 16; +constexpr int ZOOM_Y_FACTOR = 9; +constexpr int RADIUS = 8; +constexpr int NUM_TITLES = 1; + +#endif /* end of include guard: CONSTS_H_152EBF56 */ diff --git a/src/game.cpp b/src/game.cpp index 8de9b7b..a7c94db 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -4,6 +4,7 @@ #include #include "util.h" #include "renderer.h" +#include "consts.h" Game::Game(std::mt19937& rng, Muxer& muxer) : rng(rng), diff --git a/src/game.h b/src/game.h index 89b9eb2..446680a 100644 --- a/src/game.h +++ b/src/game.h @@ -10,15 +10,7 @@ #include "timer.h" #include "animation.h" #include "interpolation.h" - -const int GAME_WIDTH = 1280;//640*2; -const int GAME_HEIGHT = 720;//480*2; -const int TILE_WIDTH = 8*2; -const int TILE_HEIGHT = TILE_WIDTH; -const int INIT_ZOOM = 5; -const int ZOOM_X_FACTOR = 16; -const int ZOOM_Y_FACTOR = 9; -const int RADIUS = 8; +#include "consts.h" constexpr int TilesetIndex(int x, int y) { return x + y * 24; diff --git a/src/main.cpp b/src/main.cpp index ec253ee..4ae11d6 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,6 +5,12 @@ #include "game.h" #include "renderer.h" +enum class TitleState { + FadeIn, + Sustain, + FadeOut +}; + int main(int, char**) { std::random_device randomEngine; @@ -17,6 +23,14 @@ int main(int, char**) Game game(rng, muxer); + constexpr int titleFadeLen = 3000; + bool doneTitles = false; + int titleNum = 0; + TitleState titleState = TitleState::FadeIn; + Interpolation titleFade; + Timer titleSustain(3000); + titleFade.start(titleFadeLen); + size_t lastTime = SDL_GetTicks(); while (!game.quit) { @@ -24,9 +38,58 @@ int main(int, char**) size_t frameTime = currentTime - lastTime; lastTime = currentTime; - game.update(frameTime); - renderer.renderGame(game, true); - muxer.update(); + if (doneTitles) { + game.update(frameTime); + renderer.renderGame(game, true); + muxer.update(); + } else { + SDL_Event e; + + while (SDL_PollEvent(&e)); + + switch (titleState) { + case TitleState::FadeIn: { + titleFade.tick(frameTime); + if (titleFade.isComplete()) { + titleState = TitleState::Sustain; + titleSustain.reset(); + } + + renderer.renderTitle(titleNum, titleFade.getProgress(0, 1)); + + break; + } + case TitleState::Sustain: { + titleSustain.accumulate(frameTime); + const Uint8* state = SDL_GetKeyboardState(NULL); + if (state[SDL_SCANCODE_SPACE] || titleSustain.step()) { + titleState = TitleState::FadeOut; + titleFade.start(titleFadeLen); + } + + renderer.renderTitle(titleNum, 1); + + break; + } + case TitleState::FadeOut: { + titleFade.tick(frameTime); + renderer.renderTitle(titleNum, titleFade.getProgress(1, 0)); + + if (titleFade.isComplete()) { + titleNum++; + + if (titleNum < NUM_TITLES) { + titleState = TitleState::FadeIn; + titleFade.start(titleFadeLen); + } else { + doneTitles = true; + } + } + + break; + } + } + } } } catch (const sdl_error& ex) { 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); +} diff --git a/src/renderer.h b/src/renderer.h index ca4e607..4d5484c 100644 --- a/src/renderer.h +++ b/src/renderer.h @@ -5,6 +5,9 @@ #include #include #include +#include +#include +#include "consts.h" class Game; @@ -117,10 +120,14 @@ public: const Game& game, bool drawDark = true); + void renderTitle(int num, double fade); + static std::tuple calculateZoomRect(const Game& game); private: + void loadTextureFromFile(std::string_view path, texture_ptr& texture); + sdl_wrapper sdl_; img_wrapper img_; window_ptr win_; @@ -132,6 +139,10 @@ private: texture_ptr playerSheet_; texture_ptr tileset_; texture_ptr lamp_; + + std::array titles_; + std::array titleWidths_; + std::array titleHeights_; }; #endif /* end of include guard: RENDERER_H_6A58EC30 */ -- cgit 1.4.1