From 39d3a3c542bf2848abda61185c1fb5b68de31031 Mon Sep 17 00:00:00 2001 From: ubq323 Date: Sun, 12 Nov 2023 18:47:47 +0000 Subject: h --- main.ha | 55 ++++++++++++++++++++++++++++++------------------------- ui.ha | 39 --------------------------------------- 2 files changed, 30 insertions(+), 64 deletions(-) delete mode 100644 ui.ha diff --git a/main.ha b/main.ha index 9009947..41917c8 100644 --- a/main.ha +++ b/main.ha @@ -1,4 +1,7 @@ use math::random; +use vt; +use io; +use os; type player = struct { x: int, @@ -26,48 +29,51 @@ fn set_tile_at(r: room, x: size, y: size, t: tile) void = { r.tiles[ix] = t; }; -fn tile_repr(t: tile) str = match (t) { - case wall => yield "\x1b[90m\u2588"; - case floor => yield "\x1b[37m."; +fn get_key() u8 = { + let b = [0u8]; + io::read(os::stdin_file, b)!; + return b[0]; }; -fn draw_room(r: room) void = { - assert(r.w < 20); - assert(r.h < 20); - for (let x = 0z; x < r.w; x+=1) - for (let y = 0z; y < r.h; y+=1) - put_at(x:int,y:int, tile_repr(tile_at(r,x,y))); -}; +def W = 50; +def H = 90; export fn main() void = { - const term = setup_term(); + const term = vt::open(); + defer vt::close(term); - const tiles: [100]tile = [floor...]; - const room = room { w=10, h=10, tiles=tiles }; + vt::enablealt(term)!; + vt::disablecur(term)!; - for (let i = 0z; i < 10; i+=1) { - set_tile_at(room, 0, i, wall); - set_tile_at(room, 9, i, wall); + const tiles: [W*H]tile = [floor...]; + const room = room { w=W, h=H, tiles=tiles }; + + for (let i = 0z; i < W; i+=1) { set_tile_at(room, i, 0, wall); - set_tile_at(room, i, 9, wall); + set_tile_at(room, i, H-1, wall); + }; + for (let i = 0z; i < H; i+=1) { + set_tile_at(room, 0, i, wall); + set_tile_at(room, W-1, i, wall); }; let rand = random::init(12345); for (let i = 0; i < 6; i+=1) { - const x = (random::u64n(&rand, 9)+1): size; - const y = (random::u64n(&rand, 9)+1): size; + const x = (random::u64n(&rand, W-2)+1): size; + const y = (random::u64n(&rand, H-2)+1): size; set_tile_at(room, x,y, wall); }; - - - let player = player { x=1, y=1 }; + const playerpen = vt::newpen(vt::color::BLACK, vt::color::RED); + const playerstr = vt::tag(playerpen, "@"); + for (true) { - clear(); + vt::clear(term)!; draw_room(room); - put_at(player.x, player.y, "@"); + vt::move(term, player.y:uint+1, player.x:uint+1)!; + vt::print(term, playerstr)!; const c = get_key(): rune; let nx = player.x; let ny = player.y; @@ -85,7 +91,6 @@ export fn main() void = { }; }; - finish_term(&term); }; diff --git a/ui.ha b/ui.ha deleted file mode 100644 index 31a1865..0000000 --- a/ui.ha +++ /dev/null @@ -1,39 +0,0 @@ -use fmt; -use io; -use os; -use unix::tty; - -fn clear() void = { - fmt::fprint(os::stdout_file, "\x1b[H\x1b[2J\x1b[3J")!; -}; - -fn put_at(x: int, y: int, s: str) void = { - // 0 indexed! - fmt::fprintf(os::stdout_file, "\x1b[{};{}H{}", y+1,x+1, s)!; -}; - -fn get_key() u8 = { - // bad - let b = [0u8]; - io::read(os::stdin_file, b)!; - return b[0]; -}; - -fn setup_term() tty::termios = { - const term = tty::termios_query(os::stdin_file)!; - tty::makeraw(&term)!; - - // alternate screen, hide cursor - fmt::fprint(os::stdout_file, "\x1b[?1049h\x1b[?25l")!; - - clear(); - - return term; -}; - -fn finish_term(term: *tty::termios) void = { - // normal screen, show cursor - fmt::fprint(os::stdout_file, "\x1b[?1049l\x1b[?25h")!; - - tty::termios_restore(term); -}; -- cgit v1.2.3