]> git.localhorst.tv Git - l2e.git/blob - src/graphics/Sprite.cpp
removed stupid file headers that eclipse put in
[l2e.git] / src / graphics / Sprite.cpp
1 #include "Sprite.h"
2
3 #include "../loader/Interpreter.h"
4 #include "../loader/TypeDescription.h"
5
6 using geometry::Vector;
7 using loader::FieldDescription;
8 using loader::Interpreter;
9 using loader::TypeDescription;
10
11 namespace graphics {
12
13 void Sprite::Draw(SDL_Surface *dest, const Vector<int> &position, int col, int row) const {
14         SDL_Rect srcRect, destRect;
15         srcRect.x = offset.X() + col * Width();
16         srcRect.y = offset.Y() + row * Height();
17         srcRect.w = Width();
18         srcRect.h = Height();
19         destRect.x = position.X();
20         destRect.y = position.Y();
21         if (surface) {
22                 SDL_BlitSurface(surface, &srcRect, dest, &destRect);
23         } else {
24                 destRect.w = Width();
25                 destRect.h = Height();
26                 bool red(true);
27                 while (destRect.w > 1 && destRect.h > 1) {
28                         SDL_FillRect(dest, &destRect, SDL_MapRGB(dest->format, red ? 0xFF : 0, 0, 0));
29                         ++destRect.x;
30                         ++destRect.y;
31                         destRect.w -= 2;
32                         destRect.h -= 2;
33                         red = !red;
34                 }
35         }
36 }
37
38
39 void Sprite::CreateTypeDescription() {
40         Sprite s;
41
42         TypeDescription &td(TypeDescription::Create(TYPE_ID, "Sprite"));
43         td.SetDescription(
44                         "An image + a size and offset.\n"
45                         "The resulting section or a section offset by a multiple of its size can be drawn.");
46         td.SetConstructor(&Construct);
47         td.SetSize(sizeof(Sprite));
48
49         td.AddField("image", FieldDescription(((char *)&s.surface) - ((char *)&s), Interpreter::IMAGE_ID).SetReferenced().SetDescription("the image to cut this sprite from"));
50         td.AddField("size", FieldDescription(((char *)&s.size) - ((char *)&s), Interpreter::VECTOR_ID).SetDescription("dimensions of the sprite"));
51         td.AddField("offset", FieldDescription(((char *)&s.offset) - ((char *)&s), Interpreter::VECTOR_ID).SetDescription("offset into the image, top-left corner of the sprite's (0,0) clip"));
52 }
53
54 void Sprite::Construct(void *data) {
55         new (data) Sprite;
56 }
57
58 }