feat: drag camera
This commit is contained in:
parent
001b91e335
commit
22922a63b0
26
game.c
26
game.c
|
@ -14,7 +14,7 @@
|
|||
#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 mouse_x = 0, mouse_y = 0;
|
||||
|
||||
struct bricks bricks = {0};
|
||||
|
||||
|
@ -27,6 +27,10 @@ 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},
|
||||
|
@ -82,8 +86,7 @@ void get_prewiews() {
|
|||
void print_brick(struct brick b) {
|
||||
struct eye *eyes = (struct eye*) &b;
|
||||
for (size_t i = 0; i < 2; i++) {
|
||||
printf("{.x = %zu, .y = %zu, .val = %zu, .vertical = %zu}" , eyes[i].x, eyes[i].y, eyes[i].val, eyes[i].vertical);
|
||||
}
|
||||
printf("{.x = %d, .y = %d, .val = %d, .vertical = %d}" , eyes[i].x, eyes[i].y, eyes[i].val, eyes[i].vertical); }
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
|
@ -111,6 +114,10 @@ void key_callback(int key, int scancode, int action, int mods) {
|
|||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
@ -118,6 +125,10 @@ void cursor_position_callback(int xpos, int 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++) {
|
||||
|
@ -150,8 +161,8 @@ void mouse_button_callback(int button, int action, int mods) {
|
|||
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 = preview[i].front.x * EYE_SIZE + DOMINO_WIDTH / 2;
|
||||
int preview_y = preview[i].front.y * EYE_SIZE + 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;
|
||||
|
@ -252,11 +263,12 @@ 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, red_and_peach_dominoes[b->back.val][b->front.val], b->front.x * EYE_SIZE, b->front.y * EYE_SIZE, b->front.vertical);
|
||||
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], preview[i].front.x * EYE_SIZE, preview[i].front.y * EYE_SIZE, active.front.vertical);
|
||||
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
|
||||
|
|
Loading…
Reference in a new issue