-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIMap.h
146 lines (122 loc) · 3.78 KB
/
IMap.h
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#pragma once
#include <SDL.h>
#include <memory>
#include <vector>
#include "IMapObject.h"
#include "IMapView.h"
#include "Tools.h"
#define KEY_W 1
#define KEY_A 2
#define KEY_S 4
#define KEY_D 8
#define KEY_Space 16
/**
* Map is a central Class that manages updating all of the attached objects,
* rendering those objects on attached views, handling events from the
* underlaying libraries, and all that kind of jazz.
*
* The IMap virtual base class already comes with a m_objects and m_views vector
* attributes, helpers to add IMapObjects and IMapViews and basic
* implementations of update() and render().
*/
class IMap {
public:
using objects_t = std::vector<std::shared_ptr<IMapObject>>;
using views_t = std::vector<std::shared_ptr<IMapView>>;
protected:
/// A vector of all attached IMapObjects
objects_t m_objects{};
/// A vector of all attached IMapViews
views_t m_views{};
public:
virtual ~IMap() {}
//~~~ Object-related methods ~~~//
/**
* Add an IMapObject to the map.
*/
virtual void add(std::shared_ptr<IMapObject> o) {
o->set_map(this);
m_objects.push_back(std::move(o));
}
/**
* Add an IMapObject to the map.
* This overload will immediately wrap the argument in a shared_ptr.
*/
virtual void add(IMapObject* o) {
o->set_map(this);
m_objects.emplace_back(o);
}
/**
* Get the list of all attached objects
*/
virtual objects_t const& get_objects() { return m_objects; }
//~~~ View-related methods ~~~//
/**
* Add an IMapView to the map.
*/
virtual void add_view(std::shared_ptr<IMapView> o) {
m_views.push_back(std::move(o));
}
/**
* Add an IMapView to the map.
* This overload will immediately wrap the argument in a shared_ptr.
*/
virtual void add_view(IMapView* o) { m_views.emplace_back(o); }
/**
* Get the list of all attached views
*/
virtual views_t const& get_views() { return m_views; }
//~~~ Main methods ~~~//
/**
* init() is a method that shall be called precisely once,
* before calling the loop() method.
*
* Some of the Map implementations might require setup code,
* e.g. creating windows.
*/
virtual void init(){};
/**
* loop() is a method that will, presumably,
* run the Map until an exit or error condition is met.
*
* This will involve multiple calls to update() and render().
*/
virtual void loop() = 0;
/**
* update() is a proxy method to update all attached objects.
*/
virtual void update() {
for (auto const& obj : m_objects) obj->update();
}
/**
* render() is a method that will call appropiate methods
* on all of the attached views in order to properly render current scene.
*/
virtual void render() {
for (auto const& view : m_views) {
view->before_render();
for (auto const& obj : m_objects) view->render(obj.get());
view->after_render();
}
}
/**
* load_texture() is a method to load an SDL_Texture from a file,
* while also saving the texture's dimensions.
*/
virtual SDL_Texture* load_texture(char const* file, float* w = nullptr,
float* h = nullptr) {
return nullptr;
};
/**
* get_bounds() is a method to get the maximum allowed position
* of an object
*/
virtual Vector2D const get_bounds() { return {-1, -1}; };
/**
* get_pressed_keys() is a method that is supposed to return a number
* encoding the information on key presses.
*
* See the KEY_* macros for values corresponding to particular keys.
*/
virtual unsigned char const get_pressed_keys() { return 0; }
};