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.

150 lines
5.5 KiB

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