| 1 | /******************************************************************************************* |
| 2 | * |
| 3 | * raylib [textures] example - Texture loading and drawing a part defined by a rectangle |
| 4 | * |
| 5 | * This example has been created using raylib 1.3 (www.raylib.com) |
| 6 | * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) |
| 7 | * |
| 8 | * Copyright (c) 2014 Ramon Santamaria (@raysan5) |
| 9 | * |
| 10 | ********************************************************************************************/ |
| 11 | |
| 12 | #include "raylib.h" |
| 13 | |
| 14 | #define MAX_FRAME_SPEED 15 |
| 15 | #define MIN_FRAME_SPEED 1 |
| 16 | |
| 17 | int main(void) |
| 18 | { |
| 19 | // Initialization |
| 20 | //-------------------------------------------------------------------------------------- |
| 21 | const int screenWidth = 800; |
| 22 | const int screenHeight = 450; |
| 23 | |
| 24 | InitWindow(screenWidth, screenHeight, "raylib [texture] example - texture rectangle" ); |
| 25 | |
| 26 | // NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required) |
| 27 | Texture2D scarfy = LoadTexture("resources/scarfy.png" ); // Texture loading |
| 28 | |
| 29 | Vector2 position = { 350.0f, 280.0f }; |
| 30 | Rectangle frameRec = { 0.0f, 0.0f, (float)scarfy.width/6, (float)scarfy.height }; |
| 31 | int currentFrame = 0; |
| 32 | |
| 33 | int framesCounter = 0; |
| 34 | int framesSpeed = 8; // Number of spritesheet frames shown by second |
| 35 | |
| 36 | SetTargetFPS(60); // Set our game to run at 60 frames-per-second |
| 37 | //-------------------------------------------------------------------------------------- |
| 38 | |
| 39 | // Main game loop |
| 40 | while (!WindowShouldClose()) // Detect window close button or ESC key |
| 41 | { |
| 42 | // Update |
| 43 | //---------------------------------------------------------------------------------- |
| 44 | framesCounter++; |
| 45 | |
| 46 | if (framesCounter >= (60/framesSpeed)) |
| 47 | { |
| 48 | framesCounter = 0; |
| 49 | currentFrame++; |
| 50 | |
| 51 | if (currentFrame > 5) currentFrame = 0; |
| 52 | |
| 53 | frameRec.x = (float)currentFrame*(float)scarfy.width/6; |
| 54 | } |
| 55 | |
| 56 | if (IsKeyPressed(KEY_RIGHT)) framesSpeed++; |
| 57 | else if (IsKeyPressed(KEY_LEFT)) framesSpeed--; |
| 58 | |
| 59 | if (framesSpeed > MAX_FRAME_SPEED) framesSpeed = MAX_FRAME_SPEED; |
| 60 | else if (framesSpeed < MIN_FRAME_SPEED) framesSpeed = MIN_FRAME_SPEED; |
| 61 | //---------------------------------------------------------------------------------- |
| 62 | |
| 63 | // Draw |
| 64 | //---------------------------------------------------------------------------------- |
| 65 | BeginDrawing(); |
| 66 | |
| 67 | ClearBackground(RAYWHITE); |
| 68 | |
| 69 | DrawTexture(scarfy, 15, 40, WHITE); |
| 70 | DrawRectangleLines(15, 40, scarfy.width, scarfy.height, LIME); |
| 71 | DrawRectangleLines(15 + frameRec.x, 40 + frameRec.y, frameRec.width, frameRec.height, RED); |
| 72 | |
| 73 | DrawText("FRAME SPEED: " , 165, 210, 10, DARKGRAY); |
| 74 | DrawText(FormatText("%02i FPS" , framesSpeed), 575, 210, 10, DARKGRAY); |
| 75 | DrawText("PRESS RIGHT/LEFT KEYS to CHANGE SPEED!" , 290, 240, 10, DARKGRAY); |
| 76 | |
| 77 | for (int i = 0; i < MAX_FRAME_SPEED; i++) |
| 78 | { |
| 79 | if (i < framesSpeed) DrawRectangle(250 + 21*i, 205, 20, 20, RED); |
| 80 | DrawRectangleLines(250 + 21*i, 205, 20, 20, MAROON); |
| 81 | } |
| 82 | |
| 83 | DrawTextureRec(scarfy, frameRec, position, WHITE); // Draw part of the texture |
| 84 | |
| 85 | DrawText("(c) Scarfy sprite by Eiden Marsal" , screenWidth - 200, screenHeight - 20, 10, GRAY); |
| 86 | |
| 87 | EndDrawing(); |
| 88 | //---------------------------------------------------------------------------------- |
| 89 | } |
| 90 | |
| 91 | // De-Initialization |
| 92 | //-------------------------------------------------------------------------------------- |
| 93 | UnloadTexture(scarfy); // Texture unloading |
| 94 | |
| 95 | CloseWindow(); // Close window and OpenGL context |
| 96 | //-------------------------------------------------------------------------------------- |
| 97 | |
| 98 | return 0; |
| 99 | } |