]> git.localhorst.tv Git - space.git/blob - src/graphics/primitive.cpp
adjust cam speed to zoom level
[space.git] / src / graphics / primitive.cpp
1 #include "primitive.h"
2
3 #include <algorithm>
4
5 using namespace std;
6
7
8 namespace space {
9
10 void Fill(SDL_Surface *dst, Color c) {
11         Fill(dst, c.MapRGBA(dst));
12 }
13
14 void Fill(SDL_Surface *dst, Uint32 color) {
15         SDL_FillRect(dst, nullptr, color);
16 }
17
18
19 void HLine(SDL_Surface *dst, Vector<int> pos, int len, Color c) {
20         HLine(dst, pos, len, c.MapRGBA(dst));
21 }
22
23 void HLine(SDL_Surface *dst, Vector<int> pos, int len, Uint32 c) {
24         FillRect(dst, pos, Vector<int>(len, 1), c);
25 }
26
27 void VLine(SDL_Surface *dst, Vector<int> pos, int len, Color c) {
28         VLine(dst, pos, len, c.MapRGBA(dst));
29 }
30
31 void VLine(SDL_Surface *dst, Vector<int> pos, int len, Uint32 c) {
32         FillRect(dst, pos, Vector<int>(1, len), c);
33 }
34
35
36 void FillRect(SDL_Surface *dst, Vector<int> pos, Vector<int> size, Color c) {
37         FillRect(dst, pos, size, c.MapRGBA(dst));
38 }
39
40 void FillRect(SDL_Surface *dst, Vector<int> pos, Vector<int> size, Uint32 c) {
41         SDL_Rect destRect;
42         destRect.x = pos.x;
43         destRect.y = pos.y;
44         destRect.w = size.x;
45         destRect.h = size.y;
46         SDL_FillRect(dst, &destRect, c);
47 }
48
49 void OutlineRect(SDL_Surface *dst, Vector<int> pos, Vector<int> size, Color c) {
50         OutlineRect(dst, pos, size, c.MapRGBA(dst));
51 }
52
53 void OutlineRect(SDL_Surface *dst, Vector<int> pos, Vector<int> size, Uint32 c) {
54         SDL_Rect destRect;
55
56         destRect.x = pos.x;
57         destRect.y = pos.y;
58         destRect.w = size.x;
59         destRect.h = 1;
60         SDL_FillRect(dst, &destRect, c);
61
62         destRect.x = pos.x;
63         destRect.y = pos.y + size.y - 1;
64         destRect.w = size.x;
65         destRect.h = 1;
66         SDL_FillRect(dst, &destRect, c);
67
68         destRect.x = pos.x;
69         destRect.y = pos.y;
70         destRect.w = 1;
71         destRect.h = size.y;
72         SDL_FillRect(dst, &destRect, c);
73
74         destRect.x = pos.x + size.x - 1;
75         destRect.y = pos.y;
76         destRect.w = 1;
77         destRect.h = size.y;
78         SDL_FillRect(dst, &destRect, c);
79 }
80
81
82 void Cross(
83                 SDL_Surface *dst,
84                 Vector<int> pos,
85                 int extent,
86                 Color color) {
87         Uint32 c = color.MapRGBA(dst);
88         int len = 2 * extent + 1;
89
90         HLine(dst, Vector<int>(pos.x - extent, pos.y), len, c);
91         VLine(dst, Vector<int>(pos.x, pos.y - extent), len, c);
92 }
93
94
95 void Grid(
96                 SDL_Surface *dst,
97                 Vector<int> first,
98                 Vector<int> second,
99                 Vector<int> size,
100                 Color color) {
101         Uint32 c = color.MapRGBA(dst);
102
103         Vector<int> from = min(first, second);
104         Vector<int> to = max(first, second);
105
106         if (size.x <= 1 || size.y <= 1) {
107                 FillRect(dst, from, to - from, c);
108                 return;
109         }
110
111         if (from.x > dst->w || from.y > dst->h || to.x < 0 || to.y < 0) {
112                 return;
113         }
114
115         while (from.x < -size.x) from.x += size.x;
116         while (from.y < -size.y) from.y += size.y;
117         while (to.x > dst->w + size.x) to.x -= size.x;
118         while (to.y > dst->h + size.y) to.y -= size.y;
119
120         int width = to.x - from.x;
121         int height = to.y - from.y;
122
123         for (Vector<int> pos(from); pos.x <= to.x; pos.x += size.x) {
124                 VLine(dst, pos, height, c);
125         }
126         for (Vector<int> pos(from); pos.y <= to.y; pos.y += size.y) {
127                 HLine(dst, pos, width, c);
128         }
129 }
130
131 void Grid(
132                 SDL_Surface *dst,
133                 Vector<int> first,
134                 Vector<int> second,
135                 Vector<float> size,
136                 Color color) {
137         Uint32 c = color.MapRGBA(dst);
138
139         Vector<int> from = min(first, second);
140         Vector<int> to = max(first, second);
141
142         if (size.x <= 1 || size.y <= 1) {
143                 FillRect(dst, from, to - from, c);
144                 return;
145         }
146
147         if (from.x > dst->w || from.y > dst->h || to.x < 0 || to.y < 0) {
148                 return;
149         }
150
151         while (from.x < -size.x) from.x += size.x;
152         while (from.y < -size.y) from.y += size.y;
153         while (to.x > dst->w + size.x) to.x -= size.x;
154         while (to.y > dst->h + size.y) to.y -= size.y;
155
156         float width = to.x - from.x;
157         float height = to.y - from.y;
158
159         for (Vector<float> pos(from); pos.x <= to.x; pos.x += size.x) {
160                 VLine(dst, pos, height, c);
161         }
162         for (Vector<float> pos(from); pos.y <= to.y; pos.y += size.y) {
163                 HLine(dst, pos, width, c);
164         }
165 }
166
167 void Grid2(
168                 SDL_Surface *dst,
169                 Vector<int> first,
170                 Vector<int> second,
171                 Vector<int> size,
172                 Vector<int> n,
173                 Color color1,
174                 Color color2) {
175         Uint32 c1 = color1.MapRGBA(dst);
176         Uint32 c2 = color2.MapRGBA(dst);
177
178         Vector<int> from = min(first, second);
179         Vector<int> to = max(first, second);
180
181         if (size.x <= 1 || size.y <= 1) {
182                 FillRect(dst, from, to - from, c1);
183                 Grid(dst, from, to, size * n, color2);
184                 return;
185         }
186
187         if (from.x > dst->w || from.y > dst->h || to.x < 0 || to.y < 0) {
188                 return;
189         }
190
191         Vector<int> i(0, 0);
192         while (from.x < -size.x) { from.x += size.x; ++i.x; }
193         while (from.y < -size.y) { from.y += size.y; ++i.y; }
194         while (to.x > dst->w + size.x) to.x -= size.x;
195         while (to.y > dst->h + size.y) to.y -= size.y;
196
197         int width = to.x - from.x;
198         int height = to.y - from.y;
199
200         for (Vector<int> pos(from); pos.x <= to.x; pos.x += size.x) {
201                 VLine(dst, pos, height, (i.x++ % n.x) ? c1 : c2);
202         }
203         for (Vector<int> pos(from); pos.y <= to.y; pos.y += size.y) {
204                 HLine(dst, pos, width, (i.y++ % n.y) ? c1 : c2);
205         }
206 }
207
208 void Grid2(
209                 SDL_Surface *dst,
210                 Vector<int> first,
211                 Vector<int> second,
212                 Vector<float> size,
213                 Vector<int> n,
214                 Color color1,
215                 Color color2) {
216         Uint32 c1 = color1.MapRGBA(dst);
217         Uint32 c2 = color2.MapRGBA(dst);
218
219         Vector<int> from = min(first, second);
220         Vector<int> to = max(first, second);
221
222         if (size.x <= 1 || size.y <= 1) {
223                 FillRect(dst, from, to - from, c1);
224                 Grid(dst, from, to, size * Vector<float>(n), color2);
225                 return;
226         }
227
228         if (from.x > dst->w || from.y > dst->h || to.x < 0 || to.y < 0) {
229                 return;
230         }
231
232         Vector<int> i(0, 0);
233         while (from.x < -size.x) { from.x += size.x; ++i.x; }
234         while (from.y < -size.y) { from.y += size.y; ++i.y; }
235         while (to.x > dst->w + size.x) to.x -= size.x;
236         while (to.y > dst->h + size.y) to.y -= size.y;
237
238         float width = to.x - from.x;
239         float height = to.y - from.y;
240
241         for (Vector<float> pos(from); pos.x <= to.x; pos.x += size.x) {
242                 VLine(dst, pos, height, (i.x++ % n.x) ? c1 : c2);
243         }
244         for (Vector<float> pos(from); pos.y <= to.y; pos.y += size.y) {
245                 HLine(dst, pos, width, (i.y++ % n.y) ? c1 : c2);
246         }
247 }
248
249 }