From bd7bf65c626f9402176fcdeaef35379df4759535 Mon Sep 17 00:00:00 2001 From: nick black Date: Sat, 28 Mar 2020 00:11:00 -0400 Subject: [PATCH] colloquy: spin up a rust notcurses instance --- src/colloquy/src/main.rs | 42 ++++++++++++++++++++++++---------------- src/tetris/newpiece.h | 16 ++++++--------- 2 files changed, 31 insertions(+), 27 deletions(-) diff --git a/src/colloquy/src/main.rs b/src/colloquy/src/main.rs index 25cfbdaa2..1f321e154 100644 --- a/src/colloquy/src/main.rs +++ b/src/colloquy/src/main.rs @@ -1,3 +1,9 @@ +extern crate libnotcurses_sys as ffi; + +extern { + fn libc_stdout() -> *mut ffi::_IO_FILE; +} + fn main() { use clap::{load_yaml, App}; let yaml = load_yaml!("cli.yml"); @@ -9,21 +15,23 @@ fn main() { std::process::exit(1); } - let _ = libc::setlocale(libc::LC_ALL, std::ffi::CString::new("").unwrap().as_ptr()); - let opts: notcurses_options = libnotcurses-sys::notcurses_options { - inhibit_alternate_screen: true, - loglevel: 0, - termtype: std::ptr::null(), - retain_cursor: false, - suppress_banner: false, - no_winch_sighandler: false, - no_quit_sighandlers: false, - renderfp: std::ptr::null_mut(), - margin_t: 0, - margin_r: 0, - margin_b: 0, - margin_l: 0, - }; - let nc = notcurses_init(&opts, libc_stdout()); - notcurses_stop(nc); + unsafe{ + let _ = libc::setlocale(libc::LC_ALL, std::ffi::CString::new("").unwrap().as_ptr()); + let opts: ffi::notcurses_options = ffi::notcurses_options { + inhibit_alternate_screen: true, + loglevel: 0, + termtype: std::ptr::null(), + retain_cursor: false, + suppress_banner: false, + no_winch_sighandler: false, + no_quit_sighandlers: false, + renderfp: std::ptr::null_mut(), + margin_t: 0, + margin_r: 0, + margin_b: 0, + margin_l: 0, + }; + let nc = ffi::notcurses_init(&opts, libc_stdout()); + ffi::notcurses_stop(nc); + } } diff --git a/src/tetris/newpiece.h b/src/tetris/newpiece.h index d55e41acf..941829496 100644 --- a/src/tetris/newpiece.h +++ b/src/tetris/newpiece.h @@ -1,18 +1,15 @@ // tidx is an index into tetriminos. yoff and xoff are relative to the // terminal's origin. returns colored north-facing tetrimino on a plane. std::unique_ptr NewPiece() { - // "North-facing" tetrimino forms (form in which they are released from the - // top) are expressed in terms of two rows having between 2 and 4 columns. - // We map each game column to four columns and each game row to two rows. Each - // byte of the texture maps to one 4x4 component block (and wastes 7 bits). + // "North-facing" tetrimino forms (form in which they are released from the top) are expressed in terms of + // two rows having between 2 and 4 columns. We map each game column to four columns and each game row to two + // rows. Each byte of the texture maps to one 4x4 component block (and wastes 7 bits). static const struct tetrimino { unsigned color; const char* texture; } tetriminos[] = { // OITLJSZ - { 0xcbc900, "****"}, { 0x009caa, " ****"}, { 0x952d98, " * ***"}, - { 0xcf7900, " ****"}, { 0x0065bd, "* ***"}, { 0x69be28, " **** "}, - { 0xbd2939, "** **"} }; - + { 0xcbc900, "****"}, { 0x009caa, " ****"}, { 0x952d98, " * ***"}, { 0xcf7900, " ****"}, + { 0x0065bd, "* ***"}, { 0x69be28, " **** "}, { 0xbd2939, "** **"} }; const int tidx = random() % 7; const struct tetrimino* t = &tetriminos[tidx]; const size_t cols = strlen(t->texture); @@ -27,8 +24,7 @@ std::unique_ptr NewPiece() { n->set_fg(t->color); n->set_bg_alpha(CELL_ALPHA_TRANSPARENT); n->set_base(channels, 0, ""); - y = 0; - x = 0; + y = 0; x = 0; for(size_t i = 0 ; i < strlen(t->texture) ; ++i){ if(t->texture[i] == '*'){ if(n->putstr(y, x, "██") < 0){