survivor

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

commit
e777a08ce40785e65efd487b5d367ad3a0dbcd10
parent
7043641baad9549775e296105626e13470878791
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2023-03-30 03:35
refactor signal handlers

Diffstat

M src/main.rs 32 +++++++++++++++-----------------

1 files changed, 15 insertions, 17 deletions


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

@@ -18,20 +18,18 @@ const BLACK: [u8; 3] = [0x00, 0x00, 0x00];
   18    18 const RED: [u8; 3] = [0xff, 0x00, 0x00];
   19    19 const BLUE: [u8; 3] = [0x00, 0x00, 0xff];
   20    20 
   21    -1 static RUNNING: AtomicBool = AtomicBool::new(true);
   -1    21 static NEED_QUIT: AtomicBool = AtomicBool::new(false);
   22    22 static NEED_RESIZE: AtomicBool = AtomicBool::new(false);
   23    23 static NEED_STOP: AtomicBool = AtomicBool::new(false);
   24    24 
   25    -1 fn quit(_sig: libc::c_int) {
   26    -1     RUNNING.store(false, Ordering::Relaxed);
   27    -1 }
   28    -1 
   29    -1 fn resize(_sig: libc::c_int) {
   30    -1     NEED_RESIZE.store(true, Ordering::Relaxed);
   31    -1 }
   32    -1 
   33    -1 fn stop(_sig: libc::c_int) {
   34    -1     NEED_STOP.store(true, Ordering::Relaxed);
   -1    25 fn handle_signal(sig: libc::c_int) {
   -1    26     let var = match sig {
   -1    27         libc::SIGINT => &NEED_QUIT,
   -1    28         libc::SIGWINCH => &NEED_RESIZE,
   -1    29         libc::SIGTSTP => &NEED_STOP,
   -1    30         _ => unreachable!(),
   -1    31     };
   -1    32     var.store(true, Ordering::Relaxed);
   35    33 }
   36    34 
   37    35 fn render_bar(screen: &mut term::Screen, value: f32, y0: usize, color: [u8; 3]) {
@@ -72,13 +70,13 @@ fn main() {
   72    70     let mut screen = term::Screen::new();
   73    71     let mut game = game::Game::new();
   74    72 
   75    -1     signal(libc::SIGINT, quit as libc::sighandler_t);
   76    -1     signal(libc::SIGWINCH, resize as libc::sighandler_t);
   77    -1     signal(libc::SIGTSTP, stop as libc::sighandler_t);
   -1    73     signal(libc::SIGINT, handle_signal as libc::sighandler_t);
   -1    74     signal(libc::SIGWINCH, handle_signal as libc::sighandler_t);
   -1    75     signal(libc::SIGTSTP, handle_signal as libc::sighandler_t);
   78    76 
   79    77     let mut time0 = time::Instant::now();
   80    78 
   81    -1     while RUNNING.load(Ordering::Relaxed) {
   -1    79     while !NEED_QUIT.load(Ordering::Relaxed) {
   82    80         let mut time1 = time::Instant::now();
   83    81         let dt = (time1 - time0).as_secs_f32();
   84    82 
@@ -95,7 +93,7 @@ fn main() {
   95    93                     game.player.face = game::Dir::Right
   96    94                 }
   97    95                 b' ' => game.player.dir = None,
   98    -1                 b'q' => quit(0),
   -1    96                 b'q' => NEED_QUIT.store(true, Ordering::Relaxed),
   99    97                 _ => {}
  100    98             }
  101    99         }
@@ -111,7 +109,7 @@ fn main() {
  111   109             // when SIGCONT is received
  112   110             screen.init();
  113   111             input.cbreak();
  114    -1             signal(libc::SIGTSTP, stop as libc::sighandler_t);
   -1   112             signal(libc::SIGTSTP, handle_signal as libc::sighandler_t);
  115   113             time1 = time::Instant::now();
  116   114             NEED_STOP.store(false, Ordering::Relaxed);
  117   115         }