From 52557c8a1c78ee02bd0eda1c740a37f8d6759b0c Mon Sep 17 00:00:00 2001 From: ubq323 Date: Sun, 24 Mar 2024 22:28:12 +0000 Subject: multiple chunks, and drawing works completely correctly with moving around --- drawing.ha | 32 ++++++++++++++++++++++++-------- main.ha | 44 +++++++++++++++++++++++++++++++------------- 2 files changed, 55 insertions(+), 21 deletions(-) diff --git a/drawing.ha b/drawing.ha index 804aee4..5812109 100644 --- a/drawing.ha +++ b/drawing.ha @@ -1,4 +1,5 @@ use sdl2; +use fmt; // 2d position, x and y type pos = (i32, i32); @@ -7,13 +8,20 @@ type drawing_state = struct { // is the mouse button held down? drawing: bool, pos: pos, - picture: *picture, + pictures: []picture, }; 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. @@ -33,10 +41,12 @@ fn pidx(pic: *picture, pos: pos) size = { fn pic_set(pic: *picture, pos: pos, val: u32) void = pic.d[pidx(pic,pos)] = val; -fn picture_from_surface(surf: *sdl2::SDL_Surface) picture = picture { +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, }; fn clear_picture(pic: *picture, color: u32) void = { @@ -57,14 +67,15 @@ fn outline_picture(pic: *picture) void = { fn min(a: i32, b: i32) i32 = if (a const edata = ev.button; - dstate.pos = (edata.x, edata.y); + dstate.pos = (edata.x + camera_pos.0, edata.y + camera_pos.1); if (edata.button == 1) dstate.drawing = (edata.state == 1); case sdl2::SDL_EventType::MOUSEMOTION => const edata = ev.motion; - dstate.pos = (edata.x, edata.y); + dstate.pos = (edata.x + camera_pos.0, edata.y + camera_pos.1); case => void; }; movement(&camera_pos); - do_drawing(&dstate); - - sdl2::SDL_BlitSurface(xsurf, null, wsurf, &sdl2::SDL_Rect { - x = camera_pos.0, y = camera_pos.1, ... - })!; + + for (let i = 0z; i < len(dstate.pictures); i+=1) + render_picture(&dstate.pictures[i], wsurf, camera_pos); sdl2::SDL_UpdateWindowSurface(win)!; n += 1; @@ -61,6 +73,13 @@ 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, ... + })!; +}; + + def SPEED = 17; def DIAG_SPEED = 12; // thereabouts @@ -80,4 +99,3 @@ fn movement(pos: *pos) void = { pos.1 += dy * speed; }; - -- cgit v1.2.3