]> git.localhorst.tv Git - l2e.git/blob - src/graphics/Gauge.cpp
Merge branch 'loader'
[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::Vector;
11
12 namespace graphics {
13
14 void Gauge::Draw(SDL_Surface *dest, const Vector<int> &position, int width, Uint8 fill) const {
15         SDL_Rect srcRect, destRect;
16
17         int filledWidth = fill * (width - startWidth - endWidth) / 255;
18         int emptyWidth = (255 - fill) * (width - startWidth - endWidth) / 255;
19
20         if (!surface) {
21                 destRect.x = position.X();
22                 destRect.y = position.Y();
23                 destRect.w = filledWidth + startWidth;
24                 destRect.h = height;
25                 SDL_FillRect(dest, &destRect, SDL_MapRGB(dest->format, 0x00, 0xFF, 0x00));
26                 destRect.x += destRect.w;
27                 destRect.w = emptyWidth + endWidth;
28                 SDL_FillRect(dest, &destRect, SDL_MapRGB(dest->format, 0xFF, 0x00, 0x00));
29                 return;
30         }
31
32         // start
33         srcRect.w = startWidth;
34         srcRect.h = height;
35         destRect.x = position.X();
36         destRect.y = position.Y();
37         // full part
38         if (fill == 0) {
39                 srcRect.x = emptyOffset.X();
40                 srcRect.y = emptyOffset.Y();
41                 SDL_BlitSurface(surface, &srcRect, dest, &destRect);
42         } else {
43                 srcRect.x = fullOffset.X();
44                 srcRect.y = fullOffset.Y();
45                 SDL_BlitSurface(surface, &srcRect, dest, &destRect);
46         }
47
48         destRect.x = position.X() + startWidth;
49
50         // fill
51         srcRect.x = fullOffset.X() + startWidth;
52         srcRect.y = fullOffset.Y();
53         srcRect.w = repeatWidth;
54         while (filledWidth > repeatWidth) {
55                 SDL_BlitSurface(surface, &srcRect, dest, &destRect);
56                 destRect.x += repeatWidth;
57                 filledWidth -= repeatWidth;
58         }
59         srcRect.x = emptyOffset.X() + startWidth;
60         srcRect.y = emptyOffset.Y();
61         while (emptyWidth > repeatWidth) {
62                 SDL_BlitSurface(surface, &srcRect, dest, &destRect);
63                 destRect.x += repeatWidth;
64                 emptyWidth -= repeatWidth;
65         }
66
67         // end
68         srcRect.w = endWidth;
69         if (fill == 255) {
70                 srcRect.x = fullOffset.X() + startWidth + repeatWidth;
71                 srcRect.y = fullOffset.Y();
72                 SDL_BlitSurface(surface, &srcRect, dest, &destRect);
73         } else {
74                 srcRect.x = emptyOffset.X() + startWidth + repeatWidth;
75                 srcRect.y = emptyOffset.Y();
76                 SDL_BlitSurface(surface, &srcRect, dest, &destRect);
77         }
78
79 }
80
81 }