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