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