final commit (i think)

This commit is contained in:
wiikifox 2024-10-14 16:35:59 -04:00
commit 75180436ba
36 changed files with 1726 additions and 0 deletions

1
compile_flags.txt Normal file
View file

@ -0,0 +1 @@
-xc-header

147
src/#game.c# Normal file
View file

@ -0,0 +1,147 @@
#include "game.h"
#include "collision.h"
#include "linked_list.h"
#include "object.h"
#include "physics.h"
#include "vector.h"
#include <SDL2/SDL.h>
#include <SDL2/SDL_render.h>
#include <stdio.h>
#include <stdlib.h>
static void
update_object(struct game_data *gd, struct object *o);
static void
render_object(SDL_Renderer *renderer, struct object *o);
struct game_data *
game_init() {
struct game_data *gd = malloc(sizeof(struct game_data));
SDL_Init(SDL_INIT_EVERYTHING);
SDL_CreateWindowAndRenderer(512,
512,
0,
&gd->window,
&gd->renderer);
gd->ticks = 0;
gd->object_count = 0;
gd->object_list = NULL;
gd->drag = GAME_DRAG;
gd->gravity[0] = GAME_GRAVITY_X;
gd->gravity[1] = GAME_GRAVITY_Y;
return gd;
}
void
game_deinit(struct game_data *gd) {
struct llist *l;
while ((l = llist_pop_head(&(gd->object_list)))) {
object_deinit(gd, l->data);
free(l);
}
SDL_DestroyWindow(gd->window);
SDL_DestroyRenderer(gd->renderer);
free(gd);
SDL_Quit();
}
void
game_tick(struct game_data *gd) {
printf("tick: %lu\n", gd->ticks++);
struct llist *l = gd->object_list;
if (l == NULL) {
return;
}
do {
update_object(gd, l->data);
} while ((l = l->next));
}
void
game_render(struct game_data *gd) {
struct llist *l = gd->object_list;
if (l == NULL) {
return;
}
SDL_SetRenderDrawColor(gd->renderer, 0x00, 0x00, 0x00, 0xFF);
SDL_RenderClear(gd->renderer);
do {
render_object(gd->renderer, l->data);
} while ((l = l->next));
SDL_RenderPresent(gd->renderer);
}
/* static functions */
static void
update_object(struct game_data *gd, struct object *o) {
printf("updating object\n");
if (o->flags & OBJFLAG_STATIC) {
printf("skipping static object\n");
return;
}
printf("integrating physics\n");
vector_add(o->physics->d, o->physics->v, o->physics->d);
vector_approach_zero(o->physics->v, gd->drag, o->physics->v);
vector_add(o->physics->v, gd->gravity, o->physics->v);
printf("checking for collisions\n");
struct llist *l = gd->object_list;
vector v = {0, 0};
unsigned c = 0;
do {
struct object *n = l->data;
if (n == o) {
continue;
}
if (object_detect_overlap(COLFLAG_PUSH,
COLFLAG_PUSH,
o,
n,
v)) {
printf("checking objects overlap\n");
printf("response: %f, %f\n", v[0], v[1]);
vector_add(o->physics->d, v, o->physics->d);
printf("checked objects overlap\n");
}
printf("count: %i\n", c++);
} while ((l = l->next));
}
static void
render_object(SDL_Renderer *r, struct object *o) {
SDL_Rect rect;
struct llist *l = o->collisions;
struct collision *c;
if (l == NULL) {
return;
}
do {
c = l->data;
rect.x = c->rect.x + o->physics->d[0];
rect.y = c->rect.x + o->physics->d[1];
rect.w = c->rect.w;
rect.h = c->rect.h;
if (c->flags & COLFLAG_ENABLED) {
SDL_SetRenderDrawColor(r, 0xFF, 0xFF, 0xFF, 0xFF);
} else {
SDL_SetRenderDrawColor(r, 0xFF, 0xFF, 0xFF, 0x88);
}
SDL_RenderDrawRect(r, &rect);
} while ((l = l->next));
}

1
src/.#game.c Symbolic link
View file

@ -0,0 +1 @@
wiikifox@neoden.904

BIN
src/a.out Executable file

Binary file not shown.

28
src/character.c Normal file
View file

@ -0,0 +1,28 @@
#include "character.h"
#include "physics.h"
#include <stdlib.h>
struct character *
character_init(char *name, float mass, uint8_t collision_count,
struct collision **collisions) {
struct character *c = malloc(sizeof(struct character));
c->flags = 0;
c->name = name;
c->collision_count = collision_count;
c->damage = 0;
c->physics = physics_object_init(mass, 0, 0);
c->collisions = collisions;
c->state = CHARSTATE_IDLE;
return c;
}
void
character_deinit(struct character *c) {
uint8_t i;
for (i = c->collision_count - 1; i <= 0; ++i) {
collision_deinit(c->collisions[i]);
}
physics_object_deinit(c->physics);
free(c);
}

28
src/character.c~ Normal file
View file

