From 6d766d6dd9177ececb14ebfe2ac68cc581eeeb44 Mon Sep 17 00:00:00 2001 From: ubq323 Date: Fri, 22 Mar 2024 23:52:56 +0000 Subject: movement, multiple surfaces (sort of) --- drawing.ha | 41 +++++++++++++++++++++++++++----------- main.ha | 66 +++++++++++++++++++++++++++++++++----------------------------- 2 files changed, 65 insertions(+), 42 deletions(-) diff --git a/drawing.ha b/drawing.ha index ff2aa71..804aee4 100644 --- a/drawing.ha +++ b/drawing.ha @@ -1,14 +1,13 @@ use sdl2; - // 2d position, x and y -type pos = (int, int); +type pos = (i32, i32); type drawing_state = struct { // is the mouse button held down? drawing: bool, pos: pos, - picture: picture, + picture: *picture, }; type picture = struct { @@ -16,6 +15,7 @@ type picture = struct { w: size, h: size, }; + // 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. @@ -29,22 +29,42 @@ fn pidx(pic: *picture, pos: pos) size = { return xs + pic.w*ys; }; -fn pic_set(pic: *picture, pos: pos, val: u32) void = pic.d[pidx(pic,pos)] = val; + +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 { w = surf.w: size, h = surf.h: size, d = (surf.pixels as *opaque: *[*]u32), }; -fn min(a: int, b: int) int = if (a void; }; - do_drawing(&dstate); - - - // if (drawing) { - // circle(pic, mousex, mousey, 5, 0x00ffff); - // }; + movement(&camera_pos); - // circle(pic, 100, 100, n:int, 0xff0000); + do_drawing(&dstate); + + sdl2::SDL_BlitSurface(xsurf, null, wsurf, &sdl2::SDL_Rect { + x = camera_pos.0, y = camera_pos.1, ... + })!; sdl2::SDL_UpdateWindowSurface(win)!; n += 1; sdl2::SDL_Delay(1000/60); - - // if (n % 100 == 0) { - // const thistime = sdl2::SDL_GetTicks(); - // const dt = (thistime - lasttime): f64; - // lasttime = thistime; - // const dt = dt / 100.0; - // const fps = 1000.0/dt; - // fmt::printfln("fps: {}",fps)!; - // }; }; }; +def SPEED = 17; +def DIAG_SPEED = 12; // thereabouts + +fn movement(pos: *pos) void = { + 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; + + let speed = SPEED; + if (dx != 0 && dy != 0) speed = DIAG_SPEED; + + pos.0 += dx * speed; + pos.1 += dy * speed; +}; + -- cgit v1.2.3