feat: preview

This commit is contained in:
orangerot 2025-10-14 21:39:22 +02:00
parent e01184ed59
commit f0ba6cf564
4 changed files with 54 additions and 11 deletions

View file

@ -2,13 +2,13 @@ CFLAGS := -ggdb -Wall -Wextra
# -fsanitize=address
LDFLAGS = -lglfw -lm -lGL -I./glad/include
SOURCES = main.c game.c game.h glad/src/glad.c assets/dominos.h domino.c domino.h
SOURCES = main.c game.c game.h glad/src/glad.c assets/white_and_blue_dominoes.h assets/red_and_peach_dominoes.h domino.c domino.h
domino-dungeon: ${SOURCES}
$(CC) ${CFLAGS} -o $@ $^ ${LDFLAGS}
assets/dominos.h: src_build/domino_assets
$< assets/1bit_dominoes_asset_pack/white_and_blue_dominoes.png > $@
assets/%.h: src_build/domino_assets
$< assets/1bit_dominoes_asset_pack/$*.png $* > $@
src_build/domino_assets: src_build/domino_assets.c
$(CC) -o $@ $^ -lm

View file

Before

Width:  |  Height:  |  Size: 901 B

After

Width:  |  Height:  |  Size: 901 B

54
game.c
View file

@ -5,7 +5,8 @@
#include "game.h"
#include "domino.h"
#include "assets/dominos.h"
#include "assets/white_and_blue_dominoes.h"
#include "assets/red_and_peach_dominoes.h"
#define MIN(a,b) (((a)<(b))?(a):(b))
#define MAX(a,b) (((a)>(b))?(a):(b))
@ -21,6 +22,13 @@ size_t hand_count = 0;
struct brick active = {0};
int has_active = 0;
struct eye direction[4] = {
{.x = 0, .y = -1},
{.x = -2, .y = 0},
{.x = 1, .y = 0},
{.x = 0, .y = 1},
};
void key_callback(int key, int scancode, int action, int mods) {
if (action != GLFW_PRESS) return;
switch (key) {
@ -58,8 +66,10 @@ void mouse_button_callback(int button, int action, int mods) {
printf("click!\n");
if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_RELEASE) {
has_active = 0;
hand[hand_count++] = active;
if (has_active) {
has_active = 0;
hand[hand_count++] = active;
}
}
}
@ -113,7 +123,39 @@ 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, domino[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);
}
if (has_active) {
for (size_t i = 0; i < bricks.count * 2; i++) {
struct eye e = bricks.items.eye[i];
if (e.val != active.front.val) continue;
struct brick p = active;
p.front.x = e.x;
p.front.y = e.y;
p.back.x = e.x + 1;
p.back.y = e.y;
struct brick previews[4] = {p, p, p, p};
for (size_t ii = 0; ii < 4; ii++) {
previews[ii].front.x += direction[ii].x;
previews[ii].front.y += direction[ii].y;
previews[ii].front.val = 0;
previews[ii].back.x += direction[ii].x;
previews[ii].back.y += direction[ii].y;
for (size_t iii = 0; iii < bricks.count * 2; iii++) {
previews[ii].front.val |= ((bricks.items.eye[iii].x == previews[ii].back.x && bricks.items.eye[iii].y == previews[ii].back.y) ||
(bricks.items.eye[iii].x == previews[ii].front.x && bricks.items.eye[iii].y == previews[ii].front.y));
}
}
for (size_t ii = 0; ii < 4; ii++) {
if (!previews[ii].front.val)
draw(canvas, white_and_blue_dominoes[active.back.val][active.front.val], previews[ii].front.x * EYE_SIZE, previews[ii].front.y * EYE_SIZE);
}
}
}
// hand
@ -121,12 +163,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, domino[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);
}
// active
if (has_active) {
draw(canvas,domino[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));
}
}

View file

@ -33,7 +33,7 @@ void print_domino(unsigned char *data, int width, int n, int posx, int posy) {
int main(int argc, char **argv) {
int x,y,n;
if (argc != 2) return 1;
if (argc != 3) return 1;
unsigned char *data = stbi_load(argv[1], &x, &y, &n, 0);
// n = n < 3 ? n : 3;
@ -47,7 +47,8 @@ int main(int argc, char **argv) {
printf("#define EYE_SIZE %d\n", EYE_SIZE);
printf("#define BYTES_PER_PIXEL %d\n", n);
printf("struct image domino[%d][%d] = {\n",
printf("struct image %s[%d][%d] = {\n",
argv[2],
NUM_DOMINO_X,
NUM_DOMINO_Y
);