Skip to content

Commit 414f2e5

Browse files
authored
Merge pull request #16 from RobLoach/frame
Add SetAsepriteTagFrame() and GetAsepriteTagFrame()
2 parents 90022c0 + d2c9a6f commit 414f2e5

File tree

4 files changed

+55
-1
lines changed

4 files changed

+55
-1
lines changed

examples/raylib-aseprite-walk.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ int main() {
4747

4848
// Update
4949
//----------------------------------------------------------------------------------
50-
// Update the active moving animation.
5150

51+
// Update the active moving animation.
5252
if (IsKeyDown(KEY_UP)) {
5353
current = &up;
5454
position.y -= speed;
@@ -72,6 +72,13 @@ int main() {
7272
else {
7373
current->paused = true;
7474
}
75+
76+
// Have George stop walking with two feet on the ground.
77+
if (current->paused) {
78+
SetAsepriteTagFrame(current, 1);
79+
}
80+
81+
// Update the animation.
7582
UpdateAsepriteTag(current);
7683

7784
//----------------------------------------------------------------------------------
@@ -84,6 +91,9 @@ int main() {
8491

8592
// Draw the current walking animation.
8693
DrawAsepriteTagEx(*current, position, 0, scale, WHITE);
94+
95+
const char* text = "Use arrow keys to walk";
96+
DrawText(text, GetScreenWidth() / 2 - MeasureText(text, 20) / 2, GetScreenHeight() - 80, 20, GRAY);
8797
}
8898
EndDrawing();
8999
//----------------------------------------------------------------------------------

examples/resources/george.aseprite

112 Bytes
Binary file not shown.

include/raylib-aseprite.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ void DrawAsepriteTag(AsepriteTag tag, int posX, int posY, Color tint);
6767
void DrawAsepriteTagV(AsepriteTag tag, Vector2 position, Color tint);
6868
void DrawAsepriteTagEx(AsepriteTag tag, Vector2 position, float rotation, float scale, Color tint);
6969
void DrawAsepriteTagPro(AsepriteTag tag, Rectangle dest, Vector2 origin, float rotation, Color tint);
70+
void SetAsepriteTagFrame(AsepriteTag* tag, int frameNumber); // Sets which frame the tag is currently displaying.
71+
int GetAsepriteTagFrame(AsepriteTag tag);
7072

7173
// Aseprite Slice functions
7274
AsepriteSlice LoadAsepriteSlice(Aseprite aseprite, const char* name); // Load a slice from an Aseprite based on its name.
@@ -512,6 +514,35 @@ void UpdateAsepriteTag(AsepriteTag* tag) {
512514
tag->timer = (float)(ase->frames[tag->currentFrame].duration_milliseconds) / 1000.0f /* + tag->timer; */;
513515
}
514516

517+
/**
518+
* Set which frame the Aseprite tag is on.
519+
*
520+
* @param tag The Aseprite tag to modify.
521+
* @param frameNumber Which frame to set the active tag to. If negative, will start from the end.
522+
*/
523+
void SetAsepriteTagFrame(AsepriteTag* tag, int frameNumber) {
524+
// TODO: Need to attribute frame number for ASE_ANIMATION_DIRECTION_BACKWORDS?
525+
if (frameNumber >= 0) {
526+
tag->currentFrame = tag->tag->from_frame + frameNumber;
527+
}
528+
else {
529+
tag->currentFrame = tag->tag->to_frame + frameNumber;
530+
}
531+
532+
// Bounds
533+
if (tag->currentFrame < tag->tag->from_frame) {
534+
tag->currentFrame = tag->tag->from_frame;
535+
}
536+
else if (tag->currentFrame > tag->tag->to_frame) {
537+
tag->currentFrame = tag->tag->to_frame;
538+
}
539+
}
540+
541+
int GetAsepriteTagFrame(AsepriteTag tag) {
542+
// TODO: Need to attribute frame number for ASE_ANIMATION_DIRECTION_BACKWORDS?
543+
return tag.currentFrame - tag.tag->from_frame;
544+
}
545+
515546
void DrawAsepriteTag(AsepriteTag tag, int posX, int posY, Color tint) {
516547
DrawAseprite(tag.aseprite, tag.currentFrame, posX, posY, tint);
517548
}

test/raylib-aseprite-test.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,19 @@ int main(int argc, char *argv[]) {
6565
assert(TextIsEqual(tag2.name, "Ping-Pong"));
6666
}
6767

68+
// SetAsepriteTagFrame()
69+
{
70+
AsepriteTag tag = LoadAsepriteTagFromIndex(aseprite, 2);
71+
SetAsepriteTagFrame(&tag, 4);
72+
73+
int frame = GetAsepriteTagFrame(tag);
74+
assert(frame == 4);
75+
76+
SetAsepriteTagFrame(&tag, -3);
77+
frame = GetAsepriteTagFrame(tag);
78+
assert(frame == 9 - 3);
79+
}
80+
6881
// GetAsepriteTexture()
6982
Texture texture = GetAsepriteTexture(aseprite);
7083
assert(texture.width > 50);

0 commit comments

Comments
 (0)