Small block game in C
| 1 | #ifndef MAIN_H |
| 2 | |
| 3 | #define MAIN_H |
| 4 | |
| 5 | #include <stdio.h> |
| 6 | #include <stdint.h> |
| 7 | #include <stdbool.h> |
| 8 | #include <stdlib.h> |
| 9 | |
| 10 | #include <termios.h> |
| 11 | #include <sys/ioctl.h> |
| 12 | #include <sys/stat.h> |
| 13 | |
| 14 | #include <time.h> |
| 15 | #include <unistd.h> |
| 16 | #include <poll.h> |
| 17 | |
| 18 | #include <ctype.h> |
| 19 | |
| 20 | #include <errno.h> |
| 21 | |
| 22 | |
| 23 | #define FIELD_WIDTH 10 |
| 24 | #define FIELD_HEIGHT 21 |
| 25 | |
| 26 | #define BORDER_WIDTH 2 |
| 27 | #define TOP_GAP 1 |
| 28 | |
| 29 | #define FULL_WIDTH (FIELD_WIDTH + BORDER_WIDTH * 2) |
| 30 | #define FULL_HEIGHT (FIELD_HEIGHT + BORDER_WIDTH) |
| 31 | |
| 32 | #define FRAME 17000000 |
| 33 | |
| 34 | #define FALL 58 |
| 35 | #define BLINK 12 |
| 36 | |
| 37 | typedef enum { |
| 38 | EMPTY, I_PIECE, J_PIECE, L_PIECE, O_PIECE, S_PIECE, T_PIECE, Z_PIECE, TETRIS |
| 39 | } tetromino; |
| 40 | |
| 41 | typedef enum { |
| 42 | TOP, RIGHT, BOTTOM, LEFT |
| 43 | } direction; |
| 44 | |
| 45 | typedef struct { |
| 46 | tetromino name; |
| 47 | bool piece[4][4]; |
| 48 | int8_t x; |
| 49 | int8_t y; |
| 50 | uint8_t size; |
| 51 | direction rotation; |
| 52 | uint8_t positions; |
| 53 | } piece; |
| 54 | |
| 55 | typedef struct { |
| 56 | int8_t x; |
| 57 | int8_t y; |
| 58 | } coord; |
| 59 | |
| 60 | typedef struct { |
| 61 | uint8_t r; |
| 62 | uint8_t g; |
| 63 | uint8_t b; |
| 64 | } color; |
| 65 | |
| 66 | |
| 67 | void init_field(); |
| 68 | |
| 69 | void draw_board(piece current_piece, piece next_piece, bool *blink, bool is_piece); |
| 70 | void draw_pause(); |
| 71 | |
| 72 | |
| 73 | piece init_piece(tetromino new_piece); |
| 74 | |
| 75 | coord find_direction(piece current_piece, uint8_t x, uint8_t y); |
| 76 | |
| 77 | bool valid_position(piece current_piece); |
| 78 | |
| 79 | |
| 80 | |
| 81 | extern tetromino playfield[FULL_HEIGHT][FULL_WIDTH]; |
| 82 | extern uint8_t graying[FULL_HEIGHT][FULL_WIDTH]; |
| 83 | |
| 84 | |
| 85 | #endif |