From d3ecfcb19bb011e01a5f3c3d9145789a1b6ebf1e Mon Sep 17 00:00:00 2001 From: David Thompson Date: Tue, 7 Jan 2014 22:07:52 -0500 Subject: 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. --- 2d/game.scm | 21 ++++++++++----------- 1 file 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 -- cgit v1.2.3