diff options
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | client/main.ha | 4 | ||||
-rw-r--r-- | server/main.ha | 71 |
3 files changed, 70 insertions, 6 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..218cb60 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.ppm diff --git a/client/main.ha b/client/main.ha index 542c8f9..fa63004 100644 --- a/client/main.ha +++ b/client/main.ha @@ -62,6 +62,8 @@ export fn main() void = { let mouse_pos: pos = (0,0); let mouse_down = false; for (!quit) { + do_movement(&camera_pos); + let ev = sdl2::event { ... }; for (sdl2::SDL_PollEvent(&ev)! == 1) switch (ev.event_type) { case sdl2::SDL_EventType::QUIT => quit = true; @@ -80,7 +82,6 @@ export fn main() void = { case => void; }; - do_movement(&camera_pos); match (paintui::tick(&pstate, mouse_pos, mouse_down)) { case void => yield; @@ -99,7 +100,6 @@ export fn main() void = { }; }; - for (let i = 0z; i < len(pictures); i += 1) { const psurf = picture_surfaces[i]; const pic = &pictures[i]; diff --git a/server/main.ha b/server/main.ha index 59e1306..abcb0fe 100644 --- a/server/main.ha +++ b/server/main.ha @@ -1,25 +1,76 @@ +use bufio; use fmt; use net; use net::tcp; use net::ip; use io; use os; +use fs; +use strings; +use time; +use time::date; +use time::chrono; use unix::poll; -use bufio; use drawing; +use drawing::{pos}; use packet_reader; + def PORT: u16 = 41460; type conn = net::socket; +def CHUNKSIZE = 512; export fn main() void = { + // 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: []drawing::picture = alloc([],len(offs)); + for (let i = 0z; i < len(offs); i +=1){ + append(pictures, drawing::picture { + w = CHUNKSIZE, + h = CHUNKSIZE, + d = alloc([0...],CHUNKSIZE*CHUNKSIZE: size): *[*]u32, + world_pos = offs[i], + }); + }; + for (const p &.. pictures) drawing::clear_picture(p, 0xffffff); + + const listener = tcp::listen(ip::ANY_V4, PORT)!; - loop(listener); + loop(listener, pictures); }; -fn loop(listener: net::socket) void = { +def save_interval = 5 * time::SECOND; + +fn save_world(pictures: []drawing::picture) void = { + fmt::printfln("saving world!")!; + for (const pic &.. pictures) { + const filename = fmt::asprintf("./c.{}.{}.ppm",pic.world_pos.0, pic.world_pos.1); + fmt::printfln("\t-> {}",filename)!; + defer free(filename); + const mode = fs::mode::USER_RW | fs::mode::GROUP_RW; + const file = os::create(filename, mode)!; + defer { + fmt::printfln("\t. {}",filename)!; + io::close(file)!; + }; + const header = fmt::asprintf("P6\n{} {}\n{}\n", pic.w, pic.h, 255); + defer free(header); + io::writeall(file, strings::toutf8(header))!; + for (let i = 0z; i < pic.w * pic.h; i += 1) { + const px = pic.d[i]; + io::writeall(file, [(px&0xff): u8, ((px>>8)&0xff): u8, ((px>>16)&0xff): u8])!; + }; + }; + +}; + +fn loop(listener: net::socket, pictures: []drawing::picture) void = { // pollfds[0] is the listener // pollfds[n>0] corresponds to packet_readers[n-1] let pollfds: []poll::pollfd = alloc([ poll::pollfd { @@ -28,9 +79,20 @@ fn loop(listener: net::socket) void = { let packet_readers: []packet_reader::packet_reader = []; + let now = time::now(time::clock::MONOTONIC); + let next = time::add(now, save_interval); + for (true) { fmt::println("poll.")!; - poll::poll(pollfds, poll::INDEF)!; + const timeout = time::diff(now, next); + poll::poll(pollfds, timeout)!; + + now = time::now(time::clock::MONOTONIC); + if (time::compare(now, next) >= 0) { + save_world(pictures); + next = time::add(now, save_interval); + }; + if (0 != pollfds[0].revents & poll::event::POLLIN) { fmt::println("new conn")!; const new_conn = tcp::accept(pollfds[0].fd)!; @@ -52,6 +114,7 @@ fn loop(listener: net::socket) void = { j += 1; for (let byte .. packet_bytes) fmt::printf(" {:x}",byte)!; const op = drawing::deser_op(packet_bytes) as drawing::op_circle; + drawing::perform(pictures, op); fmt::printfln("\t({:+6},{:+6})",op.0,op.1)!; for (let k = 1z; k < len(pollfds); k += 1) { if (k == i) continue; |