audio_sound_multi.c 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*******************************************************************************************
  2. *
  3. * raylib [audio] example - sound multi
  4. *
  5. * Example complexity rating: [★★☆☆] 2/4
  6. *
  7. * Example originally created with raylib 5.0, last time updated with raylib 5.0
  8. *
  9. * Example contributed by Jeffery Myers (@JeffM2501) and reviewed by Ramon Santamaria (@raysan5)
  10. *
  11. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  12. * BSD-like license that allows static linking with closed source software
  13. *
  14. * Copyright (c) 2023-2025 Jeffery Myers (@JeffM2501)
  15. *
  16. ********************************************************************************************/
  17. #include "raylib.h"
  18. #define MAX_SOUNDS 10
  19. Sound soundArray[MAX_SOUNDS] = { 0 };
  20. int currentSound;
  21. //------------------------------------------------------------------------------------
  22. // Program main entry point
  23. //------------------------------------------------------------------------------------
  24. int main(void)
  25. {
  26. // Initialization
  27. //--------------------------------------------------------------------------------------
  28. const int screenWidth = 800;
  29. const int screenHeight = 450;
  30. InitWindow(screenWidth, screenHeight, "raylib [audio] example - sound multi");
  31. InitAudioDevice(); // Initialize audio device
  32. // Load audio file into the first slot as the 'source' sound,
  33. // this sound owns the sample data
  34. soundArray[0] = LoadSound("resources/sound.wav");
  35. // Load an alias of the sound into slots 1-9. These do not own the sound data, but can be played
  36. for (int i = 1; i < MAX_SOUNDS; i++) soundArray[i] = LoadSoundAlias(soundArray[0]);
  37. currentSound = 0; // Set the sound list to the start
  38. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  39. //--------------------------------------------------------------------------------------
  40. // Main game loop
  41. while (!WindowShouldClose()) // Detect window close button or ESC key
  42. {
  43. // Update
  44. //----------------------------------------------------------------------------------
  45. if (IsKeyPressed(KEY_SPACE))
  46. {
  47. PlaySound(soundArray[currentSound]); // Play the next open sound slot
  48. currentSound++; // Increment the sound slot
  49. // If the sound slot is out of bounds, go back to 0
  50. if (currentSound >= MAX_SOUNDS) currentSound = 0;
  51. // NOTE: Another approach would be to look at the list for the first sound
  52. // that is not playing and use that slot
  53. }
  54. //----------------------------------------------------------------------------------
  55. // Draw
  56. //----------------------------------------------------------------------------------
  57. BeginDrawing();
  58. ClearBackground(RAYWHITE);
  59. DrawText("Press SPACE to PLAY a WAV sound!", 200, 180, 20, LIGHTGRAY);
  60. EndDrawing();
  61. //----------------------------------------------------------------------------------
  62. }
  63. // De-Initialization
  64. //--------------------------------------------------------------------------------------
  65. for (int i = 1; i < MAX_SOUNDS; i++) UnloadSoundAlias(soundArray[i]); // Unload sound aliases
  66. UnloadSound(soundArray[0]); // Unload source sound data
  67. CloseAudioDevice(); // Close audio device
  68. CloseWindow(); // Close window and OpenGL context
  69. //--------------------------------------------------------------------------------------
  70. return 0;
  71. }