Guest User

Untitled

a guest
Dec 13th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. (defparameter *states* nil)
  2.  
  3. (defun state (&key start stop pause resume update quit key-down key-up
  4. lmb-down rmb-down lmb-up rmb-up)
  5. (list :start start :stop stop :pause pause :resume resume :update update
  6. :quit (or quit (lambda () t)) :key-down key-down :key-up key-up
  7. :lmb-down lmb-down :rmb-down rmb-down :lmb-up lmb-up :rmb-up rmb-up))
  8.  
  9. (defun call-event (event &rest args)
  10. (let ((handler (getf (car *states*) event)))
  11. (when handler (apply handler args))))
  12.  
  13. (defun push-state (state)
  14. (call-event :pause)
  15. (push state *states*)
  16. (call-event :start))
  17.  
  18. (defun pop-state (&optional (count 1))
  19. (dotimes (i count)
  20. (call-event :stop)
  21. (pop *states*))
  22. (call-event :resume))
  23.  
  24. (defun run (state window-width window-height window-title
  25. &key fullscreen (frame-rate 60))
  26. (sdl:with-init ()
  27. (sdl:window window-width window-height
  28. :title-caption window-title :icon-caption window-title
  29. :fullscreen fullscreen :double-buffer t)
  30. (setf (sdl:frame-rate) frame-rate)
  31. (setf *states* (list state))
  32. (call-event :start)
  33. (sdl:with-events ()
  34. (:quit-event () (or (not *states*) (call-event :quit)))
  35. (:key-down-event (:key key)
  36. (call-event :key-down key))
  37. (:key-up-event (:key key)
  38. (call-event :key-up key))
  39. (:mouse-button-down-event (:button button :x x :y y)
  40. (if (= button 1)
  41. (call-event :lmb-down x y)
  42. (call-event :rmb-down x y)))
  43. (:mouse-button-up-event (:button button :x x :y y)
  44. (if (= button 1)
  45. (call-event :lmb-up x y)
  46. (call-event :rmb-up x y)))
  47. (:idle ()
  48. (cond (*states* (call-event :update)
  49. (sdl:update-display))
  50. (t (sdl:push-quit-event)))))))
Add Comment
Please, Sign In to add comment