diff options
| author | 2019-02-24 16:03:38 -0500 | |
|---|---|---|
| committer | 2019-02-24 16:03:38 -0500 | |
| commit | af3f5f149ae74d149fd0f807d6b813ab62340cd0 (patch) | |
| tree | 7a9b6caa9a5ecd4659146020a951c502d57a6ea3 | |
| download | cetris-af3f5f149ae74d149fd0f807d6b813ab62340cd0.tar.gz cetris-af3f5f149ae74d149fd0f807d6b813ab62340cd0.tar.bz2 cetris-af3f5f149ae74d149fd0f807d6b813ab62340cd0.zip | |
initial commit
| -rw-r--r-- | .gitignore | 1 | ||||
| -rw-r--r-- | cetris.c | 15 | ||||
| -rw-r--r-- | cetris.h | 67 | ||||
| -rw-r--r-- | meson.build | 15 | ||||
| -rw-r--r-- | ncurses_ui.c | 65 | ||||
| -rw-r--r-- | ncurses_ui.h | 60 |
6 files changed, 223 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cdf1ed2 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +build*
diff --git a/cetris.c b/cetris.c new file mode 100644 index 0000000..f856810 --- /dev/null +++ b/cetris.c @@ -0,0 +1,15 @@ +#include <stdio.h>
+#include <locale.h>
+#include <ncurses.h>
+
+#include "cetris.h"
+
+void add_piece(type t) {
+ for (int x = 0; x < 4; x++) {
+ for (int y = 0; y < 4; y++) {
+ if (pieces[t][y][x]) {
+ g.board[x + 2][y] = 1;
+ }
+ }
+ }
+}
diff --git a/cetris.h b/cetris.h new file mode 100644 index 0000000..c478a2c --- /dev/null +++ b/cetris.h @@ -0,0 +1,67 @@ +#define BOARD_X 10
+#define BOARD_Y 20
+
+typedef enum {
+ O, I, S, Z, L, J, T
+} type;
+
+typedef int piece_matrix[4][4];
+
+static const piece_matrix pieces[7] = {
+ {
+ { 0, 0, 0, 0 },
+ { 0, 0, 0, 0 },
+ { 1, 1, 0, 0 },
+ { 1, 1, 0, 0 }
+ },
+
+ {
+ { 1, 0, 0, 0 },
+ { 1, 0, 0, 0 },
+ { 1, 0, 0, 0 },
+ { 1, 0, 0, 0 }
+ },
+
+ {
+ { 0, 0, 0, 0 },
+ { 0, 0, 0, 0 },
+ { 0, 1, 1, 0 },
+ { 1, 1, 0, 0 }
+ },
+
+ {
+ { 0, 0, 0, 0 },
+ { 0, 0, 0, 0 },
+ { 1, 1, 0, 0 },
+ { 0, 1, 1, 0 }
+ },
+
+ {
+ { 0, 0, 0, 0 },
+ { 1, 0, 0, 0 },
+ { 1, 0, 0, 0 },
+ { 1, 1, 0, 0 }
+ },
+
+ {
+ { 0, 0, 0, 0 },
+ { 0, 1, 0, 0 },
+ { 0, 1, 0, 0 },
+ { 1, 1, 0, 0 }
+ },
+
+ {
+ { 0, 0, 0, 0 },
+ { 0, 0, 0, 0 },
+ { 1, 1, 1, 0 },
+ { 0, 1, 0, 0 }
+ }
+};
+
+struct game {
+ int board[10][20];
+};
+
+struct game g;
+
+void add_piece(type t);
diff --git a/meson.build b/meson.build new file mode 100644 index 0000000..c472081 --- /dev/null +++ b/meson.build @@ -0,0 +1,15 @@ +project('cetris', 'c',
+ version : '0.1',
+ default_options : ['warning_level=3', 'c_std=c99'])
+
+compiler = meson.get_compiler('c')
+
+if host_machine.system() == 'windows'
+ curses = compiler.find_library('pdcurses/pdcurses.a', dirs: meson.source_root())
+else
+ curses = dependency('ncursesw')
+endif
+
+executable('cetris', ['cetris.c', 'ncurses_ui.c'],
+ dependencies: curses,
+ install : true)
diff --git a/ncurses_ui.c b/ncurses_ui.c new file mode 100644 index 0000000..8d2956e --- /dev/null +++ b/ncurses_ui.c @@ -0,0 +1,65 @@ +#include <stdlib.h>
+#include <ncurses.h>
+#include <locale.h>
+
+#include "cetris.h"
+
+#define BLOCK "[]"
+#define BOARD_TOP "/------------------\\"
+#define BOARD_MID "| |"
+#define BOARD_BOTTOM "\\------------------/"
+
+//static int term_x, term_y;
+
+void curses_init() {
+ setlocale(LC_CTYPE, "");
+ initscr();
+ noecho();
+ keypad(stdscr, TRUE);
+ curs_set(0);
+ timeout(32);
+
+ //resize_term(20, 50);
+
+ start_color();
+ init_pair(0, COLOR_MAGENTA, COLOR_BLACK);
+ init_pair(1, COLOR_GREEN, COLOR_BLACK);
+ init_pair(2, COLOR_BLUE, COLOR_BLACK);
+ init_pair(3, COLOR_YELLOW, COLOR_BLACK);
+ clear();
+}
+
+void draw_board() {
+ for (int y = 0; y < BOARD_Y + 1; y++) {
+ if (y == 0)
+ mvaddstr(y, 10, BOARD_TOP);
+ else if (y == BOARD_Y)
+ mvaddstr(y, 10, BOARD_BOTTOM);
+ else
+ mvaddstr(y, 10, BOARD_MID);
+ }
+ for (int x = 0; x < BOARD_X; x++) {
+ for (int y = 0; y < BOARD_Y; y++) {
+ if (g.board[x][y])
+ mvaddstr(y, x * 2 + 10, BLOCK);
+ }
+ }
+}
+
+int main(void) {
+ curses_init();
+ add_piece(L);
+ int c;
+ while(1) {
+ c = getch();
+ switch (c) {
+ case 'q':
+ endwin();
+ exit(1);
+ }
+ draw_board();
+ refresh();
+ }
+ return 0;
+}
+
diff --git a/ncurses_ui.h b/ncurses_ui.h new file mode 100644 index 0000000..95bb3cf --- /dev/null +++ b/ncurses_ui.h @@ -0,0 +1,60 @@ +#include <ncurses.h>
+#include <locale.h>
+
+#include "betris.h"
+
+#define BLOCK "[]"
+#define BOARD_TOP "/------------------\\"
+#define BOARD_MID "| |"
+#define BOARD_BOTTOM "\\------------------/"
+
+void curses_init() {
+ setlocale(LC_CTYPE, "");
+ initscr();
+ noecho();
+ keypad(stdscr, TRUE);
+ curs_set(0);
+ timeout(32);
+
+ resize_term(20, 50);
+
+ start_color();
+ init_pair(0, COLOR_MAGENTA, COLOR_BLACK);
+ init_pair(1, COLOR_GREEN, COLOR_BLACK);
+ init_pair(2, COLOR_BLUE, COLOR_BLACK);
+ init_pair(3, COLOR_YELLOW, COLOR_BLACK);
+ clear();
+}
+
+void draw_board() {
+ for (int y = 0; y < BOARD_Y + 1; y++) {
+ if (y == 0)
+ mvaddstr(y, 10, BOARD_TOP);
+ else if (y == BOARD_Y)
+ mvaddstr(y, 10, BOARD_BOTTOM);
+ else
+ mvaddstr(y, 10, BOARD_MID);
+ }
+ for (int x = 0; x < BOARD_X; x++) {
+ for (int y = 0; y < BOARD_Y; y++) {
+ if (g.board[x][y])
+ mvaddstr(y * 2, x + 10, BLOCK);
+ }
+ }
+}
+
+int main(void) {
+ curses_init();
+ add_piece(L);
+ int c;
+ while(1) {
+ c = getch();
+ switch (c) {
+ case 'q': exit(1);
+ }
+ draw_board();
+ refresh();
+ }
+ return 0;
+}
+
|