2025-10-13 21:18:09 +02:00
|
|
|
#include <GLFW/glfw3.h>
|
2025-10-14 14:48:54 +02:00
|
|
|
#include <stddef.h>
|
2025-10-14 06:17:32 +02:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
2025-10-13 21:18:09 +02:00
|
|
|
|
|
|
|
#include "game.h"
|
2025-10-14 14:48:54 +02:00
|
|
|
#include "domino.h"
|
2025-10-14 22:09:59 +02:00
|
|
|
#include "characters.h"
|
2025-10-14 21:39:22 +02:00
|
|
|
#include "assets/white_and_blue_dominoes.h"
|
|
|
|
#include "assets/red_and_peach_dominoes.h"
|
2025-10-13 21:18:09 +02:00
|
|
|
|
2025-10-14 04:11:36 +02:00
|
|
|
#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))
|
2025-10-13 21:18:09 +02:00
|
|
|
|
2025-10-14 16:32:45 +02:00
|
|
|
size_t mouse_x = 0, mouse_y = 0;
|
2025-10-14 04:11:36 +02:00
|
|
|
|
2025-10-14 14:48:54 +02:00
|
|
|
struct bricks bricks = {0};
|
|
|
|
|
2025-10-14 16:32:45 +02:00
|
|
|
struct brick hand[5];
|
|
|
|
size_t hand_count = 0;
|
|
|
|
|
2025-10-14 17:47:34 +02:00
|
|
|
struct brick active = {0};
|
|
|
|
int has_active = 0;
|
|
|
|
|
2025-10-14 21:39:22 +02:00
|
|
|
struct eye direction[4] = {
|
|
|
|
{.x = 0, .y = -1},
|
|
|
|
{.x = -2, .y = 0},
|
|
|
|
{.x = 1, .y = 0},
|
|
|
|
{.x = 0, .y = 1},
|
|
|
|
};
|
|
|
|
|
2025-10-14 04:11:36 +02:00
|
|
|
void key_callback(int key, int scancode, int action, int mods) {
|
2025-10-13 21:18:09 +02:00
|
|
|
if (action != GLFW_PRESS) return;
|
|
|
|
switch (key) {
|
|
|
|
case GLFW_KEY_ENTER:
|
|
|
|
break;
|
|
|
|
case GLFW_KEY_BACKSPACE:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-10-14 04:11:36 +02:00
|
|
|
void cursor_position_callback(int xpos, int ypos) {
|
|
|
|
mouse_x = xpos;
|
|
|
|
mouse_y = ypos;
|
|
|
|
}
|
|
|
|
|
|
|
|
void mouse_button_callback(int button, int action, int mods) {
|
2025-10-14 06:17:32 +02:00
|
|
|
if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) {
|
2025-10-14 17:47:34 +02:00
|
|
|
for (size_t i = 0; i < hand_count; i++) {
|
|
|
|
struct brick *b = &hand[i];
|
|
|
|
if (!has_active) {
|
|
|
|
if (b->front.x <= mouse_x && mouse_x <= b->front.x + DOMINO_WIDTH &&
|
|
|
|
b->front.y <= mouse_y && mouse_y <= b->front.y + DOMINO_HEIGHT
|
|
|
|
) {
|
|
|
|
has_active = 1;
|
|
|
|
active = *b;
|
|
|
|
active.front.x -= mouse_x;
|
|
|
|
active.front.y -= mouse_y;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
hand[i - 1] = hand[i];
|
|
|
|
}
|
2025-10-14 06:17:32 +02:00
|
|
|
}
|
2025-10-14 17:47:34 +02:00
|
|
|
if (has_active) hand_count--;
|
2025-10-14 06:17:32 +02:00
|
|
|
}
|
2025-10-14 17:47:34 +02:00
|
|
|
|
|
|
|
printf("click!\n");
|
|
|
|
if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_RELEASE) {
|
2025-10-14 21:39:22 +02:00
|
|
|
if (has_active) {
|
|
|
|
has_active = 0;
|
|
|
|
hand[hand_count++] = active;
|
|
|
|
}
|
2025-10-14 17:47:34 +02:00
|
|
|
}
|
|
|
|
|
2025-10-14 04:11:36 +02:00
|
|
|
}
|
|
|
|
|
2025-10-14 14:48:54 +02:00
|
|
|
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},
|
|
|
|
}
|
|
|
|
);
|
2025-10-14 16:32:45 +02:00
|
|
|
|
|
|
|
hand_count = 3;
|
|
|
|
hand[0] = (struct brick) {
|
2025-10-14 17:47:34 +02:00
|
|
|
.front = {.x = 12, .y = 5, .val = 0},
|
|
|
|
.back = {.x = 13, .y = 5, .val = 3},
|
2025-10-14 16:32:45 +02:00
|
|
|
};
|
|
|
|
hand[1] = (struct brick) {
|
2025-10-14 17:47:34 +02:00
|
|
|
.front = {.x = 12, .y = 5, .val = 4},
|
|
|
|
.back = {.x = 13, .y = 5, .val = 1},
|
2025-10-14 16:32:45 +02:00
|
|
|
};
|
|
|
|
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];
|
|
|
|
}
|
|
|
|
}
|
2025-10-14 14:48:54 +02:00
|
|
|
}
|
|
|
|
|
2025-10-14 22:09:59 +02:00
|
|
|
void
|
|
|
|
draw_character(
|
|
|
|
struct image canvas,
|
2025-10-14 23:22:56 +02:00
|
|
|
char *character,
|
2025-10-14 22:09:59 +02:00
|
|
|
size_t xpos, size_t ypos
|
|
|
|
) {
|
2025-10-14 23:22:56 +02:00
|
|
|
struct image texture = {
|
|
|
|
.width = 5,
|
|
|
|
.height = 6,
|
|
|
|
.bufsize = 5 * 6,
|
|
|
|
.buf = (uint32_t[30]) {0}
|
|
|
|
};
|
|
|
|
|
|
|
|
for (size_t i = 0; character[i]; i++) {
|
|
|
|
const uint32_t bitmask = characters[character[i] - ' '];
|
|
|
|
texture.buf = (uint32_t[30]) {0};
|
|
|
|
|
|
|
|
for (size_t y = 0; y < texture.height; y++) {
|
|
|
|
for (size_t x = 0; x < texture.width; x++) {
|
|
|
|
if ((bitmask >> ((texture.height - y - 1) * texture.width + (texture.width - x - 1))) & 1) {
|
|
|
|
texture.buf[y * texture.width + x] = -1;
|
|
|
|
}
|
2025-10-14 22:09:59 +02:00
|
|
|
}
|
|
|
|
}
|
2025-10-14 23:22:56 +02:00
|
|
|
draw(canvas, texture, xpos + i * texture.width, ypos);
|
2025-10-14 22:09:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//void
|
|
|
|
//draw_text(
|
2025-10-14 23:22:56 +02:00
|
|
|
//
|
2025-10-14 22:09:59 +02:00
|
|
|
//)
|
|
|
|
|
2025-10-14 16:32:45 +02:00
|
|
|
void render(struct image canvas) {
|
|
|
|
for (size_t i = 0; i < canvas.bufsize; i++) canvas.buf[i] = i;
|
2025-10-14 14:48:54 +02:00
|
|
|
|
2025-10-14 16:32:45 +02:00
|
|
|
// domino playground
|
2025-10-14 14:48:54 +02:00
|
|
|
for (size_t i = 0; i < bricks.count; i++) {
|
|
|
|
struct brick *b = &bricks.items.brick[i];
|
2025-10-14 21:39:22 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2025-10-14 14:48:54 +02:00
|
|
|
}
|
2025-10-14 06:17:32 +02:00
|
|
|
|
2025-10-14 17:47:34 +02:00
|
|
|
// hand
|
2025-10-14 16:32:45 +02:00
|
|
|
for (size_t i = 0; i < hand_count; i++) {
|
|
|
|
struct brick *b = &hand[i];
|
2025-10-14 17:47:34 +02:00
|
|
|
b->front.x = (canvas.width - hand_count *(DOMINO_WIDTH + 4))/2 + i * (DOMINO_WIDTH + 4);
|
|
|
|
b->front.y = canvas.height - DOMINO_WIDTH;
|
2025-10-14 21:39:22 +02:00
|
|
|
draw(canvas, red_and_peach_dominoes[b->back.val][b->front.val], b->front.x, b->front.y);
|
2025-10-14 06:17:32 +02:00
|
|
|
}
|
2025-10-14 16:32:45 +02:00
|
|
|
|
2025-10-14 17:47:34 +02:00
|
|
|
// active
|
|
|
|
if (has_active) {
|
2025-10-14 21:39:22 +02:00
|
|
|
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));
|
2025-10-14 17:47:34 +02:00
|
|
|
}
|
2025-10-14 22:09:59 +02:00
|
|
|
|
|
|
|
// character
|
2025-10-14 23:22:56 +02:00
|
|
|
draw_character(canvas, "Hallo Welt", 50, 20); // debugging
|
2025-10-14 22:09:59 +02:00
|
|
|
|
2025-10-13 21:18:09 +02:00
|
|
|
}
|
2025-10-14 16:32:45 +02:00
|
|
|
|