survivor

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

commit
b146e875b7faeda00f152ba26d5237612464b0bb
parent
6e598bf072ddeb51fa5175e301ab3cb11d60a621
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2023-02-21 05:15
resize screen in SIGWINCH

Diffstat

M README.md 1 -
M src/main.rs 13 ++++++++++++-
M src/term.rs 18 ++++++++++--------

3 files changed, 22 insertions, 10 deletions


diff --git a/README.md b/README.md

@@ -81,5 +81,4 @@ The aspect ratio of pixels depends on the font. You can adjust
   81    81 -	Improve balancing
   82    82 -	Add tarain
   83    83 -	More enemy waves
   84    -1 -	Adapt to terminal size change
   85    84 -	Sound

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

@@ -18,9 +18,14 @@ const RED: [u8; 3] = [0xff, 0x00, 0x00];
   18    18 const BLUE: [u8; 3] = [0x00, 0x00, 0xff];
   19    19 
   20    20 static RUNNING: AtomicBool = AtomicBool::new(true);
   -1    21 static NEED_RESIZE: AtomicBool = AtomicBool::new(false);
   21    22 
   22    23 fn quit(_sig: libc::c_int) {
   23    -1     RUNNING.fetch_and(false, Ordering::Relaxed);
   -1    24     RUNNING.store(false, Ordering::Relaxed);
   -1    25 }
   -1    26 
   -1    27 fn resize(_sig: libc::c_int) {
   -1    28     NEED_RESIZE.store(true, Ordering::Relaxed);
   24    29 }
   25    30 
   26    31 fn render_bar(screen: &mut term::Screen, value: f32, y0: usize, color: [u8; 3]) {
@@ -50,6 +55,7 @@ fn main() {
   50    55 
   51    56     unsafe {
   52    57         libc::signal(libc::SIGINT, quit as libc::sighandler_t);
   -1    58         libc::signal(libc::SIGWINCH, resize as libc::sighandler_t);
   53    59     }
   54    60 
   55    61     let mut time0 = time::Instant::now();
@@ -76,6 +82,11 @@ fn main() {
   76    82             }
   77    83         }
   78    84 
   -1    85         if NEED_RESIZE.load(Ordering::Relaxed) {
   -1    86             screen.resize();
   -1    87             NEED_RESIZE.store(false, Ordering::Relaxed);
   -1    88         }
   -1    89 
   79    90         let mut win = win::Window {
   80    91             width: screen.width,
   81    92             height: screen.height - 6,

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

@@ -105,16 +105,18 @@ impl Drop for Screen {
  105   105 
  106   106 impl Screen {
  107   107     pub fn new() -> Screen {
  108    -1         let (w, h) = get_terminal_size();
  109    -1         let width = w * 2;
  110    -1         let height = (h - 1) * 3;
   -1   108         let mut screen = Screen { width: 0, height: 0, pixels: vec![] };
   -1   109         screen.resize();
  111   110         ti::civis();
  112   111         ti::ed();
  113    -1         return Screen {
  114    -1             width: width,
  115    -1             height: height,
  116    -1             pixels: vec![vec![[0, 0, 0]; width]; height],
  117    -1         };
   -1   112         return screen;
   -1   113     }
   -1   114 
   -1   115     pub fn resize(&mut self) {
   -1   116         let (w, h) = get_terminal_size();
   -1   117         self.width = w * 2;
   -1   118         self.height = (h - 1) * 3;
   -1   119         self.pixels = vec![vec![[0, 0, 0]; self.width]; self.height];
  118   120     }
  119   121 
  120   122     pub fn set(&mut self, x: usize, y: usize, color: [u8; 3]) {