💾 Archived View for gemini.rmf-dev.com › repo › Vaati › Tetris › files › 110f4b727e811c77e8b847c6b79… captured on 2023-12-28 at 15:48:24. Gemini links have been rewritten to link to archived content
⬅️ Previous capture (2023-09-08)
-=-=-=-=-=-=-
0 /* See LICENSE file for copyright and license details. */
1 #include "config.h"
2 #include "backend.h"
3 #include "util.h"
4 #include <termbox.h>
5
6 uint8 palette[] = {
7 /* blocks */
8 TB_YELLOW,
9 TB_MAGENTA,
10 TB_BLUE,
11 TB_CYAN,
12 TB_RED,
13 TB_GREEN,
14 TB_WHITE,
15 /* borders */
16 TB_YELLOW,
17 TB_MAGENTA,
18 TB_BLUE,
19 TB_CYAN,
20 TB_RED,
21 TB_GREEN,
22 TB_WHITE,
23 TB_BLACK, /* background */
24 TB_WHITE, /* text */
25 };
26
27 int backend_width;
28 int backend_height;
29
30 int backend_init() {
31 if (tb_init()) return -1;
32 backend_width = tb_width();
33 backend_height = tb_height() * 2;
34
35 return 0;
36 }
37
38 void backend_fill(int x, int y, int w, int h, int c) {
39 int color = palette[c];
40 int _y = y + (y&1);
41 int _h = h + (h&1);
42 for (; _y < y + _h; _y += 2) {
43 int _x = x;
44 for (; _x < x + w; _x++)
45 tb_set_cell(_x, _y/2, 0x2584, color, color);
46 }
47 }
48
49 void backend_draw(int x, int y, int w, int h, int c) {
50 /* ignore */
51 (void)x; (void)y; (void)w; (void)h; (void)c;
52 }
53
54 uint8 backend_input() {
55 struct tb_event event = {0};
56 uint8 state = 0;
57 while (tb_peek_event(&event, 0) == TB_OK) {
58 switch (event.type) {
59 case TB_EVENT_RESIZE:
60 backend_width = tb_width();
61 backend_height = tb_height() * 2;
62 break;
63 case TB_EVENT_KEY:
64 switch (event.key) {
65 case TB_KEY_ESC:
66 state |= QUIT;
67 break;
68 case TB_KEY_ARROW_LEFT:
69 state |= LEFT;
70 break;
71 case TB_KEY_ARROW_RIGHT:
72 state |= RIGHT;
73 break;
74 case TB_KEY_ARROW_DOWN:
75 state |= DOWN;
76 break;
77 case TB_KEY_ENTER:
78 case TB_KEY_SPACE:
79 state |= ROTATE;
80 break;
81 default:
82 switch (event.ch) {
83 case ' ':
84 case 'r':
85 state |= ROTATE;
86 break;
87 case 'a':
88 state |= LEFT;
89 break;
90 case 'd':
91 state |= RIGHT;
92 break;
93 case 's':
94 state |= DOWN;
95 break;
96 }
97 }
98 break;
99 }
100 event.type = 0;
101 }
102 return state;
103 }
104
105 void backend_refresh() {
106 tb_present();
107 tb_clear();
108 ansi_sleep(1000 * 1000 / FPS);
109 }
110
111 void backend_clean() {
112 tb_shutdown();
113 }
114