fordon_greeman / moosicpalyer

add seeking forwards & backwards, new font Browse files
theidioticdev committed 13d ago parent 2bced12 commit 2e598c40e68431c290cae206481584edd2a088aa
Showing 4 changed files with 112 additions and 61 deletions. UnifiedSplit
+3 −2 GNUmakefile
@@ -1,9 +1,10 @@
1 -CC = cc
1 +# CC = cc
2 +CC = tcc
2 3 CFLAGS = -Wall -std=c99 -D_DEFAULT_SOURCE -O2
3 4 INCLUDES = -Iraylib/include
4 5 LDFLAGS = -Lraylib -lraylib -lwayland-client -lwayland-cursor -lwayland-egl -lxkbcommon -lGL -lm -lpthread -ldl -lX11
5 6 TARGET = moosicpalyer
6 -PREFIX ?= $(HOME)/.local/bin
7 +PREFIX ?= /usr/local/bin
7 8
8 9 all: $(TARGET)
9 10
+109 −59 main.c
@@ -7,86 +7,136 @@
7 7 int
8 8 main (int argc, char **argv)
9 9 {
10 +
10 11 const int width = 1280;
11 12 const int height = 720;
12 13
13 14 InitWindow (width, height, "moosic palyer");
14 15 SetTargetFPS (60);
15 - InitAudioDevice();
16 + InitAudioDevice ();
17 +
18 + Font font = LoadFont ("resources/IBMPlexSans-Bold.ttf");
16 19
17 20 Music song;
21 + char song_path[1024];
18 22 if (argc > 1)
19 23 {
20 - song = LoadMusicStream(argv[1]);
21 - PlayMusicStream(song);
24 + strncpy (song_path, argv[1], sizeof (song_path) - 1);
25 + song_path[sizeof (song_path) - 1] = 0;
26 + song = LoadMusicStream (song_path);
27 + PlayMusicStream (song);
28 + }
29 + else
30 + {
31 +
32 + FILE *f = popen ("zenity --file-selection", "r");
33 + if (f != NULL)
34 + {
35 +
36 +
37 + if (fgets (song_path, sizeof (song_path), f) != NULL)
38 + {
39 + song_path[strcspn (song_path, "\n")] = 0;
40 + song = LoadMusicStream (song_path);
41 + PlayMusicStream (song);
42 + }
43 + pclose (f);
44 + }
22 45 }
23 - else {
24 -
25 - FILE *f = popen("zenity --file-selection", "r");
26 - if (f != NULL)
27 - {
28 - char song_path[1024];
29 -
30 - if (fgets(song_path, sizeof(song_path), f) != NULL)
31 - {
32 - song_path[strcspn(song_path, "\n")] = 0;
33 - song = LoadMusicStream(song_path);
34 - PlayMusicStream(song);
35 - }
36 - pclose(f);
37 - }
38 - }
39 -
46 +
40 47 while (!WindowShouldClose ())
41 48 {
42 49 UpdateMusicStream (song);
43 50 float vol = GetMasterVolume ();
44 -
51 + float total = GetMusicTimeLength (song);
52 + float time_played = GetMusicTimePlayed (song);
53 +
54 + if (IsKeyPressed (KEY_Q) == true)
55 + {
56 + break;
57 + }
58 +
45 59 if (IsKeyPressed (KEY_P) == true && IsMusicStreamPlaying (song))
46 - {
47 - PauseMusicStream (song);
48 - }
60 + {
61 + PauseMusicStream (song);
62 + }
63 +
49 64 else if (IsKeyPressed (KEY_P) == true && !IsMusicStreamPlaying (song))
50 - {
51 - ResumeMusicStream (song);
52 - }
53 - else if (IsKeyPressed (KEY_EQUAL) == true && IsMusicStreamPlaying (song))
54 - {
55 - SetMasterVolume (vol + 0.1f);
56 - }
57 - else if (IsKeyPressed (KEY_MINUS) == true && IsMusicStreamPlaying (song))
58 - {
59 - SetMasterVolume (vol - 0.1f);
60 - }
61 -
65 + {
66 + ResumeMusicStream (song);
67 + }
68 + else if (IsKeyPressed (KEY_EQUAL) == true)
69 + {
70 + SetMasterVolume (vol + 0.1f);
71 + }
72 + else if (IsKeyPressed (KEY_MINUS) == true)
73 + {
74 + SetMasterVolume (vol - 0.1f);
75 + }
76 +
77 + if (IsKeyPressed (KEY_RIGHT) || IsKeyPressedRepeat (KEY_RIGHT))
78 + {
79 + float t = time_played + 5.0f;
80 + SeekMusicStream (song, t < total ? t : total - 0.1f);
81 + }
82 + if (IsKeyPressed (KEY_LEFT) || IsKeyPressedRepeat (KEY_LEFT))
83 + {
84 + float t = time_played - 5.0f;
85 + SeekMusicStream (song, t > 0.0f ? t : 0.0f);
86 + }
87 +
62 88 BeginDrawing ();
63 89 ClearBackground (BLACK);
64 -
90 +
65 91 char path_copy[1024];
66 - char song_path[1024];
67 - strncpy(path_copy, song_path, sizeof(path_copy));
68 - char *filename = basename(path_copy);
69 -
70 -
71 -
72 - const char *text = !IsMusicStreamPlaying(song)
73 - ? "paused :<"
74 - : TextFormat("currently playing %s :D\n Volume: %.2f", filename, vol);
75 - int font_size = 24;
76 - int text_width = MeasureText(text, font_size);
77 -
78 - int text_x = (width - text_width) / 2;
79 - int text_y = (height - font_size) / 2;
80 -
81 -
82 - DrawText(text, text_x, text_y , font_size, RAYWHITE);
92 + strncpy (path_copy, song_path, sizeof (path_copy));
93 + char *filename = basename (path_copy);
83 94
84 - EndDrawing ();
95 + const char *line1 = !IsMusicStreamPlaying (song)
96 + ? "paused :<" : TextFormat ("currently playing %s :D", filename);
97 +
98 + const char *line2 = TextFormat ("Volume: %.2f", vol);
99 + int tp = (int) time_played;
100 + int tt = (int) total;
101 + const char *line3 =
102 + TextFormat ("%d:%02d / %d:%02d", tp / 60, tp % 60, tt / 60, tt % 60);
103 +
104 +
105 + int font_size = 32;
106 + float spacing = 1.0f;
107 + float line_spacing = 35.0f;
85 108
109 + Vector2 width1 =
110 + MeasureTextEx (font, line1, (float) font_size, spacing);
111 + Vector2 width2 =
112 + MeasureTextEx (font, line2, (float) font_size, spacing);
113 + Vector2 width3 =
114 + MeasureTextEx (font, line3, (float) font_size, spacing);
115 +
116 + int x1 = (width - (int) width1.x) / 2;
117 + int x2 = (width - (int) width2.x) / 2;
118 + int x3 = (width - (int) width3.x) / 2;
119 + int start_y = (height - 60) / 2;
120 +
121 + DrawTextEx (font, line1, (Vector2)
122 + {
123 + (float) x1, (float) start_y}, (float) font_size, spacing,
124 + RAYWHITE);
125 + DrawTextEx (font, line2, (Vector2)
126 + {
127 + (float) x2, (float) start_y + line_spacing},
128 + (float) font_size, spacing, RAYWHITE);
129 + DrawTextEx (font, line3, (Vector2)
130 + {
131 + (float) x3, (float) start_y + line_spacing * 2},
132 + (float) font_size, spacing, RAYWHITE);
133 +
134 + EndDrawing ();
86 135 }
87 -
88 - UnloadMusicStream(song);
89 - CloseAudioDevice ();
90 - CloseWindow ();
136 +
137 + UnloadMusicStream (song);
138 + UnloadFont (font);
139 + CloseAudioDevice ();
140 + CloseWindow ();
91 141 return 0;
92 142 }
+0 −0 moosicpalyer Binary files a/moosicpalyer and b/moosicpalyer differ
+0 −0 resources/IBMPlexSans-Bold.ttf new file mode 100644, Binary files /dev/null and b/resources/IBMPlexSans-Bold.ttf differ