]> git.jsancho.org Git - gacela.git/blob - gacela_sound.lisp
e1b2a1e8a157b33a81740e6d08f1428471049285
[gacela.git] / gacela_sound.lisp
1 ;;; Gacela, a GNU Common Lisp extension for fast games development
2 ;;; Copyright (C) 2009 by Javier Sancho Fernandez <jsf at jsancho dot org>
3 ;;;
4 ;;; This program is free software: you can redistribute it and/or modify
5 ;;; it under the terms of the GNU General Public License as published by
6 ;;; the Free Software Foundation, either version 3 of the License, or
7 ;;; (at your option) any later version.
8 ;;;
9 ;;; This program is distributed in the hope that it will be useful,
10 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 ;;; GNU General Public License for more details.
13 ;;;
14 ;;; You should have received a copy of the GNU General Public License
15 ;;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17
18 (eval-when (compile load eval)
19            (when (not (find-package 'gacela)) (make-package 'gacela :nicknames '(gg) :use '(lisp)))
20            (in-package 'gacela :nicknames '(gg) :use '(lisp)))
21
22
23 ;;; Sound
24
25 (defun load-sound (filename &key static)
26   (let ((key (make-resource-sound :filename filename)))
27     (cond ((get-resource key) key)
28           (t (true-load-sound filename static)))))
29
30 (defun true-load-sound (filename static)
31   (init-audio)
32   (let ((key (make-resource-sound :filename filename))
33         (sound (Mix_LoadWAV filename)))
34     (cond ((/= sound 0)
35            (set-resource key
36                          `(:id-sound ,sound)
37                          (lambda () (true-load-sound filename static))
38                          (lambda () (Mix_FreeChunk sound))
39                          :static static)
40            key))))
41
42 (defun play-sound (sound &optional (loops 0))
43   (let ((id-sound (getf (get-resource sound) :id-sound)))
44     (/= (Mix_PlayChannel -1 id-sound loops) -1)))
45
46
47 ;;; Music
48
49 (defun load-music (filename &key static)
50   (let ((key (make-resource-music :filename filename)))
51     (cond ((get-resource key) key)
52           (t (true-load-music filename static)))))
53
54 (defun true-load-music (filename static)
55   (init-audio)
56   (let ((key (make-resource-music :filename filename))
57         (music (Mix_LoadMUS filename)))
58     (cond ((/= music 0)
59            (set-resource key
60                          `(:id-music ,music)
61                          (lambda () (true-load-music filename static))
62                          (lambda () (Mix_FreeMusic music))
63                          :static static)
64            key))))
65
66 (defun playing-music? ()
67   (/= (Mix_PlayingMusic) 0))
68
69 (defun paused-music? ()
70   (/= (Mix_PausedMusic) 0))
71
72 (defun play-music (music &optional (loops -1))
73   (cond ((not (playing-music?))
74          (let ((id-music (getf (get-resource music) :id-music)))
75            (/= (Mix_PlayMusic id-music loops) -1)))))
76
77 (defun pause-music ()
78   (cond ((and (playing-music?) (not (paused-music?)))
79          (Mix_PauseMusic)
80          t)))
81
82 (defun resume-music ()
83   (cond ((and (playing-music?) (paused-music?))
84          (Mix_ResumeMusic)
85          t)))
86
87 (defun halt-music ()
88   (cond ((playing-music?)
89          (Mix_HaltMusic)
90          t)))