From 53e18ee6e2c702a4c4db7b7de3acf97c45d3e99c Mon Sep 17 00:00:00 2001 From: Ivan Mahonin Date: Aug 02 2020 18:11:38 +0000 Subject: spriteClone --- diff --git a/src/animation.c b/src/animation.c index 65b337d..5dd43b9 100644 --- a/src/animation.c +++ b/src/animation.c @@ -318,6 +318,12 @@ Animation createAnimation(const char *path) { return createAnimationEx(path, TRUE, FALSE, FALSE); } void animationDestroy(Animation animation) { + heliSpriteUnsetAnimation(animation); + HeliDrawingState *ss = heliDrawingGetState(); + for(HeliDrawingState *s = heliDrawingGetStateStack(); s <= ss; ++s) { + if (ss->fillTexture.animation == animation) ss->fillTexture.animation = NULL; + if (ss->strokeTexture.animation == animation) ss->strokeTexture.animation = NULL; + } *(animation->prev ? &animation->prev->next : &first) = animation->next; *(animation->next ? &animation->next->prev : &last ) = animation->prev; heliArrayDestroy(&animation->frames); @@ -422,7 +428,7 @@ void animationSetPos(Animation animation, double pos) int animationGetFrame(Animation animation) { int cnt = animationGetFramesCount(animation); int i = floor(animation->pos + HELI_PRECISION); - if (i > cnt) i = cnt; + if (i >= cnt) i = cnt - 1; if (i < 0) i = 0; return i; } diff --git a/src/drawing.c b/src/drawing.c index 6776b6a..fbbf7d9 100644 --- a/src/drawing.c +++ b/src/drawing.c @@ -76,8 +76,6 @@ void background(unsigned int colorCode) { heliColorToDouble(colorCode, colorBack); } void fill(unsigned int colorCode) { heliColorToDouble(colorCode, heliDrawingGetState()->fillColor); } -void noFill() - { fill(0); } void fillTexture(Animation animation, double x, double y, double width, double height, int fixed) { HeliTextureState *s = &heliDrawingGetState()->fillTexture; s->animation = animation; @@ -87,11 +85,11 @@ void fillTexture(Animation animation, double x, double y, double width, double h s->height = height; s->fixed = fixed != 0; } +void noFill() + { fill(0); fillTexture(NULL, 0, 0, 1, 1, FALSE); } void stroke(unsigned int colorCode) { heliColorToDouble(colorCode, heliDrawingGetState()->strokeColor); } -void noStroke() - { stroke(0); } void strokeTexture(Animation animation, double x, double y, double width, double height, int fixed) { HeliTextureState *s = &heliDrawingGetState()->strokeTexture; s->animation = animation; @@ -101,6 +99,8 @@ void strokeTexture(Animation animation, double x, double y, double width, double s->height = height; s->fixed = fixed != 0; } +void noStroke() + { stroke(0); strokeTexture(NULL, 0, 0, 1, 1, FALSE); } void strokeWidth(double width) { heliDrawingGetState()->strokeWidth = width; } diff --git a/src/private.h b/src/private.h index f98d5ee..aa2cd33 100644 --- a/src/private.h +++ b/src/private.h @@ -207,6 +207,7 @@ typedef void (*HeliSpriteEashUInt)(Sprite, unsigned int); typedef void (*HeliSpriteEashDouble)(Sprite, double); HeliArray* heliSpriteGetGroups(Sprite sprite); void heliSpriteUpdate(double dt); +void heliSpriteUnsetAnimation(Animation animation); void heliSpriteFinish(); diff --git a/src/sprite.c b/src/sprite.c index 61b20ea..20ee9dd 100644 --- a/src/sprite.c +++ b/src/sprite.c @@ -140,6 +140,16 @@ void spriteDestroyTimer(Sprite sprite, double lifeTime) { sprite->lifeTime = lifeTime; } +Sprite spriteClone(Sprite sprite) { + if (!heliInitialized) return NULL; + Sprite s = calloc(1, sizeof(*s)); + memcpy(s, sprite, sizeof(*s)); + s->lifeTime = -1; + memset(&s->groups, 0, sizeof(s->groups)); + return s; +} + + double spriteGetX(Sprite sprite) { return sprite->x; } void spriteSetX(Sprite sprite, double x) { sprite->x = x; } @@ -298,6 +308,12 @@ void spriteSetColliderEx( } } +int spriteIsPointInside(Sprite sprite, double x, double y) { + HeliCollider collider; + prepareCollider(sprite, &collider); + return heliPointCollision(&collider, x, y); +} + Animation spriteGetAnimation(Sprite sprite) { return sprite->animation; } @@ -449,12 +465,6 @@ void drawSprites() { } } -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) { @@ -479,6 +489,14 @@ void heliSpriteUpdate(double dt) { HeliArray* heliSpriteGetGroups(Sprite sprite) { return &sprite->groups; } +void heliSpriteUnsetAnimation(Animation animation) { + for(int i = 0; i < sprites.count; ++i) { + Sprite s = (Sprite)sprites.items[i].value; + if (spriteGetAnimation(s) == animation) + spriteSetNoAnimation(s); + } +} + void heliSpriteFinish() { while(worldGetSpriteCount()) { spriteDestroy(worldGetSprite(0)); } diff --git a/src/sprite.h b/src/sprite.h index f003461..86defd1 100644 --- a/src/sprite.h +++ b/src/sprite.h @@ -21,6 +21,8 @@ Sprite createSpriteEx(double x, double y, double width, double height); void spriteDestroy(Sprite sprite); void spriteDestroyTimer(Sprite sprite, double lifetime); +Sprite spriteClone(Sprite sprite); + double spriteGetX(Sprite sprite); void spriteSetX(Sprite sprite, double x); @@ -75,6 +77,8 @@ int spriteBounceOff(Sprite sprite, Sprite other, double bounciness); int spritePush(Sprite sprite, Sprite other, double bounciness); int spriteCollideEx(Sprite a, Sprite b, int keepVelocityA, int keepVelocityB, double bounciness); +int spriteIsPointInside(Sprite sprite, double x, double y); + double spriteGetBounciness(Sprite sprite); void spriteSetBounciness(Sprite sprite, double bounciness); diff --git a/src/world.c b/src/world.c index e75dd14..8ad6a9d 100644 --- a/src/world.c +++ b/src/world.c @@ -106,9 +106,6 @@ double transformedMouseY() { return y; } -int mousePressedOver(Sprite sprite) - { return keyEventGetCount(KEYEVENT_MOUSE_DOWN) && mouseIsOver(sprite); } - static void resize(int w, int h) { w = w > minWidth ? w : minWidth; h = h > minHeight ? h : minHeight; diff --git a/src/world.h b/src/world.h index 178d534..64ffb99 100644 --- a/src/world.h +++ b/src/world.h @@ -42,9 +42,6 @@ double mouseY(); double transformedMouseX(); double transformedMouseY(); -int mouseIsOver(Sprite sprite); -int mousePressedOver(Sprite sprite); - void messageBox(const char *message); int questionBox(const char *question, const char *answer0, const char *answer1); int questionBox3(const char *question, const char *answer0, const char *answer1, const char *answer2);