textures_rectangle.c 4.8 KB

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