alloc big buffers on heap
This commit is contained in:
@@ -33,8 +33,8 @@ static struct {
|
||||
static void start(void) {
|
||||
memset(snek.body, -1, sizeof(snek.body));
|
||||
snek.len = 2;
|
||||
snek.body[0] = (point){{1, tim.h / 2}};
|
||||
snek.food = (point){{tim.w / 8, tim.h / 2}};
|
||||
snek.body[0] = (point){{1, tim->h / 2}};
|
||||
snek.food = (point){{tim->w / 8, tim->h / 2}};
|
||||
snek.look = (point){{1, 0}};
|
||||
}
|
||||
|
||||
@@ -53,19 +53,19 @@ static void game(void) {
|
||||
crash |= snek.body[0].xy == snek.body[i].xy;
|
||||
}
|
||||
// border crash
|
||||
crash |= snek.body[0].x < 0 || snek.body[0].x >= tim.w / 2 ||
|
||||
snek.body[0].y < 0 || snek.body[0].y >= tim.h;
|
||||
crash |= snek.body[0].x < 0 || snek.body[0].x >= tim->w / 2 ||
|
||||
snek.body[0].y < 0 || snek.body[0].y >= tim->h;
|
||||
snek.state = crash ? OVER : snek.state;
|
||||
// food
|
||||
if (snek.food.xy == snek.body[0].xy) {
|
||||
snek.len = MIN(snek.len + 2, (i32)ARRAY_SIZE(snek.body));
|
||||
snek.food.x = rand() % (tim.w / 2 - 2) + 1;
|
||||
snek.food.y = rand() % (tim.h - 2) + 1;
|
||||
snek.food.x = rand() % (tim->w / 2 - 2) + 1;
|
||||
snek.food.y = rand() % (tim->h - 2) + 1;
|
||||
}
|
||||
}
|
||||
|
||||
// draw
|
||||
if (tim.event.type == TimEvent_Draw) {
|
||||
if (tim->event.type == TimEvent_Draw) {
|
||||
// food
|
||||
tim_draw_chr(tim_cell(" ", 0, 0xc5), snek.food.x * 2 + 0, snek.food.y);
|
||||
tim_draw_chr(tim_cell(" ", 0, 0xc5), snek.food.x * 2 + 1, snek.food.y);
|
||||
@@ -81,8 +81,8 @@ static void game(void) {
|
||||
}
|
||||
|
||||
// user input
|
||||
if (tim.event.type == KEY_EVENT) {
|
||||
i32 key = tim.event.key;
|
||||
if (tim->event.type == KEY_EVENT) {
|
||||
i32 key = tim->event.key;
|
||||
if ((key == TimKey_Right || key == 'd') && snek.look.x != -1) {
|
||||
snek.look = (point){{1, 0}};
|
||||
} else if ((key == TimKey_Left || key == 'a') && snek.look.x != 1) {
|
||||
@@ -119,7 +119,7 @@ i32 main(void) {
|
||||
// draw every 10 ms
|
||||
while (tim_run(60)) {
|
||||
TimCell bg = tim_cell(" ", 0, BG);
|
||||
tim_draw_lot(bg, 0, 0, tim.w, tim.h);
|
||||
tim_draw_lot(bg, 0, 0, tim->w, tim->h);
|
||||
|
||||
if (snek.state == RUN) {
|
||||
game();
|
||||
|
||||
Reference in New Issue
Block a user