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 (process-screen-events (get-event events `(,SDL_QUIT)))
43 (process-keyboard-events (get-event events `(,SDL_KEYDOWN ,SDL_KEYUP)))))
48 (define quit-signal #f)
50 (define (process-screen-events events)
52 (process-screen-events-recursive events))
54 (define (process-screen-events-recursive events)
55 (cond ((not (null? events))
56 (let ((event (car events)))
57 (cond ((= (assoc-ref event 'type) SDL_QUIT) (set! quit-signal #t))))
58 (process-screen-events-recursive (cdr events)))))
60 (define (quit-signal?)
66 (define keymap (make-hash-table))
67 (define pressed (make-hash-table))
68 (define released (make-hash-table))
70 (define (process-keyboard-events events)
72 (process-keyboard-events-recursive events))
74 (define (process-keyboard-events-recursive events)
75 (cond ((not (null? events))
76 (let ((event (car events)))
77 (cond ((= (assoc-ref event 'type) SDL_KEYDOWN) (key-press (assoc-ref event 'key.keysym.sym)))
78 ((= (assoc-ref event 'type) SDL_KEYUP) (key-release (assoc-ref event 'key.keysym.sym)))))
79 (process-keyboard-events-recursive (cdr events)))))
82 (hash-ref keymap (get-keycode key)))
84 (define (key-pressed? key)
85 (hash-ref pressed (get-keycode key)))
87 (define (key-released? key)
88 (hash-ref released (get-keycode key)))
90 (define (key-press key-code)
91 (hash-set! keymap key-code #t)
92 (hash-set! pressed key-code #t)
93 (hash-set! released key-code #f))
95 (define (key-release key-code)
96 (hash-set! keymap key-code #f)
97 (hash-set! pressed key-code #f)
98 (hash-set! released key-code #t))
100 (define (clear-keymap)
101 (hash-clear! keymap))
103 (define (clear-key-state)
104 (hash-clear! pressed)
105 (hash-clear! released))
166 (define keynames (map (lambda (k) (cons (cdr k) (car k))) keys))
168 (define (get-keycode keyname)
169 (assoc-ref keynames keyname))
171 (define (get-keyname keycode)
172 (assoc-ref keys keycode))