+
+
+;;; Engine execution
+
+(define (start-engine engine)
+ (cond ((not (engine-running? engine))
+ (with-mutex (engine-running-mutex engine)
+ (let loop ()
+ (let ((delay 0))
+ (with-engine engine
+ (receive (e c) ((apply (engine-system engine) (engine-entities engine)))
+ (set-engine-entities! engine (list e c)))
+ (set! delay (engine-inner-properties-delay (get-component 'engine-inner-properties (car (get-entities-by-components '(engine-inner-properties)))))))
+ (usleep (inexact->exact (* delay 1000000))))
+ (if (not (engine-stopping? engine #:clean #t))
+ (loop)))))))
+
+(define (stop-engine engine)
+ (with-engine engine
+ (new-entity! '(engine-halt . #t)))
+ 'engine-halt)
+
+(define* (engine-stopping? engine #:key (clean #f))
+ (let ((halt #f))
+ (with-engine engine
+ (let halt-engine ((halts (get-entities-by-components '(engine-halt))))
+ (cond ((not (null? halts))
+ (set! halt #t)
+ (cond (clean
+ (remove-entity! (caar halts))
+ (halt-engine (cdr halts))))))))
+ halt))
+
+(export start-engine
+ stop-engine
+ engine-stopping?)