added cgmath, libanim, and libpsys
[andemo] / libs / psys / psys.h
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 #ifndef LIBPSYS_H_
19 #define LIBPSYS_H_
20
21 #include <anim/anim.h>
22 #include "rndval.h"
23 #include "pattr.h"
24
25 struct psys_particle;
26 struct psys_emitter;
27
28 typedef int (*psys_spawn_func_t)(struct psys_emitter*, struct psys_particle*, void*);
29 typedef void (*psys_update_func_t)(struct psys_emitter*, struct psys_particle*, long, float, void*);
30
31 typedef void (*psys_draw_func_t)(const struct psys_emitter*, const struct psys_particle*, void*);
32 typedef void (*psys_draw_start_func_t)(const struct psys_emitter*, void*);
33 typedef void (*psys_draw_end_func_t)(const struct psys_emitter*, void*);
34
35
36 struct psys_plane {
37         float nx, ny, nz, d;
38         float elasticity;
39         struct psys_plane *next;
40 };
41
42
43 struct psys_emitter {
44         struct anm_node prs;
45         float cur_pos[3];
46
47         struct psys_attributes attr;
48
49         /* list of active particles */
50         struct psys_particle *plist;
51         int pcount;     /* number of active particles */
52
53         /* list of collision planes */
54         struct psys_plane *planes;
55
56         /* custom spawn closure */
57         void *spawn_cls;
58         psys_spawn_func_t spawn;
59
60         /* custom particle update closure */
61         void *upd_cls;
62         psys_update_func_t update;
63
64         /* custom draw closure */
65         void *draw_cls;
66         psys_draw_func_t draw;
67         psys_draw_start_func_t draw_start;
68         psys_draw_end_func_t draw_end;
69
70         long spawn_acc;         /* partial spawn accumulator */
71         long last_update;       /* last update time (to calc dt) */
72 };
73
74
75 struct psys_particle {
76         float x, y, z;
77         float vx, vy, vz;
78         float life, max_life;
79         float base_size;
80
81         struct psys_particle_attributes *pattr;
82
83         /* current particle attr values calculated during update */
84         float r, g, b;
85         float alpha, size;
86
87         struct psys_particle *next;
88 };
89
90 #ifdef __cplusplus
91 extern "C" {
92 #endif
93
94 struct psys_emitter *psys_create(void);
95 void psys_free(struct psys_emitter *em);
96
97 int psys_init(struct psys_emitter *em);
98 void psys_destroy(struct psys_emitter *em);
99
100 /* set properties */
101
102 /* set emitter position. pos should point to 3 floats (xyz) */
103 void psys_set_pos(struct psys_emitter *em, const float *pos, long tm);
104 void psys_set_pos3f(struct psys_emitter *em, float x, float y, float z, long tm);
105 /* set emitter rotation quaternion. qrot should point to 4 floats (xyzw) */
106 void psys_set_rot(struct psys_emitter *em, const float *qrot, long tm);
107 void psys_set_rot4f(struct psys_emitter *em, float x, float y, float z, float w, long tm);
108 /* set emitter rotation by defining a rotation axis, and an angle */
109 void psys_set_rot_axis(struct psys_emitter *em, float angle, float x, float y, float z, long tm);
110 /* set rotation pivot point. pos should point to 3 floats (xyz) */
111 void psys_set_pivot(struct psys_emitter *em, const float *pivot);
112 void psys_set_pivot3f(struct psys_emitter *em, float x, float y, float z);
113
114 /* pos should be a pointer to 3 floats (xyz) */
115 void psys_get_pos(struct psys_emitter *em, float *pos, long tm);
116 /* qrot should be a pointer to 4 floats (xyzw) */
117 void psys_get_rot(struct psys_emitter *em, float *qrot, long tm);
118 /* pivot should be a pointer to 3 floats (xyz) */
119 void psys_get_pivot(struct psys_emitter *em, float *pivot);
120
121 void psys_clear_collision_planes(struct psys_emitter *em);
122 int psys_add_collision_plane(struct psys_emitter *em, const float *plane, float elast);
123
124 void psys_add_particle(struct psys_emitter *em, struct psys_particle *p);
125
126 void psys_spawn_func(struct psys_emitter *em, psys_spawn_func_t func, void *cls);
127 void psys_update_func(struct psys_emitter *em, psys_update_func_t func, void *cls);
128 void psys_draw_func(struct psys_emitter *em, psys_draw_func_t draw,
129                 psys_draw_start_func_t start, psys_draw_end_func_t end, void *cls);
130
131 /* update and render */
132
133 void psys_update(struct psys_emitter *em, long tm);
134 void psys_draw(const struct psys_emitter *em);
135
136 #ifdef __cplusplus
137 }
138 #endif
139
140 #endif  /* LIBPSYS_H_ */