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-module (gacela events)
19 #:use-module (gacela sdl)
20 #:export (process-events
27 (define (get-event events types)
28 (cond ((null? events) '())
30 (let ((res (get-event (cdr events) types))
32 (cond ((member (assoc-ref event 'type) types) (cons event res))
36 (let ((event (SDL_PollEvent)))
37 (cond ((null? event) '())
38 (else (cons event (poll-events))))))
40 (define (process-events)
41 (let ((events (poll-events)))
42 (quit! (not (null? (get-event events `(,SDL_QUIT)))))
44 (process-keyboard-events (get-event events `(,SDL_KEYDOWN ,SDL_KEYUP)))))
58 (define (process-keyboard-events events)
59 (cond ((not (null? events))
60 (let ((event (car events)))
61 (cond ((= (assoc-ref event 'type) SDL_KEYDOWN) (key-press (assoc-ref event 'key.keysym.sym)))
62 ((= (assoc-ref event 'type) SDL_KEYUP) (key-release (assoc-ref event 'key.keysym.sym)))))
63 (process-keyboard-events (cdr events)))))
66 (define key-pressed? #f)
67 (define key-released? #f)
69 (define key-release #f)
70 (define clear-keymap #f)
71 (define clear-key-state #f)
73 (let ((keymap (make-hash-table))
74 (pressed (make-hash-table))
75 (released (make-hash-table)))
78 (hash-ref keymap (get-keycode key))))
82 (hash-ref pressed (get-keycode key))))
86 (hash-ref released (get-keycode key))))
90 (hash-set! keymap key-code #t)
91 (hash-set! pressed key-code #t)
92 (hash-set! released key-code #f)))
96 (hash-set! keymap key-code #f)
97 (hash-set! pressed key-code #f)
98 (hash-set! released key-code #t)))
102 (hash-clear! keymap)))
104 (set! clear-key-state
106 (hash-clear! pressed)
107 (hash-clear! released))))
109 (define get-keycode #f)
110 (define get-keyname #f)
169 (keynames (map (lambda (k) (cons (cdr k) (car k))) keys)))
173 (assoc-ref keynames keyname)))
177 (assoc-ref keys keycode))))