diff options
author | ubq323 <ubq323@ubq323.website> | 2024-03-28 18:50:23 +0000 |
---|---|---|
committer | ubq323 <ubq323@ubq323.website> | 2024-03-28 18:50:23 +0000 |
commit | 1762e9f622c7a9045e6ea23a92a81d7dfa5dadda (patch) | |
tree | 78df664636234ccdd8de761bdda65339ed22d441 /drawing.ha | |
parent | 52557c8a1c78ee02bd0eda1c740a37f8d6759b0c (diff) |
reörganize
Diffstat (limited to 'drawing.ha')
-rw-r--r-- | drawing.ha | 100 |
1 files changed, 0 insertions, 100 deletions
diff --git a/drawing.ha b/drawing.ha deleted file mode 100644 index 5812109..0000000 --- a/drawing.ha +++ /dev/null @@ -1,100 +0,0 @@ -use sdl2; -use fmt; - -// 2d position, x and y -type pos = (i32, i32); - -type drawing_state = struct { - // is the mouse button held down? - drawing: bool, - pos: pos, - pictures: []picture, -}; - -type picture = struct { - // the surface data as u32s - d: *[*]u32, - w: size, - h: size, - - // backreference to the surface whose data we're using - surf: *sdl2::SDL_Surface, - - 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; -}; - -fn pic_set(pic: *picture, pos: pos, val: u32) void = - pic.d[pidx(pic,pos)] = val; - -fn picture_from_surface(surf: *sdl2::SDL_Surface, pos: pos) picture = picture { - w = surf.w: size, - h = surf.h: size, - d = (surf.pixels as *opaque: *[*]u32), - surf = surf, - pos = pos, -}; - -fn clear_picture(pic: *picture, color: u32) void = { - for (let i = 0z; i < pic.w*pic.h; i+=1) pic.d[i] = color; -}; - -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. -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); - }; - }; - }; -}; - -fn do_drawing(dstate: *drawing_state) void = { - if (dstate.drawing) for (let i = 0z; i < 4; i+=1) { - const pic = &dstate.pictures[i]; - const p = (dstate.pos.0 - pic.pos.0, dstate.pos.1 - pic.pos.1); - circle(pic, p, 20, 0xff0088); - }; -}; - |