summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--drawing.ha65
-rw-r--r--main.ha71
2 files changed, 84 insertions, 52 deletions
diff --git a/drawing.ha b/drawing.ha
new file mode 100644
index 0000000..ff2aa71
--- /dev/null
+++ b/drawing.ha
@@ -0,0 +1,65 @@
+use sdl2;
+
+
+// 2d position, x and y
+type pos = (int, int);
+
+type drawing_state = struct {
+ // is the mouse button held down?
+ drawing: bool,
+ pos: pos,
+ picture: picture,
+};
+
+type picture = struct {
+ d: *[*]u32,
+ 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.
+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;
+};
+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 circle(dstate: *drawing_state, c: pos, r: int, color: u32) void = {
+ const (cx,cy) = c;
+ const ymin = max(0, cy-r);
+ const ymax = min(dstate.picture.h:int-1, cy+r);
+ const xmin = max(0, cx-r);
+ const xmax = min(dstate.picture.w:int-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(&dstate.picture, (x,y), color);
+ };
+ };
+ };
+
+};
+
+fn do_drawing(dstate: *drawing_state) void = {
+ if (dstate.drawing) circle(dstate, dstate.pos, 20, 0xff0088);
+};
diff --git a/main.ha b/main.ha
index 89d197b..ca0c68e 100644
--- a/main.ha
+++ b/main.ha
@@ -5,6 +5,8 @@ export fn main() void = {
sdl2::SDL_Init(sdl2::SDL_INIT_VIDEO)!;
defer sdl2::SDL_Quit();
+ fmt::println(size(i32), size(i64), size(int), size(size))!;
+
const win = sdl2::SDL_CreateWindow("hi", sdl2::SDL_WINDOWPOS_UNDEFINED, sdl2::SDL_WINDOWPOS_UNDEFINED, 640, 480, sdl2::SDL_WindowFlags::NONE)!;
defer sdl2::SDL_DestroyWindow(win);
@@ -21,14 +23,16 @@ export fn main() void = {
fmt::println(wsurf.w * wsurf.h)!;
fmt::println(sdl2::SDL_GetPixelFormatName(format.format))!;
- const pic = picture_from_surface(wsurf);
+ let dstate = drawing_state {
+ drawing = false,
+ pos = (0,0),
+ picture = picture_from_surface(wsurf)
+ };
+
let quit = false;
let n = 0z;
let lasttime = sdl2::SDL_GetTicks();
- let drawing = false;
- let mousex = 0;
- let mousey = 0;
for (!quit) {
let ev = sdl2::event { ... };
for (sdl2::SDL_PollEvent(&ev)! == 1) switch (ev.event_type) {
@@ -38,20 +42,22 @@ export fn main() void = {
if (keysym == sdl2::SDL_Keycode::ESCAPE) quit = true;
case sdl2::SDL_EventType::MOUSEBUTTONDOWN,
sdl2::SDL_EventType::MOUSEBUTTONUP =>
- const d = ev.button;
- mousex = d.x;
- mousey = d.y;
- if (d.button == 1)
- drawing = (d.state == 1);
+ const edata = ev.button;
+ dstate.pos = (edata.x, edata.y);
+ if (edata.button == 1)
+ dstate.drawing = (edata.state == 1);
case sdl2::SDL_EventType::MOUSEMOTION =>
- const d = ev.motion;
- mousex = d.x;
- mousey = d.y;
+ const edata = ev.motion;
+ dstate.pos = (edata.x, edata.y);
case => void;
};
+ do_drawing(&dstate);
- if (drawing) circle(pic, mousex, mousey, 25, 0x00ffff);
+
+ // if (drawing) {
+ // circle(pic, mousex, mousey, 5, 0x00ffff);
+ // };
// circle(pic, 100, 100, n:int, 0xff0000);
@@ -70,43 +76,4 @@ export fn main() void = {
};
};
-def WHITE = 0x556602;
-def BLACK = 0x0;
-
-type picture = struct {
- d: []u32,
- w: size,
- h: size,
-};
-fn pidx(pic: picture, x: size, y: size) size = (pic.w*y)+x;
-fn pic_set(pic: picture, x: size, y: size, val: u32) void = pic.d[pidx(pic,x,y)] = 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)[..surf.w*surf.h],
-};
-
-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 circle(pic: picture, cx: int, cy: int, r: int, color: u32) void = {
- const ymin = max(0, cy-r);
- const ymax = min(pic.h:int-1, cy+r);
- const xmin = max(0, cx-r);
- const xmax = min(pic.w:int-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) {
- // we already made sure x and y are both greater than 0
- pic_set(pic, x:size, y:size, color);
- };
- };
- };
-
-};