@ -0,0 +1,28 @@
#include "character.h"
#include "physics.h"
#include <stdlib.h>
struct character *
character_init(char *name, float mass, uint8_t collision_count,
struct collision **collisions) {
struct character *c = malloc(sizeof(struct character));
c->flags = 0;
c->name = name;
c->collision_count = collision_count;
c->damage = 0;
c->physics = create_physics_object(mass, 0, 0);
c->collisions = collisions;
c->state = CHARSTATE_IDLE;
return c;
}
void
character_deinit(struct character *c) {
uint8_t i;
for (i = c->collision_count - 1; i <= 0; ++i) {
collision_deinit(c->collisions[i]);
}
destroy_physics_object(c->physics);
free(c);
}

37
src/character.h Normal file
View file

@ -0,0 +1,37 @@
#ifndef CHARACTER_H
#define CHARACTER_H
#include "collision.h"
#include <stdint.h>
/* State list for a `character' object. */
enum character_states { CHARSTATE_IDLE };
/* Flag list for a `character' object. */
enum character_flags; /* TODO */
/* TODO */
struct character {
uint8_t flags;
char *name;
uint8_t collision_count;
uint16_t damage;
struct physics_object *physics;
struct collision **collisions;
enum character_states state;
};
/* Creates a `character' object and returns its pointer.
*/
struct character *
character_init(char *name,
float mass,
uint8_t collision_count,
struct collision **collisions); /* can be null */
/* Frees the memory allocated for the `character' object CHARACTER.
*/
void
character_deinit(struct character *character);
#endif /* CHARACTER_H */

36
src/character.h~ Normal file
View file

@ -0,0 +1,36 @@
#ifndef CHARACTER_H
#define CHARACTER_H
#include <stdint.h>
#include "collision.h"
/* State list for a `character' object. */
enum character_states { CHARSTATE_IDLE };
/* Flag list for a `character' object. */
enum character_flags; /* TODO */
/* TODO */
struct character {
uint8_t flags;
char *name;
uint8_t collision_count;
uint16_t damage;
struct physics_object *physics;
struct collision **collisions;
enum character_states state;
};
/* Creates a `character' object and returns its pointer.
*/
struct character *
character_init(char *name, float mass,
uint8_t collision_count,
struct collision **collisions); /* can be null */
/* Frees the memory allocated for the `character' object CHARACTER.
*/
void
character_deinit(struct character *character);
#endif /* CHARACTER_H */

80
src/collision.c Normal file
View file

@ -0,0 +1,80 @@
#include "collision.h"
#include "utils.h"
#include <SDL2/SDL_rect.h>
#include <stdio.h>
#include <stdlib.h>
struct collision *
collision_init(uint8_t flags,
uint16_t layers,
int x,
int y,
int w,
int h) {
struct collision *c = malloc(sizeof(struct collision));
c->flags = flags;
c->layers = layers;
c->rect.x = x;
c->rect.y = y;
c->rect.w = w;
c->rect.h = h;
return c;
}
void
collision_deinit(struct collision *c) {
free(c);
}
bool
collision_detect_overlap(struct collision *a,
struct collision *b,
vector oa,
vector ob,
vector r) {
if (!((a->flags & COLFLAG_ENABLED) &&
(b->flags & COLFLAG_ENABLED))) {
return false;
}
if (!(a->layers & b->layers)) {
return false;
}
printf("check collision\n");
SDL_Rect ra;
SDL_Rect rb;
ra.x = a->rect.x + oa[0];
ra.y = a->rect.y + oa[1];
ra.w = a->rect.w;
ra.h = a->rect.h;
rb.x = b->rect.x + ob[0];
rb.y = b->rect.y + ob[1];
rb.w = b->rect.w;
rb.h = b->rect.h;
if (SDL_HasIntersection(&ra, &rb)) {
if (ra.x > rb.x) { /* collided from the right */
r[0] = rb.x + rb.w - ra.x;
}
if (ra.x < rb.x) { /* collided from the left */
r[0] = rb.x - ra.x + ra.w;
}
if (ra.y > rb.y) { /* collided from the bottom */
r[1] = ra.y + rb.h - ra.y;
}
if (ra.y < rb.y) { /* collided from the top */
r[1] = rb.y - ra.y + ra.h;
}
return true;
}
return false;
}

81
src/collision.c~ Normal file
View file

@ -0,0 +1,81 @@
#include "collision.h"
#include <SDL2/SDL_rect.h>
#include <stdio.h>
#include <stdlib.h>
struct collision *
collision_init(uint8_t flags,
uint16_t layers,
int x,
int y,
int w,
int h) {
struct collision *c = malloc(sizeof(struct collision));
c->flags = flags;
c->layers = layers;
c->rect.x = x;
c->rect.y = y;
c->rect.w = w;
c->rect.h = h;
return c;
}
void
collision_deinit(struct collision *c) {
free(c);
}
extern vector *
collision_detect_overlap(struct collision *a,
struct collision *b,
vector offset_a,
vector offset_b) {
if (!((a->flags & COLFLAG_ENABLED) &&
(b->flags & COLFLAG_ENABLED))) {
return NULL;
}
if (!(a->layers & b->layers)) {
return NULL;
}
printf("check collision\n");
SDL_Rect ra;
SDL_Rect rb;
ra.x = a->rect.x + offset_a[0];
ra.y = a->rect.y + offset_a[1];
ra.w = a->rect.w;
ra.h = a->rect.h;
rb.x = b->rect.x + offset_b[0];
rb.y = b->rect.y + offset_b[1];
rb.w = b->rect.w;
rb.h = b->rect.h;
if (SDL_HasIntersection(&ra, &rb)) {
vector *ret = malloc(sizeof(vector));
*ret[0] = 0;
*ret[1] = 0;
if (ra.x > rb.x) { /* collided from the right */
*ret[0] = rb.x + rb.w - ra.x;
}
if (ra.x < rb.x) { /* collided from the left */
*ret[0] = rb.x - ra.x + ra.w;
}
if (ra.y > rb.y) { /* collided from the bottom */
*ret[1] = ra.y + rb.h - ra.y;
}
if (ra.y < rb.y) { /* collided from the top */
*ret[1] = rb.y - ra.y + ra.h;
}
return ret;
}
return NULL;
}

