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)))))
49 (define (process-keyboard-events events)
50 (cond ((not (null? events))
51 (let ((event (car events)))
52 (cond ((= (assoc-ref event 'type) SDL_KEYDOWN) (key-press (assoc-ref event 'key.keysym.sym)))
53 ((= (assoc-ref event 'type) SDL_KEYUP) (key-release (assoc-ref event 'key.keysym.sym)))))
54 (process-keyboard-events (cdr events)))))
57 (define key-pressed? #f)
58 (define key-released? #f)
60 (define key-release #f)
61 (define clear-keymap #f)
62 (define clear-key-state #f)
64 (let ((keymap (make-hash-table))
65 (pressed (make-hash-table))
66 (released (make-hash-table)))
69 (hash-ref keymap (get-keycode key))))
73 (hash-ref pressed (get-keycode key))))
77 (hash-ref released (get-keycode key))))
81 (hash-set! keymap key-code #t)
82 (hash-set! pressed key-code #t)
83 (hash-set! released key-code #f)))
87 (hash-set! keymap key-code #f)
88 (hash-set! pressed key-code #f)
89 (hash-set! released key-code #t)))
93 (hash-clear! keymap)))
98 (hash-clear! released))))
100 (define get-keycode #f)
101 (define get-keyname #f)
160 (keynames (map (lambda (k) (cons (cdr k) (car k))) keys)))
164 (assoc-ref keynames keyname)))
168 (assoc-ref keys keycode))))