summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Harley2018-06-07 20:30:09 +0100
committerTom Harley2018-06-07 20:30:09 +0100
commit97fddaa4841b3dc4ec3bab15318a6c6ea054761a (patch)
tree31c87e8250f81c8129e13e237ae43e53a8c19f3b
downloadsdl-box-97fddaa4841b3dc4ec3bab15318a6c6ea054761a.tar.gz
sdl-box-97fddaa4841b3dc4ec3bab15318a6c6ea054761a.zip

Initial commit

-rw-r--r--Makefile3
-rw-r--r--hmm.c116
2 files changed, 119 insertions, 0 deletions
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..9877584
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,3 @@
+LDLIBS=-lSDL2
+
+default: hmm
diff --git a/hmm.c b/hmm.c
new file mode 100644
index 0000000..1f6227a
--- /dev/null
+++ b/hmm.c
@@ -0,0 +1,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);
+}