audio_module_playing.lua 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. -------------------------------------------------------------------------------------------
  2. --
  3. -- raylib [audio] example - Module playing (streaming)
  4. --
  5. -- This example has been created using raylib 1.6 (www.raylib.com)
  6. -- raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  7. --
  8. -- Copyright (c) 2014-2016 Ramon Santamaria (@raysan5)
  9. --
  10. -------------------------------------------------------------------------------------------
  11. MAX_CIRCLES = 64
  12. -- Initialization
  13. -------------------------------------------------------------------------------------------
  14. local screenWidth = 800
  15. local screenHeight = 450
  16. InitWindow(screenWidth, screenHeight, "raylib [audio] example - module playing (streaming)")
  17. InitAudioDevice() -- Initialize audio device
  18. local colors = { ORANGE, RED, GOLD, LIME, BLUE, VIOLET, BROWN, LIGHTGRAY, PINK,
  19. YELLOW, GREEN, SKYBLUE, PURPLE, BEIGE }
  20. -- Creates ome circles for visual effect
  21. local circles = {}
  22. for i = MAX_CIRCLES, 1, -1 do
  23. circles[i] = {}
  24. circles[i].alpha = 0.0
  25. circles[i].radius = GetRandomValue(10, 40)
  26. circles[i].position = Vector2(0, 0)
  27. circles[i].position.x = GetRandomValue(circles[i].radius, screenWidth - circles[i].radius)
  28. circles[i].position.y = GetRandomValue(circles[i].radius, screenHeight - circles[i].radius)
  29. circles[i].speed = GetRandomValue(1, 100)/20000.0
  30. circles[i].color = colors[GetRandomValue(1, 14)]
  31. end
  32. local xm = LoadMusicStream("resources/audio/mini1111.xm")
  33. PlayMusicStream(xm)
  34. local timePlayed = 0.0
  35. SetTargetFPS(60) -- Set our game to run at 60 frames-per-second
  36. -------------------------------------------------------------------------------------------
  37. -- Main game loop
  38. while not WindowShouldClose() do -- Detect window close button or ESC key
  39. -- Update
  40. ---------------------------------------------------------------------------------------
  41. for i = MAX_CIRCLES, 1, -1 do
  42. circles[i].alpha = circles[i].alpha + circles[i].speed
  43. circles[i].radius = circles[i].radius + circles[i].speed*10.0
  44. if (circles[i].alpha > 1.0) then circles[i].speed = circles[i].speed*-1 end
  45. if (circles[i].alpha <= 0.0) then
  46. circles[i].alpha = 0.0
  47. circles[i].radius = GetRandomValue(10, 40)
  48. circles[i].position.x = GetRandomValue(circles[i].radius, screenWidth - circles[i].radius)
  49. circles[i].position.y = GetRandomValue(circles[i].radius, screenHeight - circles[i].radius)
  50. circles[i].color = colors[GetRandomValue(1, 14)]
  51. circles[i].speed = GetRandomValue(1, 100)/20000.0
  52. end
  53. end
  54. -- Get timePlayed scaled to bar dimensions
  55. timePlayed = (GetMusicTimePlayed(xm)/GetMusicTimeLength(xm)*(screenWidth - 40))*2
  56. UpdateMusicStream(xm) -- Update music buffer with new stream data
  57. ---------------------------------------------------------------------------------------
  58. -- Draw
  59. ---------------------------------------------------------------------------------------
  60. BeginDrawing()
  61. ClearBackground(RAYWHITE)
  62. for i = MAX_CIRCLES, 1, -1 do
  63. DrawCircleV(circles[i].position, circles[i].radius, Fade(circles[i].color, circles[i].alpha))
  64. end
  65. -- Draw time bar
  66. DrawRectangle(20, screenHeight - 20 - 12, screenWidth - 40, 12, LIGHTGRAY)
  67. DrawRectangle(20, screenHeight - 20 - 12, timePlayed//1, 12, MAROON)
  68. DrawRectangleLines(20, screenHeight - 20 - 12, screenWidth - 40, 12, WHITE)
  69. EndDrawing()
  70. ---------------------------------------------------------------------------------------
  71. end
  72. -- De-Initialization
  73. -------------------------------------------------------------------------------------------
  74. UnloadMusicStream(xm) -- Unload music stream buffers from RAM
  75. CloseAudioDevice() -- Close audio device (music streaming is automatically stopped)
  76. CloseWindow() -- Close window and OpenGL context
  77. -------------------------------------------------------------------------------------------