From a09538068d0b423561c3f2a8336d8de8efddacd4 Mon Sep 17 00:00:00 2001 From: Tom Harley Date: Sat, 25 Jan 2020 03:48:08 +0000 Subject: Rename source file --- Makefile | 2 +- box.c | 116 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ hmm.c | 116 --------------------------------------------------------------- 3 files changed, 117 insertions(+), 117 deletions(-) create mode 100644 box.c delete mode 100644 hmm.c diff --git a/Makefile b/Makefile index 61824fd..d8e9fdd 100644 --- a/Makefile +++ b/Makefile @@ -3,4 +3,4 @@ CXXFLAGS = $(shell sdl2-config --cppflags) LDFLAGS = $(shell sdl2-config --ldflags) LDLIBS = $(shell sdl2-config --libs) -default: hmm +default: box diff --git a/box.c b/box.c new file mode 100644 index 0000000..a083e3a --- /dev/null +++ b/box.c @@ -0,0 +1,116 @@ +#include + +#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); +} diff --git a/hmm.c b/hmm.c deleted file mode 100644 index a083e3a..0000000 --- a/hmm.c +++ /dev/null @@ -1,116 +0,0 @@ -#include - -#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); -} -- cgit v1.2.1