|
| 1 | +/******************************************************************************************* |
| 2 | +* |
| 3 | +* [raylib-aseprite] example - Load a Aseprite file, and display the animated sprites. |
| 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 standing[4] = { |
| 32 | + LoadAsepriteTag(george, "Stand-Up"), |
| 33 | + LoadAsepriteTag(george, "Stand-Right"), |
| 34 | + LoadAsepriteTag(george, "Stand-Down"), |
| 35 | + LoadAsepriteTag(george, "Stand-Left") |
| 36 | + }; |
| 37 | + |
| 38 | + // Load the moving animations from the Aseprite tags. |
| 39 | + AsepriteTag moving[4] = { |
| 40 | + LoadAsepriteTag(george, "Walk-Up"), |
| 41 | + LoadAsepriteTag(george, "Walk-Right"), |
| 42 | + LoadAsepriteTag(george, "Walk-Down"), |
| 43 | + LoadAsepriteTag(george, "Walk-Left") |
| 44 | + }; |
| 45 | + |
| 46 | + // Start the player facing down. |
| 47 | + int direction = 2; |
| 48 | + |
| 49 | + // Center the player on the screen, and scale them. |
| 50 | + const float scale = 3; |
| 51 | + Vector2 playerPosition = { |
| 52 | + GetScreenWidth() / 2 - GetAsepriteWidth(george) / 2 * scale, |
| 53 | + GetScreenHeight() / 2 - GetAsepriteHeight(george) / 2 * scale |
| 54 | + }; |
| 55 | + |
| 56 | + // How fast the player should move around. |
| 57 | + const float speed = 2; |
| 58 | + |
| 59 | + // Whether or not the player is actively moving. |
| 60 | + bool playerMoving; |
| 61 | + //-------------------------------------------------------------------------------------- |
| 62 | + |
| 63 | + while(!WindowShouldClose()) { |
| 64 | + |
| 65 | + // Update |
| 66 | + //---------------------------------------------------------------------------------- |
| 67 | + if (IsKeyDown(KEY_UP)) { |
| 68 | + direction = 0; |
| 69 | + playerMoving = true; |
| 70 | + } |
| 71 | + else if (IsKeyDown(KEY_RIGHT)) { |
| 72 | + direction = 1; |
| 73 | + playerMoving = true; |
| 74 | + } |
| 75 | + else if (IsKeyDown(KEY_DOWN)) { |
| 76 | + direction = 2; |
| 77 | + playerMoving = true; |
| 78 | + } |
| 79 | + else if (IsKeyDown(KEY_LEFT)) { |
| 80 | + direction = 3; |
| 81 | + playerMoving = true; |
| 82 | + } |
| 83 | + else { |
| 84 | + playerMoving = false; |
| 85 | + } |
| 86 | + |
| 87 | + if (playerMoving) { |
| 88 | + // Update the active moving animation. |
| 89 | + UpdateAsepriteTag(&moving[direction]); |
| 90 | + |
| 91 | + // Move the player. |
| 92 | + switch (direction) { |
| 93 | + case 0: |
| 94 | + playerPosition.y -= speed; |
| 95 | + break; |
| 96 | + case 1: |
| 97 | + playerPosition.x += speed; |
| 98 | + break; |
| 99 | + case 2: |
| 100 | + playerPosition.y += speed; |
| 101 | + break; |
| 102 | + case 3: |
| 103 | + playerPosition.x -= speed; |
| 104 | + break; |
| 105 | + } |
| 106 | + } |
| 107 | + //---------------------------------------------------------------------------------- |
| 108 | + |
| 109 | + // Draw |
| 110 | + //---------------------------------------------------------------------------------- |
| 111 | + BeginDrawing(); |
| 112 | + { |
| 113 | + ClearBackground(RAYWHITE); |
| 114 | + |
| 115 | + // Draw either the moving, or standing animation. |
| 116 | + if (playerMoving) { |
| 117 | + DrawAsepriteTagEx(moving[direction], playerPosition, 0, scale, WHITE); |
| 118 | + } |
| 119 | + else { |
| 120 | + DrawAsepriteTagEx(standing[direction], playerPosition, 0, scale, WHITE); |
| 121 | + } |
| 122 | + } |
| 123 | + EndDrawing(); |
| 124 | + //---------------------------------------------------------------------------------- |
| 125 | + } |
| 126 | + |
| 127 | + // De-Initialization |
| 128 | + //-------------------------------------------------------------------------------------- |
| 129 | + |
| 130 | + UnloadAseprite(george); // Unload the Aseprite data. |
| 131 | + |
| 132 | + CloseWindow(); |
| 133 | + //-------------------------------------------------------------------------------------- |
| 134 | + |
| 135 | + return 0; |
| 136 | +} |
0 commit comments