-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain-entityx.cpp
More file actions
166 lines (136 loc) · 4.3 KB
/
main-entityx.cpp
File metadata and controls
166 lines (136 loc) · 4.3 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <SFML/Graphics.hpp>
#include <entityx/entityx.h>
#include <cstdlib>
#include <iostream>
#include <sstream>
inline float r(int a, float b = 0) { return static_cast<float>(std::rand() % (a * 1000) + b * 1000) / 1000.0; }
////////////////////////////////////////////////////////////////
struct Body {
Body(const sf::Vector2f& position, const sf::Vector2f& direction)
: position(position), direction(direction)
{}
sf::Vector2f position;
sf::Vector2f direction;
};
struct BodySystem : public entityx::System<BodySystem> {
void update(entityx::EntityManager& es, entityx::EventManager& events, entityx::TimeDelta dt) override
{
const auto fdt = static_cast<float>(dt);
es.each<Body>([fdt](auto entity, auto& body) { body.position += body.direction * fdt; });
};
};
class BounceSystem : public entityx::System<BounceSystem>
{
public:
explicit BounceSystem(sf::Vector2u size) : size(size) {}
void update(entityx::EntityManager& es, entityx::EventManager& events, entityx::TimeDelta dt) override
{
es.each<Body>([this](auto entity, auto& body) {
if (body.position.x + body.direction.x < 0 || body.position.x + body.direction.x >= size.x) {
body.direction.x = -body.direction.x;
}
if (body.position.y + body.direction.y < 0 || body.position.y + body.direction.y >= size.y) {
body.direction.y = -body.direction.y;
}
});
}
private:
sf::Vector2u size;
};
////////////////////////////////////////////////////////////////
using Renderable = std::shared_ptr<sf::Shape>;
class RenderSystem : public entityx::System<RenderSystem>
{
public:
explicit RenderSystem(sf::RenderTarget& target, sf::Font& font) : target(target)
{
text.setFont(font);
text.setPosition(sf::Vector2f(32, 2));
}
void update(entityx::EntityManager& es, entityx::EventManager& events, entityx::TimeDelta dt) override
{
es.each<Body, Renderable>([this](auto entity, auto& body, auto& renderable) {
renderable->setPosition(body.position);
target.draw(*renderable);
});
print_fps(dt, es.size());
}
private:
void print_fps(entityx::TimeDelta dt, size_t nb_entities)
{
last_update += dt;
++frame_count;
if (last_update >= 0.5) {
const double fps = frame_count / last_update;
oss.seekp(0);
oss << nb_entities << " entities (" << static_cast<int>(fps) << " fps) ";
text.setString(oss.str());
last_update = 0.0;
frame_count = 0.0;
}
target.draw(text);
}
private:
sf::RenderTarget& target;
sf::Text text;
double last_update = 0;
double frame_count = 0;
std::ostringstream oss;
};
////////////////////////////////////////////////////////////////
class GameEngine : public entityx::EntityX
{
public:
GameEngine(sf::RenderTarget& target, sf::Font& font)
{
systems.add<BodySystem>();
systems.add<BounceSystem>(target.getSize());
systems.add<RenderSystem>(target, font);
systems.configure();
}
void update(entityx::TimeDelta dt) { systems.update_all(dt); }
void create_bodies(sf::Vector2u size, int count)
{
for (int i = 0; i < count; i++) {
auto entity = entities.create();
entity.assign<Body>(sf::Vector2f(r(size.x), r(size.y)), sf::Vector2f(r(200, -200), r(200, -200)));
Renderable shape(new sf::CircleShape(20));
shape->setOrigin(10, 10);
shape->setFillColor(sf::Color(r(128, 127), r(128, 127), r(128, 127)));
entity.assign<Renderable>(shape);
}
}
};
////////////////////////////////////////////////////////////////
int main()
{
std::srand(std::time(nullptr));
sf::Font font;
if (!font.loadFromFile("Roboto.ttf")) {
std::cerr << "error: failed to load Roboto.ttf" << std::endl;
return 1;
}
sf::RenderWindow window(sf::VideoMode::getDesktopMode(), "ECS Test", sf::Style::Fullscreen);
window.setVerticalSyncEnabled(true);
GameEngine engine(window, font);
engine.create_bodies(window.getSize(), 100);
sf::Clock clock;
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
switch (event.type) {
case sf::Event::Closed:
case sf::Event::KeyPressed:
window.close();
break;
default:
break;
}
}
window.clear();
auto elapsed = clock.restart();
engine.update(elapsed.asSeconds());
window.display();
}
return 0;
}