fordon_greeman / moosicpalyer

Branch: main
Switch branches/tags
main
theidioticdev now you can build for x11 and build for wayland 279a9c7 8d ago
218 lines · 5.6 KB Raw Blame History
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <libgen.h>
5 #include "raylib.h"
6
7 #define NUM_TEXTURES 5
8
9 static void
10 get_system_font (char *dest, size_t max_len)
11 {
12 snprintf (dest, max_len, "resources/IBMPlexSans-Bold.ttf");
13
14 FILE *f = popen ("fc-match -f '%{file}' monospace", "r");
15 if (f != NULL)
16 {
17 if (fgets (dest, max_len, f) != NULL)
18 {
19 dest[strcspn (dest, "\r\n")] = 0;
20 }
21 pclose (f);
22 }
23 }
24
25 int
26 main (int argc, char **argv)
27 {
28 SetConfigFlags(FLAG_WINDOW_RESIZABLE);
29
30 const int width = 1280;
31 const int height = 720;
32
33 InitWindow (width, height, "moosic palyer");
34 /* default pat */
35 Image pat0 = GenImageChecked (width, height, 32, 32, BLUE, DARKBLUE);
36 /* linear gradrient pats */
37 Image pat1 = GenImageGradientLinear (width, height, 90, BLUE, DARKBLUE);
38 Image pat2 = GenImageGradientLinear (width, height, 135, RED, MAROON);
39 /* noise pats */
40 Image pat3 = GenImageWhiteNoise (width, height, 0.5f);
41 Image pat4 = GenImagePerlinNoise (width, height, 50, 50, 4.0f);
42
43 Texture2D textures[NUM_TEXTURES] = { 0 };
44
45 textures[0] = LoadTextureFromImage (pat0);
46 textures[1] = LoadTextureFromImage (pat1);
47 textures[2] = LoadTextureFromImage (pat2);
48 textures[3] = LoadTextureFromImage (pat3);
49 textures[4] = LoadTextureFromImage (pat4);
50
51 UnloadImage (pat0);
52 UnloadImage (pat1);
53 UnloadImage (pat2);
54 UnloadImage (pat3);
55 UnloadImage (pat4);
56
57 SetTargetFPS (60);
58 InitAudioDevice ();
59 char font_path[512];
60 get_system_font (font_path, sizeof (font_path));
61
62 Font font = LoadFont (font_path);
63 SetTextureFilter (font.texture, TEXTURE_FILTER_BILINEAR);
64
65 if (font.texture.id == 0)
66 {
67 TraceLog (LOG_WARNING, "Failed to load font");
68 }
69
70 Music song;
71 char song_path[1024];
72 if (argc > 1)
73 {
74 strncpy (song_path, argv[1], sizeof (song_path) - 1);
75 song_path[sizeof (song_path) - 1] = 0;
76 song = LoadMusicStream (song_path);
77 PlayMusicStream (song);
78 }
79 else
80 {
81 FILE *f = popen ("zenity --file-selection", "r");
82 if (f != NULL)
83 {
84 if (fgets (song_path, sizeof (song_path), f) != NULL)
85 {
86 song_path[strcspn (song_path, "\n")] = 0;
87 song = LoadMusicStream (song_path);
88 PlayMusicStream (song);
89 }
90 pclose (f);
91 }
92 }
93
94 int current = 0;
95 while (!WindowShouldClose ())
96 {
97 DrawTexture (textures[0], 0, 0, WHITE);
98
99 UpdateMusicStream (song);
100 float vol = GetMasterVolume ();
101 float total = GetMusicTimeLength (song);
102 float time_played = GetMusicTimePlayed (song);
103
104 if (IsKeyPressed (KEY_Q) == true)
105 {
106 break;
107 }
108
109 if (IsKeyPressed (KEY_TAB) == true)
110 {
111 current = (current + 1) % NUM_TEXTURES;
112 }
113 DrawTexture (textures[current], 0, 0, WHITE);
114
115 if (IsKeyPressed (KEY_P) == true && IsMusicStreamPlaying (song))
116 {
117 PauseMusicStream (song);
118 }
119
120 else if (IsKeyPressed (KEY_P) == true && !IsMusicStreamPlaying (song))
121 {
122 ResumeMusicStream (song);
123 }
124 else if (IsKeyPressed (KEY_EQUAL) == true)
125 {
126 SetMasterVolume (vol + 0.1f);
127 }
128 else if (IsKeyPressed (KEY_MINUS) == true)
129 {
130 SetMasterVolume (vol - 0.1f);
131 }
132
133 if (IsKeyPressed (KEY_RIGHT) || IsKeyPressedRepeat (KEY_RIGHT))
134 {
135 float t = time_played + 5.0f;
136 SeekMusicStream (song, t < total ? t : total - 0.1f);
137 }
138 if (IsKeyPressed (KEY_LEFT) || IsKeyPressedRepeat (KEY_LEFT))
139 {
140 float t = time_played - 5.0f;
141 SeekMusicStream (song, t > 0.0f ? t : 0.0f);
142 }
143
144 BeginDrawing ();
145 ClearBackground (BLACK);
146
147 char path_copy[1024];
148 strncpy (path_copy, song_path, sizeof (path_copy));
149 char *filename = basename (path_copy);
150
151 const char *line1 = !IsMusicStreamPlaying (song)
152 ? "puased :<" : TextFormat ("currently palying: %s :] ", filename);
153
154 const char *line2 = TextFormat ("Volume: %.2f", vol);
155 int tp = (int) time_played;
156 int tt = (int) total;
157 const char *line3 =
158 TextFormat ("%d:%02d / %d:%02d", tp / 60, tp % 60, tt / 60, tt % 60);
159 const char *line4 =
160 "TAB: change theme / =: incr vol /-: decr vol / p: pause/start mus / q: exit";
161
162 int font_size = 20;
163 float spacing = 1.0f;
164 float line_spacing = 45.0f;
165
166 Vector2 width1 =
167 MeasureTextEx (font, line1, (float) font_size, spacing);
168 Vector2 width2 =
169 MeasureTextEx (font, line2, (float) font_size, spacing);
170 Vector2 width3 =
171 MeasureTextEx (font, line3, (float) font_size, spacing);
172 Vector2 width4 =
173 MeasureTextEx (font, line4, (float) font_size, spacing);
174
175 float padding_x = 16.0f;
176 float padding_y = 12.5f;
177
178 float box_padding_x = 16.0f;
179 float box_padding_y = 12.0f;
180
181 float box_width = width4.x + box_padding_x * 2;
182 float box_height = line_spacing * 3 + width4.y + box_padding_y * 2;
183 int box_x = (width - (int) box_width) / 2;
184 int box_y = (height - (int) box_height) / 2;
185
186 DrawRectangle (box_x, box_y, box_width, box_height, Fade (BLACK, 0.9f));
187
188 DrawTextEx (font, line1, (Vector2)
189 {
190 (width - width1.x) / 2, box_y + box_padding_y},
191 font_size, spacing, RAYWHITE);
192 DrawTextEx (font, line2, (Vector2)
193 {
194 (width - width2.x) / 2,
195 box_y + box_padding_y + line_spacing}, font_size, spacing,
196 RAYWHITE);
197 DrawTextEx (font, line3, (Vector2)
198 {
199 (width - width3.x) / 2,
200 box_y + box_padding_y + line_spacing * 2}, font_size,
201 spacing, RAYWHITE);
202 DrawTextEx (font, line4, (Vector2)
203 {
204 (width - width4.x) / 2,
205 box_y + box_padding_y + line_spacing * 3}, font_size,
206
207 spacing, RAYWHITE);
208
209 EndDrawing ();
210 }
211
212 UnloadTexture (textures[0]);
213 UnloadMusicStream (song);
214 UnloadFont (font);
215 CloseAudioDevice ();
216 CloseWindow ();
217 return 0;
218 }