diff options
Diffstat (limited to 'libphysics/test')
| -rw-r--r-- | libphysics/test/Makefile | 19 | ||||
| -rw-r--r-- | libphysics/test/vis.c | 120 |
2 files changed, 139 insertions, 0 deletions
diff --git a/libphysics/test/Makefile b/libphysics/test/Makefile new file mode 100644 index 0000000..eb1d5cb --- /dev/null +++ b/libphysics/test/Makefile @@ -0,0 +1,19 @@ +CFLAGS=-I../../include -g +LDFLAGS=-L../ -lphysics `pkg-config --libs sdl3` + +all: vis + +clean: + rm -f vis + rm -f vis.o + +vis: ../libphysics.a vis.o + $(CC) $^ $(LDFLAGS) -o $@ + +%.o: %.c + $(CC) $< $(CFLAGS) -c -o $@ + +../libphysics.a: + $(MAKE) -C .. all + +.PHONY: all clean diff --git a/libphysics/test/vis.c b/libphysics/test/vis.c new file mode 100644 index 0000000..d1f3f6f --- /dev/null +++ b/libphysics/test/vis.c @@ -0,0 +1,120 @@ +#include <SDL3/SDL.h> +#include <vecmath.h> +#include <physics.h> + +#include <stdlib.h> +#include <assert.h> + +#include "../dat.h" + +#define SBODY 256 + +static void process_events(void); +static void render(void); + +static SDL_Window *window; +static SDL_Renderer *renderer; +static bool running; +static Uint64 old_ticks, new_ticks; + +void +renderrect(vec2 p, vec2 s) +{ + SDL_RenderRect(renderer, &(SDL_FRect){ + .x = p[0] - s[0], + .y = p[1] - s[1], + .w = s[0] * 2.0, + .h = s[1] * 2.0 + }); +} + +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; + } + SDL_SetRenderVSync(renderer, 1); + + #define FLOAT_RAND (2 * (rand() / (float)RAND_MAX) - 1) + + for(int i = 0; i < SBODY; i++) { + Body *b = phxnew(BTYPE_AABB); + assert(b != NULL); + phxsetpos(b, rand() % 800, rand() % 600); + phxsetsize(b, 16.0, 16.0); + phxapplyaccel(b, (float[]){ 100000 * FLOAT_RAND, 100000 * FLOAT_RAND }); + } + + phxsetmap(8, 8, (int[]) { + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 1, 0, 0, 0, 1, 0, 0, + 0, 1, 1, 0, 0, 1, 1, 0, + 0, 1, 1, 0, 0, 1, 1, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 1, 1, 0, 0, 1, 1, 0, + 0, 1, 1, 0, 0, 1, 1, 0, + 0, 0, 1, 0, 0, 0, 1, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + }); + + running = 1; + old_ticks = SDL_GetTicksNS(); + while(running) { + CollisionEvent event; + double delta; + process_events(); + + new_ticks = SDL_GetTicksNS(); + delta = (new_ticks - old_ticks) / 1000000000.0; + old_ticks = new_ticks; + phxtick(delta); + while(phxnextcollevent(&event)) { + printf("Collision with %p and %p\n", event.body1, event.body2); + } + + render(); + } + + 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; + } + } +} + +void +render(void) +{ + SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0xFF); + SDL_RenderClear(renderer); + + SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF); + for(int i = 0; i < phxbodypoolsize; i++) { + vec2 p, s; + Body *b = phxbodypool + i; + if(!b->active) + continue; + renderrect(b->pos, b->size); + } + + SDL_RenderPresent(renderer); +} |
