86 lines
2.7 KiB
Common Lisp
86 lines
2.7 KiB
Common Lisp
#|
|
|
This file is a part of stoe project.
|
|
Copyright (c) 2015 Renaud Casenave-Péré (renaud@casenave-pere.fr)
|
|
|#
|
|
|
|
(uiop:define-package :stoe/game/stoe
|
|
(:use :cl :maths
|
|
:stoe/core/utils :stoe/core/file :stoe/core/modules
|
|
:stoe/engine/render :stoe/engine/mesh)
|
|
(:import-from :stoe/engine/scene
|
|
#:attach)
|
|
(:import-from :stoe/engine/object
|
|
#:make-object #:dir)
|
|
(:import-from :stoe/game/game
|
|
#:get-current-camera
|
|
#:get-world-origin)
|
|
(:import-from :stoe/core/input
|
|
#:global-set-key
|
|
#:global-set-motion))
|
|
(in-package :stoe/game/stoe)
|
|
|
|
(let ((exit-main-loop nil))
|
|
(defun main-loop (&optional unprotected)
|
|
"Run the protected main-loop. An error will be catched with the possibility to
|
|
continue unless `unprotected' is t."
|
|
(setf exit-main-loop nil)
|
|
(let ((clock (make-clock)))
|
|
(update-current-time)
|
|
(loop while (not exit-main-loop)
|
|
do (restartable unprotected
|
|
(update-current-time)
|
|
(update-clock clock (get-delta-time))
|
|
(poll-events)
|
|
(update (clock-delta clock))))))
|
|
|
|
(defun quit ()
|
|
"Quit the main loop."
|
|
(setf exit-main-loop t)))
|
|
|
|
(global-set-key :escape #'quit)
|
|
|
|
(let (freelook-mode
|
|
start-orient
|
|
(start-coords '(0.0 . 0.0)))
|
|
(defun set-freelook (enable)
|
|
(setf freelook-mode enable)
|
|
(setf start-orient (dir (get-current-camera))))
|
|
|
|
(defun freelook-move (x y)
|
|
(if freelook-mode
|
|
(let ((dx (- (car start-coords) x))
|
|
(dy (- (cdr start-coords) y)))
|
|
(setf (dir (get-current-camera)) (q* (quat (vec 0.0 1.0 0.0) (deg-to-rad (- dx)))
|
|
start-orient
|
|
(quat (vec 1.0 0.0 0.0) (deg-to-rad dy)))))
|
|
(setf start-coords (cons x y))))
|
|
|
|
(global-set-key 3 #'set-freelook t)
|
|
(global-set-key (3 :release) #'set-freelook nil)
|
|
(global-set-motion #'freelook-move :x :y))
|
|
|
|
(defun game-start ()
|
|
(let ((f (load-file #P"../data/cube.lisp" :sync t :type 'character)))
|
|
(attach (make-object :mesh (with-input-from-string (s f)
|
|
(make-mesh (read s)))) (get-world-origin))))
|
|
|
|
(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))
|
|
|
|
(defun main (&optional argv)
|
|
"Run the program."
|
|
(initialize argv)
|
|
(unwind-protect
|
|
(progn
|
|
;; (game-start)
|
|
(main-loop))
|
|
(finalize)))
|