From cce421525f25aa33971cc70facce170c978e2979 Mon Sep 17 00:00:00 2001 From: Cory Bloor Date: Thu, 8 Jun 2017 19:43:40 -0600 Subject: [PATCH] Improve screen clear and refactor a little --- main.cxx | 21 ++++++++++----------- misc/terminal.cxx | 4 ++++ misc/terminal.h | 1 + 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/main.cxx b/main.cxx index b1e11ad..c4e6233 100644 --- a/main.cxx +++ b/main.cxx @@ -107,6 +107,7 @@ void refresh_marker_counts() { bool in_bounds = x > 0 && x < (int)X && y > 0 && y < (int)Y; if (in_bounds) { if (g_sink[y][x]) { + // remove marker by swapping with back and resizing g_markers[i--] = g_markers[--g_markers_length]; } else { g_marker_count[y][x]++; @@ -1068,17 +1069,14 @@ void draw_rows(struct buffer* buf) { } } -void refresh_screen(buffer* buf) { +void draw(buffer* buf) { buffer_clear(buf); - - clear_screen(buf); - + reposition_cursor(buf); draw_rows(buf); - buffer_write(buf); } -void process_keypress() { +bool process_keypress() { char c = '\0'; if (read(STDIN_FILENO, &c, 1) == -1 && errno != EAGAIN && errno != EINTR) { die("read"); @@ -1090,8 +1088,9 @@ void process_keypress() { g_simulate_steps++; } else if (c == 'q') { u_clear_screen(); - exit(0); + return false; } + return true; } args_t parse_args(int argc, char** argv) { @@ -1135,17 +1134,17 @@ int main(int argc, char** argv) { set_window_size_handler(&handle_window_size_changed); enable_raw_mode(); + u_clear_screen(); buffer buf = { 0, 0 }; sim_init(in); + draw(&buf); - while (1) { - refresh_screen(&buf); - process_keypress(); + while (process_keypress()) { sim_step(); + draw(&buf); } buffer_free(&buf); - return 0; } diff --git a/misc/terminal.cxx b/misc/terminal.cxx index 48771ce..8eb5e65 100644 --- a/misc/terminal.cxx +++ b/misc/terminal.cxx @@ -22,6 +22,10 @@ void clear_screen(buffer* buf) { buffer_append(buf, "\x1b[H", 3); // reposition cursor } +void reposition_cursor(buffer* buf) { + buffer_append(buf, "\x1b[H", 3); // reposition cursor +} + void hide_cursor(buffer* buf) { buffer_append(buf, "\x1b[?25l", 6); } diff --git a/misc/terminal.h b/misc/terminal.h index 31df1d7..b60e551 100644 --- a/misc/terminal.h +++ b/misc/terminal.h @@ -16,6 +16,7 @@ void die(const char* msg); void u_clear_screen(); void clear_screen(buffer* buf); +void reposition_cursor(buffer* buf); void hide_cursor(buffer* buf); void show_cursor(buffer* buf); void enable_raw_mode();