From 9a3245784817ded3500dcd55f2551913088442d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Renaud=20Casenave-P=C3=A9r=C3=A9?= Date: Sat, 11 Oct 2014 17:18:13 +0900 Subject: [PATCH] Change add-hook's semantic and export functions to register modules --- src/debug.lisp | 7 +++---- src/jobs.lisp | 6 +++--- src/modules.lisp | 15 +++++++++++++-- src/render/render.lisp | 6 +++--- src/utils.lisp | 6 ++++-- 5 files changed, 26 insertions(+), 14 deletions(-) diff --git a/src/debug.lisp b/src/debug.lisp index 27158b6..657bd78 100644 --- a/src/debug.lisp +++ b/src/debug.lisp @@ -29,9 +29,6 @@ start the swank server to accept remote connection." #+swank (swank:stop-server *swank-server-port*))) -(add-hook modules:*initialize-hook* #'initialize) -(add-hook modules:*finalize-hook* #'finalize) - (let ((time-counter 0.0) (frames-counter 0)) (defun update (delta-time) @@ -48,4 +45,6 @@ start the swank server to accept remote connection." (setf time-counter 0.0) (setf frames-counter 0)))) -(add-hook modules:*update-hook* #'update) +(modules:register-initialize-fun #'initialize) +(modules:register-finalize-fun #'finalize) +(modules:register-update-fun #'update) diff --git a/src/jobs.lisp b/src/jobs.lisp index 5c4f270..6c69a85 100644 --- a/src/jobs.lisp +++ b/src/jobs.lisp @@ -81,9 +81,9 @@ If a thread is available, assign a new job to it." (if (not (thread-alive-p (thread-thread thread))) (finalize-thread thread)))))) -(add-hook modules:*initialize-hook* #'initialize) -(add-hook modules:*finalize-hook* #'finalize) -(add-hook modules:*update-hook* #'update) +(modules:register-initialize-fun #'initialize) +(modules:register-finalize-fun #'finalize) +(modules:register-update-fun #'update) (defun push-job (fun args) "Create a new job using `fun' and `data' and push it into the job-list." diff --git a/src/modules.lisp b/src/modules.lisp index 8188abd..4dde004 100644 --- a/src/modules.lisp +++ b/src/modules.lisp @@ -8,7 +8,7 @@ (:nicknames :modules) (:use :cl :utils) - (:export :*initialize-hook* :*finalize-hook* :*update-hook* + (:export :register-initialize-fun :register-finalize-fun :register-update-fun :initialize :finalize :update)) (in-package :stoe.modules) @@ -21,7 +21,9 @@ the program argv.") "Hook run on finalization.") (defparameter *update-hook* nil - "Hook run each frame.") + "Hook run each frame. +Functions attached to this hook should expect an argument containing the time +since last frame.") (defun initialize (&optional argv) "Perform the engine and subsystems initialization process." @@ -36,3 +38,12 @@ the program argv.") (defun update (delta-time) "Update the modules each loop." (run-hook *update-hook* delta-time)) + +(defun register-initialize-fun (fun) + (add-hook *initialize-hook* fun 'append)) + +(defun register-finalize-fun (fun) + (add-hook *finalize-hook* fun)) + +(defun register-update-fun (fun) + (add-hook *update-hook* fun 'append)) diff --git a/src/render/render.lisp b/src/render/render.lisp index 97881b6..c6ffa80 100644 --- a/src/render/render.lisp +++ b/src/render/render.lisp @@ -49,9 +49,9 @@ Render a frame and swap buffers." (gl:clear :color-buffer-bit :depth-buffer-bit) (glop:swap-buffers *window*)) -(add-hook modules:*initialize-hook* #'initialize) -(add-hook modules:*finalize-hook* #'finalize) -(add-hook modules:*update-hook* #'update) +(modules:register-initialize-fun #'initialize) +(modules:register-finalize-fun #'finalize) +(modules:register-update-fun #'update) (defmethod glop:on-event (window event) (declare (ignore window event))) diff --git a/src/utils.lisp b/src/utils.lisp index 74c80ca..af8145a 100644 --- a/src/utils.lisp +++ b/src/utils.lisp @@ -71,10 +71,12 @@ ,@body) (continue () :report "Continue")))) -(defmacro add-hook (hook fun) +(defmacro add-hook (hook fun &optional append) "Setup `fun' to be called within specified `hook'." `(unless (member ,fun ,hook) - (setf ,hook (append ,hook (list ,fun))))) + ,(if append + `(setf ,hook (append ,hook (list ,fun))) + `(push ,fun ,hook)))) (defmacro remove-hook (hook fun) "Remove `fun' from `hook'."