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.

145 lines
5.3 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 = (float)GetRandomValue(10, 40);
  37. circles[i].position.x = (float)GetRandomValue((int)circles[i].radius, (int)(screenWidth - circles[i].radius));
  38. circles[i].position.y = (float)GetRandomValue((int)circles[i].radius, (int)(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. music.looping = false;
  44. float pitch = 1.0f;
  45. PlayMusicStream(music);
  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(music); // Update music buffer with new stream data
  56. // Restart music playing (stop and play)
  57. if (IsKeyPressed(KEY_SPACE))
  58. {
  59. StopMusicStream(music);
  60. PlayMusicStream(music);
  61. }
  62. // Pause/Resume music playing
  63. if (IsKeyPressed(KEY_P))
  64. {
  65. pause = !pause;
  66. if (pause) PauseMusicStream(music);
  67. else ResumeMusicStream(music);
  68. }
  69. if (IsKeyDown(KEY_DOWN)) pitch -= 0.01f;
  70. else if (IsKeyDown(KEY_UP)) pitch += 0.01f;
  71. SetMusicPitch(music, pitch);
  72. // Get timePlayed scaled to bar dimensions
  73. timePlayed = GetMusicTimePlayed(music)/GetMusicTimeLength(music)*(screenWidth - 40);
  74. // Color circles animation
  75. for (int i = MAX_CIRCLES - 1; (i >= 0) && !pause; i--)
  76. {
  77. circles[i].alpha += circles[i].speed;
  78. circles[i].radius += circles[i].speed*10.0f;
  79. if (circles[i].alpha > 1.0f) circles[i].speed *= -1;
  80. if (circles[i].alpha <= 0.0f)
  81. {
  82. circles[i].alpha = 0.0f;
  83. circles[i].radius = (float)GetRandomValue(10, 40);
  84. circles[i].position.x = (float)GetRandomValue((int)circles[i].radius, (int)(screenWidth - circles[i].radius));
  85. circles[i].position.y = (float)GetRandomValue((int)circles[i].radius, (int)(screenHeight - circles[i].radius));
  86. circles[i].color = colors[GetRandomValue(0, 13)];
  87. circles[i].speed = (float)GetRandomValue(1, 100)/2000.0f;
  88. }
  89. }
  90. //----------------------------------------------------------------------------------
  91. // Draw
  92. //----------------------------------------------------------------------------------
  93. BeginDrawing();
  94. ClearBackground(RAYWHITE);
  95. for (int i = MAX_CIRCLES - 1; i >= 0; i--)
  96. {
  97. DrawCircleV(circles[i].position, circles[i].radius, Fade(circles[i].color, circles[i].alpha));
  98. }
  99. // Draw time bar
  100. DrawRectangle(20, screenHeight - 20 - 12, screenWidth - 40, 12, LIGHTGRAY);
  101. DrawRectangle(20, screenHeight - 20 - 12, (int)timePlayed, 12, MAROON);
  102. DrawRectangleLines(20, screenHeight - 20 - 12, screenWidth - 40, 12, GRAY);
  103. EndDrawing();
  104. //----------------------------------------------------------------------------------
  105. }
  106. // De-Initialization
  107. //--------------------------------------------------------------------------------------
  108. UnloadMusicStream(music); // Unload music stream buffers from RAM
  109. CloseAudioDevice(); // Close audio device (music streaming is automatically stopped)
  110. CloseWindow(); // Close window and OpenGL context
  111. //--------------------------------------------------------------------------------------
  112. return 0;
  113. }