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/main.ha | 69 ++++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 45 insertions(+), 24 deletions(-) (limited to 'client/main.ha') 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; -- cgit v1.2.3