aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--client/main.ha69
-rw-r--r--client/paintui/paintui.ha38
-rw-r--r--drawing/drawing.ha (renamed from client/drawing.ha)62
-rw-r--r--drawing/op.ha4
4 files changed, 99 insertions, 74 deletions
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;
+};
+
diff --git a/client/drawing.ha b/drawing/drawing.ha
index 2d093ae..70bd7af 100644
--- a/client/drawing.ha
+++ b/drawing/drawing.ha
@@ -4,36 +4,23 @@ 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,
+ // 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.
-export fn pidx(pic: *picture, pos: pos) size = {
+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");
@@ -47,14 +34,6 @@ export fn pidx(pic: *picture, pos: pos) size = {
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;
};
@@ -96,32 +75,15 @@ export fn circle(picture: *picture, c: pos, r: i32, color: u32) void = {
};
};
-
-
-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);
+export fn perform(pictures: []picture, op: op) void = {
+ match (op) {
+ case op_other => 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);
};
- // 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/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);