Ophidian
 All Classes Namespaces Functions
drawable_batch.h
1 #ifndef OPHIDIAN_GUI_DRAWABLE_BATCH_H
2 #define OPHIDIAN_GUI_DRAWABLE_BATCH_H
3 
4 #include <SFML/Graphics.hpp>
5 #include "../geometry/geometry.h"
6 #include "../entity_system/entity_system.h"
7 #include "../entity_system/vector_property.h"
8 
9 namespace ophidian {
10 namespace gui {
11 
13  std::vector< sf::Vertex > m_delta;
14  const std::size_t c_DURATION;
15  std::size_t m_current;
16 public:
17  template <class T>
18  batch_animation(T & batch, std::size_t duration) :
19  m_delta(batch.vertex_count()),
20  c_DURATION(duration),
21  m_current(0)
22  {
23  }
24 
25  sf::Vertex& operator[](const std::size_t i)
26  {
27  return m_delta[i];
28  }
29 
30  batch_animation* update(sf::Vertex * v) {
31  if(m_current == c_DURATION)
32  {
33  delete this;
34  return nullptr;
35  }
36  for(std::size_t i = 0; i < m_delta.size(); ++i)
37  {
38  v[i].position.x += m_delta[i].position.x/static_cast<double>(c_DURATION);
39  v[i].position.y += m_delta[i].position.y/static_cast<double>(c_DURATION);
40  }
41  ++m_current;
42  return this;
43  }
44 };
45 
46 
47 template<std::size_t NumberOfVertices>
48 class drawable_batch : public sf::Drawable
49 {
52  sf::PrimitiveType m_primitive;
53 
54  batch_animation * m_animation;
55 
56 public:
57  drawable_batch(sf::PrimitiveType primitive) :
58  m_primitive(primitive),
59  m_animation(nullptr){
60  m_system.register_property(&m_vertices);
61  m_system.preallocate(10000000);
62  }
63 
64  bool has_animation() const {
65  return m_animation != nullptr;
66  }
67 
68  const sf::Vertex operator[](const std::size_t i) const {
69  return reinterpret_cast<const sf::Vertex*>(m_vertices.data())[i];
70  }
71 
72  void animate(batch_animation * animation)
73  {
74  m_animation = animation;
75  }
76 
77  entity_system::entity create() {
78  auto the_entity = m_system.create();
79  paint(the_entity, sf::Color::Black);
80  return the_entity;
81  }
82 
83  void destroy(entity_system::entity the_entity) {
84  m_system.destroy(the_entity);
85  }
86 
87  void transform(entity_system::entity the_entity, const sf::Transform & trans)
88  {
89  std::array<sf::Vertex, NumberOfVertices> & vertices = m_vertices[m_system.lookup(the_entity)];
90  for(sf::Vertex & v : vertices)
91  v.position = trans.transformPoint(v.position);
92  }
93 
94  void paint(entity_system::entity the_entity, const sf::Color & color)
95  {
96  std::array<sf::Vertex, NumberOfVertices> & vertices = m_vertices[m_system.lookup(the_entity)];
97  for(sf::Vertex & v : vertices)
98  v.color = color;
99  }
100 
101  void set_point(entity_system::entity the_entity, std::size_t i, const geometry::point<double> &p)
102  {
103  std::array<sf::Vertex, NumberOfVertices> & vertices = m_vertices[m_system.lookup(the_entity)];
104  vertices.at(i).position = sf::Vector2f(p.x(), p.y());
105  }
106 
107  geometry::point<double> point(entity_system::entity the_entity, std::size_t i) const {
108  const std::array<sf::Vertex, NumberOfVertices> & vertices = m_vertices[m_system.lookup(the_entity)];
109  return geometry::point<double>(vertices[i].position.x, vertices[i].position.y);
110  }
111 
112  std::size_t vertex_count() const {
113  return m_system.size()*NumberOfVertices;
114  }
115 
116  const std::array<sf::Vertex, NumberOfVertices> & points(entity_system::entity & the_entity) const {
117  return m_vertices[m_system.lookup(the_entity)];
118  }
119 
120 
121  void update()
122  {
123  if(m_animation)
124  m_animation = m_animation->update(reinterpret_cast<sf::Vertex*>(m_vertices.data()));
125  }
126 
127  void draw(sf::RenderTarget& target, sf::RenderStates states) const {
128  target.draw(reinterpret_cast<const sf::Vertex*>(m_vertices.data()), m_system.size()*NumberOfVertices, m_primitive, states);
129  }
130 
131  void clear() {
132  for(auto entity : m_system)
133  m_system.destroy(entity);
134  }
135 
136 };
137 
138 
139 
140 }
141 }
142 
143 #endif // OPHIDIAN_GUI_DRAWABLE_BATCH_H
void preallocate(std::size_t qnt)
Preallocate method.
Definition: entity_system.cpp:53
Definition: drawable_batch.h:12
Implementation of the vector property class.
Definition: vector_property.h:37
void destroy(entity e)
Destroy an entity.
Definition: entity_system.cpp:27
entity create()
Creates an entity.
Definition: entity_system.cpp:42
Definition: drawable_batch.h:48
entity_system class.
Definition: entity_system.h:40
std::size_t size() const
Returns the size of the system.
Definition: entity_system.h:82
entity_index lookup(entity e) const
Gets the index of an entity.
Definition: entity_system.h:149
void register_property(property *property)
Registers property.
Definition: entity_system.cpp:61