#| This file is a part of stoe project. Copyright (c) 2015 Renaud Casenave-Péré (renaud@casenave-pere.fr) |# (uiop:define-package :stoe (:use :cl :maths :core :engine :shader) (:reexport :maths :core :engine :shader) (:export #:main #:quit)) (in-package :stoe) (defvar *argv* nil) (defvar *swank-server-port* 4006) (defconstant +loop-step-time+ 16000.0 "The length of one game loop frame.") (defvar *last-frame-remaining-time* 0.0 "The game loop advance +loop-step-time+ at a time but when the delta time doesn't correspond we need to keep the remaining time.") (defun initialize (&optional argv) "Initialize all the modules passing the optional argv." (initialize-modules argv)) (defun finalize () "Finalize all the modules." (finalize-modules)) (defun update (delta-time) "Update all the modules passing the delta time since the last frame." (update-modules delta-time)) (let (exit-main-loop) (defun main-loop () "Run the protected main-loop. An error will be catched with the possibility to continue." (setf exit-main-loop nil) (let ((clock (make-clock))) (loop until exit-main-loop for remaining-time = 0 then delta-time for delta-time = (clock-delta clock) then (+ (clock-delta clock) remaining-time) do (progn (loop while (> delta-time +loop-step-time+) do (restartable (update +loop-step-time+) (decf delta-time +loop-step-time+))) (update-clock clock))))) (defun quit () "Quit the main loop." (setf exit-main-loop t))) (defun startup-stoe (argv) (initialize argv) (unwind-protect (main-loop) (finalize))) (defun main (&optional argv) "Run the game." (setf *argv* argv) (if (string-equal (thread-name (current-thread)) "repl-thread") (make-thread (lambda () (startup-stoe *argv*)) :name "Main Thread") (progn #+swank (swank:create-server :port *swank-server-port* :dont-close nil) (startup-stoe argv) #+swank (swank:stop-server *swank-server-port*))))