25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

124 satır
4.1 KiB

5 yıl önce
  1. /*******************************************************************************************
  2. *
  3. * raylib [textures] example - sprite explosion
  4. *
  5. * Example originally created with raylib 2.5, last time updated with raylib 3.5
  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) 2019-2024 Anata and Ramon Santamaria (@raysan5)
  11. *
  12. ********************************************************************************************/
  13. #include "raylib.h"
  14. #define NUM_FRAMES_PER_LINE 5
  15. #define NUM_LINES 5
  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 [textures] example - sprite explosion");
  26. InitAudioDevice();
  27. // Load explosion sound
  28. Sound fxBoom = LoadSound("resources/boom.wav");
  29. // Load explosion texture
  30. Texture2D explosion = LoadTexture("resources/explosion.png");
  31. // Init variables for animation
  32. float frameWidth = (float)(explosion.width/NUM_FRAMES_PER_LINE); // Sprite one frame rectangle width
  33. float frameHeight = (float)(explosion.height/NUM_LINES); // Sprite one frame rectangle height
  34. int currentFrame = 0;
  35. int currentLine = 0;
  36. Rectangle frameRec = { 0, 0, frameWidth, frameHeight };
  37. Vector2 position = { 0.0f, 0.0f };
  38. bool active = false;
  39. int framesCounter = 0;
  40. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  41. //---------------------------------------------------------------------------------------
  42. // Main game loop
  43. while (!WindowShouldClose()) // Detect window close button or ESC key
  44. {
  45. // Update
  46. //----------------------------------------------------------------------------------
  47. // Check for mouse button pressed and activate explosion (if not active)
  48. if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT) && !active)
  49. {
  50. position = GetMousePosition();
  51. active = true;
  52. position.x -= frameWidth/2.0f;
  53. position.y -= frameHeight/2.0f;
  54. PlaySound(fxBoom);
  55. }
  56. // Compute explosion animation frames
  57. if (active)
  58. {
  59. framesCounter++;
  60. if (framesCounter > 2)
  61. {
  62. currentFrame++;
  63. if (currentFrame >= NUM_FRAMES_PER_LINE)
  64. {
  65. currentFrame = 0;
  66. currentLine++;
  67. if (currentLine >= NUM_LINES)
  68. {
  69. currentLine = 0;
  70. active = false;
  71. }
  72. }
  73. framesCounter = 0;
  74. }
  75. }
  76. frameRec.x = frameWidth*currentFrame;
  77. frameRec.y = frameHeight*currentLine;
  78. //----------------------------------------------------------------------------------
  79. // Draw
  80. //----------------------------------------------------------------------------------
  81. BeginDrawing();
  82. ClearBackground(RAYWHITE);
  83. // Draw explosion required frame rectangle
  84. if (active) DrawTextureRec(explosion, frameRec, position, WHITE);
  85. EndDrawing();
  86. //----------------------------------------------------------------------------------
  87. }
  88. // De-Initialization
  89. //--------------------------------------------------------------------------------------
  90. UnloadTexture(explosion); // Unload texture
  91. UnloadSound(fxBoom); // Unload sound
  92. CloseAudioDevice();
  93. CloseWindow(); // Close window and OpenGL context
  94. //--------------------------------------------------------------------------------------
  95. return 0;
  96. }