Skip to content

Commit 573db2e

Browse files
committed
Add walking animation example
1 parent ea8620f commit 573db2e

File tree

3 files changed

+110
-5
lines changed

3 files changed

+110
-5
lines changed

README.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ int main() {
4747
{
4848
ClearBackground(RAYWHITE);
4949

50-
// Draw the 0th frame from the George sprite.
50+
// Draw the first frame from the George sprite.
5151
DrawAseprite(george, 0, 100, 100, WHITE);
5252

5353
// Draw the Walk Down animation.
@@ -103,10 +103,6 @@ bool IsAsepriteSliceReady(AsepriteSlice slice); // Return wh
103103
AsepriteSlice GenAsepriteSliceDefault(); // Generate empty Aseprite slice data.
104104
```
105105
106-
## Known Issues
107-
108-
* [#14](https://github.com/RobLoach/raylib-aseprite/issues/14) Supports only Aseprite 1.2. [`cute_aseprite.h`](https://github.com/RandyGaul/cute_headers/blob/master/cute_aseprite.h) does not yet support Aseprite 1.3, so make sure you're using 1.2.
109-
110106
## Development
111107
112108
To build the example locally, and run tests, use [cmake](https://cmake.org/).

examples/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ target_link_libraries(raylib-aseprite-numbers PUBLIC
3333
)
3434
set_property(TARGET raylib-aseprite-numbers PROPERTY C_STANDARD 99)
3535

36+
# raylib-aseprite-walk
37+
add_executable(raylib-aseprite-walk raylib-aseprite-walk.c)
38+
target_link_libraries(raylib-aseprite-walk PUBLIC
39+
raylib
40+
raylib_aseprite
41+
)
42+
set_property(TARGET raylib-aseprite-walk PROPERTY C_STANDARD 99)
43+
3644
# Copy the resources
3745
file(GLOB resources resources/*)
3846
set(test_resources)

examples/raylib-aseprite-walk.c

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*******************************************************************************************
2+
*
3+
* [raylib-aseprite] example - Load a Aseprite file, and display the animated sprite.
4+
*
5+
* This example has been created using raylib 3.5 (www.raylib.com)
6+
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
7+
*
8+
* Example by Rob Loach (@RobLoach)
9+
*
10+
* Copyright (c) 2021 Rob Loach (@RobLoach)
11+
*
12+
********************************************************************************************/
13+
14+
#include "raylib.h"
15+
16+
#define RAYLIB_ASEPRITE_IMPLEMENTATION
17+
#include "raylib-aseprite.h"
18+
19+
int main() {
20+
// Initialization
21+
//--------------------------------------------------------------------------------------
22+
const int screenWidth = 800;
23+
const int screenHeight = 450;
24+
InitWindow(screenWidth, screenHeight, "[raylib-aseprite] example");
25+
SetTargetFPS(60);
26+
27+
// Load the Aseprite file.
28+
Aseprite george = LoadAseprite("resources/george.aseprite");
29+
30+
// Load the standing animations from the Aseprite tags.
31+
AsepriteTag down = LoadAsepriteTag(george, "Walk-Down");
32+
AsepriteTag left = LoadAsepriteTag(george, "Walk-Left");
33+
AsepriteTag right = LoadAsepriteTag(george, "Walk-Right");
34+
AsepriteTag up = LoadAsepriteTag(george, "Walk-Up");
35+
AsepriteTag* current = &down;
36+
37+
// Center George on the screen.
38+
const float scale = 6;
39+
const int speed = 4;
40+
Vector2 position = {
41+
GetScreenWidth() / 2 - GetAsepriteWidth(george) / 2 * scale,
42+
GetScreenHeight() / 2 - GetAsepriteHeight(george) / 2 * scale
43+
};
44+
//--------------------------------------------------------------------------------------
45+
46+
while(!WindowShouldClose()) {
47+
48+
// Update
49+
//----------------------------------------------------------------------------------
50+
// Update the active moving animation.
51+
52+
if (IsKeyDown(KEY_UP)) {
53+
current = &up;
54+
position.y -= speed;
55+
current->paused = false;
56+
}
57+
else if (IsKeyDown(KEY_RIGHT)) {
58+
current = &right;
59+
position.x += speed;
60+
current->paused = false;
61+
}
62+
else if (IsKeyDown(KEY_DOWN)) {
63+
current = &down;
64+
position.y += speed;
65+
current->paused = false;
66+
}
67+
else if (IsKeyDown(KEY_LEFT)) {
68+
current = &left;
69+
position.x -= speed;
70+
current->paused = false;
71+
}
72+
else {
73+
current->paused = true;
74+
}
75+
UpdateAsepriteTag(current);
76+
77+
//----------------------------------------------------------------------------------
78+
79+
// Draw
80+
//----------------------------------------------------------------------------------
81+
BeginDrawing();
82+
{
83+
ClearBackground(RAYWHITE);
84+
85+
// Draw the current walking animation.
86+
DrawAsepriteTagEx(*current, position, 0, scale, WHITE);
87+
}
88+
EndDrawing();
89+
//----------------------------------------------------------------------------------
90+
}
91+
92+
// De-Initialization
93+
//--------------------------------------------------------------------------------------
94+
95+
UnloadAseprite(george); // Unload the Aseprite data.
96+
97+
CloseWindow();
98+
//--------------------------------------------------------------------------------------
99+
100+
return 0;
101+
}

0 commit comments

Comments
 (0)