]> git.localhorst.tv Git - l2e.git/blob - src/graphics/Gauge.cpp
6f44173f80f6e811473ce65cbde41005de0e570b
[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/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 Gauge::Draw(SDL_Surface *dest, const Vector<int> &position, int width, Uint8 fill) const {
21         SDL_Rect srcRect, destRect;
22
23         int filledWidth = fill * (width - startWidth - endWidth) / 255;
24         int emptyWidth = (255 - fill) * (width - startWidth - endWidth) / 255;
25
26         if (!surface) {
27                 destRect.x = position.X();
28                 destRect.y = position.Y();
29                 destRect.w = filledWidth + startWidth;
30                 destRect.h = height;
31                 SDL_FillRect(dest, &destRect, SDL_MapRGB(dest->format, 0x00, 0xFF, 0x00));
32                 destRect.x += destRect.w;
33                 destRect.w = emptyWidth + endWidth;
34                 SDL_FillRect(dest, &destRect, SDL_MapRGB(dest->format, 0xFF, 0x00, 0x00));
35                 return;
36         }
37
38         // start
39         srcRect.w = startWidth;
40         srcRect.h = height;
41         destRect.x = position.X();
42         destRect.y = position.Y();
43         // full part
44         if (fill == 0) {
45                 srcRect.x = emptyOffset.X();
46                 srcRect.y = emptyOffset.Y();
47                 SDL_BlitSurface(surface, &srcRect, dest, &destRect);
48         } else {
49                 srcRect.x = fullOffset.X();
50                 srcRect.y = fullOffset.Y();
51                 SDL_BlitSurface(surface, &srcRect, dest, &destRect);
52         }
53
54         destRect.x = position.X() + startWidth;
55
56         // fill
57         srcRect.x = fullOffset.X() + startWidth;
58         srcRect.y = fullOffset.Y();
59         srcRect.w = repeatWidth;
60         while (filledWidth > repeatWidth) {
61                 SDL_BlitSurface(surface, &srcRect, dest, &destRect);
62                 destRect.x += repeatWidth;
63                 filledWidth -= repeatWidth;
64         }
65         srcRect.x = emptyOffset.X() + startWidth;
66         srcRect.y = emptyOffset.Y();
67         while (emptyWidth > repeatWidth) {
68                 SDL_BlitSurface(surface, &srcRect, dest, &destRect);
69                 destRect.x += repeatWidth;
70                 emptyWidth -= repeatWidth;
71         }
72
73         // end
74         srcRect.w = endWidth;
75         if (fill == 255) {
76                 srcRect.x = fullOffset.X() + startWidth + repeatWidth;
77                 srcRect.y = fullOffset.Y();
78                 SDL_BlitSurface(surface, &srcRect, dest, &destRect);
79         } else {
80                 srcRect.x = emptyOffset.X() + startWidth + repeatWidth;
81                 srcRect.y = emptyOffset.Y();
82                 SDL_BlitSurface(surface, &srcRect, dest, &destRect);
83         }
84
85 }
86
87
88 void Gauge::CreateTypeDescription() {
89         Gauge g;
90
91         TypeDescription &td(TypeDescription::Create(TYPE_ID, "Gauge"));
92         td.SetDescription(
93                         "Gauges display a percentage by filling the left part different than the right.\n"
94                         "The fill level is only mapped with repeat.\n"
95                         "Start is filled if the level in greater than zero, end is filled if the level is at its maximum.");
96         td.SetConstructor(&Construct);
97         td.SetSize(sizeof(Gauge));
98
99         td.AddField("image", FieldDescription(((char *)&g.surface) - ((char *)&g), Interpreter::IMAGE_ID).SetReferenced().SetDescription("the underlying graphic from which the gauge parts are cut"));
100         td.AddField("full", FieldDescription(((char *)&g.fullOffset) - ((char *)&g), Interpreter::VECTOR_ID).SetDescription("top-left corner of the filled gauge"));
101         td.AddField("empty", FieldDescription(((char *)&g.emptyOffset) - ((char *)&g), Interpreter::VECTOR_ID).SetDescription("top-left corner of the empty gauge"));
102         td.AddField("height", FieldDescription(((char *)&g.height) - ((char *)&g), Interpreter::NUMBER_ID).SetDescription("height of the gauges"));
103         td.AddField("start", FieldDescription(((char *)&g.startWidth) - ((char *)&g), Interpreter::NUMBER_ID).SetDescription("width of the start part"));
104         td.AddField("repeat", FieldDescription(((char *)&g.repeatWidth) - ((char *)&g), Interpreter::NUMBER_ID).SetDescription("width of the repeat part"));
105         td.AddField("end", FieldDescription(((char *)&g.endWidth) - ((char *)&g), Interpreter::NUMBER_ID).SetDescription("width of the end part"));
106 }
107
108 void Gauge::Construct(void *data) {
109         new (data) Gauge;
110 }
111
112 }