Skip to content

Commit

Permalink
Improve screen clear and refactor a little
Browse files Browse the repository at this point in the history
  • Loading branch information
cgmb committed Jun 9, 2017
1 parent 2c9939e commit cce4215
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
21 changes: 10 additions & 11 deletions main.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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]++;
Expand Down Expand Up @@ -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");
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
}
4 changes: 4 additions & 0 deletions misc/terminal.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
1 change: 1 addition & 0 deletions misc/terminal.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit cce4215

Please sign in to comment.