47
src/collision.h Normal file
View file

@ -0,0 +1,47 @@
#ifndef COLLISION_H
#define COLLISION_H
#include "utils.h"
#include "vector.h"
#include <SDL2/SDL_rect.h>
#include <stdint.h>
/* TODO */
enum collision_flags {
COLFLAG_ENABLED = 0x01,
COLFLAG_PUSH = 0x02,
};
/* TODO */
struct collision {
uint8_t flags;
uint16_t layers;
SDL_Rect rect;
};
struct collision * /* `collision' constructor.
*/
collision_init(uint8_t flags,
uint16_t layers,
int x,
int y,
int w,
int h);
void /* `collision' destructor.
*/
collision_deinit(struct collision *collision);
bool /* Returns true if A and B intersect.
* A and B must share at least one collision layer.
*/
collision_detect_overlap(struct collision *a,
struct collision *b,
vector offset_a,
vector offset_b,
vector result);
#endif /* COLLISION_H */

45
src/collision.h~ Normal file
View file

@ -0,0 +1,45 @@
#ifndef COLLISION_H
#define COLLISION_H
#include "vector.h"
#include <SDL2/SDL_rect.h>
#include <stdint.h>
/* TODO */
enum collision_flags {
COLFLAG_ENABLED = 0x01,
COLFLAG_PUSH = 0x02,
};
/* TODO */
struct collision {
uint8_t flags;
uint16_t layers;
SDL_Rect rect;
};
struct collision * /* `collision' constructor.
*/
collision_init(uint8_t flags,
uint16_t layers,
int x,
int y,
int w,
int h);
void /* `collision' destructor.
*/
collision_deinit(struct collision *collision);
vector * /* Returns true if A and B intersect.
* A and B must share at least one collision layer.
*/
collision_detect_overlap(struct collision *a,
struct collision *b,
vector offset_a,
vector offset_b);
#endif /* COLLISION_H */

144
src/game.c Normal file
View file

@ -0,0 +1,144 @@
#include "game.h"
#include "collision.h"
#include "linked_list.h"
#include "object.h"
#include "physics.h"
#include "vector.h"
#include <SDL2/SDL.h>
#include <SDL2/SDL_render.h>
#include <stdio.h>
#include <stdlib.h>
static void
update_object(struct game_data *gd, struct object *o);
static void
render_object(SDL_Renderer *renderer, struct object *o);
struct game_data *
game_init() {
struct game_data *gd = malloc(sizeof(struct game_data));
SDL_Init(SDL_INIT_EVERYTHING);
SDL_CreateWindowAndRenderer(512,
512,
0,
&gd->window,
&gd->renderer);
gd->ticks = 0;
gd->object_count = 0;
gd->object_list = NULL;
gd->drag = GAME_DRAG;
gd->gravity[0] = GAME_GRAVITY_X;
gd->gravity[1] = GAME_GRAVITY_Y;
return gd;
}
void
game_deinit(struct game_data *gd) {
struct llist *l;
while ((l = llist_pop_head(&(gd->object_list)))) {
object_deinit(gd, l->data);
free(l);
}
SDL_DestroyWindow(gd->window);
SDL_DestroyRenderer(gd->renderer);
free(gd);
SDL_Quit();
}
void
game_tick(struct game_data *gd) {
printf("tick: %lu\n", gd->ticks++);
struct llist *l = gd->object_list;
if (l == NULL) {
return;
}
do {
update_object(gd, l->data);
} while ((l = l->next));
}
void
game_render(struct game_data *gd) {
struct llist *l = gd->object_list;
SDL_SetRenderDrawColor(gd->renderer, 0x00, 0x00, 0x00, 0xFF);
SDL_RenderClear(gd->renderer);
do {
render_object(gd->renderer, l->data);
} while ((l = l->next));
SDL_RenderPresent(gd->renderer);
}
/* static functions */
static void
update_object(struct game_data *gd, struct object *o) {
printf("updating object\n");
if (o->flags & OBJFLAG_STATIC) {
printf("skipping static object\n");
return;
}
printf("integrating physics\n");
vector_add(o->physics->d, o->physics->v, o->physics->d);
vector_approach_zero(o->physics->v, gd->drag, o->physics->v);
vector_add(o->physics->v, gd->gravity, o->physics->v);
printf("checking for collisions\n");
struct llist *l = gd->object_list;
vector v = {0, 0};
unsigned c = 0;
do {
struct object *n = l->data;
if (n == o) {
continue;
}
if (object_detect_overlap(COLFLAG_PUSH,
COLFLAG_PUSH,
o,
n,
v)) {
printf("checking objects overlap\n");
printf("response: %f, %f\n", v[0], v[1]);
vector_add(o->physics->d, v, o->physics->d);
printf("checked objects overlap\n");
}
printf("count: %i\n", c++);
} while ((l = l->next));
}
static void
render_object(SDL_Renderer *r, struct object *o) {
SDL_Rect rect;
struct llist *l = o->collisions;
struct collision *c;
if (l == NULL) {
return;
}
do {
c = l->data;
rect.x = c->rect.x + o->physics->d[0];
rect.y = c->rect.x + o->physics->d[1];
rect.w = c->rect.w;
rect.h = c->rect.h;
if (c->flags & COLFLAG_ENABLED) {
SDL_SetRenderDrawColor(r, 0xFF, 0xFF, 0xFF, 0xFF);
} else {
SDL_SetRenderDrawColor(r, 0xFF, 0xFF, 0xFF, 0x88);
}
SDL_RenderDrawRect(r, &rect);
} while ((l = l->next));
}

