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
|
#ifndef ENTITYXBENCHMARK_H_
#define ENTITYXBENCHMARK_H_
#include <string>
#include <vector>
#include <memory>
#include <random>
#include <numeric>
#include <functional>
#include "entityx/entityx.h"
class EntityXBenchmark {
public:
struct PositionComponent {
float x = 0.0f;
float y = 0.0f;
};
struct DirectionComponent {
float x = 0.0f;
float y = 0.0f;
};
struct ComflabulationComponent {
float thingy = 0.0;
int dingy = 0;
bool mingy = false;
std::string stringy;
};
// Convenience types for our entity system.
using Entity = entityx::Entity;
using EntityManager = entityx::EventManager;
using EventManager = entityx::EventManager;
template <typename C>
using Component = entityx::ComponentHandle<C>;
using TimeDelta = entityx::TimeDelta;
template<class S>
using System = entityx::System<S>;
class MovementSystem : public System<MovementSystem> {
public:
MovementSystem() = default;
void update(entityx::EntityManager &es, entityx::EventManager &events, entityx::TimeDelta dt) override {
Component<PositionComponent> position;
Component<DirectionComponent> direction;
for (auto entity : es.entities_with_components(position, direction)) {
position->x += direction->x * dt;
position->y += direction->y * dt;
}
}
};
class ComflabSystem : public System<ComflabSystem> {
public:
ComflabSystem() = default;
void update(entityx::EntityManager &es, entityx::EventManager &events, entityx::TimeDelta dt) override {
Component<ComflabulationComponent> comflab;
for (auto entity : es.entities_with_components(comflab)) {
comflab->thingy *= 1.000001f;
comflab->mingy = !comflab->mingy;
comflab->dingy++;
//comflab.stringy = std::to_string(comflab.dingy);
}
}
};
#ifdef USE_MORECOMPLEX_SYSTEM
class MoreComplexSystem : public System<MoreComplexSystem> {
private:
int random(int min, int max){
// Seed with a real random value, if available
static std::random_device r;
// Choose a random mean between min and max
static std::default_random_engine e1(r());
std::uniform_int_distribution<int> uniform_dist(min, max);
return uniform_dist(e1);
}
public:
MoreComplexSystem() = default;
void update(entityx::EntityManager &es, entityx::EventManager &events, entityx::TimeDelta dt) override {
Component<PositionComponent> position;
Component<DirectionComponent> direction;
Component<ComflabulationComponent> comflab;
for (auto entity : es.entities_with_components(comflab, direction, comflab)) {
if(comflab) {
std::vector<double> vec;
for(size_t i = 0;i < comflab->dingy && i < 100;i++){
vec.push_back(i * comflab->thingy);
}
int sum = std::accumulate(std::begin(vec), std::end(vec), 0.0);
int product = std::accumulate(std::begin(vec), std::end(vec), 1, std::multiplies<double>());
comflab->stringy = std::to_string(comflab->dingy);
if(position && direction && comflab->dingy % 10000 == 0) {
if(position->x > position->y) {
direction->x = random(0, 5);
direction->y = random(0, 10);
} else {
direction->x = random(0, 10);
direction->y = random(0, 5);
}
}
}
}
}
};
#endif
class Application : public entityx::EntityX {
public:
Application() {
this->systems.add<MovementSystem>();
this->systems.add<ComflabSystem>();
#ifdef USE_MORECOMPLEX_SYSTEM
this->systems.add<MoreComplexSystem>();
#endif
this->systems.configure();
}
void update(TimeDelta dt) {
this->systems.update<MovementSystem>(dt);
this->systems.update<ComflabSystem>(dt);
#ifdef USE_MORECOMPLEX_SYSTEM
this->systems.update<MoreComplexSystem>(dt);
#endif
}
};
static constexpr double fakeDeltaTime = 1.0 / 60;
};
#endif // ENTITYXBENCHMARK_H_
|