-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
scene.hpp
99 lines (85 loc) · 2.71 KB
/
scene.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#pragma once
//=====================================================================//
/*! @file
@brief シーン・クラス
@author 平松邦仁 ([email protected])
@copyright Copyright (C) 2019 Kunihito Hiramatsu @n
Released under the MIT license @n
https://github.com/hirakuni45/RX/blob/master/LICENSE
*/
//=====================================================================//
#include <cstdint>
#include <tuple>
namespace utils {
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
/*!
@brief シーン・インターフェース・クラス
*/
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
struct scene {
virtual ~scene() { } ///< デストラクタ
virtual void init() = 0; ///< シーン開始前処理
virtual void service() = 0; ///< シーン・サービス
virtual void exit() = 0; ///< シーン終了処理
};
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
/*!
@brief シーン・ディレクター・クラス
@param[in] Args 管理するシーンの全て
*/
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
template<class... Args>
class scene_director {
typedef std::tuple<Args...> SCENES;
SCENES scenes_;
scene* cur_scene_;
scene* new_scene_;
public:
//-----------------------------------------------------------------//
/*!
@brief コンストラクター
*/
//-----------------------------------------------------------------//
scene_director() noexcept : scenes_(), cur_scene_(nullptr), new_scene_(nullptr)
{ }
//-----------------------------------------------------------------//
/*!
@brief 開始(最初の型シーン)
*/
//-----------------------------------------------------------------//
void start_first() noexcept
{
auto& t = std::get<0>(scenes_);
new_scene_ = &t;
}
//-----------------------------------------------------------------//
/*!
@brief シーンの切り替え
@param[in] news シーン
*/
//-----------------------------------------------------------------//
template <class T>
void change() noexcept
{
auto& news = std::get<T>(scenes_);
new_scene_ = &news;
}
//-----------------------------------------------------------------//
/*!
@brief サービス
*/
//-----------------------------------------------------------------//
void service() noexcept
{
if(new_scene_ != nullptr) {
if(cur_scene_ != nullptr) cur_scene_->exit();
new_scene_->init();
cur_scene_ = new_scene_;
new_scene_ = nullptr;
}
if(cur_scene_ != nullptr) {
cur_scene_->service();
}
}
};
}