Re-jig simulation class to use generic functions

This commit is contained in:
2025-05-29 01:14:30 +01:00
parent 6b4652d5dd
commit 816b39e11f

View File

@@ -4,22 +4,25 @@
(in-package :ham) (in-package :ham)
(defclass simulation () (defclass simulation ()
((update :initarg :update) ((state :initarg :start)
(render :initarg :render) (params :initarg :params)
(state :initarg :start-state)
(width :initarg :width :initform 800) (width :initarg :width :initform 800)
(height :initarg :height :initform 600) (height :initarg :height :initform 600)
(running :initform nil) (running :initform nil)
simulation-thread)) simulation-thread))
(defgeneric update (simulation dt))
(defgeneric render (simulation))
(defmethod run ((sim simulation)) (defmethod run ((sim simulation))
(sdl2:make-this-thread-main (sdl2:make-this-thread-main
(lambda () (lambda ()
(with-slots (width height running) sim (with-slots (width height running) sim
(with-graphics-context (ctx :width width :height height) (with-graphics-context (ctx :width width :height height)
(loop while running do (loop while running do
(run-update sim (/ 1.0 60.0)) ; assume 60 Hz (update sim (/ 1.0 60.0)) ; assume 60 Hz
(run-render sim ctx))))))) (let ((frame (render sim)))
(display-frame ctx frame))))))))
(defmethod start ((sim simulation)) (defmethod start ((sim simulation))
(with-slots (running simulation-thread) sim (with-slots (running simulation-thread) sim
@@ -32,12 +35,3 @@
(with-slots (running simulation-thread) sim (with-slots (running simulation-thread) sim
(setf running nil) (setf running nil)
(sb-thread:join-thread simulation-thread))) (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))))