]> git.localhorst.tv Git - orbi.git/blob - src/graphics/Texture.cpp
initial collision tests
[orbi.git] / src / graphics / Texture.cpp
1 #include "Texture.h"
2
3 #include <SDL_image.h>
4
5
6 namespace orbi {
7
8 Texture::Texture()
9 : tex(nullptr)
10 , format(Color::Format)
11 , size(0, 0) {
12
13 }
14
15 Texture::~Texture() {
16         if (tex) SDL_DestroyTexture(tex);
17 }
18
19 Texture::Texture(Texture &&other)
20 : Texture() {
21         Swap(other);
22 }
23
24 Texture &Texture::operator =(Texture &&other) {
25         Texture temp(std::move(other));
26         Swap(temp);
27         return *this;
28 }
29
30
31 Texture::Texture(
32         SDL_Renderer *c,
33         Uint32 f,
34         int u,
35         Vector<int> s)
36 : tex(SDL_CreateTexture(c, f, u, s.x, s.y))
37 , format(f)
38 , size(s) {
39
40 }
41
42 Texture::Texture(
43         SDL_Renderer *c,
44         const char *file)
45 : tex(IMG_LoadTexture(c, file))
46 , format()
47 , size() {
48         SDL_QueryTexture(tex, &format, nullptr, &size.x, &size.y);
49 }
50
51
52 void Texture::Swap(Texture &other) {
53         std::swap(tex, other.tex);
54         std::swap(format, other.format);
55         std::swap(size, other.size);
56 }
57
58
59 void Texture::SetColors(const Color *values) {
60         if (format == Color::Format) {
61                 SDL_UpdateTexture(tex, nullptr, values, Size().x * sizeof(Color));
62         } else {
63                 // TODO: implement for non-Color pixel formats
64         }
65 }
66
67
68 void Texture::Fill(SDL_Renderer *canv) {
69         SDL_RenderCopy(canv, tex, nullptr, nullptr);
70 }
71
72 void Texture::Fill(SDL_Renderer *canv, Rect<int> clip) {
73         SDL_RenderCopy(canv, tex, &clip, nullptr);
74 }
75
76 void Texture::Copy(SDL_Renderer *canv, Vector<int> to) {
77         Copy(canv, Rect<int>(to, Size()));
78 }
79
80 void Texture::Copy(SDL_Renderer *canv, Rect<int> to) {
81         SDL_RenderCopy(canv, tex, nullptr, &to);
82 }
83
84 void Texture::Copy(SDL_Renderer *canv, Rect<int> clip, Vector<int> to) {
85         Copy(canv, clip, Rect<int>(to, clip.Size()));
86 }
87
88 void Texture::Copy(SDL_Renderer *canv, Rect<int> clip, Rect<int> to) {
89         SDL_RenderCopy(canv, tex, &clip, &to);
90 }
91
92 }