]> git.localhorst.tv Git - l2e.git/blob - src/graphics/Texture.cpp
f53c82fd7030f6219332e5368ae1d435b4a4481e
[l2e.git] / src / graphics / Texture.cpp
1 #include "Texture.h"
2
3 #include "../loader/Interpreter.h"
4 #include "../loader/TypeDescription.h"
5 #include "../sdl/utility.h"
6
7 using geometry::Vector;
8 using loader::FieldDescription;
9 using loader::Interpreter;
10 using loader::TypeDescription;
11
12 namespace graphics {
13
14 Texture::Texture(
15                 SDL_Surface *surface,
16                 const Vector<int> &size,
17                 const Vector<int> &offset)
18 : surface(surface)
19 , size(size)
20 , offset(offset) {
21
22 }
23
24 Texture::~Texture() {
25
26 }
27
28
29 void Texture::Render(SDL_Surface *dest, const Vector<int> &from, const Vector<int> &to) const {
30         SDL_Rect destRect;
31         destRect.x = from.X();
32         destRect.y = from.Y();
33         if (!surface || size == Vector<int>()) {
34                 destRect.w = to.X() - from.X();
35                 destRect.h = to.Y() - from.Y();
36                 SDL_FillRect(dest, &destRect, SDL_MapRGB(dest->format, 0xFF, 0x00, 0x00));
37                 return;
38         }
39
40         SDL_Rect srcRect;
41         srcRect.x = offset.X();
42         srcRect.y = offset.Y();
43
44         for (destRect.y = from.Y(); destRect.y < to.Y(); destRect.y += size.Y()) {
45                 srcRect.h = size.Y();
46                 destRect.h = size.Y();
47                 if (destRect.y + destRect.h > to.Y()) {
48                         srcRect.h = to.Y() - destRect.y;
49                         destRect.h = to.Y() - destRect.y;
50                 }
51                 for (destRect.x = from.X(); destRect.x < to.X(); destRect.x += size.X()) {
52                         srcRect.w = size.X();
53                         destRect.w = size.X();
54                         if (destRect.x + destRect.w > to.X()) {
55                                 srcRect.w = to.X() - destRect.x;
56                                 destRect.w = to.X() - destRect.x;
57                         }
58                         SDL_BlitSurface(surface, &srcRect, dest, &destRect);
59                 }
60         }
61 }
62
63
64 void Texture::CreateTypeDescription() {
65         Texture t;
66
67         TypeDescription &td(TypeDescription::Create(TYPE_ID, "Texture"));
68         td.SetConstructor(&Construct);
69         td.SetSize(sizeof(Texture));
70
71         td.AddField("image", FieldDescription(((char *)&t.surface) - ((char *)&t), Interpreter::IMAGE_ID).SetReferenced().SetDescription("image containing the texture"));
72         td.AddField("size", FieldDescription(((char *)&t.size) - ((char *)&t), Interpreter::VECTOR_ID).SetDescription("offset into the image in pixels"));
73         td.AddField("offset", FieldDescription(((char *)&t.offset) - ((char *)&t), Interpreter::VECTOR_ID).SetDescription("size of the texture in pixels"));
74 }
75
76 void Texture::Construct(void *data) {
77         new (data) Texture;
78 }
79
80 }