Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

86 Zeilen
3.6 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [audio] example - Playing sound multiple times
  4. *
  5. * Example originally created with raylib 4.6
  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) 2023 Jeffery Myers (@JeffM2501)
  11. *
  12. ********************************************************************************************/
  13. #include "raylib.h"
  14. #define MAX_SOUNDS 10
  15. Sound soundArray[MAX_SOUNDS] = { 0 };
  16. int currentSound;
  17. //------------------------------------------------------------------------------------
  18. // Program main entry point
  19. //------------------------------------------------------------------------------------
  20. int main(void)
  21. {
  22. // Initialization
  23. //--------------------------------------------------------------------------------------
  24. const int screenWidth = 800;
  25. const int screenHeight = 450;
  26. InitWindow(screenWidth, screenHeight, "raylib [audio] example - playing sound multiple times");
  27. InitAudioDevice(); // Initialize audio device
  28. // load the sound list
  29. soundArray[0] = LoadSound("resources/sound.wav"); // Load WAV audio file into the first slot as the 'source' sound
  30. // this sound owns the sample data
  31. for (int i = 1; i < MAX_SOUNDS; i++)
  32. {
  33. soundArray[i] = LoadSoundAlias(soundArray[0]); // Load an alias of the sound into slots 1-9. These do not own the sound data, but can be played
  34. }
  35. currentSound = 0; // set the sound list to the start
  36. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  37. //--------------------------------------------------------------------------------------
  38. // Main game loop
  39. while (!WindowShouldClose()) // Detect window close button or ESC key
  40. {
  41. // Update
  42. //----------------------------------------------------------------------------------
  43. if (IsKeyPressed(KEY_SPACE))
  44. {
  45. PlaySound(soundArray[currentSound]); // play the next open sound slot
  46. currentSound++; // increment the sound slot
  47. if (currentSound >= MAX_SOUNDS) // if the sound slot is out of bounds, go back to 0
  48. currentSound = 0;
  49. // Note: a better way would be to look at the list for the first sound that is not playing and use that slot
  50. }
  51. //----------------------------------------------------------------------------------
  52. // Draw
  53. //----------------------------------------------------------------------------------
  54. BeginDrawing();
  55. ClearBackground(RAYWHITE);
  56. DrawText("Press SPACE to PLAY a WAV sound!", 200, 180, 20, LIGHTGRAY);
  57. EndDrawing();
  58. //----------------------------------------------------------------------------------
  59. }
  60. // De-Initialization
  61. //--------------------------------------------------------------------------------------
  62. for (int i = 1; i < MAX_SOUNDS; i++)
  63. UnloadSoundAlias(soundArray[i]); // Unload sound aliases
  64. UnloadSound(soundArray[0]); // Unload source sound data
  65. CloseAudioDevice(); // Close audio device
  66. CloseWindow(); // Close window and OpenGL context
  67. //--------------------------------------------------------------------------------------
  68. return 0;
  69. }