summaryrefslogtreecommitdiff
path: root/game.c
blob: 56f2a4a7b4c6483a620494b28f52c490e07d200f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#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))

int mouse_x = 0, mouse_y = 0;
int eyes_front = 0, eyes_back = 0;

struct bricks bricks = {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},
      }
      );
}

void draw_image(decoded_image img) {
  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 * EYE_SIZE + y) * img.width + b->front.x * EYE_SIZE + x] =
          (*(uint32_t*) &domino[b->back.val][b->front.val][y * DOMINO_WIDTH * BYTES_PER_PIXEL + x * BYTES_PER_PIXEL]);
      }
    }
  }

  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]);
    }
  }
}