Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (defparameter *states* nil)
- (defun state (&key start stop pause resume update quit key-down key-up
- lmb-down rmb-down lmb-up rmb-up)
- (list :start start :stop stop :pause pause :resume resume :update update
- :quit (or quit (lambda () t)) :key-down key-down :key-up key-up
- :lmb-down lmb-down :rmb-down rmb-down :lmb-up lmb-up :rmb-up rmb-up))
- (defun call-event (event &rest args)
- (let ((handler (getf (car *states*) event)))
- (when handler (apply handler args))))
- (defun push-state (state)
- (call-event :pause)
- (push state *states*)
- (call-event :start))
- (defun pop-state (&optional (count 1))
- (dotimes (i count)
- (call-event :stop)
- (pop *states*))
- (call-event :resume))
- (defun run (state window-width window-height window-title
- &key fullscreen (frame-rate 60))
- (sdl:with-init ()
- (sdl:window window-width window-height
- :title-caption window-title :icon-caption window-title
- :fullscreen fullscreen :double-buffer t)
- (setf (sdl:frame-rate) frame-rate)
- (setf *states* (list state))
- (call-event :start)
- (sdl:with-events ()
- (:quit-event () (or (not *states*) (call-event :quit)))
- (:key-down-event (:key key)
- (call-event :key-down key))
- (:key-up-event (:key key)
- (call-event :key-up key))
- (:mouse-button-down-event (:button button :x x :y y)
- (if (= button 1)
- (call-event :lmb-down x y)
- (call-event :rmb-down x y)))
- (:mouse-button-up-event (:button button :x x :y y)
- (if (= button 1)
- (call-event :lmb-up x y)
- (call-event :rmb-up x y)))
- (:idle ()
- (cond (*states* (call-event :update)
- (sdl:update-display))
- (t (sdl:push-quit-event)))))))
Add Comment
Please, Sign In to add comment