]> git.localhorst.tv Git - space.git/blob - src/graphics/primitive.cpp
simple zoom
[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 }