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 (cond ((not (null? events))
53 (let ((event (car events)))
54 (cond ((= (assoc-ref event 'type) SDL_QUIT) (set! quit-signal #t))))
55 (process-screen-events (cdr events)))))
57 (define (quit-signal?)
63 (define keymap (make-hash-table))
64 (define pressed (make-hash-table))
65 (define released (make-hash-table))
67 (define (process-keyboard-events events)
69 (cond ((not (null? events))
70 (let ((event (car events)))
71 (cond ((= (assoc-ref event 'type) SDL_KEYDOWN) (key-press (assoc-ref event 'key.keysym.sym)))
72 ((= (assoc-ref event 'type) SDL_KEYUP) (key-release (assoc-ref event 'key.keysym.sym)))))
73 (process-keyboard-events (cdr events)))))
76 (hash-ref keymap (get-keycode key)))
78 (define (key-pressed? key)
79 (hash-ref pressed (get-keycode key)))
81 (define (key-released? key)
82 (hash-ref released (get-keycode key)))
84 (define (key-press key-code)
85 (hash-set! keymap key-code #t)
86 (hash-set! pressed key-code #t)
87 (hash-set! released key-code #f))
89 (define (key-release key-code)
90 (hash-set! keymap key-code #f)
91 (hash-set! pressed key-code #f)
92 (hash-set! released key-code #t))
94 (define (clear-keymap)
97 (define (clear-key-state)
99 (hash-clear! released))
160 (define keynames (map (lambda (k) (cons (cdr k) (car k))) keys))
162 (define (get-keycode keyname)
163 (assoc-ref keynames keyname))
165 (define (get-keyname keycode)
166 (assoc-ref keys keycode))