From 1762e9f622c7a9045e6ea23a92a81d7dfa5dadda Mon Sep 17 00:00:00 2001 From: ubq323 Date: Thu, 28 Mar 2024 18:50:23 +0000 Subject: reörganize MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client/drawing.ha | 100 +++++++++++++++++++++++++++++++++++++++++++++++++++++ client/main.ha | 100 +++++++++++++++++++++++++++++++++++++++++++++++++++++ drawing.ha | 100 ----------------------------------------------------- main.ha | 101 ------------------------------------------------------ server/main.ha | 43 +++++++++++++++++++++++ 5 files changed, 243 insertions(+), 201 deletions(-) create mode 100644 client/drawing.ha create mode 100644 client/main.ha delete mode 100644 drawing.ha delete mode 100644 main.ha create mode 100644 server/main.ha 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 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; +}; + diff --git a/drawing.ha b/drawing.ha deleted file mode 100644 index 5812109..0000000 --- a/drawing.ha +++ /dev/null @@ -1,100 +0,0 @@ -use sdl2; -use fmt; - -// 2d position, x and y -type pos = (i32, i32); - -type drawing_state = struct { - // is the mouse button held down? - drawing: bool, - pos: pos, - 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. -// Bounds check happens in here instead of using a slice type, so -// that it's easier to remove later. -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; -}; - -fn pic_set(pic: *picture, pos: pos, val: u32) void = - pic.d[pidx(pic,pos)] = val; - -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 = { - for (let i = 0z; i < pic.w*pic.h; i+=1) pic.d[i] = color; -}; - -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 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; -}; - diff --git a/server/main.ha b/server/main.ha new file mode 100644 index 0000000..43bdf34 --- /dev/null +++ b/server/main.ha @@ -0,0 +1,43 @@ +use fmt; +use os; +use io; +use fs; +use errors; +use drawing::{pos}; + +// type chunk = struct { +// fd: io::file, +// d: []u32, +// }; + +// def CHUNKSIZE: size = 512; +// def CHUNK_LENGTH: size = CHUNKSIZE*CHUNKSIZE*size(u32); + +// fn open_chunk_file(chunkpos: pos) (chunk | fs::error) = { +// const path = fmt::asprintf("c.{}.{}.dat", chunkpos.0, chunkpos.1); +// defer free(path); +// const fd = fs::create_file(os::cwd, path, +// fs::mode::USER_RW | fs::mode::GROUP_RW, +// fs::flag::RDWR)?; +// io::trunc(fd, CHUNK_LENGTH)?; +// const dp = io::mmap(null, CHUNK_LENGTH, +// io::prot::READ | io::prot::WRITE, +// io::mflag::SHARED, +// fd, 0)?; +// const d = (dp: *[*]u32)[..CHUNKSIZE*CHUNKSIZE]; +// return chunk { fd = fd, d = d }; +// }; + +export fn main() void = { + // match (open_chunk_file((-3,12))) { + // case let e: fs::error => + // fmt::fatal(fs::strerror(e)); + // case let ch: chunk => + // ch.d[0] = 0x12345678; + // ch.d[CHUNKSIZE*CHUNKSIZE - 1]=0xdeadbeef; + // }; + + const t = test2 { a=4, b=5 }; + thethe(&t); + +}; -- cgit v1.2.3