added 3dengfx into the repo, probably not the correct version for this
[summerhack] / src / 3dengfx / src / gfx / timeline.cpp
1 /*
2 This file is part of the graphics core library.
3
4 Copyright (c) 2004, 2005 John Tsiombikas <nuclear@siggraph.org>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21 #include "timeline.hpp"
22
23 template <class T>
24 static inline T wrap(T n, T low, T high) {
25         n -= low;
26         
27         while(n < 0) {
28                 n += high;
29         }
30         if(high) n %= high;
31         
32         return n + low;
33 }
34
35 template <class T>
36 static inline T bounce(T n, T low, T high) {
37         T interval = high - low;
38         T offs = n % interval;
39         
40         if((n / interval) % 2) {
41                 // descent
42                 return high - offs;
43         } else {
44                 return low + offs;
45         }
46 }
47
48
49 unsigned long get_timeline_time(unsigned long time, unsigned long start, unsigned long end, TimelineMode mode) {
50         switch(mode) {
51         case TIME_WRAP:
52                 time = wrap(time, start, end);
53                 break;
54
55         case TIME_BOUNCE:
56                 time = bounce(time, start, end);
57                 break;
58
59         case TIME_CLAMP:
60                 if(time < start) time = start;
61                 if(time > end) time = end;
62                 break;
63
64         case TIME_FREE:
65         default:
66                 break;
67         }
68
69         return time;
70 }
71