Skip to content

Instantly share code, notes, and snippets.

@christophejunke
Last active October 7, 2019 16:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save christophejunke/2e4d36d3729d19db487a5bb03500a5c7 to your computer and use it in GitHub Desktop.
Save christophejunke/2e4d36d3729d19db487a5bb03500a5c7 to your computer and use it in GitHub Desktop.
(defpackage :o (:use :cl :bricabrac.sdl2.event-loop :sdl2))
(in-package :o)
(defgeneric handle-event (state event type)
(:method (state event type))
(:method (state event (type (eql :quit)))
(throw :quit :quit)))
(defun clock-ms ()
(round (* (osicat:get-monotonic-time) 1000)))
(defgeneric update (state dt)
(:method ((state number) dt)
(mod (+ state (/ dt 1000)) #.(* 2 pi))))
(defgeneric display-on-window (state win)
(:method (state win)
(gl:clear-color 0.0 0.0 (+ 0.5 (/ (sin state) 3)) 1.0)
(gl:clear :color-buffer)
(gl-swap-window win)))
(defun start (state &optional (width 200) (height 200) (regular-dt 10))
(with-captured-bindings (rebind *standard-output*)
(with-everything (:window (w :w width :h height) :gl g)
(rebind
(let* ((last-tick (clock-ms)) (dt 0))
(declare (type fixnum last-tick dt))
(catch :quit
(do-events (event :event-type type :method :wait :timeout 30)
(handle-event state event type)
(let ((now (clock-ms)))
(incf dt (- now last-tick))
(setf last-tick now))
(multiple-value-bind (parts rest) (floor dt regular-dt)
(dotimes (i parts)
(decf dt regular-dt)
(setf state (update state regular-dt)))
(finish-output)
(print (setf dt rest))
(when (plusp parts)
(display-on-window state w))))))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment