diff --git a/wh-engine/actor.lisp b/wh-engine/actor.lisp index 7d9470c..984b633 100644 --- a/wh-engine/actor.lisp +++ b/wh-engine/actor.lisp @@ -92,13 +92,15 @@ (funcall fun this) (loop for child-ptr in (o! this children) when (typep child-ptr 'weak-pointer) - do (o! (weak-pointer-value child-ptr) (apply-to-tree fun)))) + do (let ((child (weak-pointer-value child-ptr))) + (unless child + (error "nil value dereferencing child-ptr in ~S" this)) + (o! child (apply-to-tree fun))))) (defmethod print-object ((this actor) stream) (print-unreadable-object (this stream :type t :identity t) - (prin1 (o! this :slot id) stream) - (princ " ") - (prin1 (o! this :slot name) stream))) + (format stream "~D ~S" + (o! this :slot id) (o! this :slot name)))) (defmethod get-component ((this actor) component-class) "Get a component of the specified class attached to this object." @@ -183,6 +185,7 @@ (defmethod resume ((this actor)) "Initialize or restore this actor's state." + (format t "=> actor resume: ~S~%" this) ;; Restore self (when (typep (o! this :slot scene) 'id-ref) ;; relink to scene @@ -218,6 +221,7 @@ (loop for child-cell on (o! this :slot children) when (typep (car child-cell) 'weak-pointer) do (rplaca child-cell (referize (car child-cell)))) + (format t "suspend -- children: ~S~%" (o! this :slot children)) (referize-setf (o! this :slot scene)) (referize-setf (o! this :slot parent))) @@ -226,11 +230,7 @@ (loop for component in (o! this components) do (when (o! component active-p) (unless (o! component started-p) (o! component (start))) - (o! component (update)))) -; (loop for child in (o! this children) -; do (when (o! child active-p) -; (o! child (update)))) - ) + (o! component (update))))) (defmethod destroy ((this actor)) "Mark this object for unloading." diff --git a/wh-engine/main.lisp b/wh-engine/main.lisp index 82c695c..1e52fce 100644 --- a/wh-engine/main.lisp +++ b/wh-engine/main.lisp @@ -98,19 +98,19 @@ "Get a scene by its ID." (find-if (lambda (scene) (eq (o! scene id) scene-id)) *world-scenes*)) -(defun attach-actor-to-world (actor scene) - "Properly attach actor and its descendents to scene, and initialize them." - ;; attach actors to scene - (apply-to-tree actor (lambda (a) - (setf (o! a :slot scene) nil) - (o! scene (add-actor a)))) - ;; (resume) -> automatically resumes children - (o! actor (resume)) - ;; FIXME make (activate) call itself recursively on children - (apply-to-tree actor (lambda (a) - (when (o! a tree-active-p) - (o! a (activate))))) - ) +(defun initialize-actors-in (scene &rest actors) + "Properly attach actors and their descendents to scene, and initialize them." + (loop for actor in actors + do ;; attach actor to scene + (o! actor (apply-to-tree (lambda (a) + (setf (o! a :slot scene) nil) + (o! scene (add-actor a))))) + ;; (resume) -> automatically resumes children + (o! actor (resume)) + ;; (activate) -> automatically activates eligible children + (when (o! actor tree-active-p) + (o! actor (activate))) + )) (defvar *view-width* 384 "View-space width in pixels.") @@ -179,9 +179,9 @@ (setf camera-view (make-instance 'view)) (o! camera-actor (add-component camera-view)) - (attach-actor-to-world test-actor test-scene) - (attach-actor-to-world test-actor-2 test-scene) - (attach-actor-to-world camera-actor test-scene) + ;;(sb-ext:gc) + (initialize-actors-in test-scene + test-actor test-actor-2 camera-actor) test-scene)) diff --git a/wh-engine/package.lisp b/wh-engine/package.lisp index 4af7643..856defb 100644 --- a/wh-engine/package.lisp +++ b/wh-engine/package.lisp @@ -15,7 +15,7 @@ make-id fixed-id *running-scenes* add-scene remove-scene get-scene update-all-scenes - attach-actor-to-world + initialize-actors-in *view-width* *view-height* *view-ppu* *pixel-scale* register-test-scene run diff --git a/wh-engine/render/view.lisp b/wh-engine/render/view.lisp index de88d62..1b7a621 100644 --- a/wh-engine/render/view.lisp +++ b/wh-engine/render/view.lisp @@ -67,13 +67,13 @@ (setf (o! this :slot renderbuffer) (gl:gen-renderbuffer)) (gl:bind-renderbuffer :renderbuffer (o! this renderbuffer)) (gl:renderbuffer-storage :renderbuffer :depth24-stencil8 *view-width* *view-height*) - (gl:bind-renderbuffer 0) + (gl:bind-renderbuffer :renderbuffer 0) ;; create framebuffer (setf (o! this :slot framebuffer) (gl:gen-framebuffer)) (gl:bind-framebuffer :framebuffer (o! this framebuffer)) (gl:framebuffer-texture-2d :framebuffer :color-attachment0 :texture-2d (o! this render-texture) 0) (gl:framebuffer-renderbuffer :framebuffer :depth-stencil-attachment :renderbuffer (o! this renderbuffer)) - (gl:bind-framebuffer 0) + (gl:bind-framebuffer :framebuffer 0) )) (defmethod activate :after ((this view) &key) diff --git a/wh-engine/scene.lisp b/wh-engine/scene.lisp index a156b09..6187c5f 100644 --- a/wh-engine/scene.lisp +++ b/wh-engine/scene.lisp @@ -27,15 +27,14 @@ (defmethod print-object ((this scene) stream) (print-unreadable-object (this stream :type t :identity t) - (prin1 (o! this :slot id) stream) - (princ " ") - (prin1 (o! this :slot name) stream))) + (format stream "~D ~S" + (o! this :slot id) (o! this :slot name)))) (defmethod add-actor ((this scene) actor) "Add an actor to this scene." (when (o! actor scene) (error "~S is already in scene ~S" actor (o! actor scene))) - (push actor (o! this :slot actors)) + (pushnew actor (o! this :slot actors)) (setf (o! actor :slot scene) (make-weak-pointer this)) actor) @@ -74,12 +73,15 @@ (defmethod resume ((this scene)) "Initialize or restore this scene's state." - ; Restore actors + ;; Restore actors (loop for actor in (o! this actors) - do (o! actor (resume)))) + unless (o! actor :slot parent) + do (format t "===> scene resume: ~S~%" actor) + (o! actor (resume)))) (defmethod suspend ((this scene)) "Prepare this scene for serialization." - ; Suspend actors + ;; Suspend actors (loop for actor in (o! this actors) - do (o! actor (suspend)))) + unless (o! actor :slot parent) + do (o! actor (suspend))))