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.

141 lines
5.5 KiB

  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. Music xm = LoadMusicStream("resources/audio/mini1111.xm");
  49. PlayMusicStream(xm);
  50. float timePlayed = 0.0f;
  51. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  52. //--------------------------------------------------------------------------------------
  53. // Main game loop
  54. while (!WindowShouldClose()) // Detect window close button or ESC key
  55. {
  56. // Update
  57. //----------------------------------------------------------------------------------
  58. for (int i = MAX_CIRCLES - 1; i >= 0; i--)
  59. {
  60. circles[i].alpha += circles[i].speed;
  61. circles[i].radius += circles[i].speed*10.0f;
  62. if (circles[i].alpha > 1.0f) circles[i].speed *= -1;
  63. if (circles[i].alpha <= 0.0f)
  64. {
  65. circles[i].alpha = 0.0f;
  66. circles[i].radius = GetRandomValue(10, 40);
  67. circles[i].position.x = GetRandomValue(circles[i].radius, screenWidth - circles[i].radius);
  68. circles[i].position.y = GetRandomValue(circles[i].radius, screenHeight - circles[i].radius);
  69. circles[i].color = colors[GetRandomValue(0, 13)];
  70. circles[i].speed = (float)GetRandomValue(1, 100)/20000.0f;
  71. }
  72. }
  73. // Get timePlayed scaled to bar dimensions
  74. timePlayed = (GetMusicTimePlayed(xm)/GetMusicTimeLength(xm)*(screenWidth - 40))*2;
  75. UpdateMusicStream(xm); // Update music buffer with new stream data
  76. //----------------------------------------------------------------------------------
  77. // Draw
  78. //----------------------------------------------------------------------------------
  79. BeginDrawing();
  80. ClearBackground(BLACK);
  81. BeginTextureMode(target); // Enable drawing to texture
  82. for (int i = MAX_CIRCLES - 1; i >= 0; i--)
  83. {
  84. DrawCircleV(circles[i].position, circles[i].radius, Fade(circles[i].color, circles[i].alpha));
  85. }
  86. EndTextureMode(); // End drawing to texture (now we have a texture available for next passes)
  87. BeginShaderMode(shader);
  88. // NOTE: Render texture must be y-flipped due to default OpenGL coordinates (left-bottom)
  89. DrawTextureRec(target.texture, (Rectangle){ 0, 0, target.texture.width, -target.texture.height }, (Vector2){ 0, 0 }, WHITE);
  90. EndShaderMode();
  91. // Draw time bar
  92. DrawRectangle(20, screenHeight - 20 - 12, screenWidth - 40, 12, LIGHTGRAY);
  93. DrawRectangle(20, screenHeight - 20 - 12, (int)timePlayed, 12, MAROON);
  94. DrawRectangleLines(20, screenHeight - 20 - 12, screenWidth - 40, 12, WHITE);
  95. EndDrawing();
  96. //----------------------------------------------------------------------------------
  97. }
  98. // De-Initialization
  99. //--------------------------------------------------------------------------------------
  100. UnloadShader(shader); // Unload shader
  101. UnloadRenderTexture(target); // Unload render texture
  102. UnloadMusicStream(xm); // Unload music stream buffers from RAM
  103. CloseAudioDevice(); // Close audio device (music streaming is automatically stopped)
  104. CloseWindow(); // Close window and OpenGL context
  105. //--------------------------------------------------------------------------------------
  106. return 0;
  107. }