GameKit  0.0.1a
C++ gamedev tools
Image.cpp
Go to the documentation of this file.
1 /*
2  * =====================================================================================
3  *
4  * Filename: Image.cpp
5  *
6  * Description:
7  *
8  * Created: 20/09/2014 16:22:12
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/Texture.hpp"
19 #include "gk/gl/Vertex.hpp"
20 #include "gk/graphics/Image.hpp"
22 
23 namespace gk {
24 
25 Image::Image(const std::string &textureName) {
26  load(ResourceHandler::getInstance().get<Texture>(textureName));
27 }
28 
29 Image::Image(const Texture &texture) {
30  load(texture);
31 }
32 
33 void Image::load(const Image &image) {
34  m_texture = image.m_texture;
35 
36  m_width = image.m_width;
37  m_height = image.m_height;
38 
39  m_clipRect = image.m_clipRect;
40  m_posRect = image.m_posRect;
41 
42  m_color = image.m_color;
43 
45 }
46 
47 void Image::load(const std::string &textureName) {
48  load(ResourceHandler::getInstance().get<Texture>(textureName));
49 }
50 
51 void Image::load(const Texture &texture) {
52  m_texture = &texture;
53 
54  m_width = m_texture->width();
56 
58  setPosRect(0, 0, m_width, m_height);
59 }
60 
61 void Image::setClipRect(float x, float y, u16 width, u16 height) {
62  m_clipRect = gk::FloatRect(x, y, width, height);
63 
66 
68 }
69 
70 void Image::setPosRect(float x, float y, u16 width, u16 height) {
71  m_posRect = gk::FloatRect(x, y, width, height);
72 
74 }
75 
77  Vertex vertices[4] = {
78  {{m_posRect.x + m_posRect.width, m_posRect.y, 0, -1}},
79  {{m_posRect.x, m_posRect.y, 0, -1}},
80  {{m_posRect.x, m_posRect.y + m_posRect.height, 0, -1}},
82  };
83 
84  FloatRect texRect{
85  m_clipRect.x / float(m_width),
86  m_clipRect.y / float(m_height),
87  m_clipRect.width / float(m_width),
88  m_clipRect.height / float(m_height)
89  };
90 
91  if (!m_isFlipped) {
92  vertices[0].texCoord[0] = texRect.x + texRect.width;
93  vertices[0].texCoord[1] = texRect.y;
94  vertices[1].texCoord[0] = texRect.x;
95  vertices[1].texCoord[1] = texRect.y;
96  vertices[2].texCoord[0] = texRect.x;
97  vertices[2].texCoord[1] = texRect.y + texRect.height;
98  vertices[3].texCoord[0] = texRect.x + texRect.width;
99  vertices[3].texCoord[1] = texRect.y + texRect.height;
100  }
101  else {
102  vertices[0].texCoord[0] = texRect.x;
103  vertices[0].texCoord[1] = texRect.y;
104  vertices[1].texCoord[0] = texRect.x + texRect.width;
105  vertices[1].texCoord[1] = texRect.y;
106  vertices[2].texCoord[0] = texRect.x + texRect.width;
107  vertices[2].texCoord[1] = texRect.y + texRect.height;
108  vertices[3].texCoord[0] = texRect.x;
109  vertices[3].texCoord[1] = texRect.y + texRect.height;
110  }
111 
112  for (u8 i = 0 ; i < 4 ; ++i) {
113  vertices[i].color[0] = m_color.r;
114  vertices[i].color[1] = m_color.g;
115  vertices[i].color[2] = m_color.b;
116  vertices[i].color[3] = m_color.a;
117  }
118 
120  m_vbo.setData(sizeof(vertices), vertices, GL_DYNAMIC_DRAW);
121  VertexBuffer::bind(nullptr);
122 }
123 
124 void Image::draw(RenderTarget &target, RenderStates states) const {
125  states.transform *= getTransform();
126 
127  states.texture = m_texture;
129 
130  glCheck(glDisable(GL_CULL_FACE));
131  glCheck(glDisable(GL_DEPTH_TEST));
132 
133  static const GLubyte indices[] = {
134  0, 1, 3,
135  3, 1, 2
136  };
137 
138  target.drawElements(m_vbo, GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, indices, states);
139 }
140 
141 }
142 
void load(const Image &image)
Definition: Image.cpp:33
static ResourceHandler & getInstance()
void draw(RenderTarget &target, RenderStates states) const override
Draw the object to a render target.
Definition: Image.cpp:124
void setClipRect(float x, float y, u16 width, u16 height)
Definition: Image.cpp:61
const Texture * texture
u16 height() const
Definition: Image.hpp:42
void updateVertexBuffer() const
Definition: Image.cpp:76
unsigned short u16
Definition: IntTypes.hpp:22
unsigned char u8
Definition: IntTypes.hpp:21
float g
Green component.
Definition: Color.hpp:112
T y
Top coordinate of the rectangle.
Definition: Rect.hpp:216
Transform transform
float a
Alpha (opacity) component.
Definition: Color.hpp:114
bool m_isFlipped
Definition: Image.hpp:66
FloatRect m_posRect
Definition: Image.hpp:62
u16 m_width
Definition: Image.hpp:58
Color m_color
Definition: Image.hpp:64
Image living on the graphics card that can be used for drawing.
Definition: Texture.hpp:30
const Transform & getTransform() const
static void bind(const VertexBuffer *vertexBuffer)
float b
Blue component.
Definition: Color.hpp:113
const Texture * m_texture
Definition: Image.hpp:53
float r
Red component.
Definition: Color.hpp:111
GLfloat texCoord[2]
Definition: Vertex.hpp:23
u16 height() const
Return the height of the texture.
Definition: Texture.hpp:124
u16 m_height
Definition: Image.hpp:59
T x
Left coordinate of the rectangle.
Definition: Rect.hpp:215
u16 width() const
Return the width of the texture.
Definition: Texture.hpp:116
#define glCheck(expr)
Definition: GLCheck.hpp:18
GLfloat color[4]
Definition: Vertex.hpp:24
VertexBuffer m_vbo
Definition: Image.hpp:55
T width
Width of the rectangle.
Definition: Rect.hpp:217
Rect< float > FloatRect
Definition: Rect.hpp:223
Image()=default
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)
FloatRect m_clipRect
Definition: Image.hpp:61
T height
Height of the rectangle.
Definition: Rect.hpp:218
u16 width() const
Definition: Image.hpp:41
void setPosRect(float x, float y, u16 width, u16 height)
Definition: Image.cpp:70