f1622aaf8f3264c0ab93e57955a629b26b692ce7
[ansitris] / src / game.c
1 #include <stdio.h>
2 #include <inttypes.h>
3 #include "game.h"
4 #include "pieces.h"
5 #include "ansi.h"
6
7 enum {
8         G_DIAMOND       = 0x04,
9         G_CHECKER       = 0xb1,
10         G_LR_CORNER     = 0xd9,
11         G_UR_CORNER     = 0xbf,
12         G_UL_CORNER     = 0xda,
13         G_LL_CORNER     = 0xc0,
14         G_CROSS         = 0xc5,
15         G_HLINE         = 0xc4,
16         G_L_TEE         = 0xc3,
17         G_R_TEE         = 0xb4,
18         G_B_TEE         = 0xc1,
19         G_T_TEE         = 0xc2,
20         G_VLINE         = 0xb3,
21         G_CDOT          = 0xf8
22 };
23
24 enum { BLACK, BLUE, GREEN, CYAN, RED, MAGENTA, YELLOW, WHITE };
25
26 /* dimensions of the whole screen */
27 #define SCR_ROWS        20
28 #define SCR_COLS        20
29
30 /* dimensions of the playfield */
31 #define PF_ROWS         18
32 #define PF_COLS         10
33 /* offset of the playfield from the left side of the screen */
34 #define PF_XOFFS        2
35
36 #define CHAR(c, fg, bg) \
37         ((uint16_t)(c) | ((uint16_t)(fg) << 12) | ((uint16_t)(bg) << 8))
38
39 uint16_t scr[SCR_COLS * SCR_ROWS];
40
41 static void wrchar(uint16_t c);
42
43
44 int init_game(void)
45 {
46         int i, j;
47         uint16_t *row = scr;
48
49         ansi_clearscr();
50
51         /* fill the screen buffer, and draw */
52         for(i=0; i<SCR_ROWS; i++) {
53                 ansi_setcursor(i, 0);
54
55                 for(j=0; j<SCR_COLS; j++) {
56                         if(i > PF_ROWS || j < PF_XOFFS - 1 || j > PF_XOFFS + PF_COLS) {
57                                 row[j] = CHAR(' ', WHITE, BLACK);
58                         } else if((i == PF_ROWS && j >= PF_XOFFS && j < PF_XOFFS + PF_COLS) ||
59                                         j == PF_XOFFS - 1 || j == PF_XOFFS + PF_COLS) {
60                                 row[j] = CHAR(G_CHECKER, WHITE, BLACK);
61                         } else {
62                                 row[j] = CHAR(' ', BLACK, WHITE);
63                         }
64
65                         wrchar(row[j]);
66                 }
67
68                 row += SCR_COLS;
69         }
70         fflush(stdout);
71
72         return 0;
73 }
74
75 void cleanup_game(void)
76 {
77         ansi_reset();
78 }
79
80 void proc_input(void)
81 {
82         int c = fgetc(stdin);
83
84         switch(c) {
85         case 27:
86                 quit = 1;
87                 break;
88
89         default:
90                 break;
91         }
92 }
93
94 static void wrchar(uint16_t c)
95 {
96         unsigned char cc = c & 0xff;
97         unsigned char ca = c >> 8;
98
99         ansi_ibmchar(cc, ca);
100         ansi_ibmchar(cc, ca);
101 }