diff options
Diffstat (limited to 'drawing.ha')
-rw-r--r-- | drawing.ha | 32 |
1 files changed, 24 insertions, 8 deletions
@@ -1,4 +1,5 @@ use sdl2; +use fmt; // 2d position, x and y type pos = (i32, i32); @@ -7,13 +8,20 @@ type drawing_state = struct { // is the mouse button held down? drawing: bool, pos: pos, - picture: *picture, + 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. @@ -33,10 +41,12 @@ fn pidx(pic: *picture, pos: pos) size = { 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 { +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 = { @@ -57,14 +67,15 @@ fn outline_picture(pic: *picture) void = { 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 +// Draws a circle onto the 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 = { +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(dstate.picture.h:i32-1, cy+r); + const ymax = min(picture.h:i32-1, cy+r); const xmin = max(0, cx-r); - const xmax = min(dstate.picture.w:i32-1, cx+r); + const xmax = min(picture.w:i32-1, cx+r); const r2 = r*r + r; @@ -73,12 +84,17 @@ fn circle(dstate: *drawing_state, c: pos, r: i32, 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(picture, (x,y), color); }; }; }; }; fn do_drawing(dstate: *drawing_state) void = { - if (dstate.drawing) circle(dstate, dstate.pos, 20, 0xff0088); + 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); + }; }; + |