audio_sound_loading.c 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*******************************************************************************************
  2. *
  3. * raylib [audio] example - Sound loading and playing (adapted for HTML5 platform)
  4. *
  5. * NOTE: This example requires OpenAL Soft library installed
  6. *
  7. * This example has been created using raylib 1.3 (www.raylib.com)
  8. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  9. *
  10. * Copyright (c) 2015 Ramon Santamaria (@raysan5)
  11. *
  12. ********************************************************************************************/
  13. #include "raylib.h"
  14. #if defined(PLATFORM_WEB)
  15. #include <emscripten/emscripten.h>
  16. #endif
  17. //----------------------------------------------------------------------------------
  18. // Global Variables Definition
  19. //----------------------------------------------------------------------------------
  20. int screenWidth = 800;
  21. int screenHeight = 450;
  22. Sound fxWav;
  23. Sound fxOgg;
  24. //----------------------------------------------------------------------------------
  25. // Module Functions Declaration
  26. //----------------------------------------------------------------------------------
  27. void UpdateDrawFrame(void); // Update and Draw one frame
  28. //----------------------------------------------------------------------------------
  29. // Main Enry Point
  30. //----------------------------------------------------------------------------------
  31. int main()
  32. {
  33. // Initialization
  34. //--------------------------------------------------------------------------------------
  35. InitWindow(screenWidth, screenHeight, "raylib [audio] example - sound loading and playing");
  36. InitAudioDevice(); // Initialize audio device
  37. fxWav = LoadSound("resources/weird.wav"); // Load WAV audio file
  38. fxOgg = LoadSound("resources/tanatana.ogg"); // Load OGG audio file
  39. #if defined(PLATFORM_WEB)
  40. emscripten_set_main_loop(UpdateDrawFrame, 0, 1);
  41. #else
  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. UpdateDrawFrame();
  48. }
  49. #endif
  50. // De-Initialization
  51. //--------------------------------------------------------------------------------------
  52. UnloadSound(fxWav); // Unload sound data
  53. UnloadSound(fxOgg); // Unload sound data
  54. CloseAudioDevice(); // Close audio device
  55. CloseWindow(); // Close window and OpenGL context
  56. //--------------------------------------------------------------------------------------
  57. return 0;
  58. }
  59. //----------------------------------------------------------------------------------
  60. // Module Functions Definition
  61. //----------------------------------------------------------------------------------
  62. void UpdateDrawFrame(void)
  63. {
  64. // Update
  65. //----------------------------------------------------------------------------------
  66. if (IsKeyPressed(KEY_SPACE)) PlaySound(fxWav); // Play WAV sound
  67. if (IsKeyPressed(KEY_ENTER)) PlaySound(fxOgg); // Play OGG sound
  68. //----------------------------------------------------------------------------------
  69. // Draw
  70. //----------------------------------------------------------------------------------
  71. BeginDrawing();
  72. ClearBackground(RAYWHITE);
  73. DrawText("Press SPACE to PLAY the WAV sound!", 200, 180, 20, LIGHTGRAY);
  74. DrawText("Press ENTER to PLAY the OGG sound!", 200, 220, 20, LIGHTGRAY);
  75. EndDrawing();
  76. //----------------------------------------------------------------------------------
  77. }