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.

119 lines
3.7 KiB

5 years ago
  1. /*******************************************************************************************
  2. *
  3. * raylib [textures] example - sprite explosion
  4. *
  5. * This example has been created using raylib 2.5 (www.raylib.com)
  6. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  7. *
  8. * Copyright (c) 2019 Anata and Ramon Santamaria (@raysan5)
  9. *
  10. ********************************************************************************************/
  11. #include "raylib.h"
  12. #define NUM_FRAMES_PER_LINE 5
  13. #define NUM_LINES 5
  14. int main(void)
  15. {
  16. // Initialization
  17. //--------------------------------------------------------------------------------------
  18. const int screenWidth = 800;
  19. const int screenHeight = 450;
  20. InitWindow(screenWidth, screenHeight, "raylib [textures] example - sprite explosion");
  21. InitAudioDevice();
  22. // Load explosion sound
  23. Sound fxBoom = LoadSound("resources/boom.wav");
  24. // Load explosion texture
  25. Texture2D explosion = LoadTexture("resources/explosion.png");
  26. // Init variables for animation
  27. int frameWidth = explosion.width/NUM_FRAMES_PER_LINE; // Sprite one frame rectangle width
  28. int frameHeight = explosion.height/NUM_LINES; // Sprite one frame rectangle height
  29. int currentFrame = 0;
  30. int currentLine = 0;
  31. Rectangle frameRec = { 0, 0, frameWidth, frameHeight };
  32. Vector2 position = { 0.0f, 0.0f };
  33. bool active = false;
  34. int framesCounter = 0;
  35. SetTargetFPS(120);
  36. //--------------------------------------------------------------------------------------
  37. // Main game loop
  38. while (!WindowShouldClose()) // Detect window close button or ESC key
  39. {
  40. // Update
  41. //----------------------------------------------------------------------------------
  42. // Check for mouse button pressed and activate explosion (if not active)
  43. if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON) && !active)
  44. {
  45. position = GetMousePosition();
  46. active = true;
  47. position.x -= frameWidth/2.0f;
  48. position.y -= frameHeight/2.0f;
  49. PlaySound(fxBoom);
  50. }
  51. // Compute explosion animation frames
  52. if (active)
  53. {
  54. framesCounter++;
  55. if (framesCounter > 2)
  56. {
  57. currentFrame++;
  58. if (currentFrame >= NUM_FRAMES_PER_LINE)
  59. {
  60. currentFrame = 0;
  61. currentLine++;
  62. if (currentLine >= NUM_LINES)
  63. {
  64. currentLine = 0;
  65. active = false;
  66. }
  67. }
  68. framesCounter = 0;
  69. }
  70. }
  71. frameRec.x = (float)frameWidth*currentFrame;
  72. frameRec.y = (float)frameHeight*currentLine;
  73. //----------------------------------------------------------------------------------
  74. // Draw
  75. //----------------------------------------------------------------------------------
  76. BeginDrawing();
  77. ClearBackground(RAYWHITE);
  78. // Draw explosion required frame rectangle
  79. if (active) DrawTextureRec(explosion, frameRec, position, WHITE);
  80. EndDrawing();
  81. //----------------------------------------------------------------------------------
  82. }
  83. // De-Initialization
  84. //--------------------------------------------------------------------------------------
  85. UnloadTexture(explosion); // Unload texture
  86. UnloadSound(fxBoom); // Unload sound
  87. CloseAudioDevice();
  88. CloseWindow(); // Close window and OpenGL context
  89. //--------------------------------------------------------------------------------------
  90. return 0;
  91. }