Last active
October 7, 2019 16:23
-
-
Save christophejunke/2e4d36d3729d19db487a5bb03500a5c7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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