]> git.localhorst.tv Git - gworm.git/blob - src/graphics/Texture.cpp
speed up rendering by caching world texture
[gworm.git] / src / graphics / Texture.cpp
1 #include "Texture.h"
2
3
4 namespace gworm {
5
6 Texture::Texture()
7 : tex(nullptr)
8 , format(Color::Format)
9 , size(0, 0) {
10
11 }
12
13 Texture::~Texture() {
14         if (tex) SDL_DestroyTexture(tex);
15 }
16
17 Texture::Texture(Texture &&other)
18 : Texture() {
19         Swap(other);
20 }
21
22 Texture &Texture::operator =(Texture &&other) {
23         Texture temp(std::move(other));
24         Swap(temp);
25         return *this;
26 }
27
28
29 Texture::Texture(
30         SDL_Renderer *c,
31         Uint32 f,
32         int u,
33         Vector<int> s)
34 : tex(SDL_CreateTexture(c, f, u, s.x, s.y))
35 , format(f)
36 , size(s) {
37
38 }
39
40
41 void Texture::Swap(Texture &other) {
42         std::swap(tex, other.tex);
43         std::swap(format, other.format);
44         std::swap(size, other.size);
45 }
46
47
48 void Texture::SetColors(const Color *values) {
49         if (format == Color::Format) {
50                 SDL_UpdateTexture(tex, nullptr, values, Size().x * sizeof(Color));
51         } else {
52                 // TODO: implement for non-Color pixel formats
53         }
54 }
55
56
57 void Texture::Fill(SDL_Renderer *canv) {
58         SDL_RenderCopy(canv, tex, nullptr, nullptr);
59 }
60
61 void Texture::Copy(SDL_Renderer *canv, Vector<int> to) {
62         Copy(canv, Rect<int>(to, Size()));
63 }
64
65 void Texture::Copy(SDL_Renderer *canv, Rect<int> to) {
66         SDL_RenderCopy(canv, tex, nullptr, &to);
67 }
68
69 }