diff options
| -rw-r--r-- | libphysics/test/Makefile | 6 | ||||
| -rw-r--r-- | libphysics/test/aabbcheck.c | 115 |
2 files changed, 120 insertions, 1 deletions
diff --git a/libphysics/test/Makefile b/libphysics/test/Makefile index c367549..7826f84 100644 --- a/libphysics/test/Makefile +++ b/libphysics/test/Makefile @@ -1,7 +1,7 @@ CFLAGS=-I../../include -g -O3 -flto LDFLAGS=-L../ -lphysics `pkg-config --libs sdl3` -lm -flto -O3 -all: vis perf +all: vis perf aabbcheck clean: rm -f vis @@ -13,6 +13,10 @@ vis: ../libphysics.a vis.o perf: ../libphysics.a perf.o $(CC) $^ $(LDFLAGS) -o $@ +aabbcheck: ../libphysics.a aabbcheck.o + $(CC) $^ $(LDFLAGS) -o $@ + + %.o: %.c $(CC) $< $(CFLAGS) -c -o $@ diff --git a/libphysics/test/aabbcheck.c b/libphysics/test/aabbcheck.c new file mode 100644 index 0000000..788035b --- /dev/null +++ b/libphysics/test/aabbcheck.c @@ -0,0 +1,115 @@ +#include <stdio.h> +#include <stdbool.h> +#include <SDL3/SDL.h> + +#include <vecmath.h> +#include <physics.h> +#include "../dat.h" + +static void process_events(); + +static SDL_Window *window; +static SDL_Renderer *renderer; +static bool running; +static Uint64 old_ticks, new_ticks; + +static void renderbody(Body *); + +static Body mousebody = { + .size = { 25, 25 }, +}; + +static Body body = { + .pos = { 400, 300 }, + .size = { 200, 200 } +}; + +int +main() +{ + if(!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO)) { + fprintf(stderr, "SDL_Init(): %s\n", SDL_GetError()); + return 1; + } + + if(!SDL_CreateWindowAndRenderer("window", 800, 600, SDL_WINDOW_OPENGL, &window, &renderer)) { + fprintf(stderr, "SDL_CreateWindowAndRenderer: %s\n", SDL_GetError()); + return -1; + } + + running = true; + old_ticks = SDL_GetTicksNS(); + while(running) { + double delta; + process_events(); + + new_ticks = SDL_GetTicksNS(); + delta = (new_ticks - old_ticks) / 1000000000.0; + old_ticks = new_ticks; + + SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0x00); + SDL_RenderClear(renderer); + if(phxaabbcheck(&body, &mousebody)) { + SDL_SetRenderDrawColor(renderer, 0x00, 0xFF, 0x00, 0x00); + } else { + SDL_SetRenderDrawColor(renderer, 0xFF, 0x00, 0x00, 0x00); + } + + renderbody(&body); + renderbody(&mousebody); + + if(phxaabbcheck(&body, &mousebody)) { + float n[2], p[2]; + + phxaabbresolv(&body, &mousebody, p, n); + SDL_RenderLine(renderer, + body.pos[0], + body.pos[1], + body.pos[0] + n[0] * body.size[0], + body.pos[1] + n[1] * body.size[1] + ); + + SDL_RenderLine(renderer, + mousebody.pos[0] - n[0] * mousebody.size[0], + mousebody.pos[1] - n[1] * mousebody.size[1], + mousebody.pos[0] - n[0] * mousebody.size[0] + p[0], + mousebody.pos[1] - n[1] * mousebody.size[1] + p[1] + ); + } + + SDL_RenderPresent(renderer); + } + + SDL_DestroyWindow(window); + SDL_Quit(); + return 0; +} + +void +process_events() +{ + SDL_Event event; + + while(SDL_PollEvent(&event)) { + switch(event.type) { + case SDL_EVENT_QUIT: + running = false; + break; + case SDL_EVENT_MOUSE_MOTION: + mousebody.pos[0] = event.motion.x; + mousebody.pos[1] = event.motion.y; + } + } +} + +void +renderbody(Body *b) +{ + SDL_RenderRect(renderer, &(SDL_FRect){ + .x = b->pos[0] - b->size[0], + .y = b->pos[1] - b->size[1], + .w = b->size[0] * 2.0, + .h = b->size[1] * 2.0 + }); +} + |
