Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
n_particles.c
Go to the documentation of this file.
1
9#include "math.h"
10
21int init_particle_system(PARTICLE_SYSTEM** psys, int max, double x, double y, double z, int max_sprites) {
22 __n_assert(!(*psys), n_log(LOG_ERR, "particle system %p already initialized", (*psys)); return FALSE);
23
24 Malloc((*psys), PARTICLE_SYSTEM, 1);
25
26 start_HiTimer(&(*psys)->timer);
27
28 (*psys)->list = new_generic_list(max);
29
30 (*psys)->source[0] = x;
31 (*psys)->source[1] = y;
32 (*psys)->source[2] = z;
33
34 if (max_sprites > 0)
35 (*psys)->sprites = (ALLEGRO_BITMAP**)calloc(max_sprites, sizeof(ALLEGRO_BITMAP*));
36 else
37 (*psys)->sprites = NULL;
38
39 return TRUE;
40} /* init_particle_system() */
41
53int add_particle(PARTICLE_SYSTEM* psys, int spr, int mode, int lifetime, int size, ALLEGRO_COLOR color, PHYSICS object) {
54 int it = 0;
55
56 PARTICLE* new_p = NULL;
57
58 if (psys->list->nb_items == psys->list->nb_max_items)
59 return FALSE;
60
61 for (it = 0; it < 3; it++) {
62 object.position[it] += psys->source[it];
63 }
64
65 Malloc(new_p, PARTICLE, 1);
66 __n_assert(new_p, return FALSE);
67
68 new_p->spr_id = spr;
69 new_p->mode = mode;
70 new_p->lifetime = lifetime;
71 new_p->color = color;
72 new_p->size = size;
73
74 memcpy(&new_p->object, &object, sizeof(PHYSICS));
75
76 return list_push(psys->list, new_p, &free);
77} /* add_particle() */
78
97int add_particle_ex(PARTICLE_SYSTEM* psys, int spr, int mode, int off_x, int off_y, int lifetime, int size, ALLEGRO_COLOR color, double vx, double vy, double vz, double ax, double ay, double az) {
98 PHYSICS object;
99 VECTOR3D_SET(object.position, off_x, off_y, 0.0);
100 VECTOR3D_SET(object.speed, vx, vy, vz);
101 VECTOR3D_SET(object.acceleration, ax, ay, az);
102 VECTOR3D_SET(object.orientation, 0.0, 0.0, 0.0);
103 VECTOR3D_SET(object.angular_speed, 0.0, 0.0, 0.0);
104 VECTOR3D_SET(object.angular_acceleration, 0.0, 0.0, 0.0);
105
106 return add_particle(psys, spr, mode, lifetime, size, color, object);
107} /* add_particle_ex () */
108
115int manage_particle_ex(PARTICLE_SYSTEM* psys, double delta_t) {
116 __n_assert(psys, return FALSE);
117
118 LIST_NODE* node = NULL;
119 PARTICLE* ptr = NULL;
120
121 node = psys->list->start;
122
123 while (node) {
124 ptr = (PARTICLE*)node->ptr;
125 if (ptr->lifetime != -1) {
126 ptr->lifetime -= delta_t / 1000.0;
127 }
128
129 if (ptr->lifetime > 0 || ptr->lifetime == -1) {
130 update_physics_position(&ptr->object, delta_t);
131 node = node->next;
132 } else {
133 LIST_NODE* node_to_kill = node;
134 node = node->next;
135 ptr = remove_list_node(psys->list, node_to_kill, PARTICLE);
136 Free(ptr);
137 }
138 }
139
140 return TRUE;
141} /* manage_particle_ex() */
142
149 __n_assert(psys, return FALSE);
150
151 double delta_t = get_usec(&psys->timer);
152 return manage_particle_ex(psys, delta_t);
153} /* manage_particle() */
154
165int draw_particle(PARTICLE_SYSTEM* psys, double xpos, double ypos, int w, int h, double range) {
166 __n_assert(psys, return FALSE);
167
168 LIST_NODE* node = NULL;
169 PARTICLE* ptr = NULL;
170
171 node = psys->list->start;
172
173 while (node) {
174 double x = 0, y = 0;
175
176 ptr = (PARTICLE*)node->ptr;
177 x = ptr->object.position[0] - xpos;
178 y = ptr->object.position[1] - ypos;
179
180 if ((x < -range) || (x > (w + range)) || (y < -range) || (y > (h + range))) {
181 node = node->next;
182 continue;
183 }
184
185 for (int it = 0; it < 3; it++) {
186 while (ptr->object.orientation[it] < 0.0)
187 ptr->object.orientation[it] += 256.0;
188
189 if (ptr->object.orientation[it] >= 256.0)
190 ptr->object.orientation[it] = fmod(ptr->object.orientation[it], 256.0);
191 }
192
193 if (ptr->mode == SINUS_PART) {
194 if (ptr->object.speed[0] != 0)
195 x = x + ptr->object.speed[0] * sin((ptr->object.position[0] / ptr->object.speed[0]));
196 else
197 x = x + ptr->object.speed[0] * sin(ptr->object.position[0]);
198
199 if (ptr->object.speed[1] != 0)
200 y = y + ptr->object.speed[1] * cos((ptr->object.speed[1] / ptr->object.speed[1]));
201 else
202 y = y + ptr->object.speed[1] * sin(ptr->object.position[1]);
203
204 if (ptr->spr_id >= 0 && ptr->spr_id < psys->max_sprites && psys->sprites[ptr->spr_id]) {
205 int spr_w = al_get_bitmap_width(psys->sprites[ptr->spr_id]);
206 int spr_h = al_get_bitmap_height(psys->sprites[ptr->spr_id]);
207
208 al_draw_rotated_bitmap(psys->sprites[ptr->spr_id], spr_w / 2, spr_h / 2, x - spr_w / 2, y - spr_h / 2, al_ftofix(ptr->object.orientation[2]), 0);
209 } else
210 al_draw_circle(x, y, ptr->size, ptr->color, 1);
211 }
212
213 if (ptr->mode & NORMAL_PART) {
214 if (ptr->spr_id >= 0 && ptr->spr_id < psys->max_sprites && psys->sprites[ptr->spr_id]) {
215 int bmp_w = al_get_bitmap_width(psys->sprites[ptr->spr_id]);
216 int bmp_h = al_get_bitmap_height(psys->sprites[ptr->spr_id]);
217
218 al_draw_rotated_bitmap(psys->sprites[ptr->spr_id], bmp_w / 2, bmp_h / 2, x - bmp_w / 2, y - bmp_h / 2, al_ftofix(ptr->object.orientation[2]), 0);
219 } else
220 al_draw_circle(x, y, ptr->size, ptr->color, 1);
221 } else if (ptr->mode & PIXEL_PART) {
222 al_draw_filled_rectangle(x - ptr->size, y - ptr->size, x + ptr->size, y + ptr->size, ptr->color);
223 } else
224 al_draw_circle(x, y, ptr->size, ptr->color, 1);
225 node = node->next;
226 }
227
228 return TRUE;
229} /* draw_particle() */
230
237 __n_assert((*psys), return FALSE);
238
239 PARTICLE* particle = NULL;
240 while ((*psys)->list->start) {
241 particle = remove_list_node((*psys)->list, (*psys)->list->start, PARTICLE);
242 Free(particle);
243 }
244 Free((*psys));
245
246 return TRUE;
247} /* free_particle_system() */
248
257int move_particles(PARTICLE_SYSTEM* psys, double vx, double vy, double vz) {
258 __n_assert(psys, return FALSE);
259
260 LIST_NODE* node = NULL;
261 PARTICLE* ptr = NULL;
262
263 node = psys->list->start;
264
265 while (node) {
266 ptr = (PARTICLE*)node->ptr;
267 ptr->object.position[0] = ptr->object.position[0] + vx;
268 ptr->object.position[1] = ptr->object.position[1] + vy;
269 ptr->object.position[2] = ptr->object.position[2] + vz;
270 node = node->next;
271 }
272 return TRUE;
273}
#define Malloc(__ptr, __struct, __size)
Malloc Handler to get errors and set to 0.
Definition n_common.h:187
#define __n_assert(__ptr, __ret)
macro to assert things
Definition n_common.h:258
#define Free(__ptr)
Free Handler to get errors.
Definition n_common.h:242
void * ptr
void pointer to store
Definition n_list.h:26
size_t nb_max_items
maximum number of item in the list.
Definition n_list.h:43
LIST_NODE * start
pointer to the start of the list
Definition n_list.h:46
size_t nb_items
number of item currently in the list
Definition n_list.h:41
struct LIST_NODE * next
pointer to the next node
Definition n_list.h:32
int list_push(LIST *list, void *ptr, void(*destructor)(void *ptr))
Add a pointer to the end of the list.
Definition n_list.c:199
#define remove_list_node(__LIST_, __NODE_, __TYPE_)
Remove macro helper for void pointer casting.
Definition n_list.h:76
Structure of a generic list node.
Definition n_list.h:24
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition n_log.h:69
#define LOG_ERR
error conditions
Definition n_log.h:56
int start_HiTimer(N_TIME *timer)
Initialize or restart from zero any N_TIME HiTimer.
Definition n_time.c:67
time_t get_usec(N_TIME *timer)
Poll any N_TIME HiTimer, returning usec, and moving currentTime to startTime.
Definition n_time.c:89
N_TIME timer
Internal: particle system timer.
Definition n_particles.h:82
int size
size of particle
Definition n_particles.h:63
int max_sprites
size of the picture library
Definition n_particles.h:87
ALLEGRO_COLOR color
color of the particle
Definition n_particles.h:66
PHYSICS object
particle physical properties
Definition n_particles.h:69
int spr_id
sprite id in library
Definition n_particles.h:61
int lifetime
lifetime
Definition n_particles.h:59
VECTOR3D source
Coordinate of emitting point.
Definition n_particles.h:79
LIST * list
list of PARTICLE pointers
Definition n_particles.h:76
int mode
particle mode: NORMAL_PART,SINUS_PART,PIXEL_PART
Definition n_particles.h:57
ALLEGRO_BITMAP ** sprites
Library of picture for the particles.
Definition n_particles.h:85
int draw_particle(PARTICLE_SYSTEM *psys, double xpos, double ypos, int w, int h, double range)
draw particles of a particle system
#define NORMAL_PART
classic moving particle
Definition n_particles.h:34
int move_particles(PARTICLE_SYSTEM *psys, double vx, double vy, double vz)
draw particles of a particle system
#define PIXEL_PART
pixel particle
Definition n_particles.h:48
int manage_particle_ex(PARTICLE_SYSTEM *psys, double delta_t)
update particles positions usting provided delta time
int free_particle_system(PARTICLE_SYSTEM **psys)
destroy and free a particle system
int manage_particle(PARTICLE_SYSTEM *psys)
update particles positions usting particle system internal timer
#define SINUS_PART
sinus based moving particle
Definition n_particles.h:36
int add_particle_ex(PARTICLE_SYSTEM *psys, int spr, int mode, int off_x, int off_y, int lifetime, int size, ALLEGRO_COLOR color, double vx, double vy, double vz, double ax, double ay, double az)
add a particle to a particle system, all in line version (you have to set the PHYSICS object paramete...
Definition n_particles.c:97
int add_particle(PARTICLE_SYSTEM *psys, int spr, int mode, int lifetime, int size, ALLEGRO_COLOR color, PHYSICS object)
add a particle to a particle system
Definition n_particles.c:53
int init_particle_system(PARTICLE_SYSTEM **psys, int max, double x, double y, double z, int max_sprites)
initialize a particle system
Definition n_particles.c:21
Structure of a single particle.
Definition n_particles.h:55
Structure of a particle system.
Definition n_particles.h:74
VECTOR3D speed
vx,vy,vz actual speed
Definition n_3d.h:54
VECTOR3D orientation
ax,ay,az actual rotation position
Definition n_3d.h:58
VECTOR3D position
x,y,z actual position
Definition n_3d.h:52
#define VECTOR3D_SET(VECTOR, X, Y, Z)
helper to set a VECTOR3D position
Definition n_3d.h:42
int update_physics_position(PHYSICS *object, double delta_t)
Update object position, reversed.
Definition n_3d.c:85
structure of the physics of an object
Definition n_3d.h:48
Particles management.