implement audio playing
This commit is contained in:
4
Makefile
4
Makefile
@@ -25,7 +25,7 @@ else
|
|||||||
RAYLIB_LIBS := -lGL -lm -lpthread -ldl -lrt -lX11
|
RAYLIB_LIBS := -lGL -lm -lpthread -ldl -lrt -lX11
|
||||||
endif
|
endif
|
||||||
|
|
||||||
OBJECTS := main.o menu.o game.o tmx.o
|
OBJECTS := main.o menu.o game.o tmx.o audio.o
|
||||||
|
|
||||||
all: catch-phase
|
all: catch-phase
|
||||||
|
|
||||||
@@ -57,7 +57,7 @@ EMFLAGS := -O2 -DPLATFORM_WEB \
|
|||||||
--shell-file shell.html \
|
--shell-file shell.html \
|
||||||
--preload-file ./res@res
|
--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
|
web/%.o: %.sex
|
||||||
mkdir -p web
|
mkdir -p web
|
||||||
|
|||||||
79
audio.sex
Normal file
79
audio.sex
Normal file
@@ -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)))
|
||||||
9
game.sex
9
game.sex
@@ -4,6 +4,7 @@
|
|||||||
(pub import pages)
|
(pub import pages)
|
||||||
(import main)
|
(import main)
|
||||||
(import utils)
|
(import utils)
|
||||||
|
(import audio)
|
||||||
(pub import tmx)
|
(pub import tmx)
|
||||||
|
|
||||||
(define MAP-FILE "res/tilemaps/SaveAss.tmx")
|
(define MAP-FILE "res/tilemaps/SaveAss.tmx")
|
||||||
@@ -108,6 +109,7 @@
|
|||||||
(pub fn mk-game ((app-state (* app-state))) page
|
(pub fn mk-game ((app-state (* app-state))) page
|
||||||
(var state _ (mem-alloc game-state))
|
(var state _ (mem-alloc game-state))
|
||||||
(= (. state app-state) app-state)
|
(= (. state app-state) app-state)
|
||||||
|
(audio-play-game-music)
|
||||||
|
|
||||||
(var map (* tmx-map) (load-tmx MAP-FILE))
|
(var map (* tmx-map) (load-tmx MAP-FILE))
|
||||||
(= (. state map) map)
|
(= (. state map) map)
|
||||||
@@ -310,7 +312,8 @@
|
|||||||
(do
|
(do
|
||||||
(= (. state fishing-minigame-active) false)
|
(= (. state fishing-minigame-active) false)
|
||||||
(= (. state bobber-y) 0.0f)
|
(= (. 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
|
(pub fn update-game ((state (* game-state))) void
|
||||||
(if (== (. state map) NULL)
|
(if (== (. state map) NULL)
|
||||||
@@ -348,13 +351,17 @@
|
|||||||
(.update (& (. state cam)) state)
|
(.update (& (. state cam)) state)
|
||||||
|
|
||||||
;; If we're near the fishing spot, show the tip and arm the fishing game
|
;; 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)
|
(= (. state near-fishing-spot) false)
|
||||||
(if (!= NULL (tmx-object-at-point (. state fishing-spots) (player-center state)))
|
(if (!= NULL (tmx-object-at-point (. state fishing-spots) (player-center state)))
|
||||||
(do
|
(do
|
||||||
(= (. state near-fishing-spot) true)
|
(= (. state near-fishing-spot) true)
|
||||||
|
(if (! was-near)
|
||||||
|
(audio-play-fishing-spot))
|
||||||
(if (IsKeyReleased KEY-X)
|
(if (IsKeyReleased KEY-X)
|
||||||
(do
|
(do
|
||||||
(= (. state fishing-minigame-active) true)
|
(= (. state fishing-minigame-active) true)
|
||||||
(= (. state bobber-y) 0.0f)
|
(= (. state bobber-y) 0.0f)
|
||||||
(= (. state bobber-vy) 0.0f)
|
(= (. state bobber-vy) 0.0f)
|
||||||
|
(audio-play-fishing-music)
|
||||||
(TraceLog LOG-INFO "Starting fishing minigame!"))))))
|
(TraceLog LOG-INFO "Starting fishing minigame!"))))))
|
||||||
|
|||||||
4
main.sex
4
main.sex
@@ -1,5 +1,6 @@
|
|||||||
(pub import pages)
|
(pub import pages)
|
||||||
(pub import utils)
|
(pub import utils)
|
||||||
|
(import audio)
|
||||||
|
|
||||||
(define RAYGUI_IMPLEMENTATION 1)
|
(define RAYGUI_IMPLEMENTATION 1)
|
||||||
(include raygui.h)
|
(include raygui.h)
|
||||||
@@ -56,6 +57,7 @@
|
|||||||
(fn main-loop ((void-state (* void))) void
|
(fn main-loop ((void-state (* void))) void
|
||||||
(var state _ (cast void-state (* app-state)))
|
(var state _ (cast void-state (* app-state)))
|
||||||
(.change-page- state)
|
(.change-page- state)
|
||||||
|
(audio-update)
|
||||||
(match (. state page)
|
(match (. state page)
|
||||||
((menu (state))
|
((menu (state))
|
||||||
(update-menu state)
|
(update-menu state)
|
||||||
@@ -70,11 +72,13 @@
|
|||||||
InitWindow WIN-WIDTH WIN-HEIGHT "Catch-Phase")
|
InitWindow WIN-WIDTH WIN-HEIGHT "Catch-Phase")
|
||||||
(call-and-check IsAudioDeviceReady "failed to init audio" 2
|
(call-and-check IsAudioDeviceReady "failed to init audio" 2
|
||||||
InitAudioDevice)
|
InitAudioDevice)
|
||||||
|
(audio-init)
|
||||||
(SetExitKey -1)
|
(SetExitKey -1)
|
||||||
(GuiSetStyle DEFAULT TEXT-SIZE 30)
|
(GuiSetStyle DEFAULT TEXT-SIZE 30)
|
||||||
(SetTargetFPS 60))
|
(SetTargetFPS 60))
|
||||||
|
|
||||||
(fn deinit () void
|
(fn deinit () void
|
||||||
|
(audio-deinit)
|
||||||
(CloseAudioDevice)
|
(CloseAudioDevice)
|
||||||
(CloseWindow))
|
(CloseWindow))
|
||||||
|
|
||||||
|
|||||||
12
menu.sex
12
menu.sex
@@ -3,6 +3,7 @@
|
|||||||
(pub import pages)
|
(pub import pages)
|
||||||
(import main)
|
(import main)
|
||||||
(import utils)
|
(import utils)
|
||||||
|
(import audio)
|
||||||
|
|
||||||
(pub record menu-state
|
(pub record menu-state
|
||||||
((app-state (* app-state))
|
((app-state (* app-state))
|
||||||
@@ -23,6 +24,7 @@
|
|||||||
(= (. state app-state) app-state)
|
(= (. state app-state) app-state)
|
||||||
(= (. state btn-to-game) 0)
|
(= (. state btn-to-game) 0)
|
||||||
(= (. state btn-exit) 0)
|
(= (. state btn-exit) 0)
|
||||||
|
(audio-play-menu-music)
|
||||||
(var img Image (LoadImage "res/img/menu.png"))
|
(var img Image (LoadImage "res/img/menu.png"))
|
||||||
(ImageResize (& img) WIN-WIDTH WIN-HEIGHT)
|
(ImageResize (& img) WIN-WIDTH WIN-HEIGHT)
|
||||||
(= (. state bg) (LoadTextureFromImage img))
|
(= (. state bg) (LoadTextureFromImage img))
|
||||||
@@ -52,12 +54,20 @@
|
|||||||
(EndDrawing))
|
(EndDrawing))
|
||||||
|
|
||||||
(pub fn update-menu ((state (* menu-state))) void
|
(pub fn update-menu ((state (* menu-state))) void
|
||||||
|
(var clicked bool false)
|
||||||
(if (== (. state btn-to-game) 1)
|
(if (== (. state btn-to-game) 1)
|
||||||
(do
|
(do
|
||||||
(= (. state btn-to-game) 0)
|
(= (. state btn-to-game) 0)
|
||||||
|
(= clicked true)
|
||||||
(.change-page (. state app-state) (sum-type page game))))
|
(.change-page (. state app-state) (sum-type page game))))
|
||||||
|
|
||||||
(if (== (. state btn-exit) 1)
|
(if (== (. state btn-exit) 1)
|
||||||
(do
|
(do
|
||||||
(= (. state btn-exit) 0)
|
(= (. 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)))
|
||||||
|
|||||||
Reference in New Issue
Block a user