summaryrefslogtreecommitdiff
path: root/frontend
diff options
context:
space:
mode:
authorAndrew Opalach <andrew@akon.city> 2019-03-04 17:28:05 -0500
committerAndrew Opalach <andrew@akon.city> 2019-03-04 17:28:05 -0500
commit7c737f4d1d21a8c666bc8b868f4f29cfe9a3cbab (patch)
treec948c3bbded6ff4fb900633821560cb3326858ca /frontend
parent90a6b134231db335ae5868d04078f23ed6844ce4 (diff)
downloadcetris-7c737f4d1d21a8c666bc8b868f4f29cfe9a3cbab.tar.gz
cetris-7c737f4d1d21a8c666bc8b868f4f29cfe9a3cbab.tar.bz2
cetris-7c737f4d1d21a8c666bc8b868f4f29cfe9a3cbab.zip
restructure project and add tspin checking
Diffstat (limited to 'frontend')
-rw-r--r--frontend/ncurses/ncurses_ui.c101
1 files changed, 101 insertions, 0 deletions
diff --git a/frontend/ncurses/ncurses_ui.c b/frontend/ncurses/ncurses_ui.c
new file mode 100644
index 0000000..1ae07b2
--- /dev/null
+++ b/frontend/ncurses/ncurses_ui.c
@@ -0,0 +1,101 @@
+#include <stdlib.h>
+#include <ncurses.h>
+#include <locale.h>
+
+#include "cetris.h"
+
+#define BLOCK "[]"
+#define PLAY_FIELD_STR " /--------------------\\ /--------\\\n"\
+ " | | | |\n"\
+ " | | \\--------/\n"\
+ " | | \n"\
+ " | | \n"\
+ " | | \n"\
+ " | | \n"\
+ " | | \n"\
+ " | | \n"\
+ " | | \n"\
+ " | | \n"\
+ " | | \n"\
+ " | | \n"\
+ " | | \n"\
+ " | | \n"\
+ " | | \n"\
+ " | | \n"\
+ " | | \n"\
+ " | | \n"\
+ " | | \n"\
+ " | | \n"\
+ " \\--------------------/"
+
+#define X_OFFSET 8
+#define Y_OFFSET 0
+
+void curses_init() {
+ setlocale(LC_CTYPE, "");
+ initscr();
+ noecho();
+ keypad(stdscr, TRUE);
+ curs_set(0);
+ timeout(1000 / CETRIS_HZ);
+
+ start_color();
+ init_pair(COLOR_NONE, COLOR_BLACK, COLOR_BLACK);
+ init_pair(COLOR_O, COLOR_MAGENTA, COLOR_BLACK);
+ init_pair(COLOR_Z, COLOR_RED, COLOR_BLACK);
+ init_pair(COLOR_S, COLOR_CYAN, COLOR_BLACK);
+ init_pair(COLOR_T, COLOR_WHITE, COLOR_BLACK);
+ init_pair(COLOR_L, COLOR_GREEN, COLOR_BLACK);
+ init_pair(COLOR_I, COLOR_BLUE, COLOR_BLACK);
+ init_pair(COLOR_J, COLOR_YELLOW, COLOR_BLACK);
+ clear();
+}
+
+void draw_board(struct cetris_game* g) {
+ mvaddstr(0, 0, PLAY_FIELD_STR);
+ for (int x = 0; x < BOARD_X; x++) {
+ for (int y = 0; y < BOARD_Y; y++) {
+ if (g->board[x][y].occupied) {
+ attron(COLOR_PAIR(g->board[x][y].c));
+ if (g->board[x][y].remove_tick > 0) {
+ if (g->tick % 2 == 0) {
+ mvaddstr(y + 1, x * 2 + X_OFFSET, BLOCK);
+ }
+ } else {
+ mvaddstr(y + 1, x * 2 + X_OFFSET, BLOCK);
+ }
+ attroff(COLOR_PAIR(g->board[x][y].c));
+ }
+ }
+ }
+}
+
+int main(void) {
+ curses_init();
+
+ struct cetris_game game;
+ init_game(&game);
+
+ int c;
+ while(1) {
+ c = getch();
+ switch (c) {
+ case 'q': endwin(); exit(1);
+ case KEY_LEFT:
+ move_left(&game); break;
+ case KEY_RIGHT:
+ move_right(&game); break;
+ case KEY_DOWN:
+ move_down(&game); break;
+ case KEY_UP:
+ rotate_clockwise(&game); break;
+ case ' ':
+ move_hard_drop(&game);
+ }
+ update_game_tick(&game);
+ draw_board(&game);
+ refresh();
+ }
+ return 0;
+}
+