domino-dungeon/game.c

83 lines
2 KiB
C
Raw Normal View History

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 06:17:32 +02:00
#include "assets/dominos.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 04:11:36 +02:00
int mouse_x = 0, mouse_y = 0;
2025-10-14 06:17:32 +02:00
int eyes_front = 0, eyes_back = 0;
2025-10-14 04:11:36 +02:00
2025-10-14 14:48:54 +02:00
struct bricks bricks = {0};
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
printf("click!\n");
if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) {
if (++eyes_back >= NUM_DOMINO_X) {
eyes_back = 0;
eyes_front = (eyes_front+1)%NUM_DOMINO_Y;
}
}
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-13 21:18:09 +02:00
void draw_image(decoded_image img) {
2025-10-14 14:48:54 +02:00
for (size_t i = 0; i < img.buf_size; i++) img.buf[i] = i;
for (size_t i = 0; i < bricks.count; i++) {
struct brick *b = &bricks.items.brick[i];
for (int y = 0; y < DOMINO_HEIGHT; y++) {
for (int x = 0; x < DOMINO_WIDTH; x++) {
img.buf[(b->front.y * 10 + y) * img.width + b->front.x * 10 + x] =
(*(uint32_t*) &domino[b->front.val][b->back.val][y * DOMINO_WIDTH * BYTES_PER_PIXEL + x * BYTES_PER_PIXEL]);
}
}
}
2025-10-14 06:17:32 +02:00
for (int y = 0; y < DOMINO_HEIGHT; y++) {
for (int x = 0; x < DOMINO_WIDTH; x++) {
img.buf[(CLAMP(mouse_y, 0, img.height) + y) * img.width + CLAMP(mouse_x, 0, img.width) + x] =
(*(uint32_t*) &domino[eyes_front][eyes_back][y * DOMINO_WIDTH * BYTES_PER_PIXEL + x * BYTES_PER_PIXEL]);
}
}
2025-10-13 21:18:09 +02:00
}