tuitris

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit 59b0acf8e7fbd00a17ab3bafa2c92319bf1fe61e
Author: Benjamin Paul <bpaul@bpaul.xyz>
Date:   Sun, 22 Aug 2021 20:19:24 +1000

initial commit

Diffstat:
AMakefile | 15+++++++++++++++
ATODO | 8++++++++
Adraw.c | 46++++++++++++++++++++++++++++++++++++++++++++++
Adraw.h | 6++++++
Adraw.o | 0
Amain.c | 83+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Amain.o | 0
Apiece.c | 107+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Apiece.h | 12++++++++++++
Apiece.o | 0
Atuitris | 0
11 files changed, 277 insertions(+), 0 deletions(-)

diff --git a/Makefile b/Makefile @@ -0,0 +1,15 @@ +.POSIX: + +NAME = tuitris + +CC = cc +CFLAGS = -O3 +LIBS = -lncurses + +OBJS = draw.o main.o piece.o + +all: $(OBJS) + $(CC) $(CFLAGS) $(LDFLAGS) -o $(NAME) $(OBJS) $(LIBS) + +clean: + rm *.o $(NAME) diff --git a/TODO b/TODO @@ -0,0 +1,8 @@ +Draw pieces from a list of pieces +move left right +hard drop +7 bag +next queue +hold +srs +soft drop diff --git a/draw.c b/draw.c @@ -0,0 +1,46 @@ +#include <ncurses.h> + +#include "piece.h" + +extern const bool piece_occupancy[PIECE_CNT][ROTATION_CNT][9]; +extern const bool I_occupancy[ROTATION_CNT][16]; + +void draw_square(int y, int x) { + mvprintw(y, x, " "); +} + +void +draw_piece(int y, int x, int piece, int rot) { + attron(COLOR_PAIR(piece)); + if (piece != I) { + for (int i = 0; i < 9; i++) { + if (piece_occupancy[piece][rot][i]) { + draw_square(y+i/3, x+2*(i%3)); + } + } + } else { + for (int i = 0; i < 16; i++) { + if (I_occupancy[rot][i]) { + draw_square(y+i/4, x+2*(i%4)); + } + } + } + attroff(COLOR_PAIR(piece)); +} + +void +draw_box(int x1, int y1, int x2, int y2) { + attron(COLOR_PAIR(8)); + for (int x = x1; x <= x2; x++) { + mvaddch(y1, x, ' '); + mvaddch(y2, x, ' '); + } + for (int y = y1; y <= y2; y++) { + mvaddch(y, x1-1, ' '); + mvaddch(y, x1, ' '); + mvaddch(y, x2+1, ' '); + mvaddch(y, x2, ' '); + } + attroff(COLOR_PAIR(8)); +} + diff --git a/draw.h b/draw.h @@ -0,0 +1,6 @@ +#pragma once + +#include <ncurses.h> + +void draw_piece(int y, int x, int piece, int rot); +void draw_box(int x1, int y1, int x2, int y2); diff --git a/draw.o b/draw.o Binary files differ. diff --git a/main.c b/main.c @@ -0,0 +1,83 @@ +#include <ncurses.h> +#include <string.h> + +#include "draw.h" +#include "piece.h" + +void +play_sprint() { + clear(); + + draw_box(COLS/2 - 10, LINES/2 - 10, COLS/2 + 10, LINES/2 + 10); + + char key; + for (;;) { + draw_piece(LINES/2 - 9, COLS/2 - 2, O, NORTH); + draw_piece(LINES/2 - 6, COLS/2 - 2, I, NORTH); + draw_piece(LINES/2 - 3, COLS/2 - 2, T, NORTH); + draw_piece(LINES/2 - 0, COLS/2 - 2, L, NORTH); + draw_piece(LINES/2 + 3, COLS/2 - 2, J, NORTH); + draw_piece(LINES/2 + 6, COLS/2 - 2, S, NORTH); + draw_piece(LINES/2 + 9, COLS/2 - 2, Z, NORTH); + key = getch(); + if (key == 'q') break; + } +} + +void +draw_menu_item(int y, const char *str) { + mvprintw(y, COLS/2 - strlen(str)/2, str); +} + +void +draw_main_menu() { + clear(); + + draw_box(COLS/2 - 10, LINES/2 - 10, COLS/2 + 10, LINES/2 + 10); + + draw_menu_item(LINES/2 - 8, "Sprint"); +} + +int +main() { + initscr(); + start_color(); + + timeout(0); + keypad(stdscr, TRUE); + cbreak(); + noecho(); + + curs_set(0); + + int key; + + init_pair(O, COLOR_BLACK, COLOR_YELLOW); + init_pair(I, COLOR_BLACK, COLOR_CYAN); + init_pair(T, COLOR_BLACK, COLOR_MAGENTA); + init_pair(L, COLOR_BLACK, 208); + init_pair(J, COLOR_BLACK, COLOR_BLUE); + init_pair(S, COLOR_BLACK, COLOR_GREEN); + init_pair(Z, COLOR_BLACK, COLOR_RED); + + init_pair(8, COLOR_BLACK, COLOR_WHITE); + + /* Main menu */ + draw_main_menu(); + for (;;) { + key = getch(); + if (key == 'q') break; + else if (key == '\n') { + play_sprint(); + draw_main_menu(); + } + else if (key == KEY_RESIZE) { + draw_main_menu(); + } + mvaddch(LINES/2 - 8, COLS/2 - 6, '>'); + } + + endwin(); + + return 0; +} diff --git a/main.o b/main.o Binary files differ. diff --git a/piece.c b/piece.c @@ -0,0 +1,107 @@ +#include <stdbool.h> + +#include "piece.h" + +/* This was defined in the tetris guidelines */ +const bool piece_occupancy[PIECE_CNT][ROTATION_CNT][9] = { + {}, + { /* O */ + {0,1,1, + 0,1,1, + 0,0,0}, + {0,1,1, + 0,1,1, + 0,0,0}, + {0,1,1, + 0,1,1, + 0,0,0}, + {0,1,1, + 0,1,1, + 0,0,0}, + }, { /* I doesnt go in this because it is 4 wide */ + }, { /* T */ + {0,1,0, + 1,1,1, + 0,0,0}, + {0,1,0, + 0,1,1, + 0,1,0}, + {0,0,0, + 1,1,1, + 0,1,0}, + {0,1,0, + 1,1,0, + 0,1,0}, + }, { /* L */ + {0,0,1, + 1,1,1, + 0,0,0}, + {0,1,0, + 0,1,0, + 0,1,1}, + {0,0,0, + 1,1,1, + 1,0,0}, + {1,1,0, + 0,1,0, + 0,1,0} + }, { /* J */ + {1,0,0, + 1,1,1, + 0,0,0}, + {0,1,1, + 0,1,0, + 0,1,0}, + {0,0,0, + 1,1,1, + 0,0,1}, + {0,1,0, + 0,1,0, + 1,1,0} + }, { /* S */ + {0,1,1, + 1,1,0, + 0,0,0}, + {0,1,0, + 0,1,1, + 0,0,1}, + {0,0,0, + 0,1,1, + 1,1,0}, + {1,0,0, + 1,1,0, + 0,1,0} + }, { /* Z */ + {1,1,0, + 0,1,1, + 0,0,0}, + {0,0,1, + 0,1,1, + 0,1,0}, + {0,0,0, + 1,1,0, + 0,1,1}, + {0,1,0, + 1,1,0, + 1,0,0} + } +}; + +const bool I_occupancy[ROTATION_CNT][16] = { + {0,0,0,0, + 1,1,1,1, + 0,0,0,0, + 0,0,0,0}, + {0,0,1,0, + 0,0,1,0, + 0,0,1,0, + 0,0,1,0}, + {0,0,0,0, + 0,0,0,0, + 1,1,1,1, + 0,0,0,0}, + {0,1,0,0, + 0,1,0,0, + 0,1,0,0, + 0,1,0,0}, +}; diff --git a/piece.h b/piece.h @@ -0,0 +1,12 @@ +#pragma once + +enum Piece { + hi, /* need this so that O is 1 so that it can be init_paired */ + O,I,T,L,J,S,Z, + PIECE_CNT, +}; + +enum Rotation { + NORTH, EAST, SOUTH, WEST, + ROTATION_CNT +}; diff --git a/piece.o b/piece.o Binary files differ. diff --git a/tuitris b/tuitris Binary files differ.