aboutsummaryrefslogtreecommitdiff
path: root/drawing.ha
diff options
context:
space:
mode:
Diffstat (limited to 'drawing.ha')
-rw-r--r--drawing.ha41
1 files changed, 30 insertions, 11 deletions
diff --git a/drawing.ha b/drawing.ha
index ff2aa71..804aee4 100644
--- a/drawing.ha
+++ b/drawing.ha
@@ -1,14 +1,13 @@
use sdl2;
-
// 2d position, x and y
-type pos = (int, int);
+type pos = (i32, i32);
type drawing_state = struct {
// is the mouse button held down?
drawing: bool,
pos: pos,
- picture: picture,
+ picture: *picture,
};
type picture = struct {
@@ -16,6 +15,7 @@ type picture = struct {
w: size,
h: size,
};
+
// Returns array index of the pixel at position pos.
// Bounds check happens in here instead of using a slice type, so
// that it's easier to remove later.
@@ -29,22 +29,42 @@ fn pidx(pic: *picture, pos: pos) size = {
return xs + pic.w*ys;
};
-fn pic_set(pic: *picture, pos: pos, val: u32) void = pic.d[pidx(pic,pos)] = val;
+
+fn pic_set(pic: *picture, pos: pos, val: u32) void =
+ pic.d[pidx(pic,pos)] = val;
+
fn picture_from_surface(surf: *sdl2::SDL_Surface) picture = picture {
w = surf.w: size,
h = surf.h: size,
d = (surf.pixels as *opaque: *[*]u32),
};
-fn min(a: int, b: int) int = if (a<b) a else b;
-fn max(a: int, b: int) int = if (a<b) b else a;
+fn clear_picture(pic: *picture, color: u32) void = {
+ for (let i = 0z; i < pic.w*pic.h; i+=1) pic.d[i] = color;
+};
-fn circle(dstate: *drawing_state, c: pos, r: int, color: u32) void = {
+fn outline_picture(pic: *picture) void = {
+ for (let x = 0; x:size < pic.w; x+=1) {
+ pic_set(pic, (x, 0), 0);
+ pic_set(pic, (x, pic.h:int-1), 0);
+ };
+ for (let y = 0; y:size < pic.h; y+=1) {
+ pic_set(pic, (0, y), 0);
+ pic_set(pic, (pic.w:int-1, y), 0);
+ };
+};
+
+fn min(a: i32, b: i32) i32 = if (a<b) a else b;
+fn max(a: i32, b: i32) i32 = if (a<b) b else a;
+
+// Draws a circle onto the dstate's picture, at given position radius color
+// Clips at the boundaries of the picture to avoid overflow.
+fn circle(dstate: *drawing_state, c: pos, r: i32, color: u32) void = {
const (cx,cy) = c;
const ymin = max(0, cy-r);
- const ymax = min(dstate.picture.h:int-1, cy+r);
+ const ymax = min(dstate.picture.h:i32-1, cy+r);
const xmin = max(0, cx-r);
- const xmax = min(dstate.picture.w:int-1, cx+r);
+ const xmax = min(dstate.picture.w:i32-1, cx+r);
const r2 = r*r + r;
@@ -53,11 +73,10 @@ fn circle(dstate: *drawing_state, c: pos, r: int, color: u32) void = {
for (let x = xmin; x<=xmax; x+=1) {
const xd = x-cx;
if (yd*yd + xd*xd <= r2) {
- pic_set(&dstate.picture, (x,y), color);
+ pic_set(dstate.picture, (x,y), color);
};
};
};
-
};
fn do_drawing(dstate: *drawing_state) void = {