147
src/game.c~ Normal file
View file

@ -0,0 +1,147 @@
#include "game.h"
#include "collision.h"
#include "linked_list.h"
#include "object.h"
#include "physics.h"
#include "vector.h"
#include <SDL2/SDL.h>
#include <SDL2/SDL_render.h>
#include <stdio.h>
#include <stdlib.h>
static void
update_object(struct game_data *gd, struct object *o);
static void
render_object(SDL_Renderer *renderer, struct object *o);
struct game_data *
game_init() {
struct game_data *gd = malloc(sizeof(struct game_data));
SDL_Init(SDL_INIT_EVERYTHING);
SDL_CreateWindowAndRenderer(512,
512,
0,
&gd->window,
&gd->renderer);
gd->ticks = 0;
gd->object_count = 0;
gd->object_list = NULL;
gd->drag = GAME_DRAG;
gd->gravity[0] = GAME_GRAVITY_X;
gd->gravity[1] = GAME_GRAVITY_Y;
return gd;
}
void
game_deinit(struct game_data *gd) {
struct llist *l;
while ((l = llist_pop_head(&(gd->object_list)))) {
object_deinit(gd, l->data);
free(l);
}
SDL_DestroyWindow(gd->window);
SDL_DestroyRenderer(gd->renderer);
free(gd);
SDL_Quit();
}
void
game_tick(struct game_data *gd) {
printf("tick: %lu\n", gd->ticks++);
struct llist *l = gd->object_list;
if (l == NULL) {
return;
}
do {
update_object(gd, l->data);
} while ((l = l->next));
}
void
game_render(struct game_data *gd) {
struct llist *l = gd->object_list;
if (l == NULL) {
return;
}
SDL_SetRenderDrawColor(gd->renderer, 0x00, 0x00, 0x00, 0xFF);
SDL_RenderClear(gd->renderer);
do {
render_object(gd->renderer, l->data);
} while ((l = l->next));
SDL_RenderPresent(gd->renderer);
}
/* static functions */
static void
update_object(struct game_data *gd, struct object *o) {
printf("updating object\n");
if (o->flags & OBJFLAG_STATIC) {
printf("skipping static object\n");
return;
}
printf("integrating physics\n");
vector_add(o->physics->d, o->physics->v, o->physics->d);
vector_approach_zero(o->physics->v, gd->drag, o->physics->v);
vector_add(o->physics->v, gd->gravity, o->physics->v);
printf("checking for collisions\n");
struct llist *l = gd->object_list;
vector v = {0, 0};
unsigned c = 0;
do {
struct object *n = l->data;
if (n == o) {
continue;
}
if (object_detect_overlap(COLFLAG_PUSH,
COLFLAG_PUSH,
o,
n,
v)) {
printf("checking objects overlap\n");
printf("response: %f, %f\n", v[0], v[1]);
vector_add(o->physics->d, v, o->physics->d);
printf("checked objects overlap\n");
}
printf("count: %i\n", c++);
} while ((l = l->next));
}
static void
render_object(SDL_Renderer *r, struct object *o) {
SDL_Rect rect;
struct llist *l = o->collisions;
struct collision *c;
if (l == NULL) {
return;
}
do {
c = l->data;
rect.x = c->rect.x + o->physics->d[0];
rect.y = c->rect.x + o->physics->d[1];
rect.w = c->rect.w;
rect.h = c->rect.h;
if (c->flags & COLFLAG_ENABLED) {
SDL_SetRenderDrawColor(r, 0xFF, 0xFF, 0xFF, 0xFF);
} else {
SDL_SetRenderDrawColor(r, 0xFF, 0xFF, 0xFF, 0x88);
}
SDL_RenderDrawRect(r, &rect);
} while ((l = l->next));
}

48
src/game.h Normal file
View file

