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.

126 regels
4.1 KiB

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