GameKit  0.0.1a
C++ gamedev tools
RectangleShape.cpp
Go to the documentation of this file.
1 /*
2  * =====================================================================================
3  *
4  * Filename: RectangleShape.cpp
5  *
6  * Description:
7  *
8  * Created: 27/09/2014 17:06:57
9  *
10  * Author: Quentin Bazin, <gnidmoo@gmail.com>
11  *
12  * =====================================================================================
13  */
14 #define GLM_FORCE_RADIANS
15 #include <glm/gtc/matrix_transform.hpp>
16 
17 #include "gk/gl/GLCheck.hpp"
18 #include "gk/gl/Shader.hpp"
19 #include "gk/gl/Vertex.hpp"
21 
22 namespace gk {
23 
25  for (u8 i = 1 ; i < 5 ; ++i) {
26  for (u8 j = 0 ; j < 6 ; ++j) {
27  m_indices[i * 6 + j] = m_indices[j] + 4 * i;
28  }
29  }
30 }
31 
33  m_color = color;
34 
35  setSize(width, height);
36 }
37 
39  float o = m_outlineThickness;
40  Vertex vertices[4 + 4 * 4] = {
41  // Rectangle vertices
42  {{m_width, 0, 0, -1}},
43  {{0, 0, 0, -1}},
44  {{0, m_height, 0, -1}},
45  {{m_width, m_height, 0, -1}},
46 
47  // Top outline
48  {{m_width + o, -o, 0, -1}},
49  {{-o, -o, 0, -1}},
50  {{-o, 0, 0, -1}},
51  {{m_width + o, 0, 0, -1}},
52 
53  // Bottom outline
54  {{m_width + o, m_height, 0, -1}},
55  {{-o, m_height, 0, -1}},
56  {{-o, m_height + o, 0, -1}},
57  {{m_width + o, m_height + o, 0, -1}},
58 
59  // Left outline
60  {{0, 0, 0, -1}},
61  {{-o, 0, 0, -1}},
62  {{-o, m_height, 0, -1}},
63  {{0, m_height, 0, -1}},
64 
65  // Right outline
66  {{m_width, 0, 0, -1}},
67  {{m_width + o, 0, 0, -1}},
68  {{m_width + o, m_height, 0, -1}},
69  {{m_width, m_height, 0, -1}},
70  };
71 
72  for (u8 i = 0 ; i < 4 ; ++i) {
73  vertices[i].color[0] = m_color.r;
74  vertices[i].color[1] = m_color.g;
75  vertices[i].color[2] = m_color.b;
76  vertices[i].color[3] = m_color.a;
77  }
78 
79  for (u8 i = 4 ; i < 4 + 4 * 4 ; ++i) {
80  vertices[i].color[0] = m_outlineColor.r;
81  vertices[i].color[1] = m_outlineColor.g;
82  vertices[i].color[2] = m_outlineColor.b;
83  vertices[i].color[3] = m_outlineColor.a;
84  }
85 
87  m_vbo.setData(sizeof(vertices), vertices, GL_DYNAMIC_DRAW);
88  VertexBuffer::bind(nullptr);
89 }
90 
91 void RectangleShape::draw(RenderTarget &target, RenderStates states) const {
92  states.transform *= getTransform();
93 
95 
96  glCheck(glDisable(GL_CULL_FACE));
97  glCheck(glDisable(GL_DEPTH_TEST));
98 
99  if(m_wireframeMode) glCheck(glPolygonMode(GL_FRONT_AND_BACK, GL_LINE));
100  target.drawElements(m_vbo, GL_TRIANGLES, 6 * 5, GL_UNSIGNED_BYTE, m_indices.data(), states);
101  if(m_wireframeMode) glCheck(glPolygonMode(GL_FRONT_AND_BACK, GL_FILL));
102 }
103 
104 } // namespace gk
105 
Utility class for manipulating RGBA colors.
Definition: Color.hpp:25
void draw(RenderTarget &target, RenderStates states) const override
Draw the object to a render target.
float height() const
unsigned char u8
Definition: IntTypes.hpp:21
float g
Green component.
Definition: Color.hpp:112
Transform transform
float a
Alpha (opacity) component.
Definition: Color.hpp:114
float width() const
std::array< GLubyte, 6 *5 > m_indices
const Transform & getTransform() const
static void bind(const VertexBuffer *vertexBuffer)
void setSize(float width, float height)
float b
Blue component.
Definition: Color.hpp:113
const Color & color() const
float r
Red component.
Definition: Color.hpp:111
void updateVertexBuffer() const
#define glCheck(expr)
Definition: GLCheck.hpp:18
GLfloat color[4]
Definition: Vertex.hpp:24
void setData(GLsizeiptr size, const GLvoid *data, GLenum usage) const
void drawElements(const VertexBuffer &vertexBuffer, GLenum mode, GLsizei count, GLenum type, const GLvoid *indices, const RenderStates &states=RenderStates::Default)