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