2024-05-07 22:10:15 +00:00
|
|
|
#include "tetr.h"
|
2024-03-25 05:34:59 +00:00
|
|
|
|
|
|
|
|
2024-05-07 22:10:15 +00:00
|
|
|
bool CreateTetrMap(struct TetrMap *map, usize wid, usize hgt)
|
|
|
|
{
|
|
|
|
u8 *blks = calloc(wid * hgt, sizeof(u8));
|
|
|
|
|
|
|
|
if (blks == nullptr)
|
|
|
|
return false;
|
2024-04-30 21:41:31 +00:00
|
|
|
|
2024-05-07 22:10:15 +00:00
|
|
|
*map = (struct TetrMap) {
|
|
|
|
.wid = wid,
|
|
|
|
.hgt = hgt,
|
2024-03-25 05:34:59 +00:00
|
|
|
|
2024-05-07 22:10:15 +00:00
|
|
|
.x = 0,
|
|
|
|
.y = 0,
|
2024-03-25 05:34:59 +00:00
|
|
|
|
2024-05-07 22:10:15 +00:00
|
|
|
.rot = 0,
|
2024-04-02 20:06:14 +00:00
|
|
|
|
2024-05-07 22:10:15 +00:00
|
|
|
.blks = blks
|
2024-03-25 05:34:59 +00:00
|
|
|
};
|
2024-05-07 22:10:15 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FreeTetrMap(struct TetrMap *map)
|
|
|
|
{
|
|
|
|
free(map->blks);
|
2024-03-25 05:34:59 +00:00
|
|
|
}
|
|
|
|
|
2024-05-07 22:10:15 +00:00
|
|
|
void TetrMapDraw(struct TetrMap *map, struct Terminal *term)
|
2024-03-25 05:34:59 +00:00
|
|
|
{
|
|
|
|
static const u8f blk_colors[8] = { 8, 14, 11, 13, 10, 9, 12, 3 };
|
|
|
|
|
2024-05-07 22:10:15 +00:00
|
|
|
for (usize y = 0; y < map->hgt; y++) {
|
|
|
|
for (usize x = 0; x < map->wid; x++) {
|
|
|
|
usize map_i = y * map->wid + x;
|
|
|
|
usize term_i = (y + map->y) * term->wid + (x + map->x) * 2;
|
2024-03-25 05:34:59 +00:00
|
|
|
|
2024-05-07 22:10:15 +00:00
|
|
|
struct Char4 *block = term->buf + term_i;
|
2024-03-25 05:34:59 +00:00
|
|
|
|
|
|
|
if (map->blks[map_i] == 0) {
|
2024-05-07 22:10:15 +00:00
|
|
|
block[0].ch = '(';
|
|
|
|
block[1].ch = ')';
|
2024-03-25 05:34:59 +00:00
|
|
|
} else {
|
2024-05-07 22:10:15 +00:00
|
|
|
block[0].ch = '[';
|
|
|
|
block[1].ch = ']';
|
2024-03-25 05:34:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
u8 fg = blk_colors[map->blks[map_i]];
|
2024-05-07 22:10:15 +00:00
|
|
|
block[0].color.fg = fg;
|
|
|
|
block[1].color.fg = fg;
|
2024-03-25 05:34:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TetrCollisionCheck(struct TetrMap *board, struct TetrMap *piece, int dx, int dy)
|
|
|
|
{
|
|
|
|
size_t i = 0;
|
|
|
|
for (size_t y = piece->y + dy; y < piece->y + piece->hgt + dy; y++) {
|
|
|
|
for (size_t x = piece->x + dx; x < piece->x + piece->wid + dx; x++) {
|
|
|
|
if(piece->blks[i] == 0)
|
|
|
|
goto next;
|
|
|
|
|
|
|
|
if(y >= board->hgt or x >= board->wid)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
size_t board_i = y * board->wid + x;
|
|
|
|
if(board->blks[board_i] != 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
next:
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|