use fmt; use sdl2; use math; use io; use net; use net::dial; use unix::poll; use drawing; use drawing::{pos,CHUNKSIZE,picture}; use client::paintui; use packet_reader; use packet_reader::{VERSION}; def NSURFS = 16; def WIN_H: i32 = 480; def WIN_W: i32 = 640; export fn main() void = { // sdl init stuff sdl2::SDL_Init(sdl2::SDL_INIT_VIDEO)!; defer sdl2::SDL_Quit(); const win = sdl2::SDL_CreateWindow("garden", sdl2::SDL_WINDOWPOS_UNDEFINED, sdl2::SDL_WINDOWPOS_UNDEFINED, 640, 480, sdl2::SDL_WindowFlags::NONE)!; defer sdl2::SDL_DestroyWindow(win); const wsurf = sdl2::SDL_GetWindowSurface(win)!; let pmgr = picture_mgr { ... }; // 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)); }; // version check let byte = [0u8]; match (io::read(conn, byte)) { case let e: io::error => fmt::fatal("error with server handshake",io::strerror(e)); case => if (byte[0] != VERSION) fmt::fatalf("invalid version: got {}, expecting {}", byte[0], VERSION); }; const pollfd: [1]poll::pollfd = [ poll::pollfd { fd = conn, events=poll::event::POLLIN | poll::event::POLLOUT, ... }]; const packet_reader = packet_reader::new(conn); // paintui state let pstate = paintui::state { size_idx = 4, ... }; let camera_pos: pos = (25, 50); let quit = false; let n = 0; let lasttime = sdl2::SDL_GetTicks(); // in SCREEN coords let mouse_pos: pos = (0,0); let mouse_down = false; process_chunk_loadedness(&pmgr, &packet_reader, camera_pos); const win_pic = picture_from_surface(wsurf, (9,9)); for (!quit) { const did_move = do_movement(&camera_pos); // let nvis = 0; // for (const pic &.. pictures) { // if (is_picture_visible(camera_pos, pic)) { // fmt::printfln(" {} {}",pic.world_pos.0, pic.world_pos.1)!; // nvis += 1; // }; // }; // fmt::printfln("{} visible",nvis)!; let ev = sdl2::event { ... }; for (sdl2::SDL_PollEvent(&ev)! == 1) switch (ev.event_type) { case sdl2::SDL_EventType::QUIT => quit = true; case sdl2::SDL_EventType::KEYDOWN => const keysym = ev.key.keysym.sym; if (keysym == sdl2::SDL_Keycode::ESCAPE) quit = true else if (sdl2::SDL_Keycode::ZERO <= keysym && keysym <= sdl2::SDL_Keycode::NINE) paintui::key(&pstate, keysym - sdl2::SDL_Keycode::ZERO); case sdl2::SDL_EventType::MOUSEBUTTONDOWN, sdl2::SDL_EventType::MOUSEBUTTONUP => const edata = ev.button; mouse_pos = (edata.x, edata.y); if (edata.button == 1) mouse_down = (edata.state == 1); case sdl2::SDL_EventType::MOUSEMOTION => const edata = ev.motion; mouse_pos = (edata.x, edata.y); case sdl2::SDL_EventType::MOUSEWHEEL => const edata = ev.wheel; paintui::mousewheel(&pstate, edata.y); case => void; }; const mouse_pos_world = (mouse_pos.0 + camera_pos.0, mouse_pos.1 + camera_pos.1); match (paintui::tick(&pstate, mouse_pos_world, mouse_down)) { case void => yield; case let op: drawing::op => perform_drawop(&pmgr, op); packet_reader::write(&packet_reader, op: packet_reader::packet_drawop)!; }; if (did_move) { packet_reader::write(&packet_reader, camera_pos: packet_reader::packet_position)!; process_chunk_loadedness(&pmgr, &packet_reader, camera_pos); }; const n = poll::poll(pollfd, poll::NONBLOCK)!; if (n > 0) { const should_read = 0 != pollfd[0].revents & poll::event::POLLIN; const should_write = 0 != pollfd[0].revents & poll::event::POLLOUT; packet_reader::service(&packet_reader, should_read, should_write)!; for (true) match (packet_reader::read_next(&packet_reader)) { case done => break; case let e: packet_reader::error => fmt::fatalf("death: packet_reader: {}", e); case let op: packet_reader::packet_drawop => perform_drawop(&pmgr, op); case let packet: packet_reader::packet_sendchunk => enact_chunkdata(&pmgr, packet, camera_pos); free(packet.chunk_data); }; }; drawing::clear_picture(&win_pic, 0xdddddd); for (const pic &.. pmgr.pictures) render_picture(pic, wsurf, camera_pos); drawing::circle_hollow(&win_pic, mouse_pos, paintui::sizes[pstate.size_idx]: i32, pstate.color); sdl2::SDL_UpdateWindowSurface(win)!; n += 1; sdl2::SDL_Delay(1000/60); }; }; 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: *picture_c, winsurf: *sdl2::SDL_Surface, camera_pos: pos) void = { sdl2::SDL_BlitSurface(pic.surface, null, winsurf, &sdl2::SDL_Rect{ x = pic.world_pos.0 - camera_pos.0, y = pic.world_pos.1 - camera_pos.1, ... })!; }; def SPEED = 17; def DIAG_SPEED = 12; // thereabouts // returned whether movement happened fn do_movement(pos: *pos) bool = { const kb = sdl2::SDL_GetKeyboardState(); let dx = 0; let dy = 0; if (kb[sdl2::SDL_Scancode::W]) dy -= 1; if (kb[sdl2::SDL_Scancode::S]) dy += 1; if (kb[sdl2::SDL_Scancode::A]) dx -= 1; if (kb[sdl2::SDL_Scancode::D]) dx += 1; const did_move = dx!=0 || dy!=0; let speed = SPEED; if (dx != 0 && dy != 0) speed = DIAG_SPEED; pos.0 += dx * speed; pos.1 += dy * speed; return did_move; };