initial commit
[shapestoy] / libs / psys / pstrack.c
1 /*
2 libpsys - reusable particle system library.
3 Copyright (C) 2011-2018  John Tsiombikas <nuclear@member.fsf.org>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18 #include "pstrack.h"
19
20 int psys_init_track(struct psys_track *track)
21 {
22         track->cache_tm = ANM_TIME_INVAL;
23
24         if(anm_init_track(&track->trk) == -1) {
25                 return -1;
26         }
27         return 0;
28 }
29
30 void psys_destroy_track(struct psys_track *track)
31 {
32         anm_destroy_track(&track->trk);
33 }
34
35 int psys_init_track3(struct psys_track3 *track)
36 {
37         track->cache_tm = ANM_TIME_INVAL;
38
39         if(anm_init_track(&track->x) == -1) {
40                 return -1;
41         }
42         if(anm_init_track(&track->y) == -1) {
43                 anm_destroy_track(&track->x);
44                 return -1;
45         }
46         if(anm_init_track(&track->z) == -1) {
47                 anm_destroy_track(&track->x);
48                 anm_destroy_track(&track->z);
49                 return -1;
50         }
51         return 0;
52 }
53
54 void psys_destroy_track3(struct psys_track3 *track)
55 {
56         anm_destroy_track(&track->x);
57         anm_destroy_track(&track->y);
58         anm_destroy_track(&track->z);
59 }
60
61 void psys_copy_track(struct psys_track *dest, const struct psys_track *src)
62 {
63         anm_copy_track(&dest->trk, &src->trk);
64         dest->cache_tm = ANM_TIME_INVAL;
65 }
66
67 void psys_copy_track3(struct psys_track3 *dest, const struct psys_track3 *src)
68 {
69         anm_copy_track(&dest->x, &src->x);
70         anm_copy_track(&dest->y, &src->y);
71         anm_copy_track(&dest->z, &src->z);
72
73         dest->cache_tm = ANM_TIME_INVAL;
74 }
75
76 void psys_eval_track(struct psys_track *track, anm_time_t tm)
77 {
78         if(track->cache_tm != tm) {
79                 track->cache_tm = tm;
80                 track->cache_val = anm_get_value(&track->trk, tm);
81         }
82 }
83
84 void psys_set_value(struct psys_track *track, anm_time_t tm, float v)
85 {
86         anm_set_value(&track->trk, tm, v);
87         track->cache_tm = ANM_TIME_INVAL;
88 }
89
90 float psys_get_value(struct psys_track *track, anm_time_t tm)
91 {
92         psys_eval_track(track, tm);
93         return track->cache_val;
94 }
95
96 float psys_get_cur_value(struct psys_track *track)
97 {
98         return track->cache_val;
99 }
100
101
102 void psys_eval_track3(struct psys_track3 *track, anm_time_t tm)
103 {
104         if(track->cache_tm != tm) {
105                 track->cache_tm = tm;
106                 track->cache_vec[0] = anm_get_value(&track->x, tm);
107                 track->cache_vec[1] = anm_get_value(&track->y, tm);
108                 track->cache_vec[2] = anm_get_value(&track->z, tm);
109         }
110 }
111
112 void psys_set_value3(struct psys_track3 *track, anm_time_t tm, float x, float y, float z)
113 {
114         anm_set_value(&track->x, tm, x);
115         anm_set_value(&track->y, tm, y);
116         anm_set_value(&track->z, tm, z);
117         track->cache_tm = ANM_TIME_INVAL;
118 }
119
120 float *psys_get_value3(struct psys_track3 *track, anm_time_t tm, float *vec)
121 {
122         psys_eval_track3(track, tm);
123         if(vec) {
124                 vec[0] = track->cache_vec[0];
125                 vec[1] = track->cache_vec[1];
126                 vec[2] = track->cache_vec[2];
127         }
128         return track->cache_vec;
129 }
130
131 float *psys_get_cur_value3(struct psys_track3 *track, float *vec)
132 {
133         if(vec) {
134                 vec[0] = track->cache_vec[0];
135                 vec[1] = track->cache_vec[1];
136                 vec[2] = track->cache_vec[2];
137         }
138         return track->cache_vec;
139 }