added cgmath, libanim, and libpsys
[andemo] / libs / psys / rndval.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 <stdlib.h>
19 #include "rndval.h"
20
21 int psys_init_anm_rnd(struct psys_anm_rnd *r)
22 {
23         if(psys_init_track(&r->value) == -1) {
24                 return -1;
25         }
26         if(psys_init_track(&r->range) == -1) {
27                 psys_destroy_track(&r->value);
28                 return -1;
29         }
30         return 0;
31 }
32
33 void psys_destroy_anm_rnd(struct psys_anm_rnd *r)
34 {
35         psys_destroy_track(&r->value);
36         psys_destroy_track(&r->range);
37 }
38
39 int psys_init_anm_rnd3(struct psys_anm_rnd3 *r)
40 {
41         if(psys_init_track3(&r->value) == -1) {
42                 return -1;
43         }
44         if(psys_init_track3(&r->range) == -1) {
45                 psys_destroy_track3(&r->value);
46                 return -1;
47         }
48         return 0;
49 }
50
51 void psys_destroy_anm_rnd3(struct psys_anm_rnd3 *r)
52 {
53         psys_destroy_track3(&r->value);
54         psys_destroy_track3(&r->range);
55 }
56
57 void psys_copy_anm_rnd(struct psys_anm_rnd *dest, const struct psys_anm_rnd *src)
58 {
59         psys_copy_track(&dest->value, &src->value);
60         psys_copy_track(&dest->range, &src->range);
61 }
62
63 void psys_copy_anm_rnd3(struct psys_anm_rnd3 *dest, const struct psys_anm_rnd3 *src)
64 {
65         psys_copy_track3(&dest->value, &src->value);
66         psys_copy_track3(&dest->range, &src->range);
67 }
68
69 void psys_set_rnd(struct psys_rnd *r, float val, float range)
70 {
71         r->value = val;
72         r->range = range;
73 }
74
75 void psys_set_rnd3(struct psys_rnd3 *r, const float *val, const float *range)
76 {
77         int i;
78         for(i=0; i<3; i++) {
79                 r->value[i] = val[i];
80                 r->range[i] = range[i];
81         }
82 }
83
84 void psys_set_anm_rnd(struct psys_anm_rnd *r, anm_time_t tm, float val, float range)
85 {
86         psys_set_value(&r->value, tm, val);
87         psys_set_value(&r->range, tm, range);
88 }
89
90 void psys_set_anm_rnd3(struct psys_anm_rnd3 *r, anm_time_t tm, const float *val, const float *range)
91 {
92         psys_set_value3(&r->value, tm, val[0], val[1], val[2]);
93         psys_set_value3(&r->range, tm, range[0], range[1], range[2]);
94 }
95
96
97 float psys_eval_rnd(struct psys_rnd *r)
98 {
99         return r->value + r->range * (float)rand() / (float)RAND_MAX - 0.5 * r->range;
100 }
101
102 void psys_eval_rnd3(struct psys_rnd3 *r, float *val)
103 {
104         val[0] = r->value[0] + r->range[0] * (float)rand() / (float)RAND_MAX - 0.5 * r->range[0];
105         val[1] = r->value[1] + r->range[1] * (float)rand() / (float)RAND_MAX - 0.5 * r->range[1];
106         val[2] = r->value[2] + r->range[2] * (float)rand() / (float)RAND_MAX - 0.5 * r->range[2];
107 }
108
109
110 float psys_eval_anm_rnd(struct psys_anm_rnd *r, anm_time_t tm)
111 {
112         struct psys_rnd tmp;
113         if(tm == ANM_TIME_INVAL) {
114                 tmp.value = psys_get_cur_value(&r->value);
115                 tmp.range = psys_get_cur_value(&r->range);
116         } else {
117                 tmp.value = psys_get_value(&r->value, tm);
118                 tmp.range = psys_get_value(&r->range, tm);
119         }
120         return psys_eval_rnd(&tmp);
121 }
122
123 void psys_eval_anm_rnd3(struct psys_anm_rnd3 *r, anm_time_t tm, float *val)
124 {
125         struct psys_rnd3 tmp;
126         if(tm == ANM_TIME_INVAL) {
127                 psys_get_cur_value3(&r->value, tmp.value);
128                 psys_get_cur_value3(&r->range, tmp.range);
129         } else {
130                 psys_get_value3(&r->value, tm, tmp.value);
131                 psys_get_value3(&r->range, tm, tmp.range);
132         }
133         psys_eval_rnd3(&tmp, val);
134 }