From: Javier Sancho Date: Mon, 13 Jan 2014 05:51:52 +0000 (+0100) Subject: Do step duration properly X-Git-Url: https://git.jsancho.org/?a=commitdiff_plain;h=8864bc71fcf9fd551bdb417a1b22655a9097dd30;p=gacela.git Do step duration properly * src/engine.scm: use step start time and end time for calculating delay and step duration * src/misc.scm: new function 'current-utime' that gets current time in microseconds --- diff --git a/src/engine.scm b/src/engine.scm index 2e3cbd3..3d05c9c 100644 --- a/src/engine.scm +++ b/src/engine.scm @@ -16,6 +16,7 @@ (define-module (gacela engine) + #:use-module (gacela misc) #:use-module (gacela system) #:use-module (ice-9 receive) #:use-module (ice-9 threads) @@ -56,10 +57,10 @@ ;;; Engine Inner Properties -(define (default-delay) 0.1) +(define (default-step) 0.1) (define (default-engine-inner-properties) - `(engine-inner-properties (delay . ,(default-delay)))) + `(engine-inner-properties (step . ,(default-step)))) ;;; Engine definitions @@ -175,12 +176,15 @@ (cond ((not (engine-running? engine)) (with-mutex (engine-running-mutex engine) (let loop () - (let ((delay 0)) + (let ((t (current-utime)) + (delay 0)) (with-engine engine (receive (e c) ((apply (engine-system engine) (engine-entities engine))) (set-engine-entities! engine (list e c))) - (set! delay (get-property '(engine-inner-properties delay)))) - (usleep (inexact->exact (* delay 1000000)))) + (set! delay (- (inexact->exact (* (get-property '(engine-inner-properties step)) 1000000)) + (- (current-utime) t)))) + (cond ((> delay 0) + (usleep delay)))) (if (not (engine-stopping? engine #:clean #t)) (loop))))))) diff --git a/src/misc.scm b/src/misc.scm new file mode 100644 index 0000000..66bcf6f --- /dev/null +++ b/src/misc.scm @@ -0,0 +1,25 @@ +;;; Gacela, a GNU Guile extension for fast games development +;;; Copyright (C) 2014 by Javier Sancho Fernandez +;;; +;;; This program is free software: you can redistribute it and/or modify +;;; it under the terms of the GNU General Public License as published by +;;; the Free Software Foundation, either version 3 of the License, or +;;; (at your option) any later version. +;;; +;;; This program is distributed in the hope that it will be useful, +;;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;;; GNU General Public License for more details. +;;; +;;; You should have received a copy of the GNU General Public License +;;; along with this program. If not, see . + + +(define-module (gacela misc)) + + +(define (current-utime) + (let ((t (gettimeofday))) + (+ (* (car t) 1000000) (cdr t)))) + +(export current-utime)