summaryrefslogtreecommitdiff
path: root/2d
diff options
context:
space:
mode:
authorDavid Thompson <dthompson2@worcester.edu>2014-01-07 22:07:52 -0500
committerDavid Thompson <dthompson2@worcester.edu>2014-01-07 22:07:52 -0500
commitd3ecfcb19bb011e01a5f3c3d9145789a1b6ebf1e (patch)
treece967722257a3dd585d77da8df7853635666f3ef /2d
parent994e6805f2082b07a3415c0b10df6474c5c6a57f (diff)
Tweak SDL event handling.
* 2d/game.scm (handle-events, read-input): Rename it. (event-handlers, register-event-handler, handle-event): Use a hash table instead of an alist.
Diffstat (limited to '2d')
-rw-r--r--2d/game.scm21
1 files changed, 10 insertions, 11 deletions
diff --git a/2d/game.scm b/2d/game.scm
index cd73f8f..b6be399 100644
--- a/2d/game.scm
+++ b/2d/game.scm
@@ -82,7 +82,7 @@ many times as `tick-interval` can divide ACCUMULATOR. The return value
is the unused accumulator time."
(if (>= accumulator (tick-interval))
(begin
- (handle-events)
+ (read-input)
(tick-agenda!)
(update (- accumulator (tick-interval))))
accumulator))
@@ -148,24 +148,23 @@ time in milliseconds that has passed since the last game update."
;;; Event Handling
;;;
-(define handle-events
+(define read-input
(let ((e (SDL:make-event)))
(lambda ()
- "Handle all events in the SDL event queue."
+ "Process all events in the input event queue."
(while (SDL:poll-event e)
(handle-event e)))))
-(define event-handlers '())
+(define event-handlers (make-hash-table))
-(define (register-event-handler event callback)
- "Register CALLBACK to respond to events of type EVENT."
- (set! event-handlers (acons event callback event-handlers)))
+(define (register-event-handler event-type proc)
+ (hashq-set! event-handlers event-type proc))
(define (handle-event e)
- "Call the relevant callback procedure for the event E."
- (let ((handler (assq-ref event-handlers (SDL:event:type e))))
- (when handler
- (handler e))))
+ "Run the relevant hook for the event E."
+ (let ((handle (hashq-get-handle event-handlers (SDL:event:type e))))
+ (when handle
+ ((car handle) e))))
;;;
;;; Frames Per Second