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