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.

140 lines
5.1 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [audio] example - Module playing (streaming)
  4. *
  5. * NOTE: This example requires OpenAL Soft library installed
  6. *
  7. * This example has been created using raylib 1.5 (www.raylib.com)
  8. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  9. *
  10. * Copyright (c) 2016 Ramon Santamaria (@raysan5)
  11. *
  12. ********************************************************************************************/
  13. #include "raylib.h"
  14. #define MAX_CIRCLES 64
  15. typedef struct {
  16. Vector2 position;
  17. float radius;
  18. float alpha;
  19. float speed;
  20. Color color;
  21. } CircleWave;
  22. int main()
  23. {
  24. // Initialization
  25. //--------------------------------------------------------------------------------------
  26. int screenWidth = 800;
  27. int screenHeight = 450;
  28. SetConfigFlags(FLAG_MSAA_4X_HINT); // NOTE: Try to enable MSAA 4X
  29. InitWindow(screenWidth, screenHeight, "raylib [audio] example - module playing (streaming)");
  30. InitAudioDevice(); // Initialize audio device
  31. Color colors[14] = { ORANGE, RED, GOLD, LIME, BLUE, VIOLET, BROWN, LIGHTGRAY, PINK,
  32. YELLOW, GREEN, SKYBLUE, PURPLE, BEIGE };
  33. // Creates ome circles for visual effect
  34. CircleWave circles[MAX_CIRCLES];
  35. for (int i = MAX_CIRCLES - 1; i >= 0; i--)
  36. {
  37. circles[i].alpha = 0.0f;
  38. circles[i].radius = GetRandomValue(10, 40);
  39. circles[i].position.x = GetRandomValue(circles[i].radius, screenWidth - circles[i].radius);
  40. circles[i].position.y = GetRandomValue(circles[i].radius, screenHeight - circles[i].radius);
  41. circles[i].speed = (float)GetRandomValue(1, 100)/20000.0f;
  42. circles[i].color = colors[GetRandomValue(0, 13)];
  43. }
  44. Music xm = LoadMusicStream("resources/mini1111.xm");
  45. PlayMusicStream(xm);
  46. float timePlayed = 0.0f;
  47. bool pause = false;
  48. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  49. //--------------------------------------------------------------------------------------
  50. // Main game loop
  51. while (!WindowShouldClose()) // Detect window close button or ESC key
  52. {
  53. // Update
  54. //----------------------------------------------------------------------------------
  55. UpdateMusicStream(xm); // Update music buffer with new stream data
  56. // Restart music playing (stop and play)
  57. if (IsKeyPressed(KEY_SPACE))
  58. {
  59. StopMusicStream(xm);
  60. PlayMusicStream(xm);
  61. }
  62. // Pause/Resume music playing
  63. if (IsKeyPressed(KEY_P))
  64. {
  65. pause = !pause;
  66. if (pause) PauseMusicStream(xm);
  67. else ResumeMusicStream(xm);
  68. }
  69. // Get timePlayed scaled to bar dimensions
  70. timePlayed = GetMusicTimePlayed(xm)/GetMusicTimeLength(xm)*(screenWidth - 40);
  71. // Color circles animation
  72. for (int i = MAX_CIRCLES - 1; (i >= 0) && !pause; i--)
  73. {
  74. circles[i].alpha += circles[i].speed;
  75. circles[i].radius += circles[i].speed*10.0f;
  76. if (circles[i].alpha > 1.0f) circles[i].speed *= -1;
  77. if (circles[i].alpha <= 0.0f)
  78. {
  79. circles[i].alpha = 0.0f;
  80. circles[i].radius = GetRandomValue(10, 40);
  81. circles[i].position.x = GetRandomValue(circles[i].radius, screenWidth - circles[i].radius);
  82. circles[i].position.y = GetRandomValue(circles[i].radius, screenHeight - circles[i].radius);
  83. circles[i].color = colors[GetRandomValue(0, 13)];
  84. circles[i].speed = (float)GetRandomValue(1, 100)/20000.0f;
  85. }
  86. }
  87. //----------------------------------------------------------------------------------
  88. // Draw
  89. //----------------------------------------------------------------------------------
  90. BeginDrawing();
  91. ClearBackground(RAYWHITE);
  92. for (int i = MAX_CIRCLES - 1; i >= 0; i--)
  93. {
  94. DrawCircleV(circles[i].position, circles[i].radius, Fade(circles[i].color, circles[i].alpha));
  95. }
  96. // Draw time bar
  97. DrawRectangle(20, screenHeight - 20 - 12, screenWidth - 40, 12, LIGHTGRAY);
  98. DrawRectangle(20, screenHeight - 20 - 12, (int)timePlayed, 12, MAROON);
  99. DrawRectangleLines(20, screenHeight - 20 - 12, screenWidth - 40, 12, GRAY);
  100. EndDrawing();
  101. //----------------------------------------------------------------------------------
  102. }
  103. // De-Initialization
  104. //--------------------------------------------------------------------------------------
  105. UnloadMusicStream(xm); // Unload music stream buffers from RAM
  106. CloseAudioDevice(); // Close audio device (music streaming is automatically stopped)
  107. CloseWindow(); // Close window and OpenGL context
  108. //--------------------------------------------------------------------------------------
  109. return 0;
  110. }