Write a class for simulations
This commit is contained in:
parent
be7fa37495
commit
25f62b4b94
3
ham.asd
3
ham.asd
@ -10,4 +10,5 @@
|
||||
:description "A framework for creating physics simulations"
|
||||
:depends-on (:sdl2)
|
||||
:components ((:file "package")
|
||||
(:file "drawing" :depends-on ("package"))))
|
||||
(:file "drawing" :depends-on ("package"))
|
||||
(:file "simulation" :depends-on ("package" "drawing"))))
|
||||
|
43
simulation.lisp
Normal file
43
simulation.lisp
Normal file
@ -0,0 +1,43 @@
|
||||
;; Copyright (c) Camden Dixie O'Brien
|
||||
;; SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
(in-package :ham)
|
||||
|
||||
(defclass simulation ()
|
||||
((update :initarg :update)
|
||||
(render :initarg :render)
|
||||
(state :initarg :start-state)
|
||||
(width :initarg :width :initform 800)
|
||||
(height :initarg :height :initform 600)
|
||||
(running :initform nil)
|
||||
simulation-thread))
|
||||
|
||||
(defmethod run ((sim simulation))
|
||||
(sdl2:make-this-thread-main
|
||||
(lambda ()
|
||||
(with-slots (width height running) sim
|
||||
(with-graphics-context (ctx :width width :height height)
|
||||
(loop while running do
|
||||
(run-update sim (/ 1.0 60.0)) ; assume 60 Hz
|
||||
(run-render sim ctx)))))))
|
||||
|
||||
(defmethod start ((sim simulation))
|
||||
(with-slots (running simulation-thread) sim
|
||||
(setf running t)
|
||||
(setf simulation-thread
|
||||
(sb-thread:make-thread (lambda () (run sim))
|
||||
:name "ham-simulation-thread"))))
|
||||
|
||||
(defmethod stop ((sim simulation))
|
||||
(with-slots (running simulation-thread) sim
|
||||
(setf running nil)
|
||||
(sb-thread:join-thread simulation-thread)))
|
||||
|
||||
(defmethod run-update ((sim simulation) dt)
|
||||
(with-slots (update state) sim
|
||||
(setf state (funcall update state dt))))
|
||||
|
||||
(defmethod run-render ((sim simulation) ctx)
|
||||
(with-slots (render state) sim
|
||||
(let ((frame (apply render state)))
|
||||
(display-frame ctx frame))))
|
Loading…
x
Reference in New Issue
Block a user