Skip to content

Instantly share code, notes, and snippets.

@voidproc
Last active August 21, 2016 15:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save voidproc/721418b49cf5cf1b354c2e0e870ca38b to your computer and use it in GitHub Desktop.
Save voidproc/721418b49cf5cf1b354c2e0e870ca38b to your computer and use it in GitHub Desktop.
Scene changing with ham::SceneManager
#include <Siv3D.hpp>
#include <HamFramework.hpp>
// こちらで詳しい解説がされています:
// http://qiita.com/hamukun8686/items/4620d630b538c78a6e02
enum class SceneType
{
Empty,
Title,
Main,
};
struct GameData
{
void Fade(double t, bool fadeIn)
{
Graphics2D::SetStencilState(StencilState::Replace);
Graphics2D::SetStencilValue(1);
Circle(Window::Center(), (fadeIn ? t : 1 - t) * (Window::ClientRect().w) / 2.0 * 1.3).draw();
Graphics2D::SetStencilState(StencilState::Test(StencilFunc::NotEqual));
Window::ClientRect().draw(Palette::Black);
Graphics2D::SetStencilState(StencilState::Default);
}
};
using SceneMgr = SceneManager<SceneType, GameData>;
struct SceneEmpty : SceneMgr::Scene
{
void update() override {}
void draw() const override
{
Window::ClientRect().draw(Palette::Black);
}
void updateFadeIn(double t) override
{
// 即タイトルシーンへ
changeScene(SceneType::Title, 750, true);
}
};
struct SceneTitle : SceneMgr::Scene
{
void update() override
{
if (Input::MouseL.clicked)
{
changeScene(SceneType::Main, 750);
}
}
void draw() const override
{
Window::ClientRect().draw(Palette::Royalblue);
FontAsset(L"font")(L"TITLE").drawCenter(Window::Center(), Palette::White);
}
};
struct SceneMain : SceneMgr::Scene
{
void update() override
{
if (Input::MouseL.clicked)
{
changeScene(SceneType::Title, 750);
}
}
void draw() const override
{
Window::ClientRect().draw(Palette::Palevioletred);
FontAsset(L"font")(L"MAIN GAME SCENE").drawCenter(Window::Center(), Palette::White);
}
void drawFadeIn(double t) const override
{
draw();
m_data->Fade(t, true);
}
void drawFadeOut(double t) const override
{
draw();
m_data->Fade(t, false);
}
};
void Main()
{
Window::Resize(640, 480);
Graphics::SetBackground(Palette::Black);
FontAsset::Register(L"font", 40, Typeface::Bold);
SceneMgr sceneMgr;
sceneMgr.add<SceneEmpty>(SceneType::Empty);
sceneMgr.add<SceneTitle>(SceneType::Title);
sceneMgr.add<SceneMain>(SceneType::Main);
while (System::Update())
{
if (!sceneMgr.updateAndDraw())
{
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment