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);
52 Texture Canvas::LoadTexture(const char *file) {
53 return Texture(canv, file);
57 void Canvas::Copy(Texture &tex, Vector<int> to) {
61 void Canvas::Copy(Texture &tex, Rect<int> clip, Vector<int> to) {
62 tex.Copy(canv, clip, to);
66 void Canvas::SetColor(Color c) {
67 SDL_SetRenderDrawColor(canv, c.r, c.g, c.b, c.a);
72 SDL_RenderClear(canv);
75 void Canvas::Outline() {
76 SDL_RenderDrawRect(canv, nullptr);
80 void Canvas::Line(Vector<int> from, Vector<int> to) {
81 SDL_RenderDrawLine(canv, from.x, from.y, to.x, to.y);
84 void Canvas::FillRect(Vector<int> pos, Vector<int> size) {
90 SDL_RenderFillRect(canv, &destRect);
93 void Canvas::OutlineRect(Vector<int> pos, Vector<int> size) {
99 SDL_RenderDrawRect(canv, &destRect);
103 void Canvas::Dot(Vector<int> pos) {
104 SDL_RenderDrawPoint(canv, pos.x, pos.y);
107 void Canvas::Cross(Vector<int> pos, int extent) {
109 Vector<int>(pos.x - extent, pos.y),
110 Vector<int>(pos.x + extent, pos.y));
112 Vector<int>(pos.x, pos.y - extent),
113 Vector<int>(pos.x, pos.y + extent));
116 void Canvas::Arrow(Vector<int> from, Vector<int> to) {
118 Vector<float> delta(to - from);
119 delta = delta / Length(delta);
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)));
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);
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);
138 template<class Scalar>
143 Vector<Scalar> step) {
144 Vector<int> from(pos);
145 Vector<int> to(pos + size);
146 Vector<int> clip(canv.Size());
148 if (from.x > clip.x || from.y > clip.y || to.x < 0 || to.y < 0) {
151 if (step.x <= 1 || step.y <= 1) {
152 canv.FillRect(pos, size);
156 if (from.x < -step.x) {
157 int skip = from.x / -step.x;
158 from.x += skip * step.x;
160 if (from.y < -step.y) {
161 int skip = from.y / -step.y;
162 from.y += skip * step.y;
164 if (to.x > clip.x + step.x) {
165 int skip = (to.x - clip.x) / step.x;
166 to.x -= skip * step.x;
168 if (to.y > clip.y + step.y) {
169 int skip = (to.y - clip.y) / step.y;
170 to.y -= skip * step.y;
173 int width = to.x - from.x;
174 int height = to.y - from.y;
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));
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));
184 template<class Scalar>
193 Vector<int> from(pos);
194 Vector<int> to(pos + size);
195 Vector<int> clip(canv.Size());
197 if (from.x > clip.x || from.y > clip.y || to.x < 0 || to.y < 0) {
200 if (step.x <= 1 || step.y <= 1) {
202 canv.FillRect(pos, size);
204 GridImpl(canv, pos, size, step * Vector<Scalar>(n));
210 if (from.x < -step.x) {
211 int skip = from.x / -step.x;
212 from.x += skip * step.x;
215 if (from.y < -step.y) {
216 int skip = from.y / -step.y;
217 from.y += skip * step.y;
220 if (to.x > clip.x + step.x) {
221 int skip = (to.x - clip.x) / step.x;
222 to.x -= skip * step.x;
224 if (to.y > clip.y + step.y) {
225 int skip = (to.y - clip.y) / step.y;
226 to.y -= skip * step.y;
229 int width = to.x - from.x;
230 int height = to.y - from.y;
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));
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));
244 void Canvas::Grid(Vector<int> pos, Vector<int> size, Vector<int> step) {
245 GridImpl(*this, pos, size, step);
248 void Canvas::Grid(Vector<int> pos, Vector<int> size, Vector<float> step) {
249 GridImpl(*this, pos, size, step);
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);
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);