292 lines
8.3 KiB
C
292 lines
8.3 KiB
C
#include <GLFW/glfw3.h>
|
|
#include <stddef.h>
|
|
#include <stdint.h>
|
|
#include <stdio.h>
|
|
#include <stdbool.h>
|
|
|
|
#include "game.h"
|
|
#include "domino.h"
|
|
#include "characters.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))
|
|
#define CLAMP(x,a,b) (MIN(MAX(x,a),b))
|
|
|
|
int mouse_x = 0, mouse_y = 0;
|
|
|
|
struct bricks bricks = {0};
|
|
|
|
struct brick hand[5];
|
|
size_t hand_count = 0;
|
|
|
|
struct brick active = {0};
|
|
bool has_active = 0;
|
|
|
|
struct brick preview[256] = {0};
|
|
size_t preview_count = 0;
|
|
|
|
int camera_x = 0;
|
|
int camera_y = 0;
|
|
bool is_dragging = 0;
|
|
|
|
#define DIRECTIONS 6
|
|
struct eye direction[DIRECTIONS] = {
|
|
{.x = 0, .y = -1},
|
|
{.x = -2, .y = 0},
|
|
{.x = 1, .y = 0},
|
|
{.x = 0, .y = 1},
|
|
{.x = -1, .y = -1},
|
|
{.x = -1, .y = 1},
|
|
};
|
|
|
|
void get_prewiews() {
|
|
preview_count = 0;
|
|
for (size_t i = 0; i < bricks.count * 2; i++) {
|
|
struct eye e = bricks.items.eye[i];
|
|
if (e.val != active.front.val && e.val != active.back.val) continue;
|
|
|
|
struct brick p = active;
|
|
|
|
p.front.x = e.x;
|
|
p.front.y = e.y;
|
|
p.back.x = e.x + !active.front.vertical;
|
|
p.back.y = e.y + active.front.vertical;
|
|
|
|
struct brick previews[DIRECTIONS] = {0};
|
|
for (size_t ii = 0; ii < DIRECTIONS; ii++) {
|
|
int offset_x = active.front.vertical ? direction[ii].y : direction[ii].x;
|
|
int offset_y = active.front.vertical ? direction[ii].x : direction[ii].y;
|
|
previews[ii] = p;
|
|
previews[ii].front.x += offset_x;
|
|
previews[ii].front.y += offset_y;
|
|
previews[ii].front.val = 0;
|
|
|
|
previews[ii].back.x += offset_x;
|
|
previews[ii].back.y += offset_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)
|
|
|| (e.val == active.front.val && direction[ii].x < 0)
|
|
|| (e.val == active.back.val && direction[ii].x >= 0)
|
|
);
|
|
}
|
|
}
|
|
|
|
for (size_t ii = 0; ii < DIRECTIONS; ii++) {
|
|
if (!previews[ii].front.val)
|
|
preview[preview_count++] = previews[ii];
|
|
}
|
|
}
|
|
}
|
|
|
|
void print_brick(struct brick b) {
|
|
struct eye *eyes = (struct eye*) &b;
|
|
for (size_t i = 0; i < 2; i++) {
|
|
printf("{.x = %d, .y = %d, .val = %d, .vertical = %d}" , 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;
|
|
}
|
|
active.front.vertical = !active.front.vertical;
|
|
get_prewiews();
|
|
printf("rotate\n");
|
|
break;
|
|
case GLFW_KEY_ENTER:
|
|
break;
|
|
case GLFW_KEY_BACKSPACE:
|
|
break;
|
|
}
|
|
}
|
|
|
|
void cursor_position_callback(int xpos, int ypos) {
|
|
if (is_dragging) {
|
|
camera_x += xpos - mouse_x;
|
|
camera_y += ypos - mouse_y;
|
|
}
|
|
mouse_x = xpos;
|
|
mouse_y = ypos;
|
|
}
|
|
|
|
void mouse_button_callback(int button, int action, int mods) {
|
|
(void) mods;
|
|
|
|
if (button == GLFW_MOUSE_BUTTON_RIGHT) {
|
|
is_dragging = (action == GLFW_PRESS);
|
|
}
|
|
|
|
if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) {
|
|
// pick up brick from hand
|
|
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];
|
|
}
|
|
}
|
|
if (has_active) {
|
|
hand_count--;
|
|
get_prewiews();
|
|
}
|
|
}
|
|
|
|
printf("click!\n");
|
|
if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_RELEASE) {
|
|
if (has_active) {
|
|
has_active = 0;
|
|
|
|
size_t min_dist = -1;
|
|
for (size_t i = 0; i < preview_count; i++) {
|
|
int active_x = active.front.x + mouse_x + DOMINO_WIDTH / 2;
|
|
int active_y = active.front.y + mouse_y + DOMINO_HEIGHT / 2;
|
|
int preview_x = camera_x + preview[i].front.x * EYE_SIZE + DOMINO_WIDTH / 2;
|
|
int preview_y = camera_y + preview[i].front.y * EYE_SIZE + DOMINO_HEIGHT / 2;
|
|
preview[i].front.val = (active_x - preview_x) * (active_x - preview_x) + (active_y - preview_y) * (active_y - preview_y);
|
|
if (min_dist == (size_t)-1) min_dist = 1;
|
|
if (preview[i].front.val < preview[min_dist].front.val) min_dist = i;
|
|
}
|
|
|
|
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;
|
|
}
|
|
preview_count = 0;
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
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 = 0},
|
|
.back = {.x = 13, .y = 5, .val = 3},
|
|
};
|
|
hand[1] = (struct brick) {
|
|
.front = {.x = 12, .y = 5, .val = 4},
|
|
.back = {.x = 13, .y = 5, .val = 1},
|
|
};
|
|
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, bool vertical) {
|
|
for (size_t y = 0; y < texture.height; y++) {
|
|
for (size_t x = 0; x < 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];
|
|
}
|
|
}
|
|
}
|
|
|
|
void
|
|
draw_character(
|
|
struct image canvas,
|
|
char *character,
|
|
size_t xpos, size_t ypos
|
|
) {
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
draw(canvas, texture, xpos + i * texture.width, ypos, 0);
|
|
}
|
|
}
|
|
|
|
//void
|
|
//draw_text(
|
|
//
|
|
//)
|
|
|
|
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, red_and_peach_dominoes[b->back.val][b->front.val], camera_x + b->front.x * EYE_SIZE, camera_y + b->front.y * EYE_SIZE, b->front.vertical);
|
|
}
|
|
|
|
// preview
|
|
for (size_t i = 0; i < preview_count; i++) {
|
|
draw(canvas, white_and_blue_dominoes[active.back.val][active.front.val], camera_x + preview[i].front.x * EYE_SIZE, camera_y + preview[i].front.y * EYE_SIZE, active.front.vertical);
|
|
}
|
|
|
|
// hand
|
|
for (size_t i = 0; i < hand_count; i++) {
|
|
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, 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), active.front.vertical);
|
|
}
|
|
|
|
// character
|
|
draw_character(canvas, "Hallo Welt", 50, 20); // debugging
|
|
|
|
}
|
|
|