]> git.localhorst.tv Git - l2e.git/blob - src/graphics/Sprite.cpp
added entity orientation awareness
[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.SetConstructor(&Construct);
52         td.SetSize(sizeof(Sprite));
53
54         td.AddField("image", FieldDescription(((char *)&s.surface) - ((char *)&s), imageId, true));
55         td.AddField("size", FieldDescription(((char *)&s.size) - ((char *)&s), vectorId, false));
56         td.AddField("offset", FieldDescription(((char *)&s.offset) - ((char *)&s), vectorId, false));
57 }
58
59 void Sprite::Construct(void *data) {
60         new (data) Sprite;
61 }
62
63 }