Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
(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)
(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)))
(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
You can’t perform that action at this time.