diff --git a/demo/src/phisics.c b/demo/src/phisics.c index 2a90724..2aae432 100644 --- a/demo/src/phisics.c +++ b/demo/src/phisics.c @@ -13,24 +13,22 @@ static Sound beep; void phisicsInit() { - ball = createSpriteEx(200+1, 200+1, 64-2, 64-2); - spriteSetAnimation(ball, createAnimation("data/sprite/breadball.png")); + ball = createSpriteEx(200+1, 200+1, 64-2, 64-2, createAnimation("data/sprite/breadball.png")); spriteSetColliderCircle(ball, 0, 0, -1); spriteSetRotateToDirection(ball, TRUE); - brick1 = createSpriteEx(200-32, 200+64, 64, 64); - spriteSetAnimation(brick1, createAnimation("data/sprite/bricks.png")); + brick1 = createSpriteEx(200-32, 200+64, 64, 64, createAnimation("data/sprite/bricks.png")); spriteSetTintColor(brick1, colorByRGB(0, 0, 1)); spriteSetColliderRectangle(brick1, 0, 0, 0, -1, -1, 20); spriteSetBounciness(brick1, 0.5); spriteSetMassLevel(brick1, 1); + spriteSetDebug(brick1, TRUE); - brick2 = createSpriteEx(200+32+1, 200+64+1, 64-2, 64-2); - spriteSetAnimation(brick2, createAnimation("data/sprite/bricks.png")); + brick2 = createSpriteEx(200+32+1, 200+64+1, 64-2, 64-2, createAnimation("data/sprite/bricks.png")); spriteSetColliderRectangle(brick2, 0, 0, 0, -1, -1, 20); + spriteSetDebug(brick2, TRUE); - brick3 = createSpriteEx(200+32+64, 200+64, 64, 64); - spriteSetAnimation(brick3, createAnimation("data/sprite/bricks.png")); + brick3 = createSpriteEx(200+32+64, 200+64, 64, 64, createAnimation("data/sprite/bricks.png")); spriteSetTintColor(brick3, colorByRGBA(1, 0, 1, 0.5)); spriteSetColliderRectangle(brick3, 0, 0, 0, -1, -1, 20); spriteSetBounciness(brick3, 2); diff --git a/demo/src/sprites.c b/demo/src/sprites.c index a2d0566..dbe2ce2 100644 --- a/demo/src/sprites.c +++ b/demo/src/sprites.c @@ -17,39 +17,34 @@ void spritesInit() { double y = 16 + 48/2; // normal - s = createSpriteEx(x, y, 48, 48); - spriteSetAnimation(s, createAnimation("data/sprite/bricks.png")); + s = createSpriteEx(x, y, 48, 48, createAnimation("data/sprite/bricks.png")); groupAdd(pulse, s); x += 64; // indexed colors - s = createSpriteEx(x, y, 48, 48); - spriteSetAnimation(s, createAnimation("data/sprite/snail-indexed.png")); + s = createSpriteEx(x, y, 48, 48, createAnimation("data/sprite/snail-indexed.png")); groupAdd(pulse, s); x += 64; // without alpha - s = createSpriteEx(x, y, 48, 48); - spriteSetAnimation(s, createAnimation("data/sprite/bricks-tile.png")); + s = createSpriteEx(x, y, 48, 48, createAnimation("data/sprite/bricks-tile.png")); groupAdd(pulse, s); x += 64; // without texture - s = createSpriteEx(x, y, 48, 48); + s = createSprite(x, y, 48, 48); spriteSetShapeColor(s, colorByName("blue")); groupAdd(pulse, s); x += 64; // with tint color - s = createSpriteEx(x, y, 48, 48); - spriteSetAnimation(s, createAnimation("data/sprite/bricks.png")); + s = createSpriteEx(x, y, 48, 48, createAnimation("data/sprite/bricks.png")); spriteSetTintColor(s, colorByName("red")); groupAdd(pulse, s); x += 64; // semi-transparent - s = createSpriteEx(x, y, 48, 48); - spriteSetAnimation(s, createAnimation("data/sprite/bricks.png")); + s = createSpriteEx(x, y, 48, 48, createAnimation("data/sprite/bricks.png")); spriteSetTintColor(s, colorByRGBA(1, 1, 1, 0.5)); groupAdd(pulse, s); x += 64; @@ -58,28 +53,21 @@ void spritesInit() { y += 64; // tiles - s = createSpriteEx(x - 48/2, y + 48/2, 48, 48); - spriteSetAnimation(s, createAnimation("data/sprite/bricks.png")); + s = createSpriteEx(x - 48/2, y + 48/2, 48, 48, createAnimation("data/sprite/bricks.png")); groupAdd(pulse, s); - s = createSpriteEx(x, y, 48, 48); - spriteSetAnimation(s, createAnimation("data/sprite/bricks-tile.png")); - s = createSpriteEx(x, y + 48, 48, 48); - spriteSetAnimation(s, createAnimation("data/sprite/bricks-tile.png")); + createSpriteEx(x, y, 48, 48, createAnimation("data/sprite/bricks-tile.png")); + createSpriteEx(x, y + 48, 48, 48, createAnimation("data/sprite/bricks-tile.png")); x -= 48; - s = createSpriteEx(x, y, 48, 48); - spriteSetAnimation(s, createAnimation("data/sprite/bricks-tile.png")); - s = createSpriteEx(x, y + 48, 48, 48); - spriteSetAnimation(s, createAnimation("data/sprite/bricks-tile.png")); + createSpriteEx(x, y, 48, 48, createAnimation("data/sprite/bricks-tile.png")); + createSpriteEx(x, y + 48, 48, 48, createAnimation("data/sprite/bricks-tile.png")); x -= 64; // blend two sprites - s = createSpriteEx(x, y, 48, 48); - spriteSetAnimation(s, createAnimation("data/sprite/bricks-tile.png")); + s = createSpriteEx(x, y, 48, 48, createAnimation("data/sprite/bricks-tile.png")); spriteSetTintColor(s, colorByRGBA(1, 1, 1, 0.5)); groupAdd(pulse, s); x -= 16; - s = createSpriteEx(x, y + 16, 48, 48); - spriteSetAnimation(s, createAnimation("data/sprite/bricks-tile.png")); + s = createSpriteEx(x, y + 16, 48, 48, createAnimation("data/sprite/bricks-tile.png")); spriteSetTintColor(s, colorByRGBA(1, 1, 1, 0.5)); groupAdd(pulse, s); x -= 64; @@ -87,8 +75,7 @@ void spritesInit() { // texture size is not power of two double k = 0.25, w = 343*k, h = 221*k; x += 48/2 - w/2; - s = createSpriteEx(x, y, w, h); - spriteSetAnimation(s, createAnimation("data/sprite/snake.png")); + s = createSpriteEx(x, y, w, h, createAnimation("data/sprite/snake.png")); spriteSetDebug(s, TRUE); groupAdd(pulse, s); } diff --git a/doc/helianthus-doc-ru.odt b/doc/helianthus-doc-ru.odt index a66c167..eda0ced 100644 Binary files a/doc/helianthus-doc-ru.odt and b/doc/helianthus-doc-ru.odt differ diff --git a/src/group.c b/src/group.c index 089ea07..4d22671 100644 --- a/src/group.c +++ b/src/group.c @@ -29,10 +29,10 @@ Group createEdgesGroupEx(double x1, double y1, double x2, double y2, double bord y1 -= b; Group g = createGroup(); - groupAdd(g, createSpriteEx(x1 + w/2, y1 + b/2, w, b)); - groupAdd(g, createSpriteEx(x1 + w/2, y2 + b/2, w, b)); - groupAdd(g, createSpriteEx(x1 + b/2, y1 + h/2, b, h)); - groupAdd(g, createSpriteEx(x2 + b/2, y1 + h/2, b, h)); + groupAdd(g, createSprite(x1 + w/2, y1 + b/2, w, b)); + groupAdd(g, createSprite(x1 + w/2, y2 + b/2, w, b)); + groupAdd(g, createSprite(x1 + b/2, y1 + h/2, b, h)); + groupAdd(g, createSprite(x2 + b/2, y1 + h/2, b, h)); groupSetMassLevelEach(g, massLevel); return g; } diff --git a/src/sprite.c b/src/sprite.c index da8ab78..bb3cceb 100644 --- a/src/sprite.c +++ b/src/sprite.c @@ -89,10 +89,15 @@ static void prepareCollider(Sprite sprite, HeliCollider *collider) { if (collider->type == COLLIDER_RECTANGLE) { double rmax = 0.5*( collider->width < collider->height ? collider->width : collider->height ); - if (collider->radius >= rmax - HELI_PRECISION) + if (collider->radius >= rmax - HELI_PRECISION) { collider->radius = rmax; - collider->width -= 2*collider->radius; - collider->height -= 2*collider->radius; + collider->type = COLLIDER_CIRCLE; + collider->width = 0; + collider->height = 0; + } else { + collider->width -= 2*collider->radius; + collider->height -= 2*collider->radius; + } } } @@ -103,7 +108,7 @@ static void autoRotate(Sprite sprite) { } -Sprite createSpriteEx(double x, double y, double width, double height) { +Sprite createSpriteEx(double x, double y, double width, double height, Animation animation) { if (!heliInitialized) return NULL; Sprite s = calloc(1, sizeof(*s)); @@ -111,6 +116,7 @@ Sprite createSpriteEx(double x, double y, double width, double height) { s->y = y; s->width = width; s->height = height; + s->animation = animation; s->scale = 1; s->collider.type = COLLIDER_RECTANGLE; @@ -137,8 +143,8 @@ Sprite createSpriteEx(double x, double y, double width, double height) { return s; } -Sprite createSprite(double x, double y) - { return createSpriteEx(x, y, 100, 100); } +Sprite createSprite(double x, double y, double width, double height) + { return createSpriteEx(x, y, width, height, NULL); } void spriteDestroy(Sprite sprite) { if (sprite->destroyCallback) sprite->destroyCallback(sprite); @@ -528,6 +534,20 @@ void heliSpriteDrawDebug(Sprite s) { line(-hw/4, 0, hw/4, 0); line(0, -hh/4, 0, hh/4); + // collider + HeliCollider cl = {}; + prepareCollider(s, &cl); + saveState(); + rotate(-s->rotation); + translate(-s->x, -s->y); + translate(cl.x, cl.y); + rotate(cl.rotation); + stroke(colorByRGBA(0, 0, 1, 0.75)); + double cw = cl.width + 2*cl.radius; + double ch = cl.height + 2*cl.radius; + rectRounded(-cw/2, -ch/2, cw, ch, cl.radius); + restoreState(); + // depth double s1 = hw*0.25; double s2 = hh*0.5; diff --git a/src/sprite.h b/src/sprite.h index 7694f8a..d45f011 100644 --- a/src/sprite.h +++ b/src/sprite.h @@ -16,8 +16,8 @@ typedef enum _Collider { } Collider; -Sprite createSprite(double x, double y); -Sprite createSpriteEx(double x, double y, double width, double height); +Sprite createSprite(double x, double y, double width, double height); +Sprite createSpriteEx(double x, double y, double width, double height, Animation animation); void spriteDestroy(Sprite sprite); void spriteDestroyTimer(Sprite sprite, double lifetime);