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.

160 lines
6.0 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-2024 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. pause = false;
  67. }
  68. // Pause/Resume music playing
  69. if (IsKeyPressed(KEY_P))
  70. {
  71. pause = !pause;
  72. if (pause) PauseMusicStream(music);
  73. else ResumeMusicStream(music);
  74. }
  75. if (IsKeyDown(KEY_DOWN)) pitch -= 0.01f;
  76. else if (IsKeyDown(KEY_UP)) pitch += 0.01f;
  77. SetMusicPitch(music, pitch);
  78. // Get timePlayed scaled to bar dimensions
  79. timePlayed = GetMusicTimePlayed(music)/GetMusicTimeLength(music)*(screenWidth - 40);
  80. // Color circles animation
  81. for (int i = MAX_CIRCLES - 1; (i >= 0) && !pause; i--)
  82. {
  83. circles[i].alpha += circles[i].speed;
  84. circles[i].radius += circles[i].speed*10.0f;
  85. if (circles[i].alpha > 1.0f) circles[i].speed *= -1;
  86. if (circles[i].alpha <= 0.0f)
  87. {
  88. circles[i].alpha = 0.0f;
  89. circles[i].radius = (float)GetRandomValue(10, 40);
  90. circles[i].position.x = (float)GetRandomValue((int)circles[i].radius, (int)(screenWidth - circles[i].radius));
  91. circles[i].position.y = (float)GetRandomValue((int)circles[i].radius, (int)(screenHeight - circles[i].radius));
  92. circles[i].color = colors[GetRandomValue(0, 13)];
  93. circles[i].speed = (float)GetRandomValue(1, 100)/2000.0f;
  94. }
  95. }
  96. //----------------------------------------------------------------------------------
  97. // Draw
  98. //----------------------------------------------------------------------------------
  99. BeginDrawing();
  100. ClearBackground(RAYWHITE);
  101. for (int i = MAX_CIRCLES - 1; i >= 0; i--)
  102. {
  103. DrawCircleV(circles[i].position, circles[i].radius, Fade(circles[i].color, circles[i].alpha));
  104. }
  105. // Draw time bar
  106. DrawRectangle(20, screenHeight - 20 - 12, screenWidth - 40, 12, LIGHTGRAY);
  107. DrawRectangle(20, screenHeight - 20 - 12, (int)timePlayed, 12, MAROON);
  108. DrawRectangleLines(20, screenHeight - 20 - 12, screenWidth - 40, 12, GRAY);
  109. // Draw help instructions
  110. DrawRectangle(20, 20, 425, 145, WHITE);
  111. DrawRectangleLines(20, 20, 425, 145, GRAY);
  112. DrawText("PRESS SPACE TO RESTART MUSIC", 40, 40, 20, BLACK);
  113. DrawText("PRESS P TO PAUSE/RESUME", 40, 70, 20, BLACK);
  114. DrawText("PRESS UP/DOWN TO CHANGE SPEED", 40, 100, 20, BLACK);
  115. DrawText(TextFormat("SPEED: %f", pitch), 40, 130, 20, MAROON);
  116. EndDrawing();
  117. //----------------------------------------------------------------------------------
  118. }
  119. // De-Initialization
  120. //--------------------------------------------------------------------------------------
  121. UnloadMusicStream(music); // Unload music stream buffers from RAM
  122. CloseAudioDevice(); // Close audio device (music streaming is automatically stopped)
  123. CloseWindow(); // Close window and OpenGL context
  124. //--------------------------------------------------------------------------------------
  125. return 0;
  126. }