stoe/src/modules.lisp
Renaud Casenave-Péré 9faa777972 Rewrite modules functions as macro
This let us recompile initialize/finalize/update functions during
runtime without having to register them once again.
2014-11-14 21:45:54 +09:00

58 lines
1.8 KiB
Common Lisp

#|
This file is a part of stoe project.
Copyright (c) 2014 Renaud Casenave-Péré (renaud@casenave-pere.fr)
|#
(in-package :cl-user)
(defpackage stoe.modules
(:nicknames :modules)
(:use :cl
:utils)
(:export :initialize :finalize :update
:defmodule)
(:import-from :alexandria
:once-only))
(in-package :stoe.modules)
(defparameter *initialize-hook* nil
"Hook run on initialization.
Functions attached to this hook should expect an optional argument containing
the program argv.")
(defparameter *finalize-hook* nil
"Hook run on finalization.")
(defparameter *update-hook* nil
"Hook run each frame.
Functions attached to this hook should expect an argument containing the time
since last frame.")
(defmacro initialize (&optional argv)
"Perform the engine and subsystems initialization process."
`(progn
(format t "Initialize...~%")
,@(loop for fun in *initialize-hook*
collect (list fun argv))))
(defmacro finalize ()
"Perform the engine and subsystems finalization process."
`(progn
(format t "Finalize...~%")
,@(loop for fun in *finalize-hook*
collect (list fun))))
(defmacro update (delta-time)
"Update the modules each loop."
`(progn
,@(loop for fun in *update-hook*
collect (list fun delta-time))))
(defmacro defmodule (module)
"Register a new module.
The module is expected to have at least `initialize', `update', and `finalize' functions.
`initialize' accepts an optional `argv' argument,
`update' accepts a delta-time argument."
`(progn
(setf *initialize-hook* (append *initialize-hook* (list (intern "INITIALIZE" ',module))))
(push (intern "FINALIZE" ',module) *finalize-hook*)
(setf *update-hook* (append *update-hook* (list (intern "UPDATE" ',module))))))