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