]> git.localhorst.tv Git - l2e.git/blob - src/graphics/Gauge.cpp
added map trigger prototype
[l2e.git] / src / graphics / Gauge.cpp
1 /*
2  * Gauge.cpp
3  *
4  *  Created on: Aug 7, 2012
5  *      Author: holy
6  */
7
8 #include "Gauge.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 Gauge::Draw(SDL_Surface *dest, const Vector<int> &position, int width, Uint8 fill) const {
19         SDL_Rect srcRect, destRect;
20
21         int filledWidth = fill * (width - startWidth - endWidth) / 255;
22         int emptyWidth = (255 - fill) * (width - startWidth - endWidth) / 255;
23
24         if (!surface) {
25                 destRect.x = position.X();
26                 destRect.y = position.Y();
27                 destRect.w = filledWidth + startWidth;
28                 destRect.h = height;
29                 SDL_FillRect(dest, &destRect, SDL_MapRGB(dest->format, 0x00, 0xFF, 0x00));
30                 destRect.x += destRect.w;
31                 destRect.w = emptyWidth + endWidth;
32                 SDL_FillRect(dest, &destRect, SDL_MapRGB(dest->format, 0xFF, 0x00, 0x00));
33                 return;
34         }
35
36         // start
37         srcRect.w = startWidth;
38         srcRect.h = height;
39         destRect.x = position.X();
40         destRect.y = position.Y();
41         // full part
42         if (fill == 0) {
43                 srcRect.x = emptyOffset.X();
44                 srcRect.y = emptyOffset.Y();
45                 SDL_BlitSurface(surface, &srcRect, dest, &destRect);
46         } else {
47                 srcRect.x = fullOffset.X();
48                 srcRect.y = fullOffset.Y();
49                 SDL_BlitSurface(surface, &srcRect, dest, &destRect);
50         }
51
52         destRect.x = position.X() + startWidth;
53
54         // fill
55         srcRect.x = fullOffset.X() + startWidth;
56         srcRect.y = fullOffset.Y();
57         srcRect.w = repeatWidth;
58         while (filledWidth > repeatWidth) {
59                 SDL_BlitSurface(surface, &srcRect, dest, &destRect);
60                 destRect.x += repeatWidth;
61                 filledWidth -= repeatWidth;
62         }
63         srcRect.x = emptyOffset.X() + startWidth;
64         srcRect.y = emptyOffset.Y();
65         while (emptyWidth > repeatWidth) {
66                 SDL_BlitSurface(surface, &srcRect, dest, &destRect);
67                 destRect.x += repeatWidth;
68                 emptyWidth -= repeatWidth;
69         }
70
71         // end
72         srcRect.w = endWidth;
73         if (fill == 255) {
74                 srcRect.x = fullOffset.X() + startWidth + repeatWidth;
75                 srcRect.y = fullOffset.Y();
76                 SDL_BlitSurface(surface, &srcRect, dest, &destRect);
77         } else {
78                 srcRect.x = emptyOffset.X() + startWidth + repeatWidth;
79                 srcRect.y = emptyOffset.Y();
80                 SDL_BlitSurface(surface, &srcRect, dest, &destRect);
81         }
82
83 }
84
85
86 void Gauge::CreateTypeDescription() {
87         Gauge g;
88
89         int imageId(TypeDescription::GetTypeId("Image"));
90         int numberId(TypeDescription::GetTypeId("Number"));
91         int vectorId(TypeDescription::GetTypeId("Vector"));
92
93         TypeDescription &td(TypeDescription::CreateOrGet("Gauge"));
94         td.SetConstructor(&Construct);
95         td.SetSize(sizeof(Gauge));
96
97         td.AddField("image", FieldDescription(((char *)&g.surface) - ((char *)&g), imageId, true));
98         td.AddField("full", FieldDescription(((char *)&g.fullOffset) - ((char *)&g), vectorId, false));
99         td.AddField("empty", FieldDescription(((char *)&g.emptyOffset) - ((char *)&g), vectorId, false));
100         td.AddField("height", FieldDescription(((char *)&g.height) - ((char *)&g), numberId, false));
101         td.AddField("start", FieldDescription(((char *)&g.startWidth) - ((char *)&g), numberId, false));
102         td.AddField("repeat", FieldDescription(((char *)&g.repeatWidth) - ((char *)&g), numberId, false));
103         td.AddField("end", FieldDescription(((char *)&g.endWidth) - ((char *)&g), numberId, false));
104 }
105
106 void Gauge::Construct(void *data) {
107         new (data) Gauge;
108 }
109
110 }