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.

98 lines
3.8 KiB

преди 11 години
преди 10 години
преди 11 години
преди 9 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 10 години
преди 11 години
преди 10 години
преди 10 години
преди 11 години
преди 10 години
преди 11 години
преди 11 години
преди 10 години
преди 11 години
преди 10 години
преди 10 години
преди 11 години
преди 11 години
преди 10 години
преди 11 години
преди 10 години
преди 11 години
  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. #include "raylib.h"
  12. #define MAX_FRAME_SPEED 15
  13. #define MIN_FRAME_SPEED 1
  14. int main()
  15. {
  16. // Initialization
  17. //--------------------------------------------------------------------------------------
  18. int screenWidth = 800;
  19. int screenHeight = 450;
  20. InitWindow(screenWidth, screenHeight, "raylib [texture] example - texture rectangle");
  21. // NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
  22. Texture2D scarfy = LoadTexture("resources/scarfy.png"); // Texture loading
  23. Vector2 position = { 350.0f, 280.0f };
  24. Rectangle frameRec = { 0, 0, scarfy.width/6, scarfy.height };
  25. int currentFrame = 0;
  26. int framesCounter = 0;
  27. int framesSpeed = 8; // Number of spritesheet frames shown by second
  28. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  29. //--------------------------------------------------------------------------------------
  30. // Main game loop
  31. while (!WindowShouldClose()) // Detect window close button or ESC key
  32. {
  33. // Update
  34. //----------------------------------------------------------------------------------
  35. framesCounter++;
  36. if (framesCounter >= (60/framesSpeed))
  37. {
  38. framesCounter = 0;
  39. currentFrame++;
  40. if (currentFrame > 5) currentFrame = 0;
  41. frameRec.x = currentFrame*scarfy.width/6;
  42. }
  43. if (IsKeyPressed(KEY_RIGHT)) framesSpeed++;
  44. else if (IsKeyPressed(KEY_LEFT)) framesSpeed--;
  45. if (framesSpeed > MAX_FRAME_SPEED) framesSpeed = MAX_FRAME_SPEED;
  46. else if (framesSpeed < MIN_FRAME_SPEED) framesSpeed = MIN_FRAME_SPEED;
  47. //----------------------------------------------------------------------------------
  48. // Draw
  49. //----------------------------------------------------------------------------------
  50. BeginDrawing();
  51. ClearBackground(RAYWHITE);
  52. DrawTexture(scarfy, 15, 40, WHITE);
  53. DrawRectangleLines(15, 40, scarfy.width, scarfy.height, LIME);
  54. DrawRectangleLines(15 + frameRec.x, 40 + frameRec.y, frameRec.width, frameRec.height, RED);
  55. DrawText("FRAME SPEED: ", 165, 210, 10, DARKGRAY);
  56. DrawText(FormatText("%02i FPS", framesSpeed), 575, 210, 10, DARKGRAY);
  57. DrawText("PRESS RIGHT/LEFT KEYS to CHANGE SPEED!", 290, 240, 10, DARKGRAY);
  58. for (int i = 0; i < MAX_FRAME_SPEED; i++)
  59. {
  60. if (i < framesSpeed) DrawRectangle(250 + 21*i, 205, 20, 20, RED);
  61. DrawRectangleLines(250 + 21*i, 205, 20, 20, MAROON);
  62. }
  63. DrawTextureRec(scarfy, frameRec, position, WHITE); // Draw part of the texture
  64. DrawText("(c) Scarfy sprite by Eiden Marsal", screenWidth - 200, screenHeight - 20, 10, GRAY);
  65. EndDrawing();
  66. //----------------------------------------------------------------------------------
  67. }
  68. // De-Initialization
  69. //--------------------------------------------------------------------------------------
  70. UnloadTexture(scarfy); // Texture unloading
  71. CloseWindow(); // Close window and OpenGL context
  72. //--------------------------------------------------------------------------------------
  73. return 0;
  74. }