diff options
Diffstat (limited to 'client')
-rw-r--r-- | client/drawing.ha | 127 | ||||
-rw-r--r-- | client/main.ha | 69 | ||||
-rw-r--r-- | client/paintui/paintui.ha | 38 |
3 files changed, 83 insertions, 151 deletions
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<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. -export 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); - }; - }; - }; -}; - - - -export 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); - }; - // hack, change this - // let buf: [8]u8 = [0...]; - // fmt::printfln("C {},{}",dstate.pos.0,dstate.pos.1)!; - // endian::leputu32(buf[0..4],dstate.pos.0: u32); - // endian::leputu32(buf[4..8],dstate.pos.1: u32); - // io::write(dstate.conn, buf)!; - - static let rand: u64 = 12345; - static let cur = 0; - let buf: [20]u8 = [0...]; - const n = random::u32n(&rand, 10) : int; - for (let i = 0; i < n; i += 1) { - buf[i] = (cur+i) : u8; - }; - cur += n; - fmt::println(n)!; - io::writeall(dstate.conn, buf[..n])!; - }; -}; - diff --git a/client/main.ha b/client/main.ha index 0065eb6..7bbb706 100644 --- a/client/main.ha +++ b/client/main.ha @@ -3,11 +3,15 @@ use sdl2; use math; use net; use net::dial; +use drawing; +use drawing::{pos}; +use client::paintui; def CHUNKSIZE = 512; def NCHUNKS = 4; export fn main() void = { + // sdl init stuff sdl2::SDL_Init(sdl2::SDL_INIT_VIDEO)!; defer sdl2::SDL_Quit(); @@ -16,40 +20,42 @@ export fn main() void = { const wsurf = sdl2::SDL_GetWindowSurface(win)!; + // create 4 pictures. later we will have more pictures + // but for now there are just 4, and they are at fixed positions const offs: [_]pos = [ (0,0), (0,CHUNKSIZE), (CHUNKSIZE,0), (CHUNKSIZE,CHUNKSIZE), ]; - let pictures: []picture = alloc([],NCHUNKS); + let pictures: []drawing::picture = alloc([],NCHUNKS); + let picture_surfaces: []*sdl2::SDL_Surface = alloc([], NCHUNKS); for (let i = 0z; i < NCHUNKS; i +=1){ const surf = sdl2::SDL_CreateRGBSurface(0, CHUNKSIZE, CHUNKSIZE, 32, 0xff0000, 0xff00, 0xff, 0)!; + append(picture_surfaces, surf); append(pictures, picture_from_surface(surf, offs[i])); }; + for (const p &.. pictures) drawing::clear_picture(p, 0xffffff); + + // connect to server const conn = match(dial::dial("tcp","localhost","41460")) { case let c: net::socket => 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; +}; + |