Fumofumotris/source/fumotris/tetr.c

84 lines
1.7 KiB
C
Raw Normal View History

2024-03-25 05:34:59 +00:00
#include <iso646.h>
#include <pthread.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2024-04-30 21:41:31 +00:00
#include "fumocommon.h"
2024-03-25 05:34:59 +00:00
#include "term.h"
2024-04-30 21:41:31 +00:00
2024-03-25 05:34:59 +00:00
struct TetrMap {
size_t wid;
size_t hgt;
size_t area;
int x;
int y;
u8 rot;
u8 *blks;
};
2024-04-02 20:06:14 +00:00
struct TetrMap NewTetrMap(u8 *blks, size_t wid, size_t hgt)
2024-03-25 05:34:59 +00:00
{
2024-04-02 20:06:14 +00:00
size_t area = wid * hgt;
memset(blks, 0, area);
2024-03-25 05:34:59 +00:00
return (struct TetrMap) {
2024-04-02 20:06:14 +00:00
wid, hgt, area,
2024-03-25 05:34:59 +00:00
0, 0, 0,
2024-04-02 20:06:14 +00:00
blks
2024-03-25 05:34:59 +00:00
};
}
2024-04-03 23:31:47 +00:00
void TetrMapToTermBuf(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 };
for (size_t y = 0; y < map->hgt; y++) {
for (size_t x = 0; x < map->wid; x++) {
size_t map_i = y * map->wid + x;
size_t buf_i = (y + map->y) * term->wid + (x + map->x) * 2;
2024-05-07 03:29:10 +00:00
struct Char4 *a = &term->ch4s[buf_i];
struct Char4 *b = &term->ch4s[buf_i + 1];
2024-03-25 05:34:59 +00:00
if (map->blks[map_i] == 0) {
a->ch = '(';
b->ch = ')';
} else {
a->ch = '[';
b->ch = ']';
}
u8 fg = blk_colors[map->blks[map_i]];
a->fg = fg;
b->fg = fg;
}
}
}
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;
}