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