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 Texture Canvas::CreateStaticTexture(Vector<int> size) {
49 return Texture(canv, Color::Format, SDL_TEXTUREACCESS_STATIC, size);
53 void Canvas::Copy(Texture &tex, Vector<int> to) {
58 void Canvas::SetColor(Color c) {
59 SDL_SetRenderDrawColor(canv, c.r, c.g, c.b, c.a);
64 SDL_RenderClear(canv);
67 void Canvas::Outline() {
68 SDL_RenderDrawRect(canv, nullptr);
72 void Canvas::Line(Vector<int> from, Vector<int> to) {
73 SDL_RenderDrawLine(canv, from.x, from.y, to.x, to.y);
76 void Canvas::FillRect(Vector<int> pos, Vector<int> size) {
82 SDL_RenderFillRect(canv, &destRect);
85 void Canvas::OutlineRect(Vector<int> pos, Vector<int> size) {
91 SDL_RenderDrawRect(canv, &destRect);
95 void Canvas::Dot(Vector<int> pos) {
96 SDL_RenderDrawPoint(canv, pos.x, pos.y);
99 void Canvas::Cross(Vector<int> pos, int extent) {
101 Vector<int>(pos.x - extent, pos.y),
102 Vector<int>(pos.x + extent, pos.y));
104 Vector<int>(pos.x, pos.y - extent),
105 Vector<int>(pos.x, pos.y + extent));
108 void Canvas::Arrow(Vector<int> from, Vector<int> to) {
110 Vector<float> delta(to - from);
111 delta = delta / Length(delta);
113 Line(to, to + Vector<int>(Rotate90(delta) * 5.0f - (delta * 5.0f)));
114 Line(to, to + Vector<int>(Rotate270(delta) * 5.0f - (delta * 5.0f)));
117 void Canvas::Triangle(Vector<int> v1, Vector<int> v2, Vector<int> v3) {
118 SDL_Point points[4] = { v1, v2, v3, v1 };
119 SDL_RenderDrawLines(canv, points, 4);
122 void Canvas::Quad(Vector<int> v1, Vector<int> v2, Vector<int> v3, Vector<int> v4) {
123 SDL_Point points[5] = { v1, v2, v3, v4, v1 };
124 SDL_RenderDrawLines(canv, points, 5);
130 template<class Scalar>
135 Vector<Scalar> step) {
136 Vector<int> from(pos);
137 Vector<int> to(pos + size);
138 Vector<int> clip(canv.Size());
140 if (from.x > clip.x || from.y > clip.y || to.x < 0 || to.y < 0) {
143 if (step.x <= 1 || step.y <= 1) {
144 canv.FillRect(pos, size);
148 if (from.x < -step.x) {
149 int skip = from.x / -step.x;
150 from.x += skip * step.x;
152 if (from.y < -step.y) {
153 int skip = from.y / -step.y;
154 from.y += skip * step.y;
156 if (to.x > clip.x + step.x) {
157 int skip = (to.x - clip.x) / step.x;
158 to.x -= skip * step.x;
160 if (to.y > clip.y + step.y) {
161 int skip = (to.y - clip.y) / step.y;
162 to.y -= skip * step.y;
165 int width = to.x - from.x;
166 int height = to.y - from.y;
168 for (Vector<Scalar> pos(from); pos.x <= to.x; pos.x += step.x) {
169 canv.Line(pos, Vector<int>(pos.x, pos.y + height));
171 for (Vector<Scalar> pos(from); pos.y <= to.y; pos.y += step.y) {
172 canv.Line(pos, Vector<int>(pos.x + width, pos.y));
176 template<class Scalar>
185 Vector<int> from(pos);
186 Vector<int> to(pos + size);
187 Vector<int> clip(canv.Size());
189 if (from.x > clip.x || from.y > clip.y || to.x < 0 || to.y < 0) {
192 if (step.x <= 1 || step.y <= 1) {
194 canv.FillRect(pos, size);
196 GridImpl(canv, pos, size, step * Vector<Scalar>(n));
202 if (from.x < -step.x) {
203 int skip = from.x / -step.x;
204 from.x += skip * step.x;
207 if (from.y < -step.y) {
208 int skip = from.y / -step.y;
209 from.y += skip * step.y;
212 if (to.x > clip.x + step.x) {
213 int skip = (to.x - clip.x) / step.x;
214 to.x -= skip * step.x;
216 if (to.y > clip.y + step.y) {
217 int skip = (to.y - clip.y) / step.y;
218 to.y -= skip * step.y;
221 int width = to.x - from.x;
222 int height = to.y - from.y;
224 for (Vector<Scalar> pos(from); pos.x <= to.x; pos.x += step.x) {
225 canv.SetColor((i.x++ % n.x) ? c1 : c2);
226 canv.Line(pos, Vector<int>(pos.x, pos.y + height));
228 for (Vector<Scalar> pos(from); pos.y <= to.y; pos.y += step.y) {
229 canv.SetColor((i.y++ % n.y) ? c1 : c2);
230 canv.Line(pos, Vector<int>(pos.x + width, pos.y));
236 void Canvas::Grid(Vector<int> pos, Vector<int> size, Vector<int> step) {
237 GridImpl(*this, pos, size, step);
240 void Canvas::Grid(Vector<int> pos, Vector<int> size, Vector<float> step) {
241 GridImpl(*this, pos, size, step);
245 Vector<int> pos, Vector<int> size, Vector<int> step,
246 Vector<int> n, Color c1, Color c2) {
247 Grid2Impl(*this, pos, size, step, n, c1, c2);
251 Vector<int> pos, Vector<int> size, Vector<float> step,
252 Vector<int> n, Color c1, Color c2) {
253 Grid2Impl(*this, pos, size, step, n, c1, c2);