aboutsummaryrefslogtreecommitdiffstats
path: root/src/util.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/util.c')
-rw-r--r--src/util.c31
1 files changed, 18 insertions, 13 deletions
diff --git a/src/util.c b/src/util.c
index 864fab6..819a956 100644
--- a/src/util.c
+++ b/src/util.c
@@ -10,24 +10,29 @@ Vector2 getScaledMousePosition()
return mousePosition;
}
-bool doesCollideWithAnimation(Rectangle rect, Animation* animation, Vector2 point)
+bool doesCollideWithAnimationData(Rectangle rect, void* data, int width, int height, int frame, Vector2 point)
{
- float xScale = (float)rect.width / animation->width;
- float yScale = (float)rect.height / animation->height;
+ // Doesn't collide with rect.
+ if (!CheckCollisionPointRec(point, rect))
+ {
+ return false;
+ }
- unsigned int frameOffset = animation->width * animation->height * 4 * animation->currentFrame;
+ float xScale = (float)width / rect.width;
+ float yScale = (float)height / rect.height;
- // Check each pixal.
- for (int row = 0; row < rect.height; ++row)
- {
- for (int col = 0; col < rect.width; ++col)
- {
- int scaledRow = row * yScale;
- int scaledCol = col * xScale;
+ int row = (point.y - rect.y) * yScale;
+ int col = (point.x - rect.x) * xScale;
- int pixalPosition = frameOffset + (animation->width * scaledRow + scaledCol);
+ unsigned int frameOffset = width * height * frame;
- }
+ // Position of the apha byte in the color.
+ unsigned int position = (frameOffset + (width * row + col)) * 4 + 3;
+
+ // Check apha at position.
+ if (*(((unsigned char*)data) + position) != 0x00)
+ {
+ return true;
}
return false;