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.

92 lines
3.3 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [audio] example - Music playing (streaming)
  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. int main()
  15. {
  16. // Initialization
  17. //--------------------------------------------------------------------------------------
  18. int screenWidth = 800;
  19. int screenHeight = 450;
  20. InitWindow(screenWidth, screenHeight, "raylib [audio] example - music playing (streaming)");
  21. InitAudioDevice(); // Initialize audio device
  22. Music music = LoadMusicStream("resources/guitar_noodling.ogg");
  23. PlayMusicStream(music);
  24. float timePlayed = 0.0f;
  25. bool pause = false;
  26. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  27. //--------------------------------------------------------------------------------------
  28. // Main game loop
  29. while (!WindowShouldClose()) // Detect window close button or ESC key
  30. {
  31. // Update
  32. //----------------------------------------------------------------------------------
  33. UpdateMusicStream(music); // Update music buffer with new stream data
  34. // Restart music playing (stop and play)
  35. if (IsKeyPressed(KEY_SPACE))
  36. {
  37. StopMusicStream(music);
  38. PlayMusicStream(music);
  39. }
  40. // Pause/Resume music playing
  41. if (IsKeyPressed(KEY_P))
  42. {
  43. pause = !pause;
  44. if (pause) PauseMusicStream(music);
  45. else ResumeMusicStream(music);
  46. }
  47. // Get timePlayed scaled to bar dimensions (400 pixels)
  48. timePlayed = GetMusicTimePlayed(music)/GetMusicTimeLength(music)*400;
  49. //----------------------------------------------------------------------------------
  50. // Draw
  51. //----------------------------------------------------------------------------------
  52. BeginDrawing();
  53. ClearBackground(RAYWHITE);
  54. DrawText("MUSIC SHOULD BE PLAYING!", 255, 150, 20, LIGHTGRAY);
  55. DrawRectangle(200, 200, 400, 12, LIGHTGRAY);
  56. DrawRectangle(200, 200, (int)timePlayed, 12, MAROON);
  57. DrawRectangleLines(200, 200, 400, 12, GRAY);
  58. DrawText("PRESS SPACE TO RESTART MUSIC", 215, 250, 20, LIGHTGRAY);
  59. DrawText("PRESS P TO PAUSE/RESUME MUSIC", 208, 280, 20, LIGHTGRAY);
  60. EndDrawing();
  61. //----------------------------------------------------------------------------------
  62. }
  63. // De-Initialization
  64. //--------------------------------------------------------------------------------------
  65. UnloadMusicStream(music); // Unload music stream buffers from RAM
  66. CloseAudioDevice(); // Close audio device (music streaming is automatically stopped)
  67. CloseWindow(); // Close window and OpenGL context
  68. //--------------------------------------------------------------------------------------
  69. return 0;
  70. }