From 0816e8e4ecd7044d1ee711ae6db8337d38ee0e14 Mon Sep 17 00:00:00 2001 From: alex-eg Date: Sun, 8 Nov 2026 20:20:10 +0300 Subject: [PATCH] implement audio playing --- Makefile | 4 +-- audio.sex | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ game.sex | 9 ++++++- main.sex | 4 +++ menu.sex | 12 ++++++++- 5 files changed, 104 insertions(+), 4 deletions(-) create mode 100644 audio.sex diff --git a/Makefile b/Makefile index 02829c2..bcbe8b5 100644 --- a/Makefile +++ b/Makefile @@ -25,7 +25,7 @@ else RAYLIB_LIBS := -lGL -lm -lpthread -ldl -lrt -lX11 endif -OBJECTS := main.o menu.o game.o tmx.o +OBJECTS := main.o menu.o game.o tmx.o audio.o all: catch-phase @@ -57,7 +57,7 @@ EMFLAGS := -O2 -DPLATFORM_WEB \ --shell-file shell.html \ --preload-file ./res@res -WEB_OBJECTS := web/menu.o web/game.o web/tmx.o +WEB_OBJECTS := web/menu.o web/game.o web/tmx.o web/audio.o web/%.o: %.sex mkdir -p web diff --git a/audio.sex b/audio.sex new file mode 100644 index 0000000..1c5e8e2 --- /dev/null +++ b/audio.sex @@ -0,0 +1,79 @@ +(include raylib.h) + +(define MENU-CLICK-FILE "res/music/01 - Menu.ogg") +(define FISHING-SPOT-FILE "res/music/02 - Fishing Spot.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 menu-music Music) +(var game-music Music) +(var fishing-music Music) + +;; NULL when no music is playing +(var current-music (* Music) NULL) + +(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 + (= menu-click (load-sound MENU-CLICK-FILE)) + (= fishing-spot-sound (load-sound FISHING-SPOT-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) + (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))))) + +(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-play-menu-click () void + (if (IsSoundValid menu-click) + (PlaySound menu-click))) + +(pub fn audio-play-fishing-spot () void + (if (IsSoundValid fishing-spot-sound) + (PlaySound fishing-spot-sound))) diff --git a/game.sex b/game.sex index 57438f5..465c735 100644 --- a/game.sex +++ b/game.sex @@ -4,6 +4,7 @@ (pub import pages) (import main) (import utils) +(import audio) (pub import tmx) (define MAP-FILE "res/tilemaps/SaveAss.tmx") @@ -108,6 +109,7 @@ (pub fn mk-game ((app-state (* app-state))) page (var state _ (mem-alloc game-state)) (= (. state app-state) app-state) + (audio-play-game-music) (var map (* tmx-map) (load-tmx MAP-FILE)) (= (. state map) map) @@ -310,7 +312,8 @@ (do (= (. state fishing-minigame-active) false) (= (. state bobber-y) 0.0f) - (= (. state bobber-vy) 0.0f)))) + (= (. state bobber-vy) 0.0f) + (audio-play-game-music)))) (pub fn update-game ((state (* game-state))) void (if (== (. state map) NULL) @@ -348,13 +351,17 @@ (.update (& (. state cam)) state) ;; If we're near the fishing spot, show the tip and arm the fishing game + (var was-near bool (. state near-fishing-spot)) (= (. state near-fishing-spot) false) (if (!= NULL (tmx-object-at-point (. state fishing-spots) (player-center state))) (do (= (. state near-fishing-spot) true) + (if (! was-near) + (audio-play-fishing-spot)) (if (IsKeyReleased KEY-X) (do (= (. state fishing-minigame-active) true) (= (. state bobber-y) 0.0f) (= (. state bobber-vy) 0.0f) + (audio-play-fishing-music) (TraceLog LOG-INFO "Starting fishing minigame!")))))) diff --git a/main.sex b/main.sex index 187db16..4600043 100644 --- a/main.sex +++ b/main.sex @@ -1,5 +1,6 @@ (pub import pages) (pub import utils) +(import audio) (define RAYGUI_IMPLEMENTATION 1) (include raygui.h) @@ -56,6 +57,7 @@ (fn main-loop ((void-state (* void))) void (var state _ (cast void-state (* app-state))) (.change-page- state) + (audio-update) (match (. state page) ((menu (state)) (update-menu state) @@ -70,11 +72,13 @@ InitWindow WIN-WIDTH WIN-HEIGHT "Catch-Phase") (call-and-check IsAudioDeviceReady "failed to init audio" 2 InitAudioDevice) + (audio-init) (SetExitKey -1) (GuiSetStyle DEFAULT TEXT-SIZE 30) (SetTargetFPS 60)) (fn deinit () void + (audio-deinit) (CloseAudioDevice) (CloseWindow)) diff --git a/menu.sex b/menu.sex index 2c00e56..59aef96 100644 --- a/menu.sex +++ b/menu.sex @@ -3,6 +3,7 @@ (pub import pages) (import main) (import utils) +(import audio) (pub record menu-state ((app-state (* app-state)) @@ -23,6 +24,7 @@ (= (. state app-state) app-state) (= (. state btn-to-game) 0) (= (. state btn-exit) 0) + (audio-play-menu-music) (var img Image (LoadImage "res/img/menu.png")) (ImageResize (& img) WIN-WIDTH WIN-HEIGHT) (= (. state bg) (LoadTextureFromImage img)) @@ -52,12 +54,20 @@ (EndDrawing)) (pub fn update-menu ((state (* menu-state))) void + (var clicked bool false) (if (== (. state btn-to-game) 1) (do (= (. state btn-to-game) 0) + (= clicked true) (.change-page (. state app-state) (sum-type page game)))) (if (== (. state btn-exit) 1) (do (= (. state btn-exit) 0) - (.quit (. state app-state))))) + (= clicked true) + (.quit (. state app-state)))) + + ;; Any menu key press is a UI interaction too + (var key-pressed bool (!= 0 (GetKeyPressed))) + (if (|| clicked key-pressed) + (audio-play-menu-click)))