8 using std::runtime_error;
13 Canvas::Canvas(SDL_Window *win, int index, Uint32 flags)
14 : canv(SDL_CreateRenderer(win, index, flags)) {
16 throw runtime_error(std::string("create canvas: ") + SDL_GetError());
21 if (canv) SDL_DestroyRenderer(canv);
24 Canvas::Canvas(Canvas &&other)
29 Canvas &Canvas::operator =(Canvas &&other) {
30 std::swap(canv, other.canv);
35 void Canvas::Present() {
36 SDL_RenderPresent(canv);
40 Vector<int> Canvas::Size() const {
43 SDL_GetRendererOutputSize(canv, &size.x, &size.y);
48 void Canvas::SetColor(Color c) {
49 SDL_SetRenderDrawColor(canv, c.r, c.g, c.b, c.a);
54 SDL_RenderClear(canv);
57 void Canvas::Outline() {
58 SDL_RenderDrawRect(canv, nullptr);
62 void Canvas::Line(Vector<int> from, Vector<int> to) {
63 SDL_RenderDrawLine(canv, from.x, from.y, to.x, to.y);
66 void Canvas::FillRect(Vector<int> pos, Vector<int> size) {
72 SDL_RenderFillRect(canv, &destRect);
75 void Canvas::OutlineRect(Vector<int> pos, Vector<int> size) {
81 SDL_RenderDrawRect(canv, &destRect);
85 void Canvas::Dot(Vector<int> pos) {
86 SDL_RenderDrawPoint(canv, pos.x, pos.y);
89 void Canvas::Cross(Vector<int> pos, int extent) {
91 Vector<int>(pos.x - extent, pos.y),
92 Vector<int>(pos.x + extent, pos.y));
94 Vector<int>(pos.x, pos.y - extent),
95 Vector<int>(pos.x, pos.y + extent));
98 void Canvas::Arrow(Vector<int> from, Vector<int> to) {
100 Vector<float> delta(to - from);
101 delta = delta / Length(delta);
103 Line(to, to + Vector<int>(Rotate90(delta) * 5.0f - (delta * 5.0f)));
104 Line(to, to + Vector<int>(Rotate270(delta) * 5.0f - (delta * 5.0f)));
107 void Canvas::Triangle(Vector<int> v1, Vector<int> v2, Vector<int> v3) {
108 SDL_Point points[4] = { v1, v2, v3, v1 };
109 SDL_RenderDrawLines(canv, points, 4);
112 void Canvas::Quad(Vector<int> v1, Vector<int> v2, Vector<int> v3, Vector<int> v4) {
113 SDL_Point points[5] = { v1, v2, v3, v4, v1 };
114 SDL_RenderDrawLines(canv, points, 5);
120 template<class Scalar>
125 Vector<Scalar> step) {
126 Vector<int> from(pos);
127 Vector<int> to(pos + size);
128 Vector<int> clip(canv.Size());
130 if (from.x > clip.x || from.y > clip.y || to.x < 0 || to.y < 0) {
133 if (step.x <= 1 || step.y <= 1) {
134 canv.FillRect(pos, size);
138 if (from.x < -step.x) {
139 int skip = from.x / -step.x;
140 from.x += skip * step.x;
142 if (from.y < -step.y) {
143 int skip = from.y / -step.y;
144 from.y += skip * step.y;
146 if (to.x > clip.x + step.x) {
147 int skip = (to.x - clip.x) / step.x;
148 to.x -= skip * step.x;
150 if (to.y > clip.y + step.y) {
151 int skip = (to.y - clip.y) / step.y;
152 to.y -= skip * step.y;
155 int width = to.x - from.x;
156 int height = to.y - from.y;
158 for (Vector<Scalar> pos(from); pos.x <= to.x; pos.x += step.x) {
159 canv.Line(pos, Vector<int>(pos.x, pos.y + height));
161 for (Vector<Scalar> pos(from); pos.y <= to.y; pos.y += step.y) {
162 canv.Line(pos, Vector<int>(pos.x + width, pos.y));
166 template<class Scalar>
175 Vector<int> from(pos);
176 Vector<int> to(pos + size);
177 Vector<int> clip(canv.Size());
179 if (from.x > clip.x || from.y > clip.y || to.x < 0 || to.y < 0) {
182 if (step.x <= 1 || step.y <= 1) {
184 canv.FillRect(pos, size);
186 GridImpl(canv, pos, size, step * Vector<Scalar>(n));
192 if (from.x < -step.x) {
193 int skip = from.x / -step.x;
194 from.x += skip * step.x;
197 if (from.y < -step.y) {
198 int skip = from.y / -step.y;
199 from.y += skip * step.y;
202 if (to.x > clip.x + step.x) {
203 int skip = (to.x - clip.x) / step.x;
204 to.x -= skip * step.x;
206 if (to.y > clip.y + step.y) {
207 int skip = (to.y - clip.y) / step.y;
208 to.y -= skip * step.y;
211 int width = to.x - from.x;
212 int height = to.y - from.y;
214 for (Vector<Scalar> pos(from); pos.x <= to.x; pos.x += step.x) {
215 canv.SetColor((i.x++ % n.x) ? c1 : c2);
216 canv.Line(pos, Vector<int>(pos.x, pos.y + height));
218 for (Vector<Scalar> pos(from); pos.y <= to.y; pos.y += step.y) {
219 canv.SetColor((i.y++ % n.y) ? c1 : c2);
220 canv.Line(pos, Vector<int>(pos.x + width, pos.y));
226 void Canvas::Grid(Vector<int> pos, Vector<int> size, Vector<int> step) {
227 GridImpl(*this, pos, size, step);
230 void Canvas::Grid(Vector<int> pos, Vector<int> size, Vector<float> step) {
231 GridImpl(*this, pos, size, step);
235 Vector<int> pos, Vector<int> size, Vector<int> step,
236 Vector<int> n, Color c1, Color c2) {
237 Grid2Impl(*this, pos, size, step, n, c1, c2);
241 Vector<int> pos, Vector<int> size, Vector<float> step,
242 Vector<int> n, Color c1, Color c2) {
243 Grid2Impl(*this, pos, size, step, n, c1, c2);