@ -0,0 +1,48 @@
#ifndef GAME_H
#define GAME_H
#include "linked_list.h"
#include "object.h"
#include "vector.h"
#include <SDL2/SDL.h>
#include <stdint.h>
#define GAME_GRAVITY_X 0.0
#define GAME_GRAVITY_Y 0.098
#define GAME_DRAG 0.005
/* forward declarations */
struct game_data { /* Stores all the game variables. Opaque.
*/
unsigned long ticks; /* tick count */
struct llist *object_list; /* linked list with `object's */
uint16_t object_count; /* total active object amount */
SDL_Window *window;
SDL_Renderer *renderer;
vector gravity;
double drag;
};
struct game_data * /* Initializes the engine and returns a pointer to
* its environment.
*/
game_init();
void /* Frees all the memory used by the engine.
*/
game_deinit(struct game_data *gd);
void /* Steps the game state once.
*/
game_tick(struct game_data *gd);
void /* Renders the current game state.
*/
game_render(struct game_data *gd);
#endif /* GAME_H */

47
src/game.h~ Normal file
View file

@ -0,0 +1,47 @@
#ifndef GAME_H
#define GAME_H
#include "linked_list.h"
#include "object.h"
#include "vector.h"
#include <SDL2/SDL.h>
#include <stdint.h>
#define GAME_GRAVITY_X 0.0
#define GAME_GRAVITY_Y 0.098
#define GAME_DRAG 0.005
/* forward declarations */
struct game_data { /* Stores all the game variables. Opaque.
*/
struct llist *object_list; /* linked list with `object's */
uint16_t object_count; /* total active object amount */
SDL_Window *window;
SDL_Renderer *renderer;
vector gravity;
double drag;
};
struct game_data * /* Initializes the engine and returns a pointer to
* its environment.
*/
game_init();
void /* Frees all the memory used by the engine.
*/
game_deinit(struct game_data *gd);
void /* Steps the game state once.
*/
game_tick(struct game_data *gd);
void /* Renders the current game state.
*/
game_render(struct game_data *gd);
#endif /* GAME_H */

63
src/linked_list.c Normal file
View file

@ -0,0 +1,63 @@
#include "linked_list.h"
#include <stdlib.h>
#include <stdio.h>
static struct llist *
new_cell(const void *data) {
struct llist *l = malloc(sizeof(struct llist));
l->data = (void *)data;
l->next = NULL;
return l;
}
struct llist *
llist_get_tail(const struct llist *list) {
struct llist *l = (struct llist *)list;
while (l->next != NULL) {
l = l->next;
}
printf("got tail from list\n");
return l;
}
struct llist *
llist_append(struct llist *list, const void *data) {
struct llist *l = new_cell(data);
llist_get_tail(list)->next = l;
return l;
}
struct llist *
llist_prepend(struct llist *list, const void *data) {
struct llist *l = new_cell(data);
l->next = list;
printf("prepended element to list\n");
return l;
}
struct llist *
llist_pop_head(struct llist **list) {
if (*list) {
struct llist *l = *list;
*list = (*list)->next;
return l;
}
return NULL;
}
struct llist *
llist_pop_tail(struct llist **list) {
if (*list) {
struct llist *l = llist_get_tail(*list);
struct llist *m = *list;
while (m->next != l) {
m = m->next;
}
m->next = NULL;
return l;
}
return NULL;
}

60
src/linked_list.c~ Normal file
View file

@ -0,0 +1,60 @@
#include "linked_list.h"
#include <stdlib.h>
static struct llist *
new_cell(const void *data) {
struct llist *l = malloc(sizeof(struct llist));
l->data = (void *)data;
l->next = NULL;
return l;
}
struct llist *
llist_get_tail(const struct llist *list) {
struct llist *l = (struct llist *)list;
while (l->next != NULL) {
l = l->next;
}
return l;
}
struct llist *
llist_append(struct llist *list, const void *data) {
struct llist *l = new_cell(data);
llist_get_tail(list)->next = l;
return l;
}
struct llist *
llist_prepend(struct llist *list, const void *data) {
struct llist *l = new_cell(data);
l->next = list;
return l;
}
struct llist *
llist_pop_head(struct llist **list) {
if (*list) {
struct llist *l = *list;
*list = (*list)->next;
return l;
}
return NULL;
}
struct llist *
llist_pop_tail(struct llist **list) {
if (*list) {
struct llist *l = llist_get_tail(*list);
struct llist *m = *list;
while (m->next != l) {
m = m->next;
}
m->next = NULL;
return l;
}
return NULL;
}

33
src/linked_list.h Normal file
View file

@ -0,0 +1,33 @@
#ifndef LINKED_LIST_H
#define LINKED_LIST_H
struct llist {
void *data;
struct llist *next;
};
struct llist * /* Returns a pointer to the last element of LIST.
*/
llist_get_tail(const struct llist *list);
struct llist * /* Appends DATA to LIST. Returns a pointer to the new
* tail of the linked list.
*/
llist_append(struct llist *list, const void *data);
struct llist * /* Prepends DATA to LIST. Returns a pointer to the new
* head of the linked list. If LIST is NULL, create a
* new list.
*/
llist_prepend(struct llist *list, const void *data);
struct llist * /* Removes the first element from LIST. Returns its
* pointer. If LIST is NULL returns NULL.
*/
llist_pop_head(struct llist **list);
struct llist * /* Removes the last element from LIST. Returns its
* pointer. If LIST is NULL returns NULL.
*/
llist_pop_tail(struct llist **list);
#endif /* LINKED_LIST_H */

