diff options
author | Jordi Vilalta Prat | 2009-02-15 06:10:59 +0000 |
---|---|---|
committer | Jordi Vilalta Prat | 2009-02-15 06:10:59 +0000 |
commit | fa6e10e9cec163845aa29e7940c86e9c9ab8a2bc (patch) | |
tree | ce87338830cc8c149e1de545246bcefe4f45da00 /engines/sci/sfx/time.c | |
parent | 7c148ddf021c990fa866b7600f979aac9a5b26c9 (diff) | |
download | scummvm-rg350-fa6e10e9cec163845aa29e7940c86e9c9ab8a2bc.tar.gz scummvm-rg350-fa6e10e9cec163845aa29e7940c86e9c9ab8a2bc.tar.bz2 scummvm-rg350-fa6e10e9cec163845aa29e7940c86e9c9ab8a2bc.zip |
Import the SCI engine sources from the FreeSCI Glutton branch (it doesn't compile yet)
svn-id: r38192
Diffstat (limited to 'engines/sci/sfx/time.c')
-rw-r--r-- | engines/sci/sfx/time.c | 131 |
1 files changed, 131 insertions, 0 deletions
diff --git a/engines/sci/sfx/time.c b/engines/sci/sfx/time.c new file mode 100644 index 0000000000..553eb406f8 --- /dev/null +++ b/engines/sci/sfx/time.c @@ -0,0 +1,131 @@ +/*************************************************************************** + time.c Copyright (C) 2003 Christoph Reichenbach + + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public Licence as + published by the Free Software Foundaton; either version 2 of the + Licence, or (at your option) any later version. + + It is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + merchantibility or fitness for a particular purpose. See the + GNU General Public Licence for more details. + + You should have received a copy of the GNU General Public Licence + along with this program; see the file COPYING. If not, write to + the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. + + + Please contact the maintainer for any program-related bug reports or + inquiries. + + Current Maintainer: + + Christoph Reichenbach (CR) <jameson@linuxgames.com> + +***************************************************************************/ + +#include <sfx_time.h> +#include <resource.h> + +sfx_timestamp_t +sfx_new_timestamp(long secs, long usecs, int frame_rate) +{ + sfx_timestamp_t r; + r.secs = secs; + r.usecs = usecs; + r.frame_rate = frame_rate; + r.frame_offset = 0; + + return r; +} + + +sfx_timestamp_t +sfx_timestamp_add(sfx_timestamp_t timestamp, int frames) +{ + timestamp.frame_offset += frames; + + if (timestamp.frame_offset < 0) { + int secsub = 1 + (-timestamp.frame_offset / timestamp.frame_rate); + + timestamp.frame_offset += timestamp.frame_rate * secsub; + timestamp.secs -= secsub; + } + + timestamp.secs += (timestamp.frame_offset / timestamp.frame_rate); + timestamp.frame_offset %= timestamp.frame_rate; + + return timestamp; +} + +int +sfx_timestamp_frame_diff(sfx_timestamp_t a, sfx_timestamp_t b) +{ + long usecdelta = 0; + + if (a.frame_rate != b.frame_rate) { + fprintf(stderr, "Fatal: The semantics of subtracting two timestamps with a different base from each other is not defined!\n"); + BREAKPOINT(); + } + + if (a.usecs != b.usecs) { +#if (SIZEOF_LONG >= 8) + usecdelta = (a.usecs * a.frame_rate) / 1000000 + - (b.usecs * b.frame_rate) / 1000000; +#else + usecdelta = ((a.usecs/1000) * a.frame_rate) / 1000 + - ((b.usecs/1000) * b.frame_rate) / 1000; +#endif + } + + return usecdelta + + (a.secs - b.secs) * a.frame_rate + + a.frame_offset - b.frame_offset; +} + +long +sfx_timestamp_usecs_diff(sfx_timestamp_t t1, sfx_timestamp_t t2) +{ + long secs1, secs2; + long usecs1, usecs2; + + sfx_timestamp_gettime(&t1, &secs1, &usecs1); + sfx_timestamp_gettime(&t2, &secs2, &usecs2); + + return (usecs1 - usecs2) + ((secs1 - secs2) * 1000000); +} + +sfx_timestamp_t +sfx_timestamp_renormalise(sfx_timestamp_t timestamp, int new_freq) +{ + sfx_timestamp_t r; + sfx_timestamp_gettime(×tamp, &r.secs, &r.usecs); + r.frame_rate = new_freq; + r.frame_offset = 0; + + return r; +} + +void +sfx_timestamp_gettime(sfx_timestamp_t *timestamp, long *secs, long *usecs) +{ + long ust = timestamp->usecs; + /* On 64 bit machines, we can do an accurate computation */ +#if (SIZEOF_LONG >= 8) + ust += (timestamp->frame_offset * 1000000l) / (timestamp->frame_rate); +#else + ust += (timestamp->frame_offset * 1000l) / (timestamp->frame_rate / 1000l); +#endif + + if (ust > 1000000) { + ust -= 1000000; + *secs = timestamp->secs + 1; + } else + *secs = timestamp->secs; + + *usecs = ust; +} + |