1 ;;; Gacela, a GNU Guile extension for fast games development
2 ;;; Copyright (C) 2009 by Javier Sancho Fernandez <jsf at jsancho dot org>
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.
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.
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/>.
18 (define (get-event events types)
19 (cond ((null? events) '())
21 (let ((res (get-event (cdr events) types))
23 (cond ((member (assoc-ref event 'type) types) (cons event res))
27 (let ((event (SDL_PollEvent)))
28 (cond ((null? event) '())
29 (else (cons event (poll-events))))))
31 (define (process-events)
32 (let ((events (poll-events)))
33 (quit? (not (null? (get-event events `(,SDL_QUIT)))))
35 (process-keyboard-events (get-event events `(,SDL_KEYDOWN ,SDL_KEYUP)))))
41 (lambda* (#:optional (value '()))
42 (if (null? value) quit (set! quit value)))))
44 (define (process-keyboard-events events)
45 (cond ((not (null? events))
46 (let ((event (car events)))
47 (cond ((= (assoc-ref event 'type) SDL_KEYDOWN) (key-press (assoc-ref event 'key.keysym.sym)))
48 ((= (assoc-ref event 'type) SDL_KEYUP) (key-release (assoc-ref event :key.keysym.sym)))))
49 (process-keyboard-events (cdr events)))))
52 (define key-pressed? #f)
53 (define key-released? #f)
55 (define key-release #f)
56 (define clear-keymap #f)
57 (define clear-key-state #f)
59 (let ((keymap (make-hash-table))
60 (pressed (make-hash-table))
61 (released (make-hash-table)))
64 (hash-ref keymap (get-keycode key))))
68 (hash-ref pressed (get-keycode key))))
72 (hash-ref released (get-keycode key))))
76 (hash-set! keymap key-code #t)
77 (hash-set! pressed key-code #t)
78 (hash-set! released key-code #f)))
82 (hash-set! keymap key-code #f)
83 (hash-set! pressed key-code #f)
84 (hash-set! released key-code #t)))
88 (hash-clear! keymap)))
93 (hash-clear! released))))
95 (define get-keycode #f)
96 (define get-keyname #f)
155 (keynames (map (lambda (k) (cons (cdr k) (car k))) keys)))
159 (assoc-ref keynames keyname)))
163 (assoc-ref keys keycode))))