]> git.localhorst.tv Git - orbi.git/blob - src/graphics/Canvas.cpp
initial collision tests
[orbi.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 orbi {
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 Texture Canvas::CreateStaticTexture(Vector<int> size) {
49         return Texture(canv, Color::Format, SDL_TEXTUREACCESS_STATIC, size);
50 }
51
52 Texture Canvas::LoadTexture(const char *file) {
53         return Texture(canv, file);
54 }
55
56
57 void Canvas::Copy(Texture &tex, Vector<int> to) {
58         tex.Copy(canv, to);
59 }
60
61 void Canvas::Copy(Texture &tex, Rect<int> clip, Vector<int> to) {
62         tex.Copy(canv, clip, to);
63 }
64
65
66 void Canvas::SetColor(Color c) {
67         SDL_SetRenderDrawColor(canv, c.r, c.g, c.b, c.a);
68 }
69
70
71 void Canvas::Fill() {
72         SDL_RenderClear(canv);
73 }
74
75 void Canvas::Outline() {
76         SDL_RenderDrawRect(canv, nullptr);
77 }
78
79
80 void Canvas::Line(Vector<int> from, Vector<int> to) {
81         SDL_RenderDrawLine(canv, from.x, from.y, to.x, to.y);
82 }
83
84 void Canvas::FillRect(Vector<int> pos, Vector<int> size) {
85         SDL_Rect destRect;
86         destRect.x = pos.x;
87         destRect.y = pos.y;
88         destRect.w = size.x;
89         destRect.h = size.y;
90         SDL_RenderFillRect(canv, &destRect);
91 }
92
93 void Canvas::OutlineRect(Vector<int> pos, Vector<int> size) {
94         SDL_Rect destRect;
95         destRect.x = pos.x;
96         destRect.y = pos.y;
97         destRect.w = size.x;
98         destRect.h = size.y;
99         SDL_RenderDrawRect(canv, &destRect);
100 }
101
102
103 void Canvas::Dot(Vector<int> pos) {
104         SDL_RenderDrawPoint(canv, pos.x, pos.y);
105 }
106
107 void Canvas::Cross(Vector<int> pos, int extent) {
108         Line(
109                 Vector<int>(pos.x - extent, pos.y),
110                 Vector<int>(pos.x + extent, pos.y));
111         Line(
112                 Vector<int>(pos.x, pos.y - extent),
113                 Vector<int>(pos.x, pos.y + extent));
114 }
115
116 void Canvas::Arrow(Vector<int> from, Vector<int> to) {
117         Line(from, to);
118         Vector<float> delta(to - from);
119         delta = delta / Length(delta);
120
121         Line(to, to + Vector<int>(Rotate90(delta) * 5.0f - (delta * 5.0f)));
122         Line(to, to + Vector<int>(Rotate270(delta) * 5.0f - (delta * 5.0f)));
123 }
124
125 void Canvas::Triangle(Vector<int> v1, Vector<int> v2, Vector<int> v3) {
126         SDL_Point points[4] = { v1, v2, v3, v1 };
127         SDL_RenderDrawLines(canv, points, 4);
128 }
129
130 void Canvas::Quad(Vector<int> v1, Vector<int> v2, Vector<int> v3, Vector<int> v4) {
131         SDL_Point points[5] = { v1, v2, v3, v4, v1 };
132         SDL_RenderDrawLines(canv, points, 5);
133 }
134
135
136 namespace {
137
138 template<class Scalar>
139 void GridImpl(
140                 Canvas &canv,
141                 Vector<int> pos,
142                 Vector<int> size,
143                 Vector<Scalar> step) {
144         Vector<int> from(pos);
145         Vector<int> to(pos + size);
146         Vector<int> clip(canv.Size());
147
148         if (from.x > clip.x || from.y > clip.y || to.x < 0 || to.y < 0) {
149                 return;
150         }
151         if (step.x <= 1 || step.y <= 1) {
152                 canv.FillRect(pos, size);
153                 return;
154         }
155
156         if (from.x < -step.x) {
157                 int skip = from.x / -step.x;
158                 from.x += skip * step.x;
159         }
160         if (from.y < -step.y) {
161                 int skip = from.y / -step.y;
162                 from.y += skip * step.y;
163         }
164         if (to.x > clip.x + step.x) {
165                 int skip = (to.x - clip.x) / step.x;
166                 to.x -= skip * step.x;
167         }
168         if (to.y > clip.y + step.y) {
169                 int skip = (to.y - clip.y) / step.y;
170                 to.y -= skip * step.y;
171         }
172
173         int width = to.x - from.x;
174         int height = to.y - from.y;
175
176         for (Vector<Scalar> pos(from); pos.x <= to.x; pos.x += step.x) {
177                 canv.Line(pos, Vector<int>(pos.x, pos.y + height));
178         }
179         for (Vector<Scalar> pos(from); pos.y <= to.y; pos.y += step.y) {
180                 canv.Line(pos, Vector<int>(pos.x + width, pos.y));
181         }
182 }
183
184 template<class Scalar>
185 void Grid2Impl(
186                 Canvas &canv,
187                 Vector<int> pos,
188                 Vector<int> size,
189                 Vector<Scalar> step,
190                 Vector<int> n,
191                 Color c1,
192                 Color c2) {
193         Vector<int> from(pos);
194         Vector<int> to(pos + size);
195         Vector<int> clip(canv.Size());
196
197         if (from.x > clip.x || from.y > clip.y || to.x < 0 || to.y < 0) {
198                 return;
199         }
200         if (step.x <= 1 || step.y <= 1) {
201                 canv.SetColor(c1);
202                 canv.FillRect(pos, size);
203                 canv.SetColor(c2);
204                 GridImpl(canv, pos, size, step * Vector<Scalar>(n));
205                 return;
206         }
207
208         Vector<int> i(0, 0);
209
210         if (from.x < -step.x) {
211                 int skip = from.x / -step.x;
212                 from.x += skip * step.x;
213                 i.x += skip;
214         }
215         if (from.y < -step.y) {
216                 int skip = from.y / -step.y;
217                 from.y += skip * step.y;
218                 i.y += skip;
219         }
220         if (to.x > clip.x + step.x) {
221                 int skip = (to.x - clip.x) / step.x;
222                 to.x -= skip * step.x;
223         }
224         if (to.y > clip.y + step.y) {
225                 int skip = (to.y - clip.y) / step.y;
226                 to.y -= skip * step.y;
227         }
228
229         int width = to.x - from.x;
230         int height = to.y - from.y;
231
232         for (Vector<Scalar> pos(from); pos.x <= to.x; pos.x += step.x) {
233                 canv.SetColor((i.x++ % n.x) ? c1 : c2);
234                 canv.Line(pos, Vector<int>(pos.x, pos.y + height));
235         }
236         for (Vector<Scalar> pos(from); pos.y <= to.y; pos.y += step.y) {
237                 canv.SetColor((i.y++ % n.y) ? c1 : c2);
238                 canv.Line(pos, Vector<int>(pos.x + width, pos.y));
239         }
240 }
241
242 }
243
244 void Canvas::Grid(Vector<int> pos, Vector<int> size, Vector<int> step) {
245         GridImpl(*this, pos, size, step);
246 }
247
248 void Canvas::Grid(Vector<int> pos, Vector<int> size, Vector<float> step) {
249         GridImpl(*this, pos, size, step);
250 }
251
252 void Canvas::Grid2(
253                 Vector<int> pos, Vector<int> size, Vector<int> step,
254                 Vector<int> n, Color c1, Color c2) {
255         Grid2Impl(*this, pos, size, step, n, c1, c2);
256 }
257
258 void Canvas::Grid2(
259                 Vector<int> pos, Vector<int> size, Vector<float> step,
260                 Vector<int> n, Color c1, Color c2) {
261         Grid2Impl(*this, pos, size, step, n, c1, c2);
262 }
263
264 }