summaryrefslogtreecommitdiff
path: root/frontends/ncurses
diff options
context:
space:
mode:
authorAndrew Opalach <andrew@akon.city> 2019-04-10 15:14:00 -0400
committerAndrew Opalach <andrew@akon.city> 2019-04-10 15:14:00 -0400
commit60699af63f92f43cc4b4b9e3050fcdd2a8468281 (patch)
tree20a18af2774daa43ae7f4352dd032a24f7e06075 /frontends/ncurses
parent4527bc20241068731c62101d0467d416119ec0c4 (diff)
downloadcetris-60699af63f92f43cc4b4b9e3050fcdd2a8468281.tar.gz
cetris-60699af63f92f43cc4b4b9e3050fcdd2a8468281.tar.bz2
cetris-60699af63f92f43cc4b4b9e3050fcdd2a8468281.zip
add opengl fronend, remove sdl frontend, refactor build system
Diffstat (limited to 'frontends/ncurses')
-rw-r--r--frontends/ncurses/meson.build8
-rw-r--r--frontends/ncurses/ncurses_ui.c174
2 files changed, 182 insertions, 0 deletions
diff --git a/frontends/ncurses/meson.build b/frontends/ncurses/meson.build
new file mode 100644
index 0000000..dafa569
--- /dev/null
+++ b/frontends/ncurses/meson.build
@@ -0,0 +1,8 @@
+src = ['ncurses_ui.c']
+
+ncurses = dependency('ncursesw')
+
+executable('cetris_curses', src,
+ dependencies: [ncurses, cetris],
+ include_directories: cetris_inc,
+ install: false)
diff --git a/frontends/ncurses/ncurses_ui.c b/frontends/ncurses/ncurses_ui.c
new file mode 100644
index 0000000..11e3f67
--- /dev/null
+++ b/frontends/ncurses/ncurses_ui.c
@@ -0,0 +1,174 @@
+#include <stdlib.h>
+#include <string.h>
+#ifdef _WIN32
+#include "_win\pdcurses\curses.h"
+#else
+#include <ncurses.h>
+#endif
+#include <locale.h>
+
+#include "cetris.h"
+
+#ifdef ASCII_COMPATIBLE
+#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"\
+ " \\--------------------/"
+#else
+#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"\
+ " ┗━━━━━━━━━━━━━━━━━━━━┛ "
+#endif
+
+#define X_OFFSET 8
+#define Y_OFFSET 0
+
+struct cetris_game game;
+
+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() {
+ mvaddstr(0, 0, PLAY_FIELD_STR);
+ for (int x = 0; x < CETRIS_BOARD_X; x++) {
+ for (int y = CETRIS_BOARD_VISABLE; y < CETRIS_BOARD_Y; y++) {
+ if (game.board[x][y].occupied) {
+ attron(COLOR_PAIR(game.board[x][y].c));
+ if (game.board[x][y].remove_tick > 0) {
+ if (game.tick % 2 == 0) {
+ mvaddstr((y - CETRIS_BOARD_VISABLE) + 1, x * 2 + X_OFFSET, BLOCK);
+ }
+ } else {
+ mvaddstr((y - CETRIS_BOARD_VISABLE) + 1, x * 2 + X_OFFSET, BLOCK);
+ }
+ attroff(COLOR_PAIR(game.board[x][y].c));
+ }
+ }
+
+ int index = game.current_index;
+ attron(COLOR_PAIR(game.piece_queue[index].c));
+ for (int x = 0; x < 4; x++) {
+ for (int y = 0; y < 4; y++) {
+ if (game.piece_queue[index].m[y][x]) {
+ if (game.piece_queue[index].t == I) {
+ mvaddstr(6 + y, (x * 2) + 36, BLOCK);
+ } else {
+ mvaddstr(6 + y, (x * 2) + 37, BLOCK);
+ }
+ }
+ }
+ }
+ attroff(COLOR_PAIR(game.piece_queue[index].c));
+
+ attron(A_BOLD);
+
+ char score[50];
+ sprintf(score, "%li", game.score);
+ mvaddstr(1, (39 + X_OFFSET) - strlen(score), score);
+
+ char level[20];
+ sprintf(level, "%i", game.level);
+ mvaddstr(3, 37, "Level");
+ mvaddstr(4, 40 - strlen(level), level);
+
+ if (game.game_over) {
+ mvaddstr(10, 6 + X_OFFSET, "GAME OVER");
+ mvaddstr(11, 5 + X_OFFSET, "r to restart");
+ }
+
+ attroff(A_BOLD);
+ }
+}
+
+int main(void) {
+ curses_init();
+
+ 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); break;
+ case 'r':
+ if (game.game_over) {
+ init_game(&game);
+ }
+ break;
+ default:
+ clear_held_key(&game);
+ }
+ update_game_tick(&game);
+ erase();
+ draw_board();
+ refresh();
+ }
+ return 0;
+}
+