summaryrefslogtreecommitdiff
path: root/drawing
diff options
context:
space:
mode:
authorubq323 <ubq323@ubq323.website>2024-04-08 02:28:59 +0100
committerubq323 <ubq323@ubq323.website>2024-04-08 02:28:59 +0100
commit40231bd839dbe5b8aa65e8f3bde31bd33b9a9190 (patch)
tree35b81246f7b741aa1bbb75f697f426b777f98974 /drawing
parentd097561c1ee24f20d5dd48fbe35301e8d9cc7cac (diff)
giant refactor, start to use drawing ops for drawing
Diffstat (limited to 'drawing')
-rw-r--r--drawing/drawing.ha89
-rw-r--r--drawing/op.ha4
2 files changed, 93 insertions, 0 deletions
diff --git a/drawing/drawing.ha b/drawing/drawing.ha
new file mode 100644
index 0000000..70bd7af
--- /dev/null
+++ b/drawing/drawing.ha
@@ -0,0 +1,89 @@
+use sdl2;
+use fmt;
+use io;
+use endian;
+use math::random;
+
+export type pos = (i32, i32);
+
+export type picture = struct {
+ // the surface data as u32s
+ d: *[*]u32,
+ w: size,
+ h: size,
+
+ // position of topleft corner in the world
+ world_pos: pos,
+
+};
+
+// 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.
+fn pidx(pic: *picture, pos: pos) size = {
+ const (x,y) = pos;
+ const (xs,ys) = (x:size, y:size);
+ assert(0 <= x, "x position must not be less than 0");
+ assert(0 <= y, "y position must not be less than 0");
+ assert(xs < pic.w, "x position must be less than picture width");
+ assert(ys < pic.h, "y position must be less than picture height");
+
+ return xs + pic.w*ys;
+};
+
+export fn pic_set(pic: *picture, pos: pos, val: u32) void =
+ pic.d[pidx(pic,pos)] = val;
+
+export fn clear_picture(pic: *picture, color: u32) void = {
+ for (let i = 0z; i < pic.w*pic.h; i+=1) pic.d[i] = color;
+};
+
+export 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 picture, at given position radius color
+// Clips at the boundaries of the picture to avoid overflow.
+export fn circle(picture: *picture, c: pos, r: i32, color: u32) void = {
+ // fmt::printfln("C {} {} {} {:x}",c.0,c.1,r,color)!;
+ const (cx,cy) = c;
+ const ymin = max(0, cy-r);
+ const ymax = min(picture.h:i32-1, cy+r);
+ const xmin = max(0, cx-r);
+ const xmax = min(picture.w:i32-1, cx+r);
+
+ const r2 = r*r + r;
+
+ for (let y = ymin; y<=ymax; y+=1) {
+ const yd = y-cy;
+ for (let x = xmin; x<=xmax; x+=1) {
+ const xd = x-cx;
+ if (yd*yd + xd*xd <= r2) {
+ pic_set(picture, (x,y), color);
+ };
+ };
+ };
+};
+
+export fn perform(pictures: []picture, op: op) void = {
+ match (op) {
+ case op_other => abort("oopsy");
+ case let o: op_circle =>
+ const x = o.0, y=o.1;
+ for (const pic &.. pictures) {
+ let pos_within_pic =
+ (x - pic.world_pos.0, y - pic.world_pos.1): pos;
+ circle(pic, pos_within_pic, 20, 0xff0088);
+ };
+ };
+};
diff --git a/drawing/op.ha b/drawing/op.ha
new file mode 100644
index 0000000..ef39161
--- /dev/null
+++ b/drawing/op.ha
@@ -0,0 +1,4 @@
+export type op_circle = pos;
+export type op_other = void;
+
+export type op = (op_circle| op_other);