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::Triangle(Vector<int> v1, Vector<int> v2, Vector<int> v3) {
99 SDL_Point points[4] = { v1, v2, v3, v1 };
100 SDL_RenderDrawPoints(canv, points, 4);
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_RenderDrawPoints(canv, points, 5);
111 template<class Scalar>
116 Vector<Scalar> step) {
117 Vector<int> from(pos);
118 Vector<int> to(pos + size);
119 Vector<int> clip(canv.Size());
121 if (from.x > clip.x || from.y > clip.y || to.x < 0 || to.y < 0) {
124 if (step.x <= 1 || step.y <= 1) {
125 canv.FillRect(pos, size);
129 if (from.x < -step.x) {
130 int skip = from.x / -step.x;
131 from.x += skip * step.x;
133 if (from.y < -step.y) {
134 int skip = from.y / -step.y;
135 from.y += skip * step.y;
137 if (to.x > clip.x + step.x) {
138 int skip = (to.x - clip.x) / step.x;
139 to.x -= skip * step.x;
141 if (to.y > clip.y + step.y) {
142 int skip = (to.y - clip.y) / step.y;
143 to.y -= skip * step.y;
146 int width = to.x - from.x;
147 int height = to.y - from.y;
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));
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));
157 template<class Scalar>
166 Vector<int> from(pos);
167 Vector<int> to(pos + size);
168 Vector<int> clip(canv.Size());
170 if (from.x > clip.x || from.y > clip.y || to.x < 0 || to.y < 0) {
173 if (step.x <= 1 || step.y <= 1) {
175 canv.FillRect(pos, size);
177 GridImpl(canv, pos, size, step);
183 if (from.x < -step.x) {
184 int skip = from.x / -step.x;
185 from.x += skip * step.x;
188 if (from.y < -step.y) {
189 int skip = from.y / -step.y;
190 from.y += skip * step.y;
193 if (to.x > clip.x + step.x) {
194 int skip = (to.x - clip.x) / step.x;
195 to.x -= skip * step.x;
197 if (to.y > clip.y + step.y) {
198 int skip = (to.y - clip.y) / step.y;
199 to.y -= skip * step.y;
202 int width = to.x - from.x;
203 int height = to.y - from.y;
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));
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));
217 void Canvas::Grid(Vector<int> pos, Vector<int> size, Vector<int> step) {
218 GridImpl(*this, pos, size, step);
221 void Canvas::Grid(Vector<int> pos, Vector<int> size, Vector<float> step) {
222 GridImpl(*this, pos, size, step);
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);
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);