31
src/linked_list.h~ Normal file
View file

@ -0,0 +1,31 @@
#ifndef LINKED_LIST_H
#define LINKED_LIST_H
struct llist {
void *data;
struct llist *next;
};
struct llist * /* Returns a pointer to the last element of LIST.
*/
llist_get_tail(const struct llist *list);
struct llist * /* Appends DATA to LIST. Returns a pointer to the new
* end of the linked list.
*/
llist_append(struct llist *list, const void *data);
struct llist * /* Prepends DATA to LIST. Returns a pointer to the new
* end of the linked list.
*/
llist_prepend(struct llist *list, const void *data);
struct llist * /* Removes the first element from LIST. Returns its
* pointer.
*/
llist_pop_head(struct llist **list);
struct llist * /* Removes the last element from LIST. Returns its
* pointer.
*/
llist_pop_tail(struct llist **list);
#endif /* LINKED_LIST_H */

44
src/main.c Normal file
View file

@ -0,0 +1,44 @@
#include "collision.h"
#include "game.h"
#include "linked_list.h"
#include "object.h"
#include "physics.h"
#include <unistd.h>
int
main(int argc, char **argv) {
struct game_data *gd = game_init();
struct object *o1 = object_init(gd, 0, 1, 5, 5, NULL);
struct object *o2 =
object_init(gd, OBJFLAG_STATIC, 1, 0, 256, NULL);
o1->physics->v[0] = 2;
o1->physics->v[1] = -2;
o1->collisions = llist_prepend(o1->collisions,
collision_init(COLFLAG_ENABLED,
0x01,
-8.0,
-8.0,
16.0,
16.0));
o2->collisions = llist_prepend(o2->collisions,
collision_init(COLFLAG_ENABLED,
0x01,
0.0,
0.0,
512.0,
16.0));
unsigned i;
for (i = 0; i < 60 * 2; ++i) {
game_tick(gd);
game_render(gd);
SDL_Delay(128);
}
game_deinit(gd);
}

44
src/main.c~ Normal file
View file

@ -0,0 +1,44 @@
#include "collision.h"
#include "game.h"
#include "linked_list.h"
#include "object.h"
#include "physics.h"
#include <unistd.h>
int
main(int argc, char **argv) {
struct game_data *gd = game_init();
struct object *o1 = object_init(gd, 0, 1, 5, 5, NULL);
struct object *o2 =
object_init(gd, OBJFLAG_STATIC, 1, 0, 256, NULL);
o1->physics->v[0] = 2;
o1->physics->v[1] = -2;
o1->collisions = llist_prepend(o1->collisions,
collision_init(COLFLAG_ENABLED,
0x01,
-8.0,
-8.0,
16.0,
16.0));
o2->collisions = llist_prepend(o2->collisions,
collision_init(COLFLAG_ENABLED,
0x01,
0.0,
0.0,
512.0,
16.0));
unsigned i;
for (i = 0; i < 60 * 5; ++i) {
game_tick(gd);
game_render(gd);
SDL_Delay(16);
}
game_deinit(gd);
}

81
src/object.c Normal file
View file

@ -0,0 +1,81 @@
#include "object.h"
#include "collision.h"
#include "game.h"
#include "linked_list.h"
#include "physics.h"
#include <stdio.h>
struct object *
object_init(struct game_data *gd,
uint8_t flags, /* standard data */
float mass,
float x,
float y, /* physics object data */
struct llist *collisions) {
struct object *o = malloc(sizeof(struct object));
o->flags = flags;
o->collisions = collisions;
o->physics = physics_object_init(mass, x, y);
gd->object_count++;
printf("created an object at %f %f\n", x, y);
gd->object_list = llist_prepend(gd->object_list, o);
return o;
}
void
object_deinit(struct game_data *gd, struct object *o) {
physics_object_deinit(o->physics);
uint8_t i;
struct llist *l;
while ((l = llist_pop_head(&o->collisions))) {
collision_deinit(l->data);
}
free(o);
}
bool
object_detect_overlap(uint8_t fa,
uint8_t fb,
struct object *a,
struct object *b,
vector r) {
printf("object check collision\n");
struct llist *la = a->collisions;
struct llist *lb = b->collisions;
r[0] = 0;
r[1] = 0;
vector v;
v[0] = 0;
v[1] = 1;
unsigned ac, bc;
ac = bc = 0;
do {
do {
if (collision_detect_overlap(la->data,
lb->data,
a->physics->d,
b->physics->d,
r)) {
r[0] = 0;
r[1] += v[1];
printf("v: %f, %f\n", v[0], v[1]);
}
printf("count b: %i\n", bc++);
} while ((lb = lb->next));
printf("count a: %i\n", ac++);
} while ((la = la->next));
printf("collision response: %f, %f\n", r[0], r[1]);
if (r[0] + r[1] == 0) {
false;
}
return true;
}

79
src/object.c~ Normal file
View file

