]> git.localhorst.tv Git - l2e.git/blob - src/graphics/Gauge.cpp
042dc2704b4705e7c152926582ce85924f3ce0ee
[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 using geometry::Point;
11
12 namespace graphics {
13
14 // TODO: add start and end "ignore" offsets
15 void Gauge::Draw(SDL_Surface *dest, Point<int> position, int width, Uint8 level) const {
16         int fullWidth(width * level / 255);
17         SDL_Rect srcRect, destRect;
18
19         // start
20         srcRect.h = height;
21         destRect.x = position.X();
22         destRect.y = position.Y();
23         // full part
24         if (fullWidth >= startWidth) { // completely filled
25                 srcRect.x = fullX;
26                 srcRect.y = fullY;
27                 srcRect.w = startWidth;
28                 SDL_BlitSurface(surface, &srcRect, dest, &destRect);
29         } else if (fullWidth > 0) { // partially filled
30                 srcRect.x = fullX;
31                 srcRect.y = fullY;
32                 srcRect.w = fullWidth;
33                 SDL_BlitSurface(surface, &srcRect, dest, &destRect);
34         }
35         // empty part
36         if (fullWidth == 0) { // completely empty
37                 srcRect.x = emptyX;
38                 srcRect.y = emptyY;
39                 srcRect.w = startWidth;
40                 SDL_BlitSurface(surface, &srcRect, dest, &destRect);
41         } else if (fullWidth < startWidth) { // partially empty
42                 srcRect.x = emptyX + fullWidth;
43                 srcRect.y = emptyY;
44                 srcRect.w = startWidth - fullWidth;
45                 destRect.x = position.X() + fullWidth;
46                 SDL_BlitSurface(surface, &srcRect, dest, &destRect);
47         }
48
49         destRect.x = position.X() + startWidth;
50
51         // fill
52         if (fullWidth >= width - endWidth) { // completely filled
53                 srcRect.x = fullX + startWidth;
54                 srcRect.y = fullY;
55                 srcRect.w = repeatWidth;
56                 int fillWidth(width - startWidth - endWidth);
57                 int fillCursor(0);
58                 while (fillCursor < fillWidth) {
59                         SDL_BlitSurface(surface, &srcRect, dest, &destRect);
60                         destRect.x += repeatWidth;
61                         fillCursor += repeatWidth;
62                 }
63         } else if (fullWidth <= startWidth) { // completely empty
64                 srcRect.x = emptyX + startWidth;
65                 srcRect.y = emptyY;
66                 srcRect.w = repeatWidth;
67                 int fillWidth(width - startWidth - endWidth);
68                 int fillCursor(0);
69                 while (fillCursor < fillWidth) {
70                         SDL_BlitSurface(surface, &srcRect, dest, &destRect);
71                         destRect.x += repeatWidth;
72                         fillCursor += repeatWidth;
73                 }
74         } else { // partially filled/empty
75                 srcRect.x = fullX + startWidth;
76                 srcRect.y = fullY;
77                 srcRect.w = repeatWidth;
78                 int fillCursor(0);
79                 while (fillCursor < fullWidth) {
80                         SDL_BlitSurface(surface, &srcRect, dest, &destRect);
81                         destRect.x += repeatWidth;
82                         fillCursor += repeatWidth;
83                 }
84                 srcRect.x = emptyX + startWidth;
85                 srcRect.y = emptyY;
86                 int remaining(width - startWidth - endWidth);
87                 while (fillCursor < remaining) {
88                         SDL_BlitSurface(surface, &srcRect, dest, &destRect);
89                         destRect.x += repeatWidth;
90                         fillCursor += repeatWidth;
91                 }
92         }
93
94         // end
95         // full part
96         if (fullWidth >= width) { // completely filled
97                 srcRect.x = fullX + startWidth + repeatWidth;
98                 srcRect.y = fullY;
99                 srcRect.w = endWidth;
100                 SDL_BlitSurface(surface, &srcRect, dest, &destRect);
101         } else if (fullWidth > (width - endWidth)) { // partially filled
102                 srcRect.x = fullX + startWidth + repeatWidth;
103                 srcRect.y = fullY;
104                 srcRect.w = endWidth - width + fullWidth;
105                 SDL_BlitSurface(surface, &srcRect, dest, &destRect);
106         }
107         // empty part
108         if (fullWidth <= width - endWidth) { // completely empty
109                 srcRect.x = emptyX + startWidth + repeatWidth;
110                 srcRect.y = emptyY;
111                 srcRect.w = startWidth;
112                 SDL_BlitSurface(surface, &srcRect, dest, &destRect);
113         } else if (fullWidth > (width - endWidth) && fullWidth < width) { // partially empty
114                 srcRect.x = emptyX + startWidth + repeatWidth + fullWidth - width + endWidth;
115                 srcRect.y = emptyY;
116                 srcRect.w = width - fullWidth;
117                 destRect.x = position.X() + fullWidth;
118                 SDL_BlitSurface(surface, &srcRect, dest, &destRect);
119         }
120
121 }
122
123 }