survivor

graphical action game for the linux terminal
git clone https://git.ce9e.org/survivor.git

commit
b8790d66679c69ab2ce177672d989eb2638db641
parent
b474cbf2e75d6c81d8843012078637de0321e02e
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2023-02-19 12:41
refactor term: clearly separate stuff that should come from terminfo

Diffstat

M src/term.rs 39 ++++++++++++++++++++++-----------------

1 files changed, 22 insertions, 17 deletions


diff --git a/src/term.rs b/src/term.rs

@@ -10,20 +10,25 @@ fn get_terminal_size() -> (usize, usize) {
   10    10     return (w[1] as usize, w[0] as usize);
   11    11 }
   12    12 
   13    -1 fn toggle_cursor(show: bool) {
   14    -1     if show {
   -1    13 mod ti {
   -1    14     pub fn cnorm() {
   15    15         print!("\x1b[?25h");
   16    -1     } else {
   -1    16     }
   -1    17     pub fn civis() {
   17    18         print!("\x1b[?25l");
   18    19     }
   19    -1 }
   20    -1 
   21    -1 fn set_bg(color: [u8; 3]) {
   22    -1     print!("\x1b[48;2;{};{};{}m", color[0], color[1], color[2]);
   23    -1 }
   24    -1 
   25    -1 fn set_fg(color: [u8; 3]) {
   26    -1     print!("\x1b[38;2;{};{};{}m", color[0], color[1], color[2]);
   -1    20     pub fn cup(x: usize, y: usize) {
   -1    21         print!("\x1b[{};{}H", x + 1, y + 1);
   -1    22     }
   -1    23     pub fn setab(color: [u8; 3]) {
   -1    24         print!("\x1b[48;2;{};{};{}m", color[0], color[1], color[2]);
   -1    25     }
   -1    26     pub fn setaf(color: [u8; 3]) {
   -1    27         print!("\x1b[38;2;{};{};{}m", color[0], color[1], color[2]);
   -1    28     }
   -1    29     pub fn sgr0() {
   -1    30         println!("\x1b[0m");
   -1    31     }
   27    32 }
   28    33 
   29    34 fn block6(block: u32) -> char {
@@ -87,7 +92,7 @@ pub struct Screen {
   87    92 
   88    93 impl Drop for Screen {
   89    94     fn drop(&mut self) {
   90    -1         toggle_cursor(true);
   -1    95         ti::cnorm();
   91    96     }
   92    97 }
   93    98 
@@ -111,8 +116,8 @@ impl Screen {
  111   116         let mut prev_bg = [0x00, 0x00, 0x00];
  112   117         let mut prev_fg = [0xff, 0xff, 0xff];
  113   118 
  114    -1         print!("\x1b[H");
  115    -1         toggle_cursor(false);
   -1   119         ti::cup(0, 0);
   -1   120         ti::civis();
  116   121         for y in 0..(self.height / 3) {
  117   122             for x in 0..(self.width / 2) {
  118   123                 let (block, bg, fg) = get_block([
@@ -124,11 +129,11 @@ impl Screen {
  124   129                     self.pixels[y * 3 + 2][x * 2 + 1],
  125   130                 ]);
  126   131                 if bg != prev_bg {
  127    -1                     set_bg(bg);
   -1   132                     ti::setab(bg);
  128   133                     prev_bg = bg;
  129   134                 }
  130   135                 if fg != prev_fg {
  131    -1                     set_fg(fg);
   -1   136                     ti::setaf(fg);
  132   137                     prev_fg = fg;
  133   138                 }
  134   139                 print!("{}", block6(block));
@@ -138,7 +143,7 @@ impl Screen {
  138   143             }
  139   144         }
  140   145 
  141    -1         println!("\x1b[0m");
   -1   146         ti::sgr0();
  142   147     }
  143   148 
  144   149     pub fn convert_x(&self, x: f32) -> i64 {