From 60699af63f92f43cc4b4b9e3050fcdd2a8468281 Mon Sep 17 00:00:00 2001 From: Andrew Opalach Date: Wed, 10 Apr 2019 15:14:00 -0400 Subject: add opengl fronend, remove sdl frontend, refactor build system --- frontends/ncurses/meson.build | 8 ++ frontends/ncurses/ncurses_ui.c | 174 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 182 insertions(+) create mode 100644 frontends/ncurses/meson.build create mode 100644 frontends/ncurses/ncurses_ui.c (limited to 'frontends/ncurses') 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 +#include +#ifdef _WIN32 +#include "_win\pdcurses\curses.h" +#else +#include +#endif +#include + +#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; +} + -- cgit v1.2.3-101-g0448