optimized drawing of player and balls, fixing flickering on some terminals

master
NetherEran 2020-09-10 11:14:45 +02:00
parent 7a4a9794b4
commit dc50184270
8 changed files with 39 additions and 8 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
snek
*.o

5
ball.c
View File

@ -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);

22
field.c
View File

@ -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);
}

View File

@ -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

9
game.c
View File

@ -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);

3
game.h
View File

@ -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;

1
main.c
View File

@ -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 ));

View File

@ -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;