]> git.localhorst.tv Git - space.git/blob - src/graphics/Canvas.cpp
93e3f212a9999fd3259c13df676aa4e85cfcd596
[space.git] / src / graphics / Canvas.cpp
1 #include "Canvas.h"
2
3 #include <algorithm>
4 #include <cassert>
5 #include <stdexcept>
6 #include <string>
7
8 using std::runtime_error;
9
10
11 namespace space {
12
13 Canvas::Canvas(SDL_Window *win, int index, Uint32 flags)
14 : canv(SDL_CreateRenderer(win, index, flags)) {
15         if (!canv) {
16                 throw runtime_error(std::string("create canvas: ") + SDL_GetError());
17         }
18 }
19
20 Canvas::~Canvas() {
21         if (canv) SDL_DestroyRenderer(canv);
22 }
23
24 Canvas::Canvas(Canvas &&other)
25 : canv(other.canv) {
26         other.canv = nullptr;
27 }
28
29 Canvas &Canvas::operator =(Canvas &&other) {
30         std::swap(canv, other.canv);
31         return *this;
32 }
33
34
35 void Canvas::Present() {
36         SDL_RenderPresent(canv);
37 }
38
39
40 Vector<int> Canvas::Size() const {
41         assert(canv);
42         Vector<int> size;
43         SDL_GetRendererOutputSize(canv, &size.x, &size.y);
44         return size;
45 }
46
47
48 void Canvas::SetColor(Color c) {
49         SDL_SetRenderDrawColor(canv, c.r, c.g, c.b, c.a);
50 }
51
52
53 void Canvas::Fill() {
54         SDL_RenderClear(canv);
55 }
56
57 void Canvas::Outline() {
58         SDL_RenderDrawRect(canv, nullptr);
59 }
60
61
62 void Canvas::Line(Vector<int> from, Vector<int> to) {
63         SDL_RenderDrawLine(canv, from.x, from.y, to.x, to.y);
64 }
65
66 void Canvas::FillRect(Vector<int> pos, Vector<int> size) {
67         SDL_Rect destRect;
68         destRect.x = pos.x;
69         destRect.y = pos.y;
70         destRect.w = size.x;
71         destRect.h = size.y;
72         SDL_RenderFillRect(canv, &destRect);
73 }
74
75 void Canvas::OutlineRect(Vector<int> pos, Vector<int> size) {
76         SDL_Rect destRect;
77         destRect.x = pos.x;
78         destRect.y = pos.y;
79         destRect.w = size.x;
80         destRect.h = size.y;
81         SDL_RenderDrawRect(canv, &destRect);
82 }
83
84
85 void Canvas::Dot(Vector<int> pos) {
86         SDL_RenderDrawPoint(canv, pos.x, pos.y);
87 }
88
89 void Canvas::Cross(Vector<int> pos, int extent) {
90         Line(
91                 Vector<int>(pos.x - extent, pos.y),
92                 Vector<int>(pos.x + extent, pos.y));
93         Line(
94                 Vector<int>(pos.x, pos.y - extent),
95                 Vector<int>(pos.x, pos.y + extent));
96 }
97
98 void Canvas::Triangle(Vector<int> v1, Vector<int> v2, Vector<int> v3) {
99         SDL_Point points[4] = { v1, v2, v3, v1 };
100         SDL_RenderDrawLines(canv, points, 4);
101 }
102
103 void Canvas::Quad(Vector<int> v1, Vector<int> v2, Vector<int> v3, Vector<int> v4) {
104         SDL_Point points[5] = { v1, v2, v3, v4, v1 };
105         SDL_RenderDrawLines(canv, points, 5);
106 }
107
108
109 namespace {
110
111 template<class Scalar>
112 void GridImpl(
113                 Canvas &canv,
114                 Vector<int> pos,
115                 Vector<int> size,
116                 Vector<Scalar> step) {
117         Vector<int> from(pos);
118         Vector<int> to(pos + size);
119         Vector<int> clip(canv.Size());
120
121         if (from.x > clip.x || from.y > clip.y || to.x < 0 || to.y < 0) {
122                 return;
123         }
124         if (step.x <= 1 || step.y <= 1) {
125                 canv.FillRect(pos, size);
126                 return;
127         }
128
129         if (from.x < -step.x) {
130                 int skip = from.x / -step.x;
131                 from.x += skip * step.x;
132         }
133         if (from.y < -step.y) {
134                 int skip = from.y / -step.y;
135                 from.y += skip * step.y;
136         }
137         if (to.x > clip.x + step.x) {
138                 int skip = (to.x - clip.x) / step.x;
139                 to.x -= skip * step.x;
140         }
141         if (to.y > clip.y + step.y) {
142                 int skip = (to.y - clip.y) / step.y;
143                 to.y -= skip * step.y;
144         }
145
146         int width = to.x - from.x;
147         int height = to.y - from.y;
148
149         for (Vector<Scalar> pos(from); pos.x <= to.x; pos.x += step.x) {
150                 canv.Line(pos, Vector<int>(pos.x, pos.y + height));
151         }
152         for (Vector<Scalar> pos(from); pos.y <= to.y; pos.y += step.y) {
153                 canv.Line(pos, Vector<int>(pos.x + width, pos.y));
154         }
155 }
156
157 template<class Scalar>
158 void Grid2Impl(
159                 Canvas &canv,
160                 Vector<int> pos,
161                 Vector<int> size,
162                 Vector<Scalar> step,
163                 Vector<int> n,
164                 Color c1,
165                 Color c2) {
166         Vector<int> from(pos);
167         Vector<int> to(pos + size);
168         Vector<int> clip(canv.Size());
169
170         if (from.x > clip.x || from.y > clip.y || to.x < 0 || to.y < 0) {
171                 return;
172         }
173         if (step.x <= 1 || step.y <= 1) {
174                 canv.SetColor(c1);
175                 canv.FillRect(pos, size);
176                 canv.SetColor(c2);
177                 GridImpl(canv, pos, size, step);
178                 return;
179         }
180
181         Vector<int> i(0, 0);
182
183         if (from.x < -step.x) {
184                 int skip = from.x / -step.x;
185                 from.x += skip * step.x;
186                 i.x += skip;
187         }
188         if (from.y < -step.y) {
189                 int skip = from.y / -step.y;
190                 from.y += skip * step.y;
191                 i.y += skip;
192         }
193         if (to.x > clip.x + step.x) {
194                 int skip = (to.x - clip.x) / step.x;
195                 to.x -= skip * step.x;
196         }
197         if (to.y > clip.y + step.y) {
198                 int skip = (to.y - clip.y) / step.y;
199                 to.y -= skip * step.y;
200         }
201
202         int width = to.x - from.x;
203         int height = to.y - from.y;
204
205         for (Vector<Scalar> pos(from); pos.x <= to.x; pos.x += step.x) {
206                 canv.SetColor((i.x++ % n.x) ? c1 : c2);
207                 canv.Line(pos, Vector<int>(pos.x, pos.y + height));
208         }
209         for (Vector<Scalar> pos(from); pos.y <= to.y; pos.y += step.y) {
210                 canv.SetColor((i.y++ % n.y) ? c1 : c2);
211                 canv.Line(pos, Vector<int>(pos.x + width, pos.y));
212         }
213 }
214
215 }
216
217 void Canvas::Grid(Vector<int> pos, Vector<int> size, Vector<int> step) {
218         GridImpl(*this, pos, size, step);
219 }
220
221 void Canvas::Grid(Vector<int> pos, Vector<int> size, Vector<float> step) {
222         GridImpl(*this, pos, size, step);
223 }
224
225 void Canvas::Grid2(
226                 Vector<int> pos, Vector<int> size, Vector<int> step,
227                 Vector<int> n, Color c1, Color c2) {
228         Grid2Impl(*this, pos, size, step, n, c1, c2);
229 }
230
231 void Canvas::Grid2(
232                 Vector<int> pos, Vector<int> size, Vector<float> step,
233                 Vector<int> n, Color c1, Color c2) {
234         Grid2Impl(*this, pos, size, step, n, c1, c2);
235 }
236
237 }