#include <cairo.h>
#include "private.h"
#include "sprite.h"
#include "world.h"
#include "group.h"
struct _Sprite {
double x;
double y;
double rotation;
double width;
double height;
double scale;
double vx;
double vy;
double rotationSpeed;
HeliCollider collider;
double bounciness;
int rotateToDirection;
int mirrorX;
int mirrorY;
double depth;
double lifeTime;
int visible;
int tag;
int debug;
double shapeColor[4];
double tintColor[4];
int playing;
int frame;
HeliArray groups;
HeliAnimation *animation;
};
static HeliArray sprites;
static HeliArray spritesSorted;
static void prepareCollider(Sprite sprite, HeliCollider *collider) {
collider->type = sprite->collider.type;
collider->x = sprite->collider.x + sprite->x;
collider->y = sprite->collider.y + sprite->y;
collider->width = sprite->collider.width;
collider->height = sprite->collider.height;
collider->radius = sprite->collider.radius;
collider->rotation = sprite->collider.rotation + sprite->rotation;
// auto-size
if (collider->type == COLLIDER_RECTANGLE) {
if (collider->width < -HELI_PRECISION) collider->width = sprite->width;
if (collider->height < -HELI_PRECISION) collider->height = sprite->height;
if (collider->radius < -HELI_PRECISION) collider->radius = 0;
} else
if (collider->type == COLLIDER_CIRCLE) {
collider->width = 0;
collider->height = 0;
if (collider->radius < -HELI_PRECISION) collider->radius = 0.5*sprite->width;
}
// scale
collider->width *= sprite->scale;
collider->height *= sprite->scale;
collider->radius *= sprite->scale;
// fix small values
if (collider->width < HELI_PRECISION) collider->width = HELI_PRECISION;
if (collider->height < HELI_PRECISION) collider->height = HELI_PRECISION;
if (collider->radius < HELI_PRECISION) collider->radius = HELI_PRECISION;
// fix round corners
if (collider->type == COLLIDER_RECTANGLE) {
double rmax = 0.5*( collider->width < collider->height
? collider->width : collider->height );
if (collider->radius >= rmax - HELI_PRECISION)
collider->radius = rmax;
collider->width -= 2*collider->radius;
collider->height -= 2*collider->radius;
}
}
Sprite createSpriteEx(double x, double y, double width, double height) {
Sprite s = calloc(1, sizeof(*s));
s->x = x;
s->y = y;
s->width = width;
s->height = height;
s->scale = 1;
s->collider.type = COLLIDER_RECTANGLE;
s->collider.width = s->collider.height = -1;
s->bounciness = 1;
s->mirrorX = 1;
s->mirrorY = 1;
s->lifeTime = -1;
s->visible = TRUE;
s->shapeColor[0] = s->shapeColor[1] = s->shapeColor[2] = 0.5;
s->shapeColor[3] = 1;
s->tintColor[0] = s->tintColor[1] = s->tintColor[2] = s->tintColor[3] = 1;
heliArrayInsert(&sprites, -1, s, NULL);
heliArrayInsert(&spritesSorted, -1, s, NULL);
return s;
}
Sprite createSprite(double x, double y)
{ return createSpriteEx(x, y, 100, 100); }
void spriteDestroy(Sprite sprite) {
spriteSetNoAnimation(sprite);
while(sprite->groups.count > 0)
groupRemove((Group)sprite->groups.items[0].value, sprite);
for(int i = 0; i < sprites.count; ++i)
if (sprites.items[i].value == sprite)
{ heliArrayRemove(&sprites, i); break; }
for(int i = 0; i < spritesSorted.count; ++i)
if (spritesSorted.items[i].value == sprite)
{ heliArrayRemove(&spritesSorted, i); break; }
free(sprite);
}
void spriteDestroyTimer(Sprite sprite, double lifeTime) {
if (lifeTime <= 0) { spriteDestroy(sprite); return; }
sprite->lifeTime = lifeTime;
}
double spriteGetX(Sprite sprite) { return sprite->x; }
void spriteSetX(Sprite sprite, double x) { sprite->x = x; }
double spriteGetY(Sprite sprite) { return sprite->y; }
void spriteSetY(Sprite sprite, double y) { sprite->y = y; }
double spriteGetVelocityX(Sprite sprite) { return sprite->vx; }
void spriteSetVelocityX(Sprite sprite, double x)
{ spriteSetVelocityXY(sprite, x, sprite->vy); }
double spriteGetVelocityY(Sprite sprite) { return sprite->vy; }
void spriteSetVelocityY(Sprite sprite, double y)
{ spriteSetVelocityXY(sprite, sprite->vx, y); }
double spriteGetScale(Sprite sprite) { return sprite->scale; }
void spriteSetScale(Sprite sprite, double scale) { sprite->scale = scale; }
double spriteGetWidth(Sprite sprite) { return sprite->width; }
void spriteSetWidth(Sprite sprite, double width)
{ sprite->width = width > 0 ? width : 0; }
double spriteGetHeight(Sprite sprite) { return sprite->height; }
void spriteSetHeight(Sprite sprite, double height)
{ sprite->height = height > 0 ? height : 0; }
int spriteGetRotateToDirection(Sprite sprite) { return sprite->rotateToDirection; }
void spriteSetRotateToDirection(Sprite sprite, int rotateToDirection) {
if (rotateToDirection) {
sprite->rotateToDirection = TRUE;
sprite->rotation = spriteGetDirection(sprite);
} else {
sprite->rotateToDirection = FALSE;
}
}
double spriteGetRotation(Sprite sprite) { return sprite->rotation; }
void spriteSetRotation(Sprite sprite, double rotation)
{ if (!sprite->rotateToDirection) sprite->rotation = rotation; }
double spriteGetRotationSpeed(Sprite sprite) { return sprite->rotationSpeed; }
void spriteSetRotationSpeed(Sprite sprite, double rotationSpeed)
{ sprite->rotationSpeed = rotationSpeed; }
int spriteGetMirrorX(Sprite sprite) { return sprite->mirrorX; }
void spriteSetMirrorX(Sprite sprite, int mirrorX)
{ sprite->mirrorX = mirrorX < 0 ? -1 : 1; }
int spriteGetMirrorY(Sprite sprite) { return sprite->mirrorY; }
void spriteSetMirrorY(Sprite sprite, int mirrorY)
{ sprite->mirrorY = mirrorY < 0 ? -1 : 1; }
double spriteGetDepth(Sprite sprite) { return sprite->depth; }
void spriteSetDepth(Sprite sprite, double depth)
{ sprite->depth = depth; }
int spriteGetVisible(Sprite sprite) { return sprite->visible; }
void spriteSetVisible(Sprite sprite, int visible)
{ sprite->visible = visible != FALSE; }
int spriteGetTag(Sprite sprite) { return sprite->tag; }
void spriteSetTag(Sprite sprite, int tag)
{ sprite->tag = tag; }
int spriteGetDebug(Sprite sprite) { return sprite->debug; }
void spriteSetDebug(Sprite sprite, int debug)
{ sprite->debug = debug != FALSE; }
int spriteOverlap(Sprite a, Sprite b)
{ return spriteCollideEx(a, b, TRUE, TRUE, 0); }
int spriteCollide(Sprite a, Sprite b, double bounciness)
{ return spriteCollideEx(a, b, FALSE, FALSE, bounciness); }
int spriteBounceOff(Sprite sprite, Sprite other, double bounciness)
{ return spriteCollideEx(sprite, other, FALSE, TRUE, bounciness); }
int spritePush(Sprite sprite, Sprite other, double bounciness)
{ return spriteCollideEx(sprite, other, TRUE, FALSE, bounciness); }
int spriteCollideEx(Sprite a, Sprite b, int keepVelocityA, int keepVelocityB, double bounciness) {
if (a == b) return FALSE;
HeliCollider ca, cb;
prepareCollider(a, &ca);
prepareCollider(b, &cb);
double dx = 0, dy = 0;
double vx = a->vx - b->vx;
double vy = a->vy - b->vy;
bounciness = fabs(bounciness * a->bounciness * b->bounciness);
if (!heliCheckCollision(&ca, &cb, &dx, &dy, &vx, &vy, bounciness))
return FALSE;
if ( keepVelocityA && !keepVelocityB) {
b->x -= dx;
b->y -= dy;
spriteSetVelocityXY(b, a->vx - vx, a->vy - vy);
} else
if (!keepVelocityA && keepVelocityB) {
a->x += dx;
a->y += dy;
spriteSetVelocityXY(a, b->vx + vx, b->vy + vy);
} else
if (!keepVelocityA && !keepVelocityB) {
double vxAvg = 0.5*(a->vx + b->vx);
double vyAvg = 0.5*(a->vy + b->vy);
a->x += 0.5*dx;
a->y += 0.5*dy;
spriteSetVelocityXY(a, vxAvg + 0.5*vx, vyAvg + 0.5*vy);
b->x -= 0.5*dx;
b->y -= 0.5*dy;
spriteSetVelocityXY(b, vxAvg - 0.5*vx, vyAvg - 0.5*vy);
}
return TRUE;
}
double spriteGetBounciness(Sprite sprite) { return sprite->bounciness; }
void spriteSetBounciness(Sprite sprite, double bounciness)
{ sprite->bounciness = bounciness > 0 ? bounciness : 0; }
void spriteSetCollider(Sprite sprite, Collider type, double xOffset, double yOffset, double rotationOffset)
{ spriteSetColliderEx(sprite, type, xOffset, yOffset, rotationOffset, -1, -1, -1); }
void spriteSetColliderCircle(Sprite sprite, double xOffset, double yOffset, double radius)
{ spriteSetColliderEx(sprite, COLLIDER_CIRCLE, xOffset, yOffset, 0, 0, 0, radius); }
void spriteSetColliderRectangle(
Sprite sprite, double xOffset, double yOffset, double rotationOffset,
double width, double height, double cornersRadius )
{
spriteSetColliderEx(
sprite, COLLIDER_RECTANGLE,
xOffset, yOffset, rotationOffset,
width, height, cornersRadius);
}
void spriteSetColliderEx(
Sprite sprite, Collider type,
double xOffset, double yOffset, double rotationOffset,
double width, double height, double radius )
{
sprite->collider.type = type;
sprite->collider.x = xOffset;
sprite->collider.y = yOffset;
sprite->collider.rotation = rotationOffset;
if (type == COLLIDER_CIRCLE) {
sprite->collider.width = 0;
sprite->collider.height = 0;
sprite->collider.radius = radius;
} else
if (type == COLLIDER_RECTANGLE) {
sprite->collider.width = width;
sprite->collider.height = height;
sprite->collider.radius = radius;
} else {
sprite->collider.width = 0;
sprite->collider.height = 0;
sprite->collider.radius = 0;
}
}
void spriteSetAnimation(Sprite sprite, const char *path) {
HeliAnimation *prev = sprite->animation;
sprite->animation = heliAnimationLoad(path);
if (prev) heliAnimationUnref(prev);
spriteSetFrame(sprite, sprite->frame);
}
void spriteSetNoAnimation(Sprite sprite) {
if (sprite->animation) heliAnimationUnref(sprite->animation);
sprite->animation = NULL;
}
void spritePlay(Sprite sprite) { sprite->playing = TRUE; }
void spritePause(Sprite sprite) { sprite->playing = FALSE; }
void spriteNextFrame(Sprite sprite) { spriteSetFrame(sprite, sprite->frame + 1); }
void spriteSetFrame(Sprite sprite, int frame) {
sprite->frame = frame >= 0 && sprite->animation && sprite->animation->frames.count > 0
? frame % sprite->animation->frames.count : 0;
}
void spriteSetShapeColor(Sprite sprite, const char *color)
{ heliParseColor(color, sprite->shapeColor); }
void spriteSetTintColor(Sprite sprite, const char *color)
{ heliParseColor(color, sprite->tintColor); }
void spriteSetVelocityXY(Sprite sprite, double x, double y) {
sprite->vx = x;
sprite->vy = y;
if (sprite->rotateToDirection)
sprite->rotation = spriteGetDirection(sprite);
}
void spriteSetSpeedAndDirection(Sprite sprite, double speed, double angle) {
double a = angle*(PI/180);
spriteSetVelocityXY(sprite, cos(a)*speed, sin(a)*speed);
}
double spriteGetSpeed(Sprite sprite)
{ return sqrt(sprite->vx*sprite->vx + sprite->vy*sprite->vy); }
double spriteGetDirection(Sprite sprite) {
return fabs(sprite->vx) > HELI_PRECISION || fabs(sprite->vy) > HELI_PRECISION
? atan2(sprite->vy, sprite->vx)*(180/PI) : 0;
}
void spritePointTo(Sprite sprite, double x, double y)
{ spriteSetRotation( sprite, atan2(y - sprite->y, x - sprite->x)*(180/PI) ); }
double spriteGetScaledWidth(Sprite sprite)
{ return sprite->width * sprite->scale; }
double spriteGetScaledHeight(Sprite sprite)
{ return sprite->height * sprite->scale; }
int worldGetSpriteCount()
{ return sprites.count; }
Sprite worldGetSprite(int i)
{ return (Sprite)heliArrayGetValue(&sprites, i); }
static void drawSpriteDebug(cairo_t *cr, Sprite s) {
cairo_save(cr);
cairo_set_line_width(cr, 0.5);
cairo_translate(cr, s->x, s->y);
cairo_rotate(cr, s->rotation*(PI/180));
cairo_scale(cr, s->scale*s->mirrorX, s->scale*s->mirrorY);
double hw = s->width*0.5;
double hh = s->height*0.5;
cairo_set_source_rgba(cr, 0, 0, 0, 0.75);
cairo_move_to(cr, -hw, -hh);
cairo_line_to(cr, hw, -hh);
cairo_line_to(cr, hw, hh);
cairo_line_to(cr, -hw, hh);
cairo_close_path(cr);
cairo_stroke(cr);
cairo_move_to(cr, -hw*0.25, 0);
cairo_line_to(cr, hw*0.25, 0);
cairo_move_to(cr, 0, -hh*0.25);
cairo_line_to(cr, 0, hh*0.25);
cairo_stroke(cr);
char buf[1024];
snprintf(buf, sizeof(buf)-1, "%lf", s->depth);
double s1 = hw*0.25;
double s2 = hh*0.5;
cairo_set_font_size(cr, s1 < s2 ? s1 : s2);
cairo_move_to(cr, -hw*0.9, -hh*0.5);
cairo_show_text(cr, buf);
cairo_restore(cr);
}
static void drawSprite(cairo_t *cr, Sprite s) {
cairo_save(cr);
cairo_translate(cr, s->x, s->y);
cairo_rotate(cr, s->rotation*(PI/180));
cairo_scale(cr, s->scale*s->mirrorX, s->scale*s->mirrorY);
double hw = s->width*0.5;
double hh = s->height*0.5;
cairo_surface_t *frame = s->animation
? (cairo_surface_t*)heliArrayGetValue(&s->animation->frames, s->frame) : NULL;
double a = s->tintColor[3];
int tint = fabs(s->tintColor[0] - 1) > HELI_PRECISION
|| fabs(s->tintColor[1] - 1) > HELI_PRECISION
|| fabs(s->tintColor[2] - 1) > HELI_PRECISION;
int alpha = fabs(a) > HELI_PRECISION;
int visible = fabs(a) > HELI_PRECISION;
if (visible) {
if (tint) cairo_push_group(cr);
if (frame) {
// image
double ihw = cairo_image_surface_get_width(frame)*0.5;
double ihh = cairo_image_surface_get_height(frame)*0.5;
if (ihw > HELI_PRECISION && ihh > HELI_PRECISION) {
cairo_save(cr);
cairo_scale(cr, hw/ihw, hh/ihh);
cairo_set_source_surface(cr, frame, -ihw, -ihh);
if (alpha) cairo_paint_with_alpha(cr, a); else cairo_paint(cr);
cairo_restore(cr);
}
} else {
// rectangle
cairo_set_source_rgba(cr, s->shapeColor[0], s->shapeColor[1], s->shapeColor[2], s->shapeColor[3]*a);
cairo_move_to(cr, -hw, -hh);
cairo_line_to(cr, hw, -hh);
cairo_line_to(cr, hw, hh);
cairo_line_to(cr, -hw, hh);
cairo_close_path(cr);
cairo_fill(cr);
}
if (tint) {
cairo_pattern_t *spriteGroup = cairo_pop_group(cr);
cairo_push_group(cr);
cairo_set_source(cr, spriteGroup);
cairo_paint(cr);
cairo_set_source_rgba(cr, s->tintColor[0], s->tintColor[1], s->tintColor[2], 1);
cairo_set_operator(cr, CAIRO_OPERATOR_MULTIPLY);
cairo_mask(cr, spriteGroup);
cairo_pop_group_to_source(cr);
cairo_paint(cr);
cairo_pattern_destroy(spriteGroup);
}
}
cairo_restore(cr);
}
void drawSprites() {
// sort
int found = TRUE;
while(found) {
found = FALSE;
// forward
for(int i = 1; i < spritesSorted.count; ++i) {
if ( ((Sprite)spritesSorted.items[i-1].value)->depth
< ((Sprite)spritesSorted.items[i ].value)->depth )
{
void *x = spritesSorted.items[i].value;
spritesSorted.items[i].value = spritesSorted.items[i-1].value;
spritesSorted.items[i-1].value = x;
found = TRUE;
}
}
if (!found) break;
// backward
found = FALSE;
for(int i = spritesSorted.count - 1; i > 0; --i) {
if ( ((Sprite)spritesSorted.items[i-1].value)->depth
< ((Sprite)spritesSorted.items[i ].value)->depth )
{
void *x = spritesSorted.items[i].value;
spritesSorted.items[i].value = spritesSorted.items[i-1].value;
spritesSorted.items[i-1].value = x;
found = TRUE;
}
}
}
if (heliCairo) {
// draw
for(int i = 0; i < spritesSorted.count; ++i) {
Sprite s = (Sprite)(spritesSorted.items[i].value);
if (s->visible) drawSprite(heliCairo, s);
}
// draw debug marks
for(int i = 0; i < spritesSorted.count; ++i) {
Sprite s = (Sprite)(spritesSorted.items[i].value);
if (s->debug) drawSpriteDebug(heliCairo, s);
}
}
}
int mouseIsOver(Sprite sprite) {
HeliCollider collider;
prepareCollider(sprite, &collider);
return heliPointCollision(&collider, mouseX(), mouseY());
}
void heliSpriteUpdate(double dt) {
// auto-remove
for(int i = sprites.count - 1; i > 0; --i) {
Sprite s = (Sprite)sprites.items[i].value;
if (s->lifeTime >= -HELI_PRECISION) {
s->lifeTime -= dt;
if (s->lifeTime <= HELI_PRECISION)
spriteDestroy(s);
}
}
// update
for(int i = 0; i < sprites.count; ++i) {
Sprite s = (Sprite)sprites.items[i].value;
s->x += s->vx*dt;
s->y += s->vy*dt;
if (!s->rotateToDirection)
s->rotation += s->rotationSpeed*dt;
if (s->playing) spriteNextFrame(s);
}
}
HeliArray* heliSpriteGetGroups(Sprite sprite)
{ return &sprite->groups; }
void heliSpriteFinish() {
while(worldGetSpriteCount())
{ spriteDestroy(worldGetSprite(0)); }
heliArrayDestroy(&sprites);
heliArrayDestroy(&spritesSorted);
}