Fireworks Engine  v2.0
Lightweight Sandbox Game Engine using OpenGL for additional Customisation and Quick Prototyping
renderable2d.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <vector>
4 #include <iostream>
5 
6 #include <src/graphics/shader.h>
11 #include <src/graphics/texture.h>
13 
14 namespace fireworks { namespace graphics {
15 
17  struct VertexData
18  {
19  glm::vec3 vertex;
20  glm::vec2 uv;
21  float tid;
22  glm::vec4 color;
23  };
24 
26  enum class Primitive2D
27  {
28  Circle,
29  Triangle,
30  Quad
31  };
32 
37  {
38  public:
40  std::uint32_t objectID;
46  bool flippedX;
48  bool flippedY;
50  std::vector<components::Component*> components;
51  protected:
53  glm::vec2 m_Size;
55  glm::vec3 m_Position;
57  glm::vec4 m_Color;
59  float m_Rotation;
63 mutable std::vector<glm::vec2> m_UV;
66  private:
67  static std::uint32_t m_UniqueID;
68  public:
76  Renderable2D(glm::vec3 position, glm::vec2 size, glm::vec4 color, Primitive2D primitive2d)
77  : m_Position(position), m_Size(size), m_Color(color), m_Primitive2D(primitive2d), shader(nullptr), m_Texture(nullptr), m_Rotation(0.0f)
78  {
79  objectID = ++m_UniqueID;
80  setUVDefaults();
81  }
82 
91  Renderable2D(glm::vec3 position, glm::vec2 size, glm::vec4 color, Primitive2D primitive2d, Shader* shader)
92  : m_Position(position), m_Size(size), m_Color(color), m_Primitive2D(primitive2d), shader(shader), m_Texture(nullptr), m_Rotation(0.0f)
93  {
94  objectID = ++m_UniqueID;
95  shader->enable();
96  GLint texIDs[] =
97  {
98  0, 1, 2, 3, 4, 5, 6, 7,
99  8, 9, 10, 11, 12, 13, 14, 15
100  };
101  shader->setUniform1iv("textures", texIDs, 8);
102  shader->disable();
103 
104  setUVDefaults();
105  }
106 
107  virtual ~Renderable2D() { }
108 
110  virtual void submit(Renderer2D* renderer) const
111  {
112  renderer->submit(this);
113  }
114 
118  template <typename T>
120  {
121  components.push_back(component);
122 
123  if ((dynamic_cast<physics::RigidBody2D*>(component)))
124  std::cout << "Added a rigibody2d component" << std::endl;
125  }
126 
128  template <>
129  void AddComponent<physics::RigidBody2D>(components::Component* component)
130  {
131  components.push_back(component);
132  //if ((dynamic_cast<physics::RigidBody2D*>(component)))
133  std::cout << "Added a rigibody2d component in the rigidBody overload" << std::endl;
134 
135  this->addedRigidBody2D();
136  }
137 
141  inline const glm::vec3& getPosition() const { const_cast<Renderable2D*>(this)->GetRigidBody2DPosition(); return m_Position; }
145  inline const float& getRotation() const { const_cast<Renderable2D*>(this)->GetRigidBody2DRotation(); return m_Rotation; }
147  inline const glm::vec2& getSize() const { return m_Size; }
149  inline const glm::vec4& getColor() const { return m_Color; }
151  inline const Primitive2D getPrimitive() const { return m_Primitive2D; }
153  inline const std::vector<glm::vec2>& getUV() const { return m_UV; }
155  inline const Texture* getTexture() const { return m_Texture; }
157  inline const GLuint getTID() const { return m_Texture == nullptr ? 0 : m_Texture->getID(); }
158 
159  // Custom Methods
161  void flipX() { flippedX = true; }
163  void unflipX() { flippedX = false; }
165  void flipY() { flippedY = true; }
167  void unflipY() { flippedY = false; }
168 
169  protected:
171  Renderable2D() : m_Texture(nullptr)
172  {
173  objectID = ++m_UniqueID;
174  setUVDefaults();
175  }
176 
178  virtual void addedRigidBody2D() { }
179  private:
180  void setUVDefaults()
181  {
182  if (m_Primitive2D == Primitive2D::Quad)
183  {
184  m_UV.push_back(glm::vec2(0, 0)); // Bottom Left
185  m_UV.push_back(glm::vec2(0, 1)); // Top Left
186  m_UV.push_back(glm::vec2(1, 1)); // Top Right
187  m_UV.push_back(glm::vec2(1, 0)); // Bottom Right
188  }
189  else if (m_Primitive2D == Primitive2D::Triangle)
190  {
191  m_UV.clear();
192  m_UV.push_back(glm::vec2(0, 0)); // Bottom Left
193  m_UV.push_back(glm::vec2(0.5, 1)); // Top Middle
194  m_UV.push_back(glm::vec2(1, 0)); // Bottom Right
195  }
196  }
197  void GetRigidBody2DPosition()
198  {
199  for (int i = 0; i < components.size(); i++)
200  {
201  if (physics::RigidBody2D* rb = (dynamic_cast<physics::RigidBody2D*>(components[i])))
202  {
203  m_Position = rb->GetPositionInPixels();
204  }
205  else
206  return;
207  }
208  }
209  void GetRigidBody2DRotation()
210  {
211  for (int i = 0; i < components.size(); i++)
212  {
213  if (physics::RigidBody2D* rb = (dynamic_cast<physics::RigidBody2D*>(components[i])))
214  {
215  m_Rotation = rb->GetRotation();
216  }
217  else
218  return;
219  }
220  }
221  };
222 
223 } }
224 
const std::vector< glm::vec2 > & getUV() const
Gets the UV coordinates of the 2D Renderable.
Definition: renderable2d.h:153
std::vector< glm::vec2 > m_UV
The UV coordinates of the renderable.
Definition: renderable2d.h:63
void flipX()
Flip the Renderable along the X-axis.
Definition: renderable2d.h:161
glm::vec4 color
Definition: renderable2d.h:22
const Primitive2D getPrimitive() const
Gets the primitive shape that is used to draw the renderable.
Definition: renderable2d.h:151
void unflipY()
Unflip the Renderable along the X-axis.
Definition: renderable2d.h:167
virtual void submit(Renderer2D *renderer) const
Virtual overload of the submit function, customize the way you can submit the renderable to the rende...
Definition: renderable2d.h:110
glm::vec3 vertex
Definition: renderable2d.h:19
const glm::vec4 & getColor() const
Gets the color of the renderable.
Definition: renderable2d.h:149
Renderable2D()
Create an empty renderable.
Definition: renderable2d.h:171
RigidBody 2D Physics component to simulate real time physics.
Definition: rigidbody2d.h:27
The Class responsible for drawing the basic Renderable objects onto the screen.
Definition: renderable2d.h:36
The class responsible for loading Textures.
Definition: texture.h:11
glm::vec2 uv
Definition: renderable2d.h:20
Renderable2D(glm::vec3 position, glm::vec2 size, glm::vec4 color, Primitive2D primitive2d)
Creates the renderable.
Definition: renderable2d.h:76
glm::vec2 m_Size
The size of the renderable.
Definition: renderable2d.h:53
The structure of the Vertex Data.
Definition: renderable2d.h:17
Class for creating components.
Definition: component.h:22
glm::vec4 m_Color
The color of the renderable.
Definition: renderable2d.h:57
const GLuint getTID() const
Gets the ID of the renderables texture.
Definition: renderable2d.h:157
Definition: audioclip.cpp:3
const glm::vec3 & getPosition() const
Gets the position of the Renderable.
Definition: renderable2d.h:141
Shader * shader
The Shader used to draw the particular renderable.
Definition: renderable2d.h:44
virtual void submit(const Renderable2D *renderable)=0
Begins to submit the renderables to render queue.
const unsigned int getID() const
Gets the ID of the texture.
Definition: texture.h:40
float tid
Definition: renderable2d.h:21
std::vector< components::Component * > components
The collection of Components that is attached to the renderable.
Definition: renderable2d.h:50
The class responsible for creating amazing shaders.
Definition: Shader.h:22
virtual ~Renderable2D()
Definition: renderable2d.h:107
Primitive2D m_Primitive2D
The primitive that is used to draw the renderable.
Definition: renderable2d.h:61
void flipY()
Flip the Renderable along the Y-axis.
Definition: renderable2d.h:165
void enable()
Enables the shader.
Definition: Shader.cpp:119
float m_Rotation
The rotation of the renderable along the Z-Axis.
Definition: renderable2d.h:59
const float & getRotation() const
Gets the rotation of the Renderable.
Definition: renderable2d.h:145
Primitive2D
Different variations of primitives.
Definition: renderable2d.h:26
virtual void addedRigidBody2D()
Component callback functions.
Definition: renderable2d.h:178
Texture * m_Texture
The texture (if any) used by the renderable.
Definition: renderable2d.h:65
Renderable2D(glm::vec3 position, glm::vec2 size, glm::vec4 color, Primitive2D primitive2d, Shader *shader)
Creates the renderable.
Definition: renderable2d.h:91
Forward declaration of the graphics::Renderable2D.
Definition: renderer2d.h:15
void disable()
Disables the shader.
Definition: Shader.cpp:124
void AddComponent(components::Component *component)
Template methods of various ways of adding components to the renderable.
Definition: renderable2d.h:119
bool flippedX
tells whether or not the renderable is flipped on the X-axis or not
Definition: renderable2d.h:46
const Texture * getTexture() const
Gets the texture that is used to draw the renderable.
Definition: renderable2d.h:155
bool flippedY
tells whether or not the renderable is flipped on the Y-axis or not
Definition: renderable2d.h:48
std::uint32_t objectID
The unique ID of the renderable object.
Definition: renderable2d.h:40
void unflipX()
Unflip the Renderable along the X-axis.
Definition: renderable2d.h:163
glm::vec3 m_Position
The position of the renderable.
Definition: renderable2d.h:55
void setUniform1iv(const GLchar *name, int *value, GLsizei count)
Definition: Shader.cpp:94
const glm::vec2 & getSize() const
Gets the size of the renderable.
Definition: renderable2d.h:147