diff options
Diffstat (limited to 'client')
-rw-r--r-- | client/drawing.ha | 100 | ||||
-rw-r--r-- | client/main.ha | 100 |
2 files changed, 200 insertions, 0 deletions
diff --git a/client/drawing.ha b/client/drawing.ha new file mode 100644 index 0000000..e164d33 --- /dev/null +++ b/client/drawing.ha @@ -0,0 +1,100 @@ +use sdl2; +use fmt; + +// 2d position, x and y +export type pos = (i32, i32); + +export type drawing_state = struct { + // is the mouse button held down? + drawing: bool, + pos: pos, + pictures: []picture, +}; + +export 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. +// Bounds check happens in here instead of using a slice type, so +// that it's easier to remove later. +export fn pidx(pic: *picture, pos: pos) size = { + const (x,y) = pos; + const (xs,ys) = (x:size, y:size); + assert(0 <= x, "x position must not be less than 0"); + assert(0 <= y, "y position must not be less than 0"); + assert(xs < pic.w, "x position must be less than picture width"); + assert(ys < pic.h, "y position must be less than picture height"); + + return xs + pic.w*ys; +}; + +export fn pic_set(pic: *picture, pos: pos, val: u32) void = + pic.d[pidx(pic,pos)] = val; + +export 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, +}; + +export fn clear_picture(pic: *picture, color: u32) void = { + for (let i = 0z; i < pic.w*pic.h; i+=1) pic.d[i] = color; +}; + +export fn outline_picture(pic: *picture) void = { + for (let x = 0; x:size < pic.w; x+=1) { + pic_set(pic, (x, 0), 0); + pic_set(pic, (x, pic.h:int-1), 0); + }; + for (let y = 0; y:size < pic.h; y+=1) { + pic_set(pic, (0, y), 0); + pic_set(pic, (pic.w:int-1, y), 0); + }; +}; + +fn min(a: i32, b: i32) i32 = if (a<b) a else b; +fn max(a: i32, b: i32) i32 = if (a<b) b else a; + +// Draws a circle onto the picture, at given position radius color +// Clips at the boundaries of the picture to avoid overflow. +export fn circle(picture: *picture, c: pos, r: i32, color: u32) void = { + // fmt::printfln("C {} {} {} {:x}",c.0,c.1,r,color)!; + const (cx,cy) = c; + const ymin = max(0, cy-r); + const ymax = min(picture.h:i32-1, cy+r); + const xmin = max(0, cx-r); + const xmax = min(picture.w:i32-1, cx+r); + + const r2 = r*r + r; + + for (let y = ymin; y<=ymax; y+=1) { + const yd = y-cy; + for (let x = xmin; x<=xmax; x+=1) { + const xd = x-cx; + if (yd*yd + xd*xd <= r2) { + pic_set(picture, (x,y), color); + }; + }; + }; +}; + +export fn do_drawing(dstate: *drawing_state) void = { + if (dstate.drawing) for (let i = 0z; i < 4; i+=1) { + const pic = &dstate.pictures[i]; + const p = (dstate.pos.0 - pic.pos.0, dstate.pos.1 - pic.pos.1); + circle(pic, p, 20, 0xff0088); + }; +}; + diff --git a/client/main.ha b/client/main.ha new file mode 100644 index 0000000..ba1736e --- /dev/null +++ b/client/main.ha @@ -0,0 +1,100 @@ +use fmt; +use sdl2; +use math; + +def CHUNKSIZE = 512; +def NCHUNKS = 4; + +export fn main() void = { + sdl2::SDL_Init(sdl2::SDL_INIT_VIDEO)!; + defer sdl2::SDL_Quit(); + + const win = sdl2::SDL_CreateWindow("hi", 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)!; + + const offs: [_]pos = [ + (0,0), (0,CHUNKSIZE), (CHUNKSIZE,0), (CHUNKSIZE,CHUNKSIZE), + ]; + + let pictures: []picture = alloc([],NCHUNKS); + for (let i = 0z; i < NCHUNKS; i +=1){ + const surf = sdl2::SDL_CreateRGBSurface(0, + CHUNKSIZE, CHUNKSIZE, 32, 0xff0000, 0xff00, 0xff, 0)!; + append(pictures, picture_from_surface(surf, offs[i])); + }; + + let dstate = drawing_state { + drawing = false, + pos = (0,0), + pictures = pictures, + }; + + let camera_pos: pos = (25, 50); + + for (let i = 0z; i < 4; i += 1) { + const p = &dstate.pictures[i]; + clear_picture(p, 0xffffff); + }; + + let quit = false; + let n = 0; + let lasttime = sdl2::SDL_GetTicks(); + for (!quit) { + 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; + 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); + if (edata.button == 1) + dstate.drawing = (edata.state == 1); + case sdl2::SDL_EventType::MOUSEMOTION => + const edata = ev.motion; + dstate.pos = (edata.x + camera_pos.0, edata.y + camera_pos.1); + case => void; + }; + + movement(&camera_pos); + do_drawing(&dstate); + + for (let i = 0z; i < len(dstate.pictures); i+=1) + render_picture(&dstate.pictures[i], wsurf, camera_pos); + + sdl2::SDL_UpdateWindowSurface(win)!; + n += 1; + sdl2::SDL_Delay(1000/60); + }; +}; + +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 + +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; +}; + |