]> git.localhorst.tv Git - l2e.git/blob - src/graphics/Texture.cpp
new language, new compiler
[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 math::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::Draw(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         srcRect.h = size.Y();
44         srcRect.w = size.X();
45
46         const Vector<int> total = to - from;
47         const Vector<int> over = total % size;
48         const Vector<int> target = from + (total - over);
49
50         for (destRect.y = from.Y(); destRect.y < target.Y(); destRect.y += size.Y()) {
51                 for (destRect.x = from.X(); destRect.x < target.X(); destRect.x += size.X()) {
52                         SDL_BlitSurface(surface, &srcRect, dest, &destRect);
53                 }
54         }
55         if (over.X() > 0) {
56                 destRect.x = target.X();
57                 srcRect.w = over.X();
58                 srcRect.h = size.Y();
59                 for (destRect.y = from.Y(); destRect.y < target.Y(); destRect.y += size.Y()) {
60                         SDL_BlitSurface(surface, &srcRect, dest, &destRect);
61                 }
62         }
63         if (over.Y() > 0) {
64                 destRect.y = target.Y();
65                 srcRect.w = size.X();
66                 srcRect.h = over.Y();
67                 for (destRect.x = from.X(); destRect.x < target.X(); destRect.x += size.X()) {
68                         SDL_BlitSurface(surface, &srcRect, dest, &destRect);
69                 }
70                 if (over.X() > 0) {
71                         srcRect.w = over.X();
72                         srcRect.h = over.Y();
73                         destRect.x = target.X();
74                         destRect.y = target.Y();
75                         SDL_BlitSurface(surface, &srcRect, dest, &destRect);
76                 }
77         }
78 }
79
80
81 void Texture::CreateTypeDescription() {
82         Texture t;
83
84         TypeDescription &td(TypeDescription::Create(TYPE_ID, "Texture"));
85         td.SetConstructor(&Construct);
86         td.SetSize(sizeof(Texture));
87
88         td.AddField("image", FieldDescription(((char *)&t.surface) - ((char *)&t), Interpreter::IMAGE_ID).SetReferenced().SetDescription("image containing the texture"));
89         td.AddField("size", FieldDescription(((char *)&t.size) - ((char *)&t), Interpreter::VECTOR_ID).SetDescription("offset into the image in pixels"));
90         td.AddField("offset", FieldDescription(((char *)&t.offset) - ((char *)&t), Interpreter::VECTOR_ID).SetDescription("size of the texture in pixels"));
91 }
92
93 void Texture::Construct(void *data) {
94         new (data) Texture;
95 }
96
97 }