diff options
| -rw-r--r-- | dat.h | 35 | ||||
| -rw-r--r-- | physics.c | 101 | ||||
| -rw-r--r-- | sdl3_main.c | 144 |
3 files changed, 136 insertions, 144 deletions
@@ -1,8 +1,39 @@ +#define BULLET_SIZE 2 +#define ENTITY_SIZE 16 + + typedef struct { float pos[2]; float size[2]; float vel[2]; } Body; -int checkcollision(Body *b1, Body *b2); -void resolvecoll(Body *b1, Body *b2, float p[2]); +typedef struct { + enum { + EPLAYER, + EGHOST, + EBULLET, + } type; + + bool active; + Body body; + float time; +} Entity; + +typedef struct { + Entity *self; + Entity *target; +} EntityCollision; + +typedef void (EntityCollisionProc)(Entity *self, Entity *other); + +void entphysics(float delta); + +void enqcoll(EntityCollision *c); +EntityCollision *deqcoll(void); + +extern Entity entitybuffer[2048]; +extern int ebufi; + +extern char map_data[]; +extern int map_width, map_height; @@ -1,8 +1,77 @@ #include <math.h> +#include <stdbool.h> +#include <stddef.h> #include "dat.h" static void mink(Body *a, Body *b, float *minx, float *miny, float *maxx, float *maxy); +static int checkcollision(Body *a, Body *b); +static void resolvecoll(Body *a, Body *b, float p[2]); + +static EntityCollision ecoll[2048]; +static int ecollstart, ecollend, ecollsi; + +void +entphysics(float delta) +{ + for(int i = 0; i < ebufi; i++) { + Entity *e = entitybuffer + i; + if(!e->active) + continue; + + for(int y = 0; y < map_height; y++) + for(int x = 0; x < map_width; x++) { + if(!map_data[x + y * map_width]) + continue; + + Body b = { + .pos = { + x * ENTITY_SIZE * 2.0, + y * ENTITY_SIZE * 2.0, + }, + .size = { ENTITY_SIZE, ENTITY_SIZE }, + }; + + if(checkcollision(&e->body, &b)) { + float p[2]; + resolvecoll(&e->body, &b, p); + + for(int i = 0; i < 2; i++) + e->body.pos[i] -= p[i]; + + if(p[0] != 0.0) { + e->body.vel[0] = 0; + } else { + e->body.vel[1] = 0; + } + } + } + + for(int j = i; j < ebufi; j++) { + Entity *f = entitybuffer + j; + if(!f->active) + break; + + if(checkcollision(&e->body, &f->body)) { + EntityCollision cl; + + cl.self = e; + cl.target = f; + + enqcoll(&cl); + } + } + } + + for(int i = 0; i < ebufi; i++) { + Entity *e = entitybuffer + i; + if(!e->active) + continue; + + for(int j = 0; j < 2; j++) + e->body.pos[j] += e->body.vel[j] * delta; + } +} int checkcollision(Body *a, Body *b) @@ -47,3 +116,35 @@ mink(Body *a, Body *b, float *minx, float *miny, float *maxx, float *maxy) *miny = p[1] - s[1]; *maxy = p[1] + s[1]; } + +void +enqcoll(EntityCollision *c) +{ + if(ecollsi >= 2048) + return; + + ecollsi++; + + ecoll[ecollend++] = *c; + if(ecollend >= 2048) + ecollend = 0; +} + +EntityCollision * +deqcoll(void) +{ + EntityCollision *c; + if(ecollsi <= 0) + return NULL; + + ecollsi--; + + c = ecoll + ecollstart++; + + if(ecollstart >= 2048) + ecollstart = 0; + + return c; +} + + diff --git a/sdl3_main.c b/sdl3_main.c index 507a64c..b5455ce 100644 --- a/sdl3_main.c +++ b/sdl3_main.c @@ -8,43 +8,6 @@ #include "dat.h" -#define BULLET_SIZE 2 -#define ENTITY_SIZE 16 - -typedef struct { - Body body; -} Player; - -typedef struct { - Body body; - bool active; -} Ghost; - -typedef struct { - Body body; - float time; - bool active; -} Bullet; - -typedef struct { - enum { - EPLAYER, - EGHOST, - EBULLET, - } type; - - bool active; - Body body; - float time; -} Entity; - -typedef struct { - Entity *self; - Entity *target; -} EntityCollision; - -typedef void (EntityCollisionProc)(Entity *self, Entity *other); - static void render(void); static void update(float delta); static void process_events(void); @@ -52,11 +15,6 @@ static void process_events(void); static Entity *allocentity(void); static void freeentity(Entity *); -static void entphysics(float delta); - -static void enqcoll(EntityCollision *c); -static EntityCollision *deqcoll(void); - static EntityCollisionProc bulletcollproc; static EntityCollisionProc *collisionProcs[] = { @@ -70,20 +28,14 @@ static Uint64 old_ticks, new_ticks; static Entity *player; -static Entity entitybuffer[2048]; -static int ebufi; - -static EntityCollision ecoll[2048]; -static int ecollstart, ecollend, ecollsi; +Entity entitybuffer[2048]; +int ebufi; static float camera_x, camera_y; static bool shot; static int shotx, shoty; -extern char map_data[]; -extern int map_width, map_height; - int main() { @@ -368,98 +320,6 @@ freeentity(Entity *e) } void -entphysics(float delta) -{ - for(int i = 0; i < ebufi; i++) { - Entity *e = entitybuffer + i; - if(!e->active) - continue; - - for(int y = 0; y < map_height; y++) - for(int x = 0; x < map_width; x++) { - if(!map_data[x + y * map_width]) - continue; - - Body b = { - .pos = { - x * ENTITY_SIZE * 2.0, - y * ENTITY_SIZE * 2.0, - }, - .size = { ENTITY_SIZE, ENTITY_SIZE }, - }; - - if(checkcollision(&e->body, &b)) { - float p[2]; - resolvecoll(&e->body, &b, p); - - for(int i = 0; i < 2; i++) - e->body.pos[i] -= p[i]; - - if(p[0] != 0.0) { - e->body.vel[0] = 0; - } else { - e->body.vel[1] = 0; - } - } - } - - for(int j = i; j < ebufi; j++) { - Entity *f = entitybuffer + j; - if(!f->active) - break; - - if(checkcollision(&e->body, &f->body)) { - EntityCollision cl; - - cl.self = e; - cl.target = f; - - enqcoll(&cl); - } - } - } - - for(int i = 0; i < ebufi; i++) { - Entity *e = entitybuffer + i; - if(!e->active) - continue; - - for(int j = 0; j < 2; j++) - e->body.pos[j] += e->body.vel[j] * delta; - } -} - -void -enqcoll(EntityCollision *c) -{ - if(ecollsi >= 2048) - return; - - ecollsi++; - - ecoll[ecollend++] = *c; - if(ecollend >= 2048) - ecollend = 0; -} - -EntityCollision * -deqcoll(void) -{ - EntityCollision *c; - if(ecollsi <= 0) - return NULL; - - ecollsi--; - - c = ecoll + ecollstart++; - - if(ecollstart >= 2048) - ecollstart = 0; - - return c; -} - -void bulletcollproc(Entity *self, Entity *target) { if(target->type == EGHOST) { |
