fordon_greeman / moosicpalyer

Branch: 2e598c4
Switch branches/tags
main
theidioticdev add seeking forwards & backwards, new font 2e598c4 13d ago
142 lines · 3.4 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 int
8 main (int argc, char **argv)
9 {
10
11 const int width = 1280;
12 const int height = 720;
13
14 InitWindow (width, height, "moosic palyer");
15 SetTargetFPS (60);
16 InitAudioDevice ();
17
18 Font font = LoadFont ("resources/IBMPlexSans-Bold.ttf");
19
20 Music song;
21 char song_path[1024];
22 if (argc > 1)
23 {
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 }
45 }
46
47 while (!WindowShouldClose ())
48 {
49 UpdateMusicStream (song);
50 float vol = GetMasterVolume ();
51 float total = GetMusicTimeLength (song);
52 float time_played = GetMusicTimePlayed (song);
53
54 if (IsKeyPressed (KEY_Q) == true)
55 {
56 break;
57 }
58
59 if (IsKeyPressed (KEY_P) == true && IsMusicStreamPlaying (song))
60 {
61 PauseMusicStream (song);
62 }
63
64 else if (IsKeyPressed (KEY_P) == true && !IsMusicStreamPlaying (song))
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
88 BeginDrawing ();
89 ClearBackground (BLACK);
90
91 char path_copy[1024];
92 strncpy (path_copy, song_path, sizeof (path_copy));
93 char *filename = basename (path_copy);
94
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;
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 ();
135 }
136
137 UnloadMusicStream (song);
138 UnloadFont (font);
139 CloseAudioDevice ();
140 CloseWindow ();
141 return 0;
142 }