(include raylib.h) (include string.h) (import storage) (define MENU-CLICK-FILE "res/music/01 - Menu.ogg") (define FISHING-SPOT-FILE "res/music/02 - Fishing Spot.ogg") (define FISHING-START-FILE "res/music/03 - Catch!.ogg") (define GOTCHA-FILE "res/music/04 - Gotcha!.ogg") (define MENU-MUSIC-FILE "res/music/Catch-Phase 01 - Fishing.ogg") (define GAME-MUSIC-FILE "res/music/Catch-Phase 03 - Aqua World.ogg") (define FISHING-MUSIC-FILE "res/music/Catch-Phase 05 - FIH.ogg") (var menu-click Sound) (var fishing-spot-sound Sound) (var fishing-start-sound Sound) (var gotcha-sound Sound) (var menu-music Music) (var game-music Music) (var fishing-music Music) ;; NULL when no music is playing (var current-music (* Music) NULL) (var audio-unlocked bool false) (var music-enabled bool true) (fn apply-music-volume () void (if (!= current-music NULL) (do (var volume float 0.0f) (if music-enabled (= volume 1.0f)) (SetMusicVolume (* current-music) volume)))) (fn load-music-setting () void (var path (* const char) (storage-settings-path)) (if (== path NULL) (return)) (var text (* char) (LoadFileText path)) (if (== text NULL) (return)) (if (!= (strstr text "music=0") NULL) (= music-enabled false)) (UnloadFileText text) (if music-enabled (TraceLog LOG-INFO "music setting: on") (TraceLog LOG-INFO "music setting: off"))) (fn save-music-setting () void (var path (* const char) (storage-settings-path)) (if (== path NULL) (return)) (var ok bool false) (if music-enabled (= ok (SaveFileText path "music=1\n")) (= ok (SaveFileText path "music=0\n"))) (if (! ok) (TraceLog LOG-WARNING "failed to save music setting to \"%s\"" path)) (storage-sync)) (fn load-sound ((file (* const char))) Sound (var sound Sound (LoadSound file)) (if (! (IsSoundValid sound)) (TraceLog LOG-WARNING "failed to load sound \"%s\"" file)) (return sound)) (fn load-music ((file (* const char))) Music (var music Music (LoadMusicStream file)) (if (! (IsMusicValid music)) (TraceLog LOG-WARNING "failed to load music \"%s\"" file)) (return music)) (pub fn audio-init () void (load-music-setting) (= menu-click (load-sound MENU-CLICK-FILE)) (= fishing-spot-sound (load-sound FISHING-SPOT-FILE)) (= fishing-start-sound (load-sound FISHING-START-FILE)) (= gotcha-sound (load-sound GOTCHA-FILE)) (= menu-music (load-music MENU-MUSIC-FILE)) (= game-music (load-music GAME-MUSIC-FILE)) (= fishing-music (load-music FISHING-MUSIC-FILE))) (pub fn audio-deinit () void (if (!= current-music NULL) (StopMusicStream (* current-music))) (= current-music NULL) (UnloadSound menu-click) (UnloadSound fishing-spot-sound) (UnloadSound fishing-start-sound) (UnloadSound gotcha-sound) (UnloadMusicStream menu-music) (UnloadMusicStream game-music) (UnloadMusicStream fishing-music)) (fn switch-music ((music (* Music))) void (if (== current-music music) (return)) (if (!= current-music NULL) (StopMusicStream (* current-music))) (= current-music NULL) (if (IsMusicValid (* music)) (do (= current-music music) (PlayMusicStream (* music)) (apply-music-volume)))) (pub fn audio-update () void (if (!= current-music NULL) (UpdateMusicStream (* current-music)))) (pub fn audio-play-menu-music () void (switch-music (& menu-music))) (pub fn audio-play-game-music () void (switch-music (& game-music))) (pub fn audio-play-fishing-music () void (switch-music (& fishing-music))) (pub fn audio-music-enabled () bool (return music-enabled)) (pub fn audio-toggle-music () void (= music-enabled (! music-enabled)) (apply-music-volume) (save-music-setting) (if music-enabled (TraceLog LOG-INFO "music setting: on") (TraceLog LOG-INFO "music setting: off"))) ;; Browsers keep the audio context suspended until the first user gesture. ;; Restart the current track once after that gesture so the music actually begins. #+emscripten (pub fn audio-unlock () void (if audio-unlocked (return)) (= audio-unlocked true) (if (!= current-music NULL) (do (StopMusicStream (* current-music)) (PlayMusicStream (* current-music)) (apply-music-volume)))) #-emscripten (pub fn audio-unlock () void (return)) (pub fn audio-play-menu-click () void (audio-unlock) (if (IsSoundValid menu-click) (PlaySound menu-click))) (pub fn audio-play-fishing-spot () void (if (IsSoundValid fishing-spot-sound) (PlaySound fishing-spot-sound))) (pub fn audio-play-fishing-start () void (if (IsSoundValid fishing-start-sound) (PlaySound fishing-start-sound))) (pub fn audio-play-gotcha () void (if (IsSoundValid gotcha-sound) (PlaySound gotcha-sound)))