Skip to content

Instantly share code, notes, and snippets.

@PuercoPop
Created April 21, 2014 19:54
Show Gist options
  • Save PuercoPop/11154418 to your computer and use it in GitHub Desktop.
Save PuercoPop/11154418 to your computer and use it in GitHub Desktop.
(in-package :famiclom)
;; Launch an sdl-window. After Pressing p, the inputs are polled and
;; then. displayed in the window or written to stdout.
(defstruct pad
(buttons (make-array 8 :element-type 'boolean :initial-element nil))
(strobe '(:a :b :select :start :up :down :left :right))
(index 0 :type fixnum)) ;; Used for strobe method
(defun reset (pad)
"Set the button array to nil and the strobe read index to 0.")
(defun strobe (pad)
"Do read the element in the buttons arrat being pointed to by the index. If
at the end of the array, return nil."
(if (<= (length (pad-buttons pad))
(pad-index pad))
'end
(prog1
(elt (pad-buttons pad) (pad-index pad))
(incf (pad-index pad)))))
(defun poll (pad)
"Poll the pads 20 times and reset the pads")
(defvar *pad* (make-pad)
"An input device to retrieve commands from.")
(defvar *keymap*
'((:a . :sdl-key-z)
(:b . :sdl-key-x)
(:select . :sdl-key-space)
(:start . :sdl-key-return)
(:up . :sdl-key-up)
(:down . :sdl-key-down)
(:left . :sdl-key-left)
(:right . :sdl-key-right)))
(let ((pad (make-pad)))
(sdl:with-init (sdl:sdl-init-video)
(sdl:window 256 240 :bpp 24 :sw t)
(sdl:enable-key-repeat 10 10)
(sdl:with-events (:poll)
(:quit-event () t)
(:key-down-event
(:key key-pressed)
(cond
((eq key-pressed :sdl-key-p)
(loop
:for key = (strobe pad)
:until (eq 'end key)
:do (format t "~A~%" key)))
((eq key-pressed :sdl-key-i)
(format t "~A~%" (pad-buttons pad) ))
(t
(loop
:for (button . key) :in *keymap*
:for index :from 0 :upto 7
:when (eq key-pressed key)
:do
(setf (elt (pad-buttons pad) index ) t))))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment