domino-dungeon/game.c
2025-10-14 16:32:45 +02:00

109 lines
2.7 KiB
C

#include <GLFW/glfw3.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include "game.h"
#include "domino.h"
#include "assets/dominos.h"
#define MIN(a,b) (((a)<(b))?(a):(b))
#define MAX(a,b) (((a)>(b))?(a):(b))
#define CLAMP(x,a,b) (MIN(MAX(x,a),b))
size_t mouse_x = 0, mouse_y = 0;
int eyes_front = 0, eyes_back = 0;
struct bricks bricks = {0};
struct brick hand[5];
size_t hand_count = 0;
void key_callback(int key, int scancode, int action, int mods) {
if (action != GLFW_PRESS) return;
switch (key) {
case GLFW_KEY_ENTER:
break;
case GLFW_KEY_BACKSPACE:
break;
}
}
void cursor_position_callback(int xpos, int ypos) {
mouse_x = xpos;
mouse_y = ypos;
}
void mouse_button_callback(int button, int action, int mods) {
if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) {
printf("click!\n");
if (++eyes_back >= NUM_DOMINO_X - 1) {
eyes_back = 0;
eyes_front = (eyes_front+1)%NUM_DOMINO_Y;
}
printf("%d %d\n", eyes_front, eyes_back);
}
}
void init() {
bricks_append(
&bricks,
(struct brick) {
.front = {.x = 10, .y = 5, .val = 3},
.back = {.x = 11, .y = 5, .val = 2},
}
);
bricks_append(
&bricks,
(struct brick) {
.front = {.x = 12, .y = 5, .val = 2},
.back = {.x = 13, .y = 5, .val = 5},
}
);
hand_count = 3;
hand[0] = (struct brick) {
.front = {.x = 12, .y = 5, .val = 2},
.back = {.x = 13, .y = 5, .val = 5},
};
hand[1] = (struct brick) {
.front = {.x = 12, .y = 5, .val = 2},
.back = {.x = 13, .y = 5, .val = 5},
};
hand[2] = (struct brick) {
.front = {.x = 12, .y = 5, .val = 2},
.back = {.x = 13, .y = 5, .val = 5},
};
}
void
draw(
struct image canvas, struct image texture,
size_t xpos, size_t ypos
) {
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];
}
}
}
void render(struct image canvas) {
for (size_t i = 0; i < canvas.bufsize; i++) canvas.buf[i] = i;
// 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);
}
for (size_t i = 0; i < hand_count; i++) {
struct brick *b = &hand[i];
draw(canvas, domino[b->back.val][b->front.val], (canvas.width - hand_count *(DOMINO_WIDTH + 4))/2 + i * (DOMINO_WIDTH + 4), canvas.height - DOMINO_WIDTH);
}
draw(canvas,domino[eyes_front][eyes_back], CLAMP(mouse_x, 0, canvas.width), CLAMP(mouse_y, 0, canvas.height));
}