Skip to content

Commit

Permalink
Add SplashScene and transition effect
Browse files Browse the repository at this point in the history
  • Loading branch information
copyrat90 committed May 17, 2021
1 parent 15523dd commit d0d788f
Show file tree
Hide file tree
Showing 5 changed files with 182 additions and 0 deletions.
8 changes: 8 additions & 0 deletions include/Constants.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once

#include <bn_color.h>

namespace sym::constants
{
constexpr const bn::color TRANSPARENT_BG_COLOR(4, 4, 6);
}
26 changes: 26 additions & 0 deletions include/Scene.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#pragma once

#include <bn_optional.h>

namespace sym
{

enum class SceneType
{
SPLASH,
TITLE,
GAME,
LICENSE
};

class Scene
{
public:
virtual ~Scene() = default;
[[nodiscard]] virtual bn::optional<SceneType> Update() = 0;

protected:
Scene() = default;
};

} // namespace sym
41 changes: 41 additions & 0 deletions include/SplashScene.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#pragma once

#include "Scene.h"

#include <bn_array.h>
#include <bn_blending_actions.h>
#include <bn_regular_bg_ptr.h>

namespace sym
{

class SplashScene : public Scene
{
public:
SplashScene();
~SplashScene() = default;
[[nodiscard]] bn::optional<SceneType> Update() final;

private:
void SwapBgWhenNeeded();
void SwapBg();
/**
* @brief Update the alpha transition action
*
* @return bn::optional<SceneType> Next scene type when transition is done, otherwise bn::nullopt
*/
[[nodiscard]] bn::optional<SceneType> Transition();

static constexpr int BG_SWAP_UPDATE_COUNT = 30;
static constexpr int BG_SWAP_COUNT = 8;
static constexpr int FADE_UPDATE_COUNT = 60;

bn::array<bn::regular_bg_ptr, 2> bgs_;
int bgSwapCounter_ = BG_SWAP_COUNT;
int bgSwapUpdateCounter_ = BG_SWAP_UPDATE_COUNT;
bool isShowingFirstBg_ = false;
bool isFadeDone_ = false;
bn::optional<bn::blending_transparency_alpha_to_action> transparentAction_;
};

} // namespace sym
72 changes: 72 additions & 0 deletions src/SplashScene.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#include "SplashScene.h"

#include <bn_blending_actions.h>
#include <bn_fixed.h>
#include <bn_keypad.h>
#include <bn_optional.h>

#include "bn_regular_bg_items_bg_splash1.h"
#include "bn_regular_bg_items_bg_splash2.h"

namespace sym
{

SplashScene::SplashScene()
: bgs_{bn::regular_bg_items::bg_splash1.create_bg(0, 0), bn::regular_bg_items::bg_splash2.create_bg(0, 0)}
{
bgs_[0].set_visible(isShowingFirstBg_);
for (auto& bg : bgs_)
bg.set_blending_enabled(true);
}

bn::optional<SceneType> SplashScene::Update()
{
SwapBgWhenNeeded();

if (bn::keypad::a_pressed())
{
bgSwapCounter_ = 0;
}

if (0 >= bgSwapCounter_)
{
return Transition();
}

return bn::nullopt;
}

void SplashScene::SwapBgWhenNeeded()
{
if (0 >= bgSwapUpdateCounter_--)
{
bgSwapUpdateCounter_ = BG_SWAP_UPDATE_COUNT;
--bgSwapCounter_;
SwapBg();
}
}

void SplashScene::SwapBg()
{
isShowingFirstBg_ = !isShowingFirstBg_;
bgs_[0].set_visible(isShowingFirstBg_);
bgs_[1].set_visible(!isShowingFirstBg_);
}

bn::optional<SceneType> SplashScene::Transition()
{
if (!transparentAction_)
{
transparentAction_ = bn::blending_transparency_alpha_to_action(FADE_UPDATE_COUNT, 0);
}

if (transparentAction_->done())
{
return SceneType::TITLE;
}

transparentAction_->update();
return bn::nullopt;
}

} // namespace sym
35 changes: 35 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,46 @@
#include <bn_bg_palettes.h>
#include <bn_color.h>
#include <bn_core.h>
#include <bn_memory.h>

#include "Constants.h"
#include "SplashScene.h"
#include "bn_assert.h"

int main()
{
bn::core::init();

bn::bg_palettes::set_transparent_color(sym::constants::TRANSPARENT_BG_COLOR);
bn::unique_ptr<sym::Scene> scene(new sym::SplashScene);
bn::optional<sym::SceneType> nextScene;

while (true)
{
if (scene)
{
nextScene = scene->Update();
}

bn::core::update();

if (nextScene)
{
switch (*nextScene)
{
case sym::SceneType::SPLASH:
scene.reset(new sym::SplashScene);
break;
case sym::SceneType::TITLE:
// TODO: scene.reset(new sym::TitleScene);
break;
// case sym::SceneType::GAME:
// break;
// case sym::SceneType::LICENSE:
// break;
default:
BN_ERROR("Unknown SceneType: ", (int)*nextScene);
}
}
}
}

0 comments on commit d0d788f

Please sign in to comment.