|
|
3f9996 |
|
|
|
3f9996 |
#include "private.h"
|
|
|
3f9996 |
|
|
|
3f9996 |
|
|
|
3f9996 |
|
|
|
3f9996 |
static char* heliStringCopy(char *x) {
|
|
|
3f9996 |
int len = strlen(x) + 1;
|
|
|
3f9996 |
char *cp = malloc(len + 1);
|
|
|
3f9996 |
memcpy(cp, x, len);
|
|
|
3f9996 |
return cp;
|
|
|
3f9996 |
}
|
|
|
3f9996 |
|
|
|
3f9996 |
static void heliLowercase(char *x)
|
|
|
3f9996 |
{ while(*x) *x = tolower(*x); }
|
|
|
3f9996 |
|
|
|
3f9996 |
|
|
|
3f9996 |
|
|
|
3f9996 |
int heliArrayInit(HeliArray *a) {
|
|
|
3f9996 |
a->items = NULL;
|
|
|
3f9996 |
a->count = a->allocated = 0;
|
|
|
3f9996 |
}
|
|
|
3f9996 |
|
|
|
3f9996 |
void heliArrayClear(HeliArray *a, int freeData) {
|
|
|
3f9996 |
if (freeData)
|
|
|
3f9996 |
for(int i = 0; i < a->count; ++i)
|
|
|
3f9996 |
free(a->items[i]);
|
|
|
3f9996 |
a->count = 0;
|
|
|
3f9996 |
}
|
|
|
3f9996 |
|
|
|
3f9996 |
int heliArrayDestroy(HeliArray *a, int freeData) {
|
|
|
3f9996 |
heliArrayClear(a, freeData);
|
|
|
3f9996 |
free(a->items);
|
|
|
3f9996 |
a->items = NULL;
|
|
|
3f9996 |
a->count = a->allocated = 0;
|
|
|
3f9996 |
}
|
|
|
3f9996 |
|
|
|
3f9996 |
void* heliArrayGet(HeliArray *a, int i)
|
|
|
3f9996 |
{ return i >= 0 && i < a->count ? a->items[i] : NULL; }
|
|
|
3f9996 |
|
|
|
3f9996 |
void heliArrayInsert(HeliArray *a, int i, void *item) {
|
|
|
3f9996 |
if (i < 0 || i > a->count)
|
|
|
3f9996 |
i = a->count;
|
|
|
3f9996 |
if (a->allocated < a->count + 1) {
|
|
|
3f9996 |
a->allocated += a->allocated/4 + 32;
|
|
|
3f9996 |
a->items = realloc(a->items, a->allocated);
|
|
|
3f9996 |
}
|
|
|
3f9996 |
if (i < a->count)
|
|
|
3f9996 |
memmove(&a->items[i+1], &a->items[i], (a->count-i)*sizeof(a->items[0]));
|
|
|
3f9996 |
++a->count;
|
|
|
3f9996 |
a->items[i] = item;
|
|
|
3f9996 |
}
|
|
|
3f9996 |
|
|
|
3f9996 |
void heliArrayAppend(HeliArray *a, void *data)
|
|
|
3f9996 |
{ heliArrayInsert(a, a->count, data); }
|
|
|
3f9996 |
|
|
|
3f9996 |
void heliArrayRemove(HeliArray *a, int i, int freeData) {
|
|
|
3f9996 |
if (i < 0 || i >= a->count)
|
|
|
3f9996 |
return;
|
|
|
3f9996 |
if (freeData) free(a->items[i]);
|
|
|
3f9996 |
memmove(&a->items[i], &a->items[i+1], (a->count-i-1)*sizeof(a->items[0]));
|
|
|
3f9996 |
--a->count;
|
|
|
3f9996 |
}
|
|
|
3f9996 |
|
|
|
3f9996 |
int heliStringsetFind(HeliArray *a, char *x, int *gtOrEqIndex) {
|
|
|
3f9996 |
for(int i = 0; i < a->count; ++i) {
|
|
|
3f9996 |
int cmp = strcmp((char*)a->items[i], x);
|
|
|
3f9996 |
if (cmp < 0) continue;
|
|
|
3f9996 |
if (gtOrEqIndex) *gtOrEqIndex = i;
|
|
|
3f9996 |
return cmp == 0;
|
|
|
3f9996 |
}
|
|
|
3f9996 |
if (gtOrEqIndex) *gtOrEqIndex = a->count;
|
|
|
3f9996 |
return 0;
|
|
|
3f9996 |
}
|
|
|
3f9996 |
|
|
|
3f9996 |
int heliStringsetGet(HeliArray *a, char *x)
|
|
|
3f9996 |
{ return heliStringsetFind(a, x, NULL); }
|
|
|
3f9996 |
|
|
|
3f9996 |
void heliStringsetAdd(HeliArray *a, char *x) {
|
|
|
3f9996 |
int i;
|
|
|
3f9996 |
if (heliStringsetFind(a, x, &i)) return;
|
|
|
3f9996 |
heliArrayInsert(a, i, heliStringCopy(x));
|
|
|
3f9996 |
}
|
|
|
3f9996 |
|
|
|
3f9996 |
void heliStringsetRemove(HeliArray *a, char *x) {
|
|
|
3f9996 |
int i;
|
|
|
3f9996 |
if (!heliStringsetFind(a, x, &i)) return;
|
|
|
3f9996 |
heliArrayRemove(a, i, TRUE);
|
|
|
3f9996 |
}
|
|
|
3f9996 |
|
|
|
3f9996 |
|
|
|
3f9996 |
|
|
|
3f9996 |
int heliCheckCollision(HeliCollider *a, HeliCollider *b, double *normX, double *normY) {
|
|
|
3f9996 |
// corner with side
|
|
|
3f9996 |
// corner with circle
|
|
|
3f9996 |
// circle with circle
|
|
|
3f9996 |
#warning "TODO: heliCheckCollision: not implemented yet"
|
|
|
3f9996 |
return FALSE;
|
|
|
3f9996 |
}
|
|
|
3f9996 |
|
|
|
3f9996 |
int heliPointCollision(HeliCollider *c, double x, double y) {
|
|
|
3f9996 |
x -= c->x;
|
|
|
3f9996 |
y -= c->y;
|
|
|
3f9996 |
if (c->type == COLLIDER_CIRCLE) {
|
|
|
3f9996 |
return x*x + y*y <= c->radius*c->radius;
|
|
|
3f9996 |
} else
|
|
|
3f9996 |
if (c->type == COLLIDER_RECTANGLE) {
|
|
|
3f9996 |
double a = c->rotation/180.0*M_PI;
|
|
|
3f9996 |
double sn = sin(a);
|
|
|
3f9996 |
double cn = cos(a);
|
|
|
3f9996 |
return fabs(x*cn - y*sn) <= c->width*0.5
|
|
|
3f9996 |
&& fabs(x*sn + y*cn) <= c->height*0.5;
|
|
|
3f9996 |
}
|
|
|
3f9996 |
return FALSE;
|
|
|
3f9996 |
}
|