@ -0,0 +1,79 @@
#include "object.h"
#include "collision.h"
#include "game.h"
#include "linked_list.h"
#include "physics.h"
#include <stdio.h>
struct object *
object_init(struct game_data *gd,
uint8_t flags, /* standard data */
float mass,
float x,
float y, /* physics object data */
struct llist *collisions) {
struct object *o = malloc(sizeof(struct object));
o->flags = flags;
o->collisions = collisions;
o->physics = physics_object_init(mass, x, y);
gd->object_count++;
printf("created an object at %f %f\n", x, y);
gd->object_list = llist_prepend(gd->object_list, o);
return o;
}
void
object_deinit(struct game_data *gd, struct object *o) {
physics_object_deinit(o->physics);
uint8_t i;
struct llist *l;
while ((l = llist_pop_head(&o->collisions))) {
collision_deinit(l->data);
}
free(o);
}
vector *
object_detect_overlap(uint8_t fa,
uint8_t fb,
struct object *a,
struct object *b) {
printf("object check collision\n");
struct llist *la = a->collisions;
struct llist *lb = b->collisions;
vector *v;
vector *r = malloc(sizeof(vector));
*r[0] = 0;
*r[1] = 0;
unsigned ac, bc;
ac = bc = 0;
do {
do {
if ((v = collision_detect_overlap(la->data,
lb->data,
a->physics->d,
b->physics->d))) {
*r[0] += *v[0];
*r[1] += *v[1];
printf("free temporal vec\n");
free(v);
}
printf("count b: %i\n", bc++);
} while ((lb = lb->next));
printf("count a: %i\n", ac++);
} while ((la = la->next));
printf("collision response: %f, %f\n", *r[0], *r[1]);
if (*r[0] + *r[1] == 0) {
return NULL;
}
return r;
}

49
src/object.h Normal file
View file

@ -0,0 +1,49 @@
#ifndef OBJECT_H
#define OBJECT_H
#include "linked_list.h"
#include "utils.h"
#include "vector.h"
#include <stdint.h>
/* forward declarations */
struct game_data;
struct collision;
enum object_flags {
OBJFLAG_STATIC = 0x02,
};
struct object {
uint8_t flags;
struct llist *collisions;
struct physics_object *physics;
};
struct object * /* `object' constructor.
*/
object_init(struct game_data *gd,
uint8_t flags,
float mass,
float x,
float y,
struct llist *collisions);
void /* `object' destructor.
*/
object_deinit(struct game_data *gd, struct object *object);
bool /* Returns true if A and B intersect.
* Calls `collision_detect_overlap()' for each
* `collision' in A and B. FLAGS_A works as a filter
* for A, and FLAGS_B as a filter for B. `collision's
* that do not match AT LEAST the given flags will be
* ignored.
*/
object_detect_overlap(uint8_t flags_a,
uint8_t flags_b,
struct object *a,
struct object *b,
vector response);
#endif /* OBJECT_H */

47
src/object.h~ Normal file
View file

@ -0,0 +1,47 @@
#ifndef OBJECT_H
#define OBJECT_H
#include "linked_list.h"
#include "vector.h"
#include <stdint.h>
/* forward declarations */
struct game_data;
struct collision;
enum object_flags {
OBJFLAG_STATIC = 0x02,
};
struct object {
uint8_t flags;
struct llist *collisions;
struct physics_object *physics;
};
struct object * /* `object' constructor.
*/
object_init(struct game_data *gd,
uint8_t flags,
float mass,
float x,
float y,
struct llist *collisions);
void /* `object' destructor.
*/
object_deinit(struct game_data *gd, struct object *object);
vector * /* Returns true if A and B intersect.
* Calls `collision_detect_overlap()' for each
* `collision' in A and B. FLAGS_A works as a filter
* for A, and FLAGS_B as a filter for B. `collision's
* that do not match AT LEAST the given flags will be
* ignored.
*/
object_detect_overlap(uint8_t flags_a,
uint8_t flags_b,
struct object *a,
struct object *b);
#endif /* OBJECT_H */

20
src/physics.c Normal file
View file

@ -0,0 +1,20 @@
#include "physics.h"
#include <stdlib.h>
struct physics_object *
physics_object_init(float m, float x, float y) {
struct physics_object *po = malloc(sizeof(struct physics_object));
po->flags = 0;
po->m = m;
po->d[0] = x;
po->d[1] = y;
po->v[0] = 0;
po->v[1] = 0;
return po;
}
void
physics_object_deinit(struct physics_object *po) {
free(po);
}

20
src/physics.c~ Normal file
View file

@ -0,0 +1,20 @@
#include "physics.h"
#include <stdlib.h>
struct physics_object *
physics_object_init(float m, float x, float y) {
struct physics_object *po = malloc(sizeof(struct physics_object));
po->flags = 0;
po->m = m;
po->d[0] = x;
po->d[1] = y;
po->v[0] = 2;
po->v[1] = 0;
return po;
}
void
physics_object_deinit(struct physics_object *po) {
free(po);
}

25
src/physics.h Normal file
View file

