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