2048-homework/src/tui.c

58 lines
1.4 KiB
C
Raw Normal View History

2021-11-15 08:57:27 +00:00
#include "tui.h"
#include <stdio.h>
#include <curses.h>
2021-11-15 15:33:31 +00:00
const uint32_t space_between_cells_x = 6;
const uint32_t space_between_cells_y = 2;
2021-11-15 10:00:37 +00:00
2021-11-15 08:57:27 +00:00
// entering and leaving screen using ANSI escape sequence
void tui_init() {
initscr();
cbreak();
noecho();
nonl();
intrflush(stdscr, FALSE);
keypad(stdscr, TRUE);
}
void tui_destroy() {}
void tui_loop(Game *game) {
while(true){
2021-11-15 15:33:31 +00:00
// printing the game state is fun
clear();
2021-11-15 21:20:16 +00:00
for (size_t i = 0; i < game->field_size_y; i++) {
for (size_t j = 0; j < game->field_size_x; j++) {
2021-11-15 15:33:31 +00:00
mvprintw(i * space_between_cells_y, j * space_between_cells_x, "%d", game->field[i][j]);
}
}
2021-11-15 08:57:27 +00:00
char input = getch();
2021-11-15 10:00:37 +00:00
Direction dir = NoDirection;
// these are not magic numbers
// these are just codes for arrows,
// numpad and Vim bindings
switch (input) {
case 3:
case 107:
case 56:
dir = Up;
break;
case 2:
case 106:
case 50:
dir = Down;
break;
case 4:
case 104:
case 52:
dir = Left;
break;
case 5:
case 108:
case 54:
dir = Right;
break;
}
game_move(game, dir);
2021-11-15 08:57:27 +00:00
}
}