@ -0,0 +1,25 @@
#ifndef PHYSICS_H
#define PHYSICS_H
#include "vector.h"
#include <stdint.h>
struct physics_object {
uint16_t flags;
float m; /* mass */
vector d; /* displacement (position) */
vector v; /* velocity */
};
struct physics_object * /* `physics_object' constructor.
*/
physics_object_init(float m,
float x, /* displacement[0] */
float y); /* displacement[1] */
void /* `physics_object' destructor.
*/
physics_object_deinit(struct physics_object *physics_object);
#endif /* PHYSICS_H */

26
src/physics.h~ Normal file
View file

@ -0,0 +1,26 @@
#ifndef PHYSICS_H
#define PHYSICS_H
#include "vector.h"
#include <stdint.h>
struct physics_object {
uint16_t flags;
float m; /* mass */
vector d; /* displacement (position) */
vector v; /* velocity */
};
/* Creates a `physics_object' object and returns its pointer.
*/
struct physics_object *
physics_object_init(float m, float x, /* displacement[0] */
float y); /* displacement[1] */
/* Frees the memory allocated for the `physics_object' object
* PHYSICS_OBJECT.
*/
void
physics_object_deinit(struct physics_object *physics_object);
#endif /* PHYSICS_H */

8
src/utils.h Normal file
View file

@ -0,0 +1,8 @@
#ifndef UTILS_H
#define UTILS_H
typedef char bool;
#define true 1
#define false 0
#endif /* UTILS_H */

8
src/utils.h~ Normal file
View file

@ -0,0 +1,8 @@
#ifndef UTILS_H
#define UTILS_H
typedef char bool;
#define true 1;
#define false 0;
#endif /* UTILS_H */

89
src/vector.c Normal file
View file

@ -0,0 +1,89 @@
#include "vector.h"
#include <math.h>
void
vector_add(const vector a, const vector b, vector r) {
r[0] = a[0] + b[0];
r[1] = a[1] + b[1];
}
void
vector_sub(const vector a, const vector b, vector r) {
r[0] = a[0] - b[0];
r[1] = a[1] - b[1];
}
void
vector_scale(const vector v, float s, vector r) {
r[0] = v[0] * s;
r[1] = v[1] * s;
}
void
vector_inc(const vector v, float s, vector r) {
r[0] = v[0] + s;
r[1] = v[1] + s;
}
void
vector_dec(const vector v, float s, vector r) {
r[0] = v[0] - s;
r[1] = v[1] - s;
}
void
vector_approach_zero(const vector v, float s, vector r) {
/* FIXME: i have no fucking clue if this function works */
s = fabs(s); /* TODO: anotate this on the header later */
if (s == 0) {
r[0] = v[0];
r[1] = v[1];
return;
}
if (v[0] == 0) {
r[0] = 0;
}
if (v[1] == 0) {
r[1] = 0;
}
if (v[0] < 0) {
if (-v[0] < s) {
r[0] = 0;
} else {
r[0] = v[0] + s;
}
}
if (v[0] > 0) {
if (v[0] < s) {
r[0] = 0;
} else {
r[0] = v[0] - s;
}
}
if (v[1] < 0) {
if (-v[1] < s) {
r[1] = 0;
} else {
r[1] = v[1] + s;
}
}
if (v[1] > 0) {
if (v[1] < s) {
r[1] = 0;
} else {
r[1] = v[1] - s;
}
}
}
void
vector_invert(const vector v, vector r) {
r[0] = -v[0];
r[1] = -v[1];
}

28
src/vector.c~ Normal file
View file

@ -0,0 +1,28 @@
#include "vector.h"
#include <stdlib.h>
vector *
vector_init(float x, float y) {
vector *v = malloc(sizeof(vector));
*v[0] = x;
*v[1] = y;
return v;
}
void
vector_deinit(vector *v) {
free(v);
}
void
vector_add(const vector *a, const vector *b, vector *r) {
*r[0] = *a[0] + *b[0];
*r[1] = *a[1] + *b[1];
}
void
vector_scale(const vector *v, float s, vector *r) {
*r[0] = *v[0] * s;
*r[1] = *v[1] * s;
}

27
src/vector.h Normal file
View file

@ -0,0 +1,27 @@
#ifndef VECTOR_H
#define VECTOR_H
typedef double vector[2];
void
vector_add(const vector a, const vector b, vector result);
void
vector_sub(const vector a, const vector b, vector result);
void
vector_scale(const vector base, float scale, vector result);
void
vector_inc(const vector base, float scale, vector result);
void
vector_dec(const vector base, float scale, vector result);
void
vector_approach_zero(const vector base, float scale, vector result);
void
vector_invert(const vector base, vector result);
#endif /* VECTOR_H */

27
src/vector.h~ Normal file
View file

@ -0,0 +1,27 @@
#ifndef VECTOR_H
#define VECTOR_H
typedef float vector[2];
void
vector_add(const vector a, const vector b, vector result);
void
vector_sub(const vector a, const vector b, vector result);
void
vector_scale(const vector base, float scale, vector result);
void
vector_inc(const vector base, float scale, vector result);
void
vector_dec(const vector base, float scale, vector result);
void
vector_approach_zero(const vector base, float scale, vector result);
void
vector_invert(const vector base, vector result);
#endif /* VECTOR_H */