-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain-entt.cpp
More file actions
173 lines (142 loc) · 4.23 KB
/
main-entt.cpp
File metadata and controls
173 lines (142 loc) · 4.23 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
167
168
169
170
171
172
173
#include <SFML/Graphics.hpp>
#include <entt/entity/registry.hpp>
#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 {
void update(entt::DefaultRegistry ®istry, double dt)
{
const auto fdt = static_cast<float>(dt);
registry.view<Body>().each([fdt](auto entity, auto& body) { body.position += body.direction * fdt; });
};
};
class BounceSystem
{
public:
explicit BounceSystem(sf::Vector2u size) : size(size) {}
void update(entt::DefaultRegistry ®istry, double dt)
{
registry.view<Body>().each([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:
explicit RenderSystem(sf::RenderTarget& target, sf::Font& font) : target(target)
{
text.setFont(font);
text.setPosition(sf::Vector2f(32, 2));
}
void update(entt::DefaultRegistry ®istry, double dt)
{
int count = 0;
registry.view<Body, Renderable>().each([this, &count](auto entity, auto& body, auto& renderable) {
renderable->setPosition(body.position);
target.draw(*renderable);
++count;
});
print_fps(dt, count);
}
private:
void print_fps(double 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
{
entt::DefaultRegistry registry;
BodySystem body_system;
BounceSystem bounce_system;
RenderSystem render_system;
public:
GameEngine(sf::RenderTarget& target, sf::Font& font)
: bounce_system(target.getSize()), render_system(target, font)
{}
void update(double dt) {
body_system.update(registry, dt);
bounce_system.update(registry, dt);
render_system.update(registry, dt);
}
void create_bodies(sf::Vector2u size, int count)
{
for (int i = 0; i < count; i++) {
auto entity = registry.create();
registry.assign<Body>(entity, 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)));
registry.assign<Renderable>(entity, 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;
}