summaryrefslogtreecommitdiff
path: root/hmm.c
blob: 1f6227aec00a5d421a5487da4ef063fa34211ab5 (plain) (blame)
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <SDL2/SDL.h>

#define debug(...) fprintf(stderr, __VA_ARGS__)

#define vec2(t) struct { \
  t x; \
  t y; \
}

#define vec3(t) struct { \
  t x; \
  t y; \
  t z; \
}

typedef vec2(int) pos;
typedef vec2(int) size;
typedef vec3(int) coord;

struct vertex {
  coord coord;
  struct vertex* connections[3];
};

struct cube {
  struct vertex points[8];
};

int init_video() {
  return SDL_Init(SDL_INIT_VIDEO);
}

int running = 1;

SDL_Window* window;

struct cube cube;

SDL_Window* init_window_raw(int x, int y, int w, int h, char* name) {
  SDL_Window* window = SDL_CreateWindow(
      name, x, y, w, h,
      SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE
  );
  if (!window) {
    debug("error creating window: %s\n", SDL_GetError());
    return NULL;
  }

  SDL_Renderer* rend = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
  if (!rend) {
    debug("error creating renderer for %p: %s\n", window, SDL_GetError());
    return NULL;
  }

  return window;
}

SDL_Window* init_window(pos pos, size size, char* name) {
  return init_window_raw(pos.x, pos.y, size.x, size.y, name);
}

int draw_line(coord a, coord b) {
  return SDL_RenderDrawLine(SDL_GetRenderer(window), a.x, a.y, b.x, b.y);
}

int draw_loop(void* data) {
  while (running) {
    for (int i = 0; i < 8; ++i) {
      for (int j = 0; j < 3; ++j) {
        draw_line(cube.points[i].coord, cube.points[i].connections[j]->coord);
      }
    }
  }
}

#define init_coord(a, b, c) { .x = a, .y = b, .z = c }
#define ptv(x) (&(vertices[x]))
#define connect(v, i, x) vertices[v].connections[i] = ptv(x)
#define connect3(v, a, b, c) do { \
  connect(v, 0, a); \
  connect(v, 1, b); \
  connect(v, 2, c); \
} while (0)

struct cube make_cube(int x, int y, int z, int s) {
  struct vertex vertices[8] = {
    { .coord = init_coord(x,   y,   z  ) }, // 0
    { .coord = init_coord(x+s, y,   z  ) }, // 1
    { .coord = init_coord(x,   y+s, z  ) }, // 2
    { .coord = init_coord(x+s, y+s, z  ) }, // 3
    { .coord = init_coord(x,   y,   z+s) }, // 4
    { .coord = init_coord(x+s, y,   z+s) }, // 5
    { .coord = init_coord(x,   y+s, z+s) }, // 6
    { .coord = init_coord(x+s, y+s, z+s) }  // 7
  };
  connect3(0, 1, 2, 4);
  connect3(1, 0, 3, 5);
  connect3(2, 0, 3, 6);
  connect3(3, 1, 2, 7);
  connect3(4, 0, 5, 6);
  connect3(5, 1, 4, 7);
  connect3(6, 2, 4, 7);
  connect3(7, 3, 5, 6);
}

int phys_loop(void* DATA) {
  while (1) {}
}

int main(int argc, char* args[]) {
  if (!init_video()) return 1;
  cube = make_cube(50, 50, 50, 20);
  window = init_window_raw(10, 10, 100, 100, "Hi");
  SDL_CreateThread(draw_loop, "trh_draw_loop", NULL);
  phys_loop(NULL);
}