Carp/examples/sounds.carp
Jorge Acereda 9b08b6df3c Merge
2020-05-11 16:10:35 +02:00

51 lines
1.7 KiB
Plaintext

(load "SDL.carp")
(load "SDL_mixer.carp")
(Project.config "title" "Sounds")
(def fx1 (the (Ptr Mix_Chunk) NULL))
(defn play-sound-fx1 []
(ignore (Mixer.play-channel Mixer.any-free-channel fx1 0)))
(defn event-handler [app state event]
(let [et (SDL.Event.type event)]
(cond
;; Quit event
(= et SDL.Event.quit)
(SDLApp.stop app)
;; Key events
(= et SDL.Event.key-down)
(let [key (SDL.Event.keycode event)]
(cond
(= key SDL.Keycode.escape) (SDLApp.stop app)
(= key SDL.Keycode.return) (do (play-sound-fx1)
state)
state))
;; Other event
state)))
(defn main []
(let [app (SDLApp.create "Sound Effects with SDL_mixer" 400 300)
rend @(SDLApp.renderer &app)]
(do
(let [flags Mixer.mp3-support]
(when (/= flags (Mixer.init flags))
(println* "Mixer.init error: " &(from-cstr (Mixer.get-error)))))
(if (Mixer.ok? (Mixer.open-audio 22050 Mixer.default-format 2 4096))
()
(println* "Mixer.open-audio error: " &(from-cstr (Mixer.get-error))))
(set! fx1 (Mixer.load-wav (cstr "resources/fx1.wav")))
(assert (not-null? fx1))
(let-do [n (Mixer.nr-of-music-decoders)]
(println* "Nr of music decoders: " n)
(for [i 0 n]
(println* " - " &(from-cstr (Mixer.get-music-decoder i)))))
(let-do [music (Mixer.load-music (cstr "resources/song.mp3"))]
(println* "Music is " (if (null? music) "null." "not null."))
(println* "Play result: " (Mixer.play-music music -1))
)
(SDLApp.run-with-callbacks &app event-handler id SDLApp.default-draw 0))))