code wiki / _hdl_build / nx_platformer_play.nx

nx_platformer_play.nx source

↩ module page · 66 lines · 2653 B

1// nx_platformer_play.nx -- the PLATFORMER as a native, pure-Nishi, terminal-playable game. Bottom-up: integer 2// no-float logic (nx_wasm_platformer) + ANSI truecolor render (nx_platformer_term) + raw-mode keyboard, all via 3// sys_* -- NO blit, NO browser, NO JS, NO third-party. The hero auto-runs; press SPACE/W to jump the floating 4// coins, reach the cyan exit. Run it in a terminal: builds to /tmp/nx_platformer_play.sov.elf (run directly). 5// license_tier: ORIGINAL 6import "nx_platformer_term.nx" 7import "nx_wasm_platformer.nx" 8import "nx_syscalls.nx" 9const K_MAGIC_2097152: i64 = 2097152 10const K_MAGIC_131072: i64 = 131072 11const K_MAGIC_1500: i64 = 1500 12 13// raw, NON-BLOCKING terminal (VMIN=0/VTIME=0 -> sys_read returns 0 when no key). 1 if a tty, 0 otherwise. 14func praw_on(orig: *u8, raw: *u8) -> i64 { 15 let g: i64 = sys_ioctl(0, 0x5401, orig as i64) 16 if g != 0 { return 0 } 17 var i: i64 = 0 18 while i < 60 { raw[i] = orig[i]; i = i + 1 } 19 var lf: i64 = raw[12] as i64 20 lf = lf & (255 - 2) // ~ICANON 21 lf = lf & (255 - 8) // ~ECHO 22 raw[12] = lf as u8 23 raw[17 + 6] = 0 as u8 // VMIN = 0 24 raw[17 + 5] = 0 as u8 // VTIME = 0 25 sys_ioctl(0, 0x5402, raw as i64) 26 return 1 27} 28func praw_off(orig: *u8) -> i64 { sys_ioctl(0, 0x5402, orig as i64); return 0 } 29 30func main(argc: i64, argv: *i64) -> i64 { 31 let base: i64 = sys_mmap(K_MAGIC_2097152) as i64 32 init_impl(base) 33 let grid: *i64 = sys_mmap(8 * ROWS * COLS) as *i64 34 let buf: *u8 = sys_mmap(K_MAGIC_131072) 35 let orig: *u8 = sys_mmap(128); let raw: *u8 = sys_mmap(128) 36 let tty: i64 = praw_on(orig, raw) 37 let cls: *u8 = sys_mmap(8); cls[0] = 27 as u8; cls[1] = 91 as u8; cls[2] = 50 as u8; cls[3] = 74 as u8 // ESC[2J 38 sys_write(1, cls, 4) 39 let ib: *u8 = sys_mmap(8) 40 var running: i64 = 1 41 var frames: i64 = 0 42 var winhold: i64 = 0 43 while running == 1 { 44 pt_frame(base, grid) 45 let n: i64 = pt_emit(base, grid, buf) 46 sys_write(1, buf, n) 47 var cmd: i64 = 2 48 let r: i64 = sys_read(0, ib, 1) 49 if r > 0 { 50 let c: i64 = ib[0] as i64 51 if c == 113 { running = 0 } 52 if c == 81 { running = 0 } 53 if c == 32 { cmd = 0 } 54 if c == 119 { cmd = 0 } 55 } 56 if running == 1 { 57 if sget(base, 7) == 0 { tick_impl(base, cmd) } else { winhold = winhold + 1; if winhold > 24 { running = 0 } } 58 sys_sleep_ms(60) 59 frames = frames + 1 60 if frames > K_MAGIC_1500 { running = 0 } 61 } 62 } 63 if tty == 1 { praw_off(orig) } 64 sys_write(1, "\n" as *u8, 1) 65 return 0 66}