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.

138 lines
5.0 KiB

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