|
| 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