audio_module_playing.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. #if defined(PLATFORM_WEB)
  15. #include <emscripten/emscripten.h>
  16. #endif
  17. #define MAX_CIRCLES 32
  18. //----------------------------------------------------------------------------------
  19. // Types and Structures Definition
  20. //----------------------------------------------------------------------------------
  21. typedef struct {
  22. Vector2 position;
  23. float radius;
  24. float alpha;
  25. float speed;
  26. Color color;
  27. } CircleWave;
  28. //----------------------------------------------------------------------------------
  29. // Global Variables Definition
  30. //----------------------------------------------------------------------------------
  31. int screenWidth = 800;
  32. int screenHeight = 450;
  33. Color colors[14] = { ORANGE, RED, GOLD, LIME, BLUE, VIOLET, BROWN, LIGHTGRAY, PINK,
  34. YELLOW, GREEN, SKYBLUE, PURPLE, BEIGE };
  35. // Creates ome circles for visual effect
  36. CircleWave circles[MAX_CIRCLES];
  37. Music xm;
  38. float timePlayed = 0.0f;
  39. static bool pause = false;
  40. //----------------------------------------------------------------------------------
  41. // Module Functions Declaration
  42. //----------------------------------------------------------------------------------
  43. void UpdateDrawFrame(void); // Update and Draw one frame
  44. //----------------------------------------------------------------------------------
  45. // Main Enry Point
  46. //----------------------------------------------------------------------------------
  47. int main()
  48. {
  49. // Initialization
  50. //--------------------------------------------------------------------------------------
  51. InitWindow(screenWidth, screenHeight, "raylib [audio] example - module playing (streaming)");
  52. InitAudioDevice(); // Initialize audio device
  53. for (int i = MAX_CIRCLES - 1; i >= 0; i--)
  54. {
  55. circles[i].alpha = 0.0f;
  56. circles[i].radius = GetRandomValue(10, 40);
  57. circles[i].position.x = GetRandomValue(circles[i].radius, screenWidth - circles[i].radius);
  58. circles[i].position.y = GetRandomValue(circles[i].radius, screenHeight - circles[i].radius);
  59. circles[i].speed = (float)GetRandomValue(1, 100)/20000.0f;
  60. circles[i].color = colors[GetRandomValue(0, 13)];
  61. }
  62. xm = LoadMusicStream("resources/mini1111.xm");
  63. PlayMusicStream(xm); // Play module stream
  64. #if defined(PLATFORM_WEB)
  65. emscripten_set_main_loop(UpdateDrawFrame, 0, 1);
  66. #else
  67. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  68. //--------------------------------------------------------------------------------------
  69. // Main game loop
  70. while (!WindowShouldClose()) // Detect window close button or ESC key
  71. {
  72. UpdateDrawFrame();
  73. }
  74. #endif
  75. // De-Initialization
  76. //--------------------------------------------------------------------------------------
  77. UnloadMusicStream(xm); // Unload music stream buffers from RAM
  78. CloseAudioDevice(); // Close audio device (music streaming is automatically stopped)
  79. CloseWindow(); // Close window and OpenGL context
  80. //--------------------------------------------------------------------------------------
  81. return 0;
  82. }
  83. //----------------------------------------------------------------------------------
  84. // Module Functions Definition
  85. //----------------------------------------------------------------------------------
  86. void UpdateDrawFrame(void)
  87. {
  88. // Update
  89. //----------------------------------------------------------------------------------
  90. UpdateMusicStream(xm); // Update music buffer with new stream data
  91. // Restart music playing (stop and play)
  92. if (IsKeyPressed(KEY_SPACE))
  93. {
  94. StopMusicStream(xm);
  95. PlayMusicStream(xm);
  96. }
  97. // Pause/Resume music playing
  98. if (IsKeyPressed(KEY_P))
  99. {
  100. pause = !pause;
  101. if (pause) PauseMusicStream(xm);
  102. else ResumeMusicStream(xm);
  103. }
  104. // Get timePlayed scaled to bar dimensions
  105. timePlayed = GetMusicTimePlayed(xm)/GetMusicTimeLength(xm)*(screenWidth - 40);
  106. for (int i = MAX_CIRCLES - 1; i >= 0; i--)
  107. {
  108. circles[i].alpha += circles[i].speed;
  109. circles[i].radius += circles[i].speed*10.0f;
  110. if (circles[i].alpha > 1.0f) circles[i].speed *= -1;
  111. if (circles[i].alpha <= 0.0f)
  112. {
  113. circles[i].alpha = 0.0f;
  114. circles[i].radius = GetRandomValue(10, 40);
  115. circles[i].position.x = GetRandomValue(circles[i].radius, screenWidth - circles[i].radius);
  116. circles[i].position.y = GetRandomValue(circles[i].radius, screenHeight - circles[i].radius);
  117. circles[i].color = colors[GetRandomValue(0, 13)];
  118. circles[i].speed = (float)GetRandomValue(1, 100)/20000.0f;
  119. }
  120. }
  121. //----------------------------------------------------------------------------------
  122. // Draw
  123. //----------------------------------------------------------------------------------
  124. BeginDrawing();
  125. ClearBackground(RAYWHITE);
  126. for (int i = MAX_CIRCLES - 1; i >= 0; i--)
  127. {
  128. DrawCircleV(circles[i].position, circles[i].radius, Fade(circles[i].color, circles[i].alpha));
  129. }
  130. // Draw time bar
  131. DrawRectangle(20, screenHeight - 20 - 12, screenWidth - 40, 12, LIGHTGRAY);
  132. DrawRectangle(20, screenHeight - 20 - 12, (int)timePlayed, 12, MAROON);
  133. DrawRectangleLines(20, screenHeight - 20 - 12, screenWidth - 40, 12, GRAY);
  134. EndDrawing();
  135. //----------------------------------------------------------------------------------
  136. }