From 816b39e11f9f2e584a7e64cbc0354a1d0ea93fd7 Mon Sep 17 00:00:00 2001 From: Camden Dixie O'Brien Date: Thu, 29 May 2025 01:14:30 +0100 Subject: [PATCH] Re-jig simulation class to use generic functions --- simulation.lisp | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/simulation.lisp b/simulation.lisp index 59db1f2..ce2c793 100644 --- a/simulation.lisp +++ b/simulation.lisp @@ -4,22 +4,25 @@ (in-package :ham) (defclass simulation () - ((update :initarg :update) - (render :initarg :render) - (state :initarg :start-state) + ((state :initarg :start) + (params :initarg :params) (width :initarg :width :initform 800) (height :initarg :height :initform 600) (running :initform nil) simulation-thread)) +(defgeneric update (simulation dt)) +(defgeneric render (simulation)) + (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))))))) + (update sim (/ 1.0 60.0)) ; assume 60 Hz + (let ((frame (render sim))) + (display-frame ctx frame)))))))) (defmethod start ((sim simulation)) (with-slots (running simulation-thread) sim @@ -32,12 +35,3 @@ (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))))