fordon_greeman / moosicpalyer

Branch: e6fd8b4
Switch branches/tags
main
theidioticdev default to the system's monospace font e6fd8b4 13d ago
167 lines · 3.9 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 static void
8 get_system_font (char *dest, size_t max_len)
9 {
10 snprintf( dest, max_len, "resources/IBMPlexSans-Bold.ttf");
11
12 FILE *f = popen("fc-match -f '%{file}' monospace", "r");
13 if (f != NULL)
14 {
15 if (fgets (dest, max_len, f) != NULL)
16 {
17 dest[strcspn (dest, "\r\n")] = 0;
18 }
19 pclose (f);
20 }
21 }
22
23
24 int
25 main (int argc, char **argv)
26 {
27
28 const int width = 1280;
29 const int height = 720;
30
31
32 InitWindow (width, height, "moosic palyer");
33 SetTargetFPS (60);
34 InitAudioDevice ();
35 char font_path[512];
36 get_system_font (font_path, sizeof (font_path));
37
38 Font font = LoadFont (font_path);
39
40 if (font.texture.id == 0)
41 {
42 TraceLog (LOG_WARNING, "Failed to load font");
43 }
44
45 Music song;
46 char song_path[1024];
47 if (argc > 1)
48 {
49 strncpy (song_path, argv[1], sizeof (song_path) - 1);
50 song_path[sizeof (song_path) - 1] = 0;
51 song = LoadMusicStream (song_path);
52 PlayMusicStream (song);
53 }
54 else
55 {
56
57 FILE *f = popen ("zenity --file-selection", "r");
58 if (f != NULL)
59 {
60
61
62 if (fgets (song_path, sizeof (song_path), f) != NULL)
63 {
64 song_path[strcspn (song_path, "\n")] = 0;
65 song = LoadMusicStream (song_path);
66 PlayMusicStream (song);
67 }
68 pclose (f);
69 }
70 }
71
72 while (!WindowShouldClose ())
73 {
74 UpdateMusicStream (song);
75 float vol = GetMasterVolume ();
76 float total = GetMusicTimeLength (song);
77 float time_played = GetMusicTimePlayed (song);
78
79 if (IsKeyPressed (KEY_Q) == true)
80 {
81 break;
82 }
83
84 if (IsKeyPressed (KEY_P) == true && IsMusicStreamPlaying (song))
85 {
86 PauseMusicStream (song);
87 }
88
89 else if (IsKeyPressed (KEY_P) == true && !IsMusicStreamPlaying (song))
90 {
91 ResumeMusicStream (song);
92 }
93 else if (IsKeyPressed (KEY_EQUAL) == true)
94 {
95 SetMasterVolume (vol + 0.1f);
96 }
97 else if (IsKeyPressed (KEY_MINUS) == true)
98 {
99 SetMasterVolume (vol - 0.1f);
100 }
101
102 if (IsKeyPressed (KEY_RIGHT) || IsKeyPressedRepeat (KEY_RIGHT))
103 {
104 float t = time_played + 5.0f;
105 SeekMusicStream (song, t < total ? t : total - 0.1f);
106 }
107 if (IsKeyPressed (KEY_LEFT) || IsKeyPressedRepeat (KEY_LEFT))
108 {
109 float t = time_played - 5.0f;
110 SeekMusicStream (song, t > 0.0f ? t : 0.0f);
111 }
112
113 BeginDrawing ();
114 ClearBackground (BLACK);
115
116 char path_copy[1024];
117 strncpy (path_copy, song_path, sizeof (path_copy));
118 char *filename = basename (path_copy);
119
120 const char *line1 = !IsMusicStreamPlaying (song)
121 ? "paused :<" : TextFormat ("currently palying: %s :] ", filename);
122
123 const char *line2 = TextFormat ("Volume: %.2f", vol);
124 int tp = (int) time_played;
125 int tt = (int) total;
126 const char *line3 =
127 TextFormat ("%d:%02d / %d:%02d", tp / 60, tp % 60, tt / 60, tt % 60);
128
129
130 int font_size = 24;
131 float spacing = 1.0f;
132 float line_spacing = 35.0f;
133
134 Vector2 width1 =
135 MeasureTextEx (font, line1, (float) font_size, spacing);
136 Vector2 width2 =
137 MeasureTextEx (font, line2, (float) font_size, spacing);
138 Vector2 width3 =
139 MeasureTextEx (font, line3, (float) font_size, spacing);
140
141 int x1 = (width - (int) width1.x) / 2;
142 int x2 = (width - (int) width2.x) / 2;
143 int x3 = (width - (int) width3.x) / 2;
144 int start_y = (height - 60) / 2;
145
146 DrawTextEx (font, line1, (Vector2)
147 {
148 (float) x1, (float) start_y}, (float) font_size, spacing,
149 RAYWHITE);
150 DrawTextEx (font, line2, (Vector2)
151 {
152 (float) x2, (float) start_y + line_spacing},
153 (float) font_size, spacing, RAYWHITE);
154 DrawTextEx (font, line3, (Vector2)
155 {
156 (float) x3, (float) start_y + line_spacing * 2},
157 (float) font_size, spacing, RAYWHITE);
158
159 EndDrawing ();
160 }
161
162 UnloadMusicStream (song);
163 UnloadFont (font);
164 CloseAudioDevice ();
165 CloseWindow ();
166 return 0;
167 }