-
Notifications
You must be signed in to change notification settings - Fork 172
/
Copy pathsounds.carp
50 lines (43 loc) · 1.66 KB
/
sounds.carp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
(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))))