2021-11-25 22:28:50 +00:00
|
|
|
;;;; wh-engine/main.lisp
|
|
|
|
(in-package wh-engine)
|
|
|
|
|
2022-02-22 17:16:02 +00:00
|
|
|
(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)))
|
|
|
|
))
|
2022-02-22 03:17:17 +00:00
|
|
|
|
2022-02-23 23:31:50 +00:00
|
|
|
(defvar *delta-time* 0.0
|
|
|
|
"Time in seconds since the last game tick.")
|
|
|
|
|
2021-11-25 22:28:50 +00:00
|
|
|
(defun register-test-scene ()
|
2022-02-23 23:31:50 +00:00
|
|
|
(let ((test-scene (new! scene :id -1 :name "Test scene")))
|
2021-11-25 22:28:50 +00:00
|
|
|
(add-scene test-scene)
|
2022-02-22 17:16:02 +00:00
|
|
|
(initialize-actors-in test-scene
|
2022-02-23 23:31:50 +00:00
|
|
|
(new! actor
|
|
|
|
:name "Actor"
|
2022-02-25 00:39:53 +00:00
|
|
|
:component (new! whe/render:drawable-test))
|
2022-02-23 23:31:50 +00:00
|
|
|
(new! actor
|
|
|
|
:name "Actor 2"
|
|
|
|
:location (vec2 0.5 0.5)
|
|
|
|
:rotation (coerce (/ pi 4) 'single-float)
|
|
|
|
:z-layer -1
|
2022-02-25 00:39:53 +00:00
|
|
|
:component (new! whe/render:drawable-test :colour (vec4 0 1 0 1))
|
2022-02-23 23:31:50 +00:00
|
|
|
:child (new! actor
|
|
|
|
:name "Child Actor"
|
|
|
|
:location (vec2 0 0.5)
|
|
|
|
:z-layer -2
|
2022-02-25 00:39:53 +00:00
|
|
|
:component (new! whe/render:drawable-test :colour (vec4 0 1 1 1))
|
2022-02-23 23:31:50 +00:00
|
|
|
:child (new! actor
|
|
|
|
:name "Grandchild Actor"
|
|
|
|
:location (vec2 0 1)
|
|
|
|
:scale (vec2 0.25 0.25)
|
|
|
|
:z-layer 1
|
2022-02-25 00:39:53 +00:00
|
|
|
:component (new! whe/render:drawable-test :colour (vec4 1 1 0 1)))))
|
2022-02-23 23:31:50 +00:00
|
|
|
(new! actor
|
|
|
|
:name "Camera"
|
2022-02-25 00:39:53 +00:00
|
|
|
:component (new! whe/render:view)))
|
2021-11-25 22:28:50 +00:00
|
|
|
test-scene))
|
|
|
|
|
2022-02-25 00:39:53 +00:00
|
|
|
(defmacro run ()
|
2021-11-25 22:28:50 +00:00
|
|
|
"Run the main game loop."
|
2022-02-25 00:39:53 +00:00
|
|
|
`(sdl2:with-init (:everything)
|
|
|
|
(format t "wh-engine: using SDL ~D.~D.~D~%"
|
|
|
|
sdl2-ffi:+sdl-major-version+
|
|
|
|
sdl2-ffi:+sdl-minor-version+
|
|
|
|
sdl2-ffi:+sdl-patchlevel+)
|
|
|
|
(finish-output)
|
|
|
|
(sdl2:with-window (win :flags '(:shown :opengl)
|
|
|
|
:w (* whe/render:*view-width* whe/render:*pixel-scale*) :h (* whe/render:*view-height* whe/render:*pixel-scale*)
|
|
|
|
:title (format nil "wh-engine ~1{~D.~D.~D~} (Affero GPL; NON-FREE USAGE PROHIBITED)"
|
|
|
|
+version+))
|
|
|
|
(sdl2:with-gl-context (gl-context win)
|
|
|
|
(sdl2:gl-make-current win gl-context)
|
2022-02-25 00:50:52 +00:00
|
|
|
(let ((prev-tick (sdl2:get-ticks))
|
2022-02-25 00:39:53 +00:00
|
|
|
(this-tick (sdl2:get-ticks))
|
|
|
|
(prev-profiling-tick (sdl2:get-performance-counter))
|
|
|
|
(profiling-scale (/ (sdl2:get-performance-frequency) 1000.0)))
|
2022-02-25 00:50:52 +00:00
|
|
|
|
2022-02-25 00:39:53 +00:00
|
|
|
,@(loop for system in *world-systems*
|
|
|
|
append `((,(second system))))
|
|
|
|
|
|
|
|
(sdl2:with-event-loop (:method :poll)
|
|
|
|
(:quit () t)
|
|
|
|
(:idle ()
|
|
|
|
(setf prev-profiling-tick (sdl2:get-performance-counter))
|
|
|
|
;; calculate delta-time
|
|
|
|
(setf this-tick (sdl2:get-ticks))
|
|
|
|
(setf *delta-time* (* (- this-tick prev-tick) 0.001))
|
|
|
|
(setf prev-tick this-tick)
|
|
|
|
(format t "~%Δt = ~S (~S FPS)~%" *delta-time* (/ 1.0 *delta-time*))
|
|
|
|
;; update
|
|
|
|
(loop for scene in *world*
|
|
|
|
do (o! scene (update)))
|
|
|
|
(format t "game=~S " (/ (- (sdl2:get-performance-counter) prev-profiling-tick) profiling-scale))
|
|
|
|
,@(loop for system in *world-systems*
|
|
|
|
append `((,(third system))
|
|
|
|
(format t ,(format nil "~S~A" (first system) "=~S ")
|
|
|
|
(/ (- (sdl2:get-performance-counter) prev-profiling-tick) profiling-scale))
|
|
|
|
))
|
|
|
|
(sdl2:gl-swap-window win)))
|
|
|
|
)))))
|