feat: rotate bricks

This commit is contained in:
orangerot 2025-10-15 10:40:39 +02:00
parent 91c8751e14
commit fd1b446f5a
2 changed files with 37 additions and 13 deletions

View file

@ -4,7 +4,7 @@
#define DOMINO_H
struct eye {
size_t x,y,val;
size_t x,y,val,vertical;
};
struct brick {

48
game.c
View file

@ -2,6 +2,7 @@
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdbool.h>
#include "game.h"
#include "domino.h"
@ -21,7 +22,7 @@ struct brick hand[5];
size_t hand_count = 0;
struct brick active = {0};
int has_active = 0;
bool has_active = 0;
struct brick preview[256] = {0};
size_t preview_count = 0;
@ -66,9 +67,30 @@ void get_prewiews() {
}
}
void print_brick(struct brick b) {
struct eye *eyes = (struct eye*) &b;
for (size_t i = 0; i < 2; i++) {
printf("{.x = %zu, .y = %zu, .val = %zu, .vertical = %zu}" , eyes[i].x, eyes[i].y, eyes[i].val, eyes[i].vertical);
}
printf("\n");
}
void key_callback(int key, int scancode, int action, int mods) {
(void) scancode;
(void) mods;
if (action != GLFW_PRESS) return;
switch (key) {
case GLFW_KEY_R:
if (active.front.vertical) {
struct eye tmp = active.front;
active.front.val = active.back.val;
active.back.val = tmp.val;
get_prewiews();
}
active.front.vertical = !active.front.vertical;
printf("rotate\n");
break;
case GLFW_KEY_ENTER:
break;
case GLFW_KEY_BACKSPACE:
@ -82,6 +104,8 @@ void cursor_position_callback(int xpos, int ypos) {
}
void mouse_button_callback(int button, int action, int mods) {
(void) mods;
if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) {
// pick up brick from hand
for (size_t i = 0; i < hand_count; i++) {
@ -123,8 +147,10 @@ void mouse_button_callback(int button, int action, int mods) {
if (preview[min_dist].front.val < EYE_SIZE * EYE_SIZE && preview_count) {
preview[min_dist].front.val = active.front.val;
preview[min_dist].front.vertical = active.front.vertical;
preview[min_dist].back.val = active.back.val;
bricks_append(&bricks, preview[min_dist]);
for (size_t i = 0; i < bricks.count; i++) print_brick(bricks.items.brick[i]);
} else {
hand[hand_count++] = active;
}
@ -165,14 +191,12 @@ void init() {
};
}
void
draw(
struct image canvas, struct image texture,
size_t xpos, size_t ypos
) {
void draw( struct image canvas, struct image texture, size_t xpos, size_t ypos, bool vertical) {
for (size_t y = 0; y < texture.height; y++) {
for (size_t x = 0; x < texture.width; x++) {
canvas.buf[(ypos + y) * canvas.width + xpos + x] = texture.buf[y * texture.width + x];
size_t canvas_y = vertical ? x : y;
size_t canvas_x = vertical ? y : x;
canvas.buf[(ypos + canvas_y) * canvas.width + xpos + canvas_x] = texture.buf[y * texture.width + x];
}
}
}
@ -201,7 +225,7 @@ draw_character(
}
}
}
draw(canvas, texture, xpos + i * texture.width, ypos);
draw(canvas, texture, xpos + i * texture.width, ypos, 0);
}
}
@ -216,11 +240,11 @@ void render(struct image canvas) {
// domino playground
for (size_t i = 0; i < bricks.count; i++) {
struct brick *b = &bricks.items.brick[i];
draw(canvas, red_and_peach_dominoes[b->back.val][b->front.val], b->front.x * EYE_SIZE, b->front.y * EYE_SIZE);
draw(canvas, red_and_peach_dominoes[b->back.val][b->front.val], b->front.x * EYE_SIZE, b->front.y * EYE_SIZE, b->front.vertical);
}
for (size_t i = 0; i < preview_count; i++) {
draw(canvas, white_and_blue_dominoes[active.back.val][active.front.val], preview[i].front.x * EYE_SIZE, preview[i].front.y * EYE_SIZE);
draw(canvas, white_and_blue_dominoes[active.back.val][active.front.val], preview[i].front.x * EYE_SIZE, preview[i].front.y * EYE_SIZE, active.front.vertical);
}
// hand
@ -228,12 +252,12 @@ void render(struct image canvas) {
struct brick *b = &hand[i];
b->front.x = (canvas.width - hand_count *(DOMINO_WIDTH + 4))/2 + i * (DOMINO_WIDTH + 4);
b->front.y = canvas.height - DOMINO_WIDTH;
draw(canvas, red_and_peach_dominoes[b->back.val][b->front.val], b->front.x, b->front.y);
draw(canvas, red_and_peach_dominoes[b->back.val][b->front.val], b->front.x, b->front.y, b->front.vertical);
}
// active
if (has_active) {
draw(canvas, red_and_peach_dominoes[active.back.val][active.front.val], CLAMP(mouse_x + active.front.x, 0, canvas.width), CLAMP(mouse_y + active.front.y, 0, canvas.height));
draw(canvas, red_and_peach_dominoes[active.back.val][active.front.val], CLAMP(mouse_x + active.front.x, 0, canvas.width), CLAMP(mouse_y + active.front.y, 0, canvas.height), active.front.vertical);
}
// character