audio_module_playing.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*******************************************************************************************
  2. *
  3. * raylib [audio] example - Module playing (streaming)
  4. *
  5. * NOTE: This example requires OpenAL Soft library installed
  6. *
  7. * This example has been created using raylib 1.5 (www.raylib.com)
  8. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  9. *
  10. * Copyright (c) 2016 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. int main()
  23. {
  24. // Initialization
  25. //--------------------------------------------------------------------------------------
  26. int screenWidth = 800;
  27. int screenHeight = 450;
  28. InitWindow(screenWidth, screenHeight, "raylib [audio] example - module playing (streaming)");
  29. InitAudioDevice(); // Initialize audio device
  30. Color colors[14] = { ORANGE, RED, GOLD, LIME, BLUE, VIOLET, BROWN, LIGHTGRAY, PINK,
  31. YELLOW, GREEN, SKYBLUE, PURPLE, BEIGE };
  32. // Creates ome circles for visual effect
  33. CircleWave circles[MAX_CIRCLES];
  34. for (int i = MAX_CIRCLES - 1; i >= 0; i--)
  35. {
  36. circles[i].alpha = 0.0f;
  37. circles[i].radius = GetRandomValue(10, 40);
  38. circles[i].position.x = GetRandomValue(circles[i].radius, screenWidth - circles[i].radius);
  39. circles[i].position.y = GetRandomValue(circles[i].radius, screenHeight - circles[i].radius);
  40. circles[i].speed = (float)GetRandomValue(1, 100)/20000.0f;
  41. circles[i].color = colors[GetRandomValue(0, 13)];
  42. }
  43. // Load postprocessing bloom shader
  44. Shader shader = LoadShader("resources/shaders/glsl330/base.vs",
  45. "resources/shaders/glsl330/bloom.fs");
  46. // Create a RenderTexture2D to be used for render to texture
  47. RenderTexture2D target = LoadRenderTexture(screenWidth, screenHeight);
  48. PlayMusicStream(0, "resources/audio/2t2m_spa.xm"); // Play module stream
  49. float timePlayed = 0.0f;
  50. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  51. //--------------------------------------------------------------------------------------
  52. // Main game loop
  53. while (!WindowShouldClose()) // Detect window close button or ESC key
  54. {
  55. // Update
  56. //----------------------------------------------------------------------------------
  57. for (int i = MAX_CIRCLES - 1; i >= 0; i--)
  58. {
  59. circles[i].alpha += circles[i].speed;
  60. circles[i].radius += circles[i].speed*10.0f;
  61. if (circles[i].alpha > 1.0f) circles[i].speed *= -1;
  62. if (circles[i].alpha <= 0.0f)
  63. {
  64. circles[i].alpha = 0.0f;
  65. circles[i].radius = GetRandomValue(10, 40);
  66. circles[i].position.x = GetRandomValue(circles[i].radius, screenWidth - circles[i].radius);
  67. circles[i].position.y = GetRandomValue(circles[i].radius, screenHeight - circles[i].radius);
  68. circles[i].color = colors[GetRandomValue(0, 13)];
  69. circles[i].speed = (float)GetRandomValue(1, 100)/20000.0f;
  70. }
  71. }
  72. // Get timePlayed scaled to bar dimensions
  73. timePlayed = (GetMusicTimePlayed(0)/GetMusicTimeLength(0)*(screenWidth - 40))*2;
  74. UpdateMusicStream(0); // Update music buffer with new stream data
  75. //----------------------------------------------------------------------------------
  76. // Draw
  77. //----------------------------------------------------------------------------------
  78. BeginDrawing();
  79. ClearBackground(BLACK);
  80. BeginTextureMode(target); // Enable drawing to texture
  81. for (int i = MAX_CIRCLES - 1; i >= 0; i--)
  82. {
  83. DrawCircleV(circles[i].position, circles[i].radius, Fade(circles[i].color, circles[i].alpha));
  84. }
  85. EndTextureMode(); // End drawing to texture (now we have a texture available for next passes)
  86. BeginShaderMode(shader);
  87. // NOTE: Render texture must be y-flipped due to default OpenGL coordinates (left-bottom)
  88. DrawTextureRec(target.texture, (Rectangle){ 0, 0, target.texture.width, -target.texture.height }, (Vector2){ 0, 0 }, WHITE);
  89. EndShaderMode();
  90. // Draw time bar
  91. DrawRectangle(20, screenHeight - 20 - 12, screenWidth - 40, 12, LIGHTGRAY);
  92. DrawRectangle(20, screenHeight - 20 - 12, (int)timePlayed, 12, MAROON);
  93. DrawRectangleLines(20, screenHeight - 20 - 12, screenWidth - 40, 12, WHITE);
  94. EndDrawing();
  95. //----------------------------------------------------------------------------------
  96. }
  97. // De-Initialization
  98. //--------------------------------------------------------------------------------------
  99. UnloadShader(shader); // Unload shader
  100. UnloadRenderTexture(target); // Unload render texture
  101. CloseAudioDevice(); // Close audio device (music streaming is automatically stopped)
  102. CloseWindow(); // Close window and OpenGL context
  103. //--------------------------------------------------------------------------------------
  104. return 0;
  105. }