Skip to content

Instantly share code, notes, and snippets.

@PuercoPop
Last active August 29, 2015 14:03
Show Gist options
  • Save PuercoPop/6000c42741e714c93f23 to your computer and use it in GitHub Desktop.
Save PuercoPop/6000c42741e714c93f23 to your computer and use it in GitHub Desktop.
(defpattern sdl-event (type key state)
`(and (sdl:event-type (type (eq ,type)))
(sdl::key-key (key (eq ,key)) ,state)))
(setf (fdefinition 'event-typep) #'sdl:key-down-p)
(sdl:with-init (sdl:sdl-init-video)
(sdl:window 256 240 :bpp 24 :sw t)
(sdl:enable-key-repeat 10 10)
(unwind-protect (let ((event (sdl:new-event)))
(loop
with start-time = (get-decoded-time)
for current-time = (get-decoded-time)
for event-poll = (sdl-cffi::SDL-Poll-Event event)
for event-type = (sdl:event-type event)
until (< 5 (- current-time start-time))
do
(match event
((key-down-event :key :sdl-key-z state)
(format t "You Pressed Z!~%" state)))))
(sdl:free-event event)))
;; Error
;; The value of KEYWORD is #.(SB-SYS:INT-SAP #X7FFFE002E230), which is not of type KEYWORD.
;; [Condition of type SIMPLE-TYPE-ERROR]
;; Macro-expansion
;; (BLOCK #:BLOCK1908
;; (TAGBODY
;; (RETURN-FROM #:BLOCK1908
;; (SYMBOL-MACROLET ((OPTIMA::%FAIL (GO #:FAIL1909)))
;; (IF (KEY-DOWN-EVENTP EVENT)
;; (LET ((#:G1906 (KEY-DOWN-EVENTKEY EVENT))
;; (#:G1907 (KEY-DOWN-EVENTSTATE EVENT)))
;; (DECLARE (IGNORABLE #:G1906 #:G1907))
;; (IF (EQ #:G1906 ':SDL-KEY-Z) <=== error here
;; (IF (NULL #:G1907)
;; (FORMAT T "You Pressed Z!~%" STATE)
;; (GO #:FAIL1909))
;; (GO #:FAIL1909)))
;; (GO #:FAIL1909))))
;; #:FAIL1909
;; (RETURN-FROM #:BLOCK1908 NIL)))
;; I'm hoping to replace the cond with a succcint match clause in the lines of
(match event
(key-down-event :key :sdl-key-z) (format t "You Pressed Z!~%"))
;; My idea is something along the lines of this. The muffles('*') are meant
;; to highlight were I need the match-candidate (arg)
(defpattern key-down-event (&key state scancode key mod mod-key unicode)
`(when (and (eq :key-down-event (sdl:event-type *event*))
(eq :sdl-key-z (sdl::key-key *event*) key))))
;; A sample 'event-loop'
(sdl:with-init (sdl:sdl-init-video)
(sdl:window 256 240 :bpp 24 :sw t)
(sdl:enable-key-repeat 10 10)
(let ((event (sdl:new-event)))
(loop
with start-time = (get-decoded-time)
for current-time = (get-decoded-time)
for event-poll = (sdl-cffi::SDL-Poll-Event event)
for event-type = (sdl:event-type event)
until (< 5 (- current-time start-time))
do
(cond
((and (eq :key-down-event event-type) (eq :sdl-key-z (sdl::key-key event)))
(format t "Z: ~A~%" (sdl::key-state event)))
((eq :key-down-event event-type)
(format t "Down: ~A~%" (sdl::key-state event)))
((eq :key-up-event event-type)
(format t "UP: ~A~%" (sdl::key-state event)))
;; ((not (eq :NO-EVENT event-type)) (format t "~A~%" event-type ))
)
)
(sdl:free-event event)))
;; The meat
(defun check-for-input ()
(let ((event (sdl:new-event)))
(loop
;; Pops the event-queue and binds it to event, return 0 if no pending
;; events.
:for event-poll = (sdl-cffi::SDL-Poll-event event)
:for event-type = (sdl:event-type event)
:until (or (= 0 event-poll)
(eq :no-event event-type)
(eq :quit-event event-type))
:do
(when (and (eq :key-down-event event-type) (eq :sdl-key-z (sdl::key-key event)))
(format t "You Pressed Z!~%")))
(sdl:free-event event)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment