fordon_greeman / moosicpalyer

Branch: 277f22f
Switch branches/tags
main
theidioticdev fix raylib running under xwayland rather than uh being wayland native, styled the program 277f22f 12d ago
183 lines · 4.8 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
26 int
27 main (int argc, char **argv)
28 {
29 /* SetConfigFlags(FLAG_WINDOW_RESIZABLE); */
30
31 const int width = 1280;
32 const int height = 720;
33
34 InitWindow (width, height, "moosic palyer");
35
36 Image checked = GenImageChecked (width, height, 32, 32, BLUE, DARKBLUE);
37 Texture2D textures[NUM_TEXTURES] = { 0 };
38 textures[0] = LoadTextureFromImage (checked);
39 UnloadImage(checked);
40
41 SetTargetFPS (60);
42 InitAudioDevice ();
43 char font_path[512];
44 get_system_font (font_path, sizeof (font_path));
45
46 Font font = LoadFont (font_path);
47 SetTextureFilter(font.texture, TEXTURE_FILTER_BILINEAR);
48
49 if (font.texture.id == 0)
50 {
51 TraceLog (LOG_WARNING, "Failed to load font");
52 }
53
54 Music song;
55 char song_path[1024];
56 if (argc > 1)
57 {
58 strncpy (song_path, argv[1], sizeof (song_path) - 1);
59 song_path[sizeof (song_path) - 1] = 0;
60 song = LoadMusicStream (song_path);
61 PlayMusicStream (song);
62 }
63 else
64 {
65 FILE *f = popen ("zenity --file-selection", "r");
66 if (f != NULL)
67 {
68 if (fgets (song_path, sizeof (song_path), f) != NULL)
69 {
70 song_path[strcspn (song_path, "\n")] = 0;
71 song = LoadMusicStream (song_path);
72 PlayMusicStream (song);
73 }
74 pclose (f);
75 }
76 }
77
78 while (!WindowShouldClose ())
79 {
80 DrawTexture(textures[0], 0, 0, WHITE);
81
82 UpdateMusicStream (song);
83 float vol = GetMasterVolume ();
84 float total = GetMusicTimeLength (song);
85 float time_played = GetMusicTimePlayed (song);
86
87 if (IsKeyPressed (KEY_Q) == true)
88 {
89 break;
90 }
91
92 if (IsKeyPressed (KEY_P) == true && IsMusicStreamPlaying (song))
93 {
94 PauseMusicStream (song);
95 }
96
97 else if (IsKeyPressed (KEY_P) == true && !IsMusicStreamPlaying (song))
98 {
99 ResumeMusicStream (song);
100 }
101 else if (IsKeyPressed (KEY_EQUAL) == true)
102 {
103 SetMasterVolume (vol + 0.1f);
104 }
105 else if (IsKeyPressed (KEY_MINUS) == true)
106 {
107 SetMasterVolume (vol - 0.1f);
108 }
109
110 if (IsKeyPressed (KEY_RIGHT) || IsKeyPressedRepeat (KEY_RIGHT))
111 {
112 float t = time_played + 5.0f;
113 SeekMusicStream (song, t < total ? t : total - 0.1f);
114 }
115 if (IsKeyPressed (KEY_LEFT) || IsKeyPressedRepeat (KEY_LEFT))
116 {
117 float t = time_played - 5.0f;
118 SeekMusicStream (song, t > 0.0f ? t : 0.0f);
119 }
120
121 BeginDrawing ();
122 ClearBackground (BLACK);
123
124 char path_copy[1024];
125 strncpy (path_copy, song_path, sizeof (path_copy));
126 char *filename = basename (path_copy);
127
128 const char *line1 = !IsMusicStreamPlaying (song)
129 ? "paused :<" : TextFormat ("currently palying: %s :] ", filename);
130
131 const char *line2 = TextFormat ("Volume: %.2f", vol);
132 int tp = (int) time_played;
133 int tt = (int) total;
134 const char *line3 =
135 TextFormat ("%d:%02d / %d:%02d", tp / 60, tp % 60, tt / 60, tt % 60);
136
137
138 int font_size = 20;
139 float spacing = 1.0f;
140 float line_spacing = 45.0f;
141
142 Vector2 width1 =
143 MeasureTextEx (font, line1, (float) font_size, spacing);
144 Vector2 width2 =
145 MeasureTextEx (font, line2, (float) font_size, spacing);
146 Vector2 width3 =
147 MeasureTextEx (font, line3, (float) font_size, spacing);
148
149 int x1 = (width - (int) width1.x) / 2;
150 int x2 = (width - (int) width2.x) / 2;
151 int x3 = (width - (int) width3.x) / 2;
152 int start_y = (height - 60) / 2;
153
154 float padding_x = 16.0f;
155 float padding_y = 12.5f;
156
157 DrawRectangle((int)(x1 - padding_x), (int)(start_y - padding_y), (int)(width1.x + padding_x * 2), (int)(width1.y + padding_y * 2), Fade(BLACK, 0.9f));
158 DrawRectangle((int)(x1 - padding_x), (int)(start_y + line_spacing - padding_y), (int)(width1.x + padding_x * 2), (int)(width1.y + padding_y * 2), Fade(BLACK, 0.9f));
159 DrawRectangle((int)(x1 - padding_x), (int)(start_y + line_spacing * 2 - padding_y), (int)(width1.x + padding_x * 2), (int)(width1.y + padding_y * 2), Fade(BLACK, 0.9f));
160
161 DrawTextEx (font, line1, (Vector2)
162 {
163 (float) x1, (float) start_y}, (float) font_size, spacing,
164 RAYWHITE);
165 DrawTextEx (font, line2, (Vector2)
166 {
167 (float) x2, (float) start_y + line_spacing},
168 (float) font_size, spacing, RAYWHITE);
169 DrawTextEx (font, line3, (Vector2)
170 {
171 (float) x3, (float) start_y + line_spacing * 2},
172 (float) font_size, spacing, RAYWHITE);
173
174 EndDrawing ();
175 }
176
177 UnloadTexture (textures[0]);
178 UnloadMusicStream (song);
179 UnloadFont (font);
180 CloseAudioDevice ();
181 CloseWindow ();
182 return 0;
183 }