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.

139 lines
5.0 KiB

пре 8 година
пре 5 година
пре 8 година
пре 5 година
пре 8 година
пре 5 година
пре 8 година
пре 5 година
пре 8 година
пре 5 година
пре 8 година
пре 5 година
пре 8 година
пре 8 година
пре 5 година
пре 8 година
пре 8 година
пре 5 година
пре 5 година
пре 5 година
пре 5 година
пре 5 година
пре 5 година
пре 8 година
пре 5 година
пре 8 година
пре 5 година
пре 8 година
пре 8 година
пре 8 година
пре 5 година
пре 8 година
пре 5 година
пре 8 година
пре 8 година
пре 8 година
пре 5 година
пре 8 година
  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. music.looping = false;
  44. PlayMusicStream(music);
  45. float timePlayed = 0.0f;
  46. bool pause = false;
  47. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  48. //--------------------------------------------------------------------------------------
  49. // Main game loop
  50. while (!WindowShouldClose()) // Detect window close button or ESC key
  51. {
  52. // Update
  53. //----------------------------------------------------------------------------------
  54. UpdateMusicStream(music); // Update music buffer with new stream data
  55. // Restart music playing (stop and play)
  56. if (IsKeyPressed(KEY_SPACE))
  57. {
  58. StopMusicStream(music);
  59. PlayMusicStream(music);
  60. }
  61. // Pause/Resume music playing
  62. if (IsKeyPressed(KEY_P))
  63. {
  64. pause = !pause;
  65. if (pause) PauseMusicStream(music);
  66. else ResumeMusicStream(music);
  67. }
  68. // Get timePlayed scaled to bar dimensions
  69. timePlayed = GetMusicTimePlayed(music)/GetMusicTimeLength(music)*(screenWidth - 40);
  70. // Color circles animation
  71. for (int i = MAX_CIRCLES - 1; (i >= 0) && !pause; i--)
  72. {
  73. circles[i].alpha += circles[i].speed;
  74. circles[i].radius += circles[i].speed*10.0f;
  75. if (circles[i].alpha > 1.0f) circles[i].speed *= -1;
  76. if (circles[i].alpha <= 0.0f)
  77. {
  78. circles[i].alpha = 0.0f;
  79. circles[i].radius = GetRandomValue(10, 40);
  80. circles[i].position.x = GetRandomValue(circles[i].radius, screenWidth - circles[i].radius);
  81. circles[i].position.y = GetRandomValue(circles[i].radius, screenHeight - circles[i].radius);
  82. circles[i].color = colors[GetRandomValue(0, 13)];
  83. circles[i].speed = (float)GetRandomValue(1, 100)/2000.0f;
  84. }
  85. }
  86. //----------------------------------------------------------------------------------
  87. // Draw
  88. //----------------------------------------------------------------------------------
  89. BeginDrawing();
  90. ClearBackground(RAYWHITE);
  91. for (int i = MAX_CIRCLES - 1; i >= 0; i--)
  92. {
  93. DrawCircleV(circles[i].position, circles[i].radius, Fade(circles[i].color, circles[i].alpha));
  94. }
  95. // Draw time bar
  96. DrawRectangle(20, screenHeight - 20 - 12, screenWidth - 40, 12, LIGHTGRAY);
  97. DrawRectangle(20, screenHeight - 20 - 12, (int)timePlayed, 12, MAROON);
  98. DrawRectangleLines(20, screenHeight - 20 - 12, screenWidth - 40, 12, GRAY);
  99. EndDrawing();
  100. //----------------------------------------------------------------------------------
  101. }
  102. // De-Initialization
  103. //--------------------------------------------------------------------------------------
  104. UnloadMusicStream(music); // Unload music stream buffers from RAM
  105. CloseAudioDevice(); // Close audio device (music streaming is automatically stopped)
  106. CloseWindow(); // Close window and OpenGL context
  107. //--------------------------------------------------------------------------------------
  108. return 0;
  109. }