You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 rivejä
4.1 KiB

11 vuotta sitten
11 vuotta sitten
11 vuotta sitten
11 kuukautta sitten
11 vuotta sitten
5 vuotta sitten
11 vuotta sitten
5 vuotta sitten
11 vuotta sitten
5 vuotta sitten
5 vuotta sitten
11 vuotta sitten
5 vuotta sitten
5 vuotta sitten
5 vuotta sitten
5 vuotta sitten
5 vuotta sitten
11 vuotta sitten
11 vuotta sitten
11 vuotta sitten
5 vuotta sitten
5 vuotta sitten
5 vuotta sitten
11 vuotta sitten
11 vuotta sitten
11 vuotta sitten
11 vuotta sitten
  1. /*******************************************************************************************
  2. *
  3. * raylib [textures] example - Sprite animation
  4. *
  5. * Example originally created with raylib 1.3, last time updated with raylib 1.3
  6. *
  7. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  8. * BSD-like license that allows static linking with closed source software
  9. *
  10. * Copyright (c) 2014-2024 Ramon Santamaria (@raysan5)
  11. *
  12. ********************************************************************************************/
  13. #include "raylib.h"
  14. #define MAX_FRAME_SPEED 15
  15. #define MIN_FRAME_SPEED 1
  16. //------------------------------------------------------------------------------------
  17. // Program main entry point
  18. //------------------------------------------------------------------------------------
  19. int main(void)
  20. {
  21. // Initialization
  22. //--------------------------------------------------------------------------------------
  23. const int screenWidth = 800;
  24. const int screenHeight = 450;
  25. InitWindow(screenWidth, screenHeight, "raylib [texture] example - sprite anim");
  26. // NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
  27. Texture2D scarfy = LoadTexture("resources/scarfy.png"); // Texture loading
  28. Vector2 position = { 350.0f, 280.0f };
  29. Rectangle frameRec = { 0.0f, 0.0f, (float)scarfy.width/6, (float)scarfy.height };
  30. int currentFrame = 0;
  31. int framesCounter = 0;
  32. int framesSpeed = 8; // Number of spritesheet frames shown by second
  33. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  34. //--------------------------------------------------------------------------------------
  35. // Main game loop
  36. while (!WindowShouldClose()) // Detect window close button or ESC key
  37. {
  38. // Update
  39. //----------------------------------------------------------------------------------
  40. framesCounter++;
  41. if (framesCounter >= (60/framesSpeed))
  42. {
  43. framesCounter = 0;
  44. currentFrame++;
  45. if (currentFrame > 5) currentFrame = 0;
  46. frameRec.x = (float)currentFrame*(float)scarfy.width/6;
  47. }
  48. // Control frames speed
  49. if (IsKeyPressed(KEY_RIGHT)) framesSpeed++;
  50. else if (IsKeyPressed(KEY_LEFT)) framesSpeed--;
  51. if (framesSpeed > MAX_FRAME_SPEED) framesSpeed = MAX_FRAME_SPEED;
  52. else if (framesSpeed < MIN_FRAME_SPEED) framesSpeed = MIN_FRAME_SPEED;
  53. //----------------------------------------------------------------------------------
  54. // Draw
  55. //----------------------------------------------------------------------------------
  56. BeginDrawing();
  57. ClearBackground(RAYWHITE);
  58. DrawTexture(scarfy, 15, 40, WHITE);
  59. DrawRectangleLines(15, 40, scarfy.width, scarfy.height, LIME);
  60. DrawRectangleLines(15 + (int)frameRec.x, 40 + (int)frameRec.y, (int)frameRec.width, (int)frameRec.height, RED);
  61. DrawText("FRAME SPEED: ", 165, 210, 10, DARKGRAY);
  62. DrawText(TextFormat("%02i FPS", framesSpeed), 575, 210, 10, DARKGRAY);
  63. DrawText("PRESS RIGHT/LEFT KEYS to CHANGE SPEED!", 290, 240, 10, DARKGRAY);
  64. for (int i = 0; i < MAX_FRAME_SPEED; i++)
  65. {
  66. if (i < framesSpeed) DrawRectangle(250 + 21*i, 205, 20, 20, RED);
  67. DrawRectangleLines(250 + 21*i, 205, 20, 20, MAROON);
  68. }
  69. DrawTextureRec(scarfy, frameRec, position, WHITE); // Draw part of the texture
  70. DrawText("(c) Scarfy sprite by Eiden Marsal", screenWidth - 200, screenHeight - 20, 10, GRAY);
  71. EndDrawing();
  72. //----------------------------------------------------------------------------------
  73. }
  74. // De-Initialization
  75. //--------------------------------------------------------------------------------------
  76. UnloadTexture(scarfy); // Texture unloading
  77. CloseWindow(); // Close window and OpenGL context
  78. //--------------------------------------------------------------------------------------
  79. return 0;
  80. }