From dc501842702c9259694e98815474cf30b59e25a1 Mon Sep 17 00:00:00 2001 From: NetherEran Date: Thu, 10 Sep 2020 11:14:45 +0200 Subject: [PATCH] optimized drawing of player and balls, fixing flickering on some terminals --- .gitignore | 2 ++ ball.c | 5 +++++ field.c | 22 ++++++++++++++++++++++ field.h | 2 ++ game.c | 9 ++------- game.h | 3 +++ main.c | 1 + player.c | 3 ++- 8 files changed, 39 insertions(+), 8 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e915804 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +snek +*.o diff --git a/ball.c b/ball.c index 81b1c74..a12a486 100644 --- a/ball.c +++ b/ball.c @@ -9,6 +9,8 @@ void init_ball(BALL* b, int x, int y, char dir, int value) { b -> position.x = x; b -> position.y = y; + b -> old_position.x = x; + b -> old_position.y = y; b -> direction = dir; b -> value = value; b -> color = value + 2; @@ -80,6 +82,9 @@ void move_ball(FIELD* f, int i) { POINT direction_vector; direction_to_point(&direction_vector, b -> direction); add_to_point(&direction_vector, pos); + + b -> old_position.x = b -> position.x; + b -> old_position.y = b -> position.y; bounce(b, f); diff --git a/field.c b/field.c index 621a274..bcaac00 100644 --- a/field.c +++ b/field.c @@ -96,3 +96,25 @@ void redraw_field(FIELD* f) { draw_player(f); wrefresh(f -> win); } + +void update_ballpos(FIELD* f) { + for (int i = 0; i < f -> ball_count; i++) { + BALL* b = f -> balls[i]; + wattrset(f -> win, COLOR_PAIR(7)); + mvwprintw(f -> win, b -> old_position.y, b -> old_position.x * 2, " "); + wattrset(f -> win, COLOR_PAIR(b -> color)); + mvwprintw(f -> win, b -> position.y, b -> position.x * 2, " "); + } + wrefresh(f -> win); +} +void update_playerpos(FIELD* f) { + PLAYER* player = f -> player; + wattrset(f -> win, COLOR_PAIR(7)); + mvwprintw(f -> win, player -> old_tail -> y, player -> old_tail -> x * 2, " "); + wattrset(f -> win, COLOR_PAIR(2)); + mvwprintw(f -> win, + player -> body[player -> head_ptr] -> y, + player -> body[player -> head_ptr] -> x * 2, + " "); + wrefresh(f -> win); +} diff --git a/field.h b/field.h index 48a0d46..613f2ea 100644 --- a/field.h +++ b/field.h @@ -5,6 +5,8 @@ void redraw_field(FIELD* f); void init_field(FIELD* f, int width, int height, GAME_STATE* state); void add_ball(FIELD* f, int max_value); void destroy_field(FIELD* f); +void update_ballpos(FIELD* f); +void update_playerpos(FIELD* f); #endif diff --git a/game.c b/game.c index 589a8c7..09a37d4 100644 --- a/game.c +++ b/game.c @@ -54,7 +54,6 @@ void play_game(GAME_STATE* state) { FD_ZERO(&inputs); FD_SET(0, &inputs); - int redraw = 0; char player_tick = 0; while(1) { if (state -> status) { @@ -69,23 +68,19 @@ void play_game(GAME_STATE* state) { int sel_ret = select(FD_SETSIZE, &test_fds, (fd_set *) 0, (fd_set *) 0, &timeout); - redraw = 0; if (sel_ret == 0) { player_tick = (player_tick + 1) % 3; - redraw = 1; move_balls(f); + update_ballpos(f); if (!player_tick) { move_player(f); - redraw = 1; + update_playerpos(f); } timeout.tv_sec = 0; timeout.tv_usec = 50000; } - if (redraw) { - redraw_field(f); - } } free(f); diff --git a/game.h b/game.h index 941e0b6..dabea76 100644 --- a/game.h +++ b/game.h @@ -10,6 +10,7 @@ typedef struct { typedef struct { char direction; POINT position; + POINT old_position; int value; char color; int move_tick; @@ -21,6 +22,8 @@ typedef struct { int next_head_dir_x; int next_head_dir_y; + + POINT* old_tail; POINT** body; int head_ptr; diff --git a/main.c b/main.c index eb06b27..fe98638 100644 --- a/main.c +++ b/main.c @@ -20,6 +20,7 @@ int main(int argc, char** argv) { init_pair(4, COLOR_WHITE, COLOR_RED); init_pair(5, COLOR_WHITE, COLOR_BLUE); init_pair(6, COLOR_WHITE, COLOR_YELLOW); + init_pair(7, COLOR_WHITE, COLOR_BLACK); srandom(time( (time_t *) 0 )); diff --git a/player.c b/player.c index 2191d95..bfbe998 100644 --- a/player.c +++ b/player.c @@ -64,7 +64,8 @@ void move_player(FIELD* field) { POINT* oldhead = player -> body[player -> head_ptr]; player -> head_ptr = (player -> head_ptr + 1) % player -> length; - free(player -> body[player -> head_ptr]); + free(player -> old_tail); + player -> old_tail = player -> body[player -> head_ptr]; POINT* newhead = malloc(sizeof(POINT)); newhead -> x = oldhead -> x + player -> head_dir_x; newhead -> y = oldhead -> y + player -> head_dir_y;