Sprite.cpp
1// Copyright 2019 Arthur Sonzogni. All rights reserved.
2// Use of this source code is governed by the MIT license that can be found in
3// the LICENSE file.
4
5#include <smk/Framebuffer.hpp>
6#include <smk/OpenGL.hpp>
7#include <smk/Shader.hpp>
8#include <smk/Sprite.hpp>
9#include <smk/VertexArray.hpp>
10#include <vector>
11
12namespace smk {
13
14/// @brief A Sprite for drawing a texture.
15/// @param texture The Texture to be displayed.
16Sprite::Sprite(const Texture& texture) : Sprite() {
17 SetTexture(texture);
18}
19
20/// @brief A Sprite for drawing a part of a Texture.
21/// @param texture The Texture to be displayed.
22/// @param rectangle A rectangle in the texture to be used.
23Sprite::Sprite(const Texture& texture, const Rectangle rectangle) {
25 SetTextureRectangle(rectangle);
26}
27
28/// @brief A sprite for drawing the content of a Framebuffer.
29/// @param framebuffer The framebuffer to be used.
30Sprite::Sprite(Framebuffer& framebuffer) {
31 Transformable::SetTexture(framebuffer.color_texture());
32 float l = 0.F;
33 float r = 1.F;
34 float t = 0.F;
35 float b = 1.F;
36 auto www = float(framebuffer.color_texture().width());
37 auto hhh = float(framebuffer.color_texture().height());
38 SetVertexArray(VertexArray(std::vector<Vertex>({
39 {{0.F, 0.F}, {l, b}},
40 {{0.F, hhh}, {l, t}},
41 {{www, hhh}, {r, t}},
42 {{0.F, 0.F}, {l, b}},
43 {{www, hhh}, {r, t}},
44 {{www, 0.F}, {r, b}},
45 })));
46}
47
48/// @brief Update the sprite's texture.
49/// @param texture The Texture to be displayed.
50void Sprite::SetTexture(const Texture& texture) {
53 0.F,
54 0.F,
55 float(texture.width()),
56 float(texture.height()),
57 });
58}
59
60/// @brief Update the sprite texture area.
61/// @param texture The Texture to be displayed.
62/// @param rectangle A rectangle in the texture to be used.
63void Sprite::SetTextureRectangle(const Rectangle& rectangle) {
64 float l = (rectangle.left + 0.5F) / texture().width(); // NOLINT
65 float r = (rectangle.right - 0.5F) / texture().width(); // NOLINT
66 float t = (rectangle.top + 0.5F) / texture().height(); // NOLINT
67 float b = (rectangle.bottom - 0.5F) / texture().height(); // NOLINT
68 float www = rectangle.width();
69 float hhh = rectangle.height();
70 SetVertexArray(VertexArray(std::vector<Vertex>({
71 {{0.F, 0.F}, {l, t}},
72 {{0.F, hhh}, {l, b}},
73 {{www, hhh}, {r, b}},
74 {{0.F, 0.F}, {l, t}},
75 {{www, hhh}, {r, b}},
76 {{www, 0.F}, {r, t}},
77 })));
78}
79
80} // namespace smk
An off-screen drawable area. You can also draw it later in a smk::Sprite.
Definition: Framebuffer.hpp:18
void SetTexture(const Texture &texture)
Update the sprite's texture.
Definition: Sprite.cpp:50
void SetTextureRectangle(const Rectangle &rectangle)
Update the sprite texture area.
Definition: Sprite.cpp:63
void SetTexture(Texture texture)
Set the object's texture.
void SetVertexArray(VertexArray vertex_array)
Set the object's shape.
An array of smk::Vertex moved to the GPU memory. This represent a set of triangles to be drawn by the...
Definition: VertexArray.hpp:20
int height() const
Access the height of the texture.
Definition: Texture.cpp:201
int width() const
Access the width of the texture.
Definition: Texture.cpp:195