]> git.localhorst.tv Git - l2e.git/blob - src/graphics/Frame.cpp
added hard support for path names in source files
[l2e.git] / src / graphics / Frame.cpp
1 /*
2  * Frame.cpp
3  *
4  *  Created on: Aug 7, 2012
5  *      Author: holy
6  */
7
8 #include "Frame.h"
9
10 using geometry::Vector;
11
12 namespace graphics {
13
14 // TODO: maybe create a cache for frames?
15 void Frame::Draw(SDL_Surface *dest, const Vector<int> &position, int width, int height) const {
16         if (!surface) {
17                 SDL_Rect rect;
18                 rect.x = position.X();
19                 rect.y = position.Y();
20                 rect.w = width;
21                 rect.h = height;
22                 SDL_FillRect(dest, &rect, SDL_MapRGB(dest->format, 0xFF, 0x00, 0x00));
23                 return;
24         }
25         // top-left corner
26         SDL_Rect srcRect;
27         srcRect.x = offset.X();
28         srcRect.y = offset.Y();
29         srcRect.w = BorderWidth();
30         srcRect.h = BorderHeight();
31         SDL_Rect destRect;
32         destRect.x = position.X();
33         destRect.y = position.Y();
34         SDL_BlitSurface(surface, &srcRect, dest, &destRect);
35
36         // top border
37         srcRect.x += BorderWidth();
38         srcRect.w = RepeatWidth();
39         destRect.x += BorderWidth();
40         int fullRepeatWidth(width - (2 * BorderWidth()));
41         int repeatCursor(0);
42         while (repeatCursor < fullRepeatWidth) {
43                 SDL_BlitSurface(surface, &srcRect, dest, &destRect);
44                 destRect.x += RepeatWidth();
45                 repeatCursor += RepeatWidth();
46         }
47
48         // top-right corner
49         srcRect.x += RepeatWidth();
50         srcRect.w = BorderWidth();
51         SDL_BlitSurface(surface, &srcRect, dest, &destRect);
52
53         // middle
54         destRect.y += BorderHeight();
55         int fullRepeatHeight(height - (2 * BorderHeight()));
56         int hRepeatCursor(0);
57         while (hRepeatCursor < fullRepeatHeight) {
58
59                 // left border
60                 srcRect.x = offset.X();
61                 srcRect.y = offset.Y() + BorderHeight();
62                 srcRect.w = BorderWidth();
63                 srcRect.h = RepeatHeight();
64                 destRect.x = position.X();
65                 SDL_BlitSurface(surface, &srcRect, dest, &destRect);
66
67                 // fill
68                 repeatCursor = 0;
69                 srcRect.x += BorderWidth();
70                 srcRect.w = RepeatWidth();
71                 destRect.x += BorderWidth();
72                 while (repeatCursor < fullRepeatWidth) {
73                         SDL_BlitSurface(surface, &srcRect, dest, &destRect);
74                         destRect.x += RepeatWidth();
75                         repeatCursor += RepeatWidth();
76                 }
77
78                 // right border
79                 srcRect.x += RepeatWidth();
80                 srcRect.w = BorderWidth();
81                 SDL_BlitSurface(surface, &srcRect, dest, &destRect);
82
83                 destRect.y += RepeatHeight();
84                 hRepeatCursor += RepeatHeight();
85         }
86
87         // bottom-left corner
88         srcRect.x = offset.X();
89         srcRect.y = offset.Y() + BorderHeight() + RepeatHeight();
90         srcRect.w = BorderWidth();
91         srcRect.h = BorderHeight();
92         destRect.x = position.X();
93         SDL_BlitSurface(surface, &srcRect, dest, &destRect);
94
95         // bottom border
96         srcRect.x += BorderWidth();
97         srcRect.w = RepeatWidth();
98         destRect.x += BorderWidth();
99         repeatCursor = 0;
100         while (repeatCursor < fullRepeatWidth) {
101                 SDL_BlitSurface(surface, &srcRect, dest, &destRect);
102                 destRect.x += RepeatWidth();
103                 repeatCursor += RepeatWidth();
104         }
105         if (fullRepeatWidth < fullRepeatWidth) {
106                 srcRect.w = fullRepeatWidth - fullRepeatWidth;
107                 SDL_BlitSurface(surface, &srcRect, dest, &destRect);
108                 destRect.x += fullRepeatWidth - fullRepeatWidth;
109         }
110
111         // bottom-right corner
112         srcRect.x += RepeatWidth();
113         srcRect.w = BorderWidth();
114         SDL_BlitSurface(surface, &srcRect, dest, &destRect);
115 }
116
117 }