summary refs log tree commit diff stats
path: root/src/system_manager.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/system_manager.h')
-rw-r--r--src/system_manager.h68
1 files changed, 68 insertions, 0 deletions
diff --git a/src/system_manager.h b/src/system_manager.h new file mode 100644 index 0000000..b03c3f2 --- /dev/null +++ b/src/system_manager.h
@@ -0,0 +1,68 @@
1#ifndef SYSTEM_MANAGER_H_544E6056
2#define SYSTEM_MANAGER_H_544E6056
3
4#include <list>
5#include <memory>
6#include <map>
7#include <typeindex>
8#include <stdexcept>
9#include "system.h"
10
11class SystemManager {
12private:
13
14 std::list<std::unique_ptr<System>> loop;
15 std::map<std::type_index, System*> systems;
16
17public:
18
19 template <class T, class... Args>
20 void emplaceSystem(Game& game, Args&&... args)
21 {
22 std::unique_ptr<T> ptr(new T(game, std::forward<Args>(args)...));
23 std::type_index systemType = typeid(T);
24
25 systems[systemType] = ptr.get();
26 loop.push_back(std::move(ptr));
27 }
28
29 template <class T>
30 T& getSystem()
31 {
32 std::type_index systemType = typeid(T);
33
34 if (!systems.count(systemType))
35 {
36 throw std::invalid_argument("Cannot get non-existent system");
37 }
38
39 return *dynamic_cast<T*>(systems[systemType]);
40 }
41
42 void tick(double dt)
43 {
44 for (std::unique_ptr<System>& sys : loop)
45 {
46 sys->tick(dt);
47 }
48 }
49
50 virtual void render(Texture& texture)
51 {
52 for (std::unique_ptr<System>& sys : loop)
53 {
54 sys->render(texture);
55 }
56 }
57
58 virtual void input(int key, int action)
59 {
60 for (std::unique_ptr<System>& sys : loop)
61 {
62 sys->input(key, action);
63 }
64 }
65
66};
67
68#endif /* end of include guard: SYSTEM_MANAGER_H_544E6056 */