From 40231bd839dbe5b8aa65e8f3bde31bd33b9a9190 Mon Sep 17 00:00:00 2001 From: ubq323 Date: Mon, 8 Apr 2024 02:28:59 +0100 Subject: giant refactor, start to use drawing ops for drawing --- client/drawing.ha | 127 ---------------------------------------------- client/main.ha | 69 ++++++++++++++++--------- client/paintui/paintui.ha | 38 ++++++++++++++ drawing/drawing.ha | 89 ++++++++++++++++++++++++++++++++ drawing/op.ha | 4 ++ 5 files changed, 176 insertions(+), 151 deletions(-) delete mode 100644 client/drawing.ha create mode 100644 client/paintui/paintui.ha create mode 100644 drawing/drawing.ha create mode 100644 drawing/op.ha diff --git a/client/drawing.ha b/client/drawing.ha deleted file mode 100644 index 2d093ae..0000000 --- a/client/drawing.ha +++ /dev/null @@ -1,127 +0,0 @@ -use sdl2; -use fmt; -use io; -use endian; -use math::random; - -// 2d position, x and y -export type pos = (i32, i32); - -export type drawing_state = struct { - // is the mouse button held down? - drawing: bool, - pos: pos, - pictures: []picture, - - // hack. change this - conn: io::file, -}; - -export 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. -export 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 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, -}; - -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 yield c; case let err: net::error => fmt::fatal("couldn't connect to server:",net::strerror(err)); }; - let dstate = drawing_state { - drawing = false, - pos = (0,0), - pictures = pictures, - conn = conn, - }; - - let camera_pos: pos = (25, 50); - for (let i = 0z; i < 4; i += 1) { - const p = &dstate.pictures[i]; - clear_picture(p, 0xffffff); - }; + // paintui state + let pstate = paintui::state { ... }; + let camera_pos: pos = (25, 50); let quit = false; let n = 0; let lasttime = sdl2::SDL_GetTicks(); + + // in WORLD coords + let mouse_pos: pos = (0,0); + let mouse_down = false; for (!quit) { let ev = sdl2::event { ... }; for (sdl2::SDL_PollEvent(&ev)! == 1) switch (ev.event_type) { @@ -60,20 +66,28 @@ export fn main() void = { case sdl2::SDL_EventType::MOUSEBUTTONDOWN, sdl2::SDL_EventType::MOUSEBUTTONUP => const edata = ev.button; - dstate.pos = (edata.x + camera_pos.0, edata.y + camera_pos.1); + mouse_pos = (edata.x + camera_pos.0, edata.y + camera_pos.1); if (edata.button == 1) - dstate.drawing = (edata.state == 1); + mouse_down = (edata.state == 1); case sdl2::SDL_EventType::MOUSEMOTION => const edata = ev.motion; - dstate.pos = (edata.x + camera_pos.0, edata.y + camera_pos.1); + mouse_pos = (edata.x + camera_pos.0, edata.y + camera_pos.1); case => void; }; - movement(&camera_pos); - do_drawing(&dstate); + do_movement(&camera_pos); + + match (paintui::tick(&pstate, mouse_pos, mouse_down)) { + case void => yield; + case let op: drawing::op => + drawing::perform(pictures, op); + }; - for (let i = 0z; i < len(dstate.pictures); i+=1) - render_picture(&dstate.pictures[i], wsurf, camera_pos); + for (let i = 0z; i < len(pictures); i += 1) { + const psurf = picture_surfaces[i]; + const pic = &pictures[i]; + render_picture(pic, psurf, wsurf, camera_pos); + }; sdl2::SDL_UpdateWindowSurface(win)!; n += 1; @@ -81,9 +95,16 @@ export fn main() void = { }; }; -fn render_picture(pic: *picture, winsurf: *sdl2::SDL_Surface, camera_pos: pos) void = { - sdl2::SDL_BlitSurface(pic.surf, null, winsurf, &sdl2::SDL_Rect{ - x = pic.pos.0 - camera_pos.0, y = pic.pos.1 - camera_pos.1, ... +fn picture_from_surface(surf: *sdl2::SDL_Surface, world_pos: pos) drawing::picture = drawing::picture { + w = surf.w: size, + h = surf.h: size, + d = (surf.pixels as *opaque: *[*]u32), + world_pos = world_pos, +}; + +fn render_picture(pic: *drawing::picture, surf: *sdl2::SDL_Surface, winsurf: *sdl2::SDL_Surface, camera_pos: pos) void = { + sdl2::SDL_BlitSurface(surf, null, winsurf, &sdl2::SDL_Rect{ + x = pic.world_pos.0 - camera_pos.0, y = pic.world_pos.1 - camera_pos.1, ... })!; }; @@ -91,7 +112,7 @@ fn render_picture(pic: *picture, winsurf: *sdl2::SDL_Surface, camera_pos: pos) v def SPEED = 17; def DIAG_SPEED = 12; // thereabouts -fn movement(pos: *pos) void = { +fn do_movement(pos: *pos) void = { const kb = sdl2::SDL_GetKeyboardState(); let dx = 0; let dy = 0; diff --git a/client/paintui/paintui.ha b/client/paintui/paintui.ha new file mode 100644 index 0000000..ce55bb7 --- /dev/null +++ b/client/paintui/paintui.ha @@ -0,0 +1,38 @@ +// paintui handles turning sdl input things +// into drawing instructions +// it might have some kind of state machine +// for doing smooth lines between discrete mouse positions + +// all mouse pos stuff in this module is in world coordinates +// someone else is responsible for doing the transform from +// screen to world coords. + +use drawing; +use drawing::{pos}; + +export type state = struct { + // last known mouse position, in world coordinates + last_mouse_pos: pos, + // last known mouse pressed-ness + last_mouse_pressed: bool + +}; + +export fn tick( + pstate: *state, + mouse_pos: pos, mouse_pressed: bool +) (void | drawing::op) = { + defer { + pstate.last_mouse_pos = mouse_pos; + pstate.last_mouse_pressed = mouse_pressed; + }; + + // if mouse down and (position moved OR newly pressed) + return if (mouse_pressed + && (mouse_pos.0 != pstate.last_mouse_pos.0 + || mouse_pos.1 != pstate.last_mouse_pos.1 + || (!pstate.last_mouse_pressed))) + mouse_pos: drawing::op_circle + else void; +}; + 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 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); -- cgit v1.2.3