GameKit  0.0.1a
C++ gamedev tools
Image.hpp
Go to the documentation of this file.
1 /*
2  * =====================================================================================
3  *
4  * Filename: Image.hpp
5  *
6  * Description:
7  *
8  * Created: 20/09/2014 16:21:56
9  *
10  * Author: Quentin Bazin, <gnidmoo@gmail.com>
11  *
12  * =====================================================================================
13  */
14 #ifndef GK_IMAGE_HPP_
15 #define GK_IMAGE_HPP_
16 
17 #include "gk/graphics/Color.hpp"
18 #include "gk/core/Rect.hpp"
19 #include "gk/gl/IDrawable.hpp"
20 #include "gk/gl/Transformable.hpp"
21 #include "gk/gl/VertexBuffer.hpp"
22 
23 namespace gk {
24 
25 class Image : public IDrawable, public Transformable {
26  public:
27  Image() = default;
28  Image(const std::string &textureName);
29  Image(const Texture &texture);
30 
31  void load(const Image &image);
32  void load(const std::string &textureName);
33  void load(const Texture &texture);
34 
35  const FloatRect &clipRect() const { return m_clipRect; }
36  void setClipRect(float x, float y, u16 width, u16 height);
37 
38  const FloatRect &posRect() const { return m_posRect; }
39  void setPosRect(float x, float y, u16 width, u16 height);
40 
41  u16 width() const { return m_width * getScale().x; }
42  u16 height() const { return m_height * getScale().y; }
43 
44  void setColor(const Color &color) { m_color = color; updateVertexBuffer(); }
45  void setAlphaMod(u8 alpha) { m_color.a = alpha / 255.0f; updateVertexBuffer(); }
46  void setFlip(bool isFlipped) { m_isFlipped = isFlipped; }
47 
48  protected:
49  void updateVertexBuffer() const;
50 
51  void draw(RenderTarget &target, RenderStates states) const override;
52 
53  const Texture *m_texture = nullptr;
54 
56 
57  private:
58  u16 m_width = 0;
60 
63 
65 
66  bool m_isFlipped = false;
67 };
68 
69 } // namespace gk
70 
71 #endif // GK_IMAGE_HPP_
void load(const Image &image)
Definition: Image.cpp:33
void draw(RenderTarget &target, RenderStates states) const override
Draw the object to a render target.
Definition: Image.cpp:124
const Vector3f & getScale() const
void setClipRect(float x, float y, u16 width, u16 height)
Definition: Image.cpp:61
u16 height() const
Definition: Image.hpp:42
void setAlphaMod(u8 alpha)
Definition: Image.hpp:45
Utility class for manipulating RGBA colors.
Definition: Color.hpp:25
void updateVertexBuffer() const
Definition: Image.cpp:76
unsigned short u16
Definition: IntTypes.hpp:22
unsigned char u8
Definition: IntTypes.hpp:21
void setFlip(bool isFlipped)
Definition: Image.hpp:46
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 FloatRect & clipRect() const
Definition: Image.hpp:35
const Texture * m_texture
Definition: Image.hpp:53
Abstract base class for objects that can be drawn to a render target.
Definition: IDrawable.hpp:25
void setColor(const Color &color)
Definition: Image.hpp:44
u16 m_height
Definition: Image.hpp:59
VertexBuffer m_vbo
Definition: Image.hpp:55
const FloatRect & posRect() const
Definition: Image.hpp:38
Image()=default
FloatRect m_clipRect
Definition: Image.hpp:61
u16 width() const
Definition: Image.hpp:41
void setPosRect(float x, float y, u16 width, u16 height)
Definition: Image.cpp:70