Nilorea Library

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

All Data Structures Files Functions Variables Typedefs Macros Groups
n_particles.c
Go to the documentation of this file.
1
14 #include "n_particles.h"
15
16
17
18 int init_particle_system( PARTICLE_SYSTEM **psys , int max , double x , double y , double z )
19 {
20 Malloc( (*psys) , PARTICLE_SYSTEM , 1 );
21 if( !(*psys) )
22 return FALSE ;
23
24 (*psys) -> list_start = (*psys) -> list_end = NULL ;
25
26 start_HiTimer( &(*psys) -> timer );
27
28 (*psys) -> max_particles = max ;
29 (*psys) -> nb_particles = 0 ;
30 (*psys) -> x = x ;
31 (*psys) -> y = y ;
32 (*psys) -> z = z ;
33
34 return TRUE ;
35 }
36
37 int add_particle( PARTICLE_SYSTEM *psys , BITMAP *spr , int mode , int off_x , int off_y , int lifetime , int color ,
38 double vx , double vy , double vz ,
39 double ax , double ay , double az )
40 {
41 if( psys -> nb_particles >= psys -> max_particles )
42 return FALSE ;
43
44 if( !psys -> list_start ) /* no particles */
45 {
46 Malloc( psys -> list_start , PARTICLE , 1 );
47
48 psys -> list_start -> prev = psys -> list_start -> next = NULL ;
49 psys -> list_end = psys -> list_start ;
50
51 psys -> list_start -> sprite = spr ;
52 psys -> list_start -> mode = mode ;
53 psys -> list_start -> off_x = off_x ;
54 psys -> list_start -> off_y = off_y ;
55 psys -> list_start -> lifetime = lifetime ;
56 psys -> list_start -> x = psys -> x + off_x ;
57 psys -> list_start -> y = psys -> y + off_y ;
58 psys -> list_start -> z = psys -> z ;
59 psys -> list_start -> ax = ax ;
60 psys -> list_start -> ay = ay ;
61 psys -> list_start -> az = az ;
62 psys -> list_start -> vx = vx ;
63 psys -> list_start -> vy = vy ;
64 psys -> list_start -> vz = vz ;
65 psys -> list_start -> color = color ;
66
67 psys -> nb_particles ++ ;
68
69 return TRUE;
70 }
71
72 Malloc( psys -> list_end -> next , PARTICLE , 1 );
73 psys -> list_end -> next -> prev = psys -> list_end ;
74 psys -> list_end -> next -> next = NULL ;
75 psys -> list_end = psys -> list_end -> next ;
76 psys -> list_end -> sprite = spr ;
77 psys -> list_end -> mode = mode ;
78 psys -> list_end -> off_x = off_x ;
79 psys -> list_end -> off_y = off_y ;
80 psys -> list_end -> lifetime = lifetime ;
81 psys -> list_end -> x = psys -> x + off_x ;
82 psys -> list_end -> y = psys -> y + off_y ;
83 psys -> list_end -> z = psys -> z ;
84 psys -> list_end -> ax = ax ;
85 psys -> list_end -> ay = ay ;
86 psys -> list_end -> az = az ;
87 psys -> list_end -> vx = vx ;
88 psys -> list_end -> vy = vy ;
89 psys -> list_end -> vz = vz ;
90 psys -> list_end -> color = color ;
91
92 psys -> nb_particles ++ ;
93
94 return TRUE ;
95
96 }
97
98 int manage_particle( PARTICLE_SYSTEM *psys)
99 {
100 PARTICLE *ptr = NULL , *next = NULL , *prev = NULL ;
101
102 double tmp_v = 0 ;
103
104 ptr = psys -> list_start ;
105
106 double delta = 0 ;
107
108 delta = get_msec( &psys -> timer ) ;
109
110 while( ptr )
111 {
112 ptr -> lifetime -= delta ;
113
114 if( ptr -> lifetime > 0 )
115 {
116 tmp_v = ptr -> vx + ( ptr -> ax * ((delta * delta)/1000) ) / 2 ;
117 ptr -> x = ptr -> x + delta * ( ptr -> vx + tmp_v ) / 2000 ;
118 ptr -> vx = tmp_v ;
119
120 tmp_v = ptr -> vy + ( ptr -> ay * ((delta * delta)/1000) ) / 2 ;
121 ptr -> y = ptr -> y + delta * ( ptr -> vy + tmp_v ) / 2000 ;
122 ptr -> vy = tmp_v ;
123
124 tmp_v = ptr -> vz + ( ptr -> az * ((delta * delta)/1000) ) / 2 ;
125 ptr -> z = ptr -> z + delta * ( ptr -> vz + tmp_v ) / 2000 ;
126 ptr -> vz = tmp_v ;
127
128 ptr = ptr -> next ;
129 }
130 else
131 {
132 next = ptr -> next ;
133 prev = ptr -> prev ;
134
135 if( next )
136 {
137 if( prev )
138 next -> prev = prev ;
139 else
140 {
141 next -> prev = NULL ;
142 psys -> list_start = next ;
143 }
144 }
145
146 if( prev )
147 {
148 if( next )
149 prev -> next = next ;
150 else
151 {
152 prev -> next = NULL ;
153 psys -> list_end = prev ;
154 }
155 }
156
157 Free( ptr );
158 psys -> nb_particles -- ;
159 if( psys -> nb_particles <= 0 )
160 psys -> list_start = psys -> list_end = NULL ;
161
162 ptr = next ;
163 }
164 }
165
166 return TRUE;
167 }
168
169 int draw_particle( BITMAP *bmp , PARTICLE_SYSTEM *psys )
170 {
171 PARTICLE *ptr = NULL ;
172
173 double x = 0 , y = 0 ;
174
175 ptr = psys -> list_start ;
176
177 while( ptr )
178 {
179 x = ptr -> x ;
180 y = ptr -> y ;
181
182 if( ptr -> mode == SINUS_PART )
183 {
184 if( ptr -> vx != 0 )
185 x = x + ptr -> vx * sin( (ptr -> x/ptr -> vx) ) ;
186 else
187 x = x + ptr -> vx * sin( ptr -> x );
188 if( ptr -> vy != 0 )
189 y = y + ptr -> vy * cos( (ptr -> y/ptr -> vy) ) ;
190 else
191 y = y + ptr -> vy * sin( ptr -> y ) ;
192 }
193
194 if( ptr -> mode == TRANS_PART )
195 {
196 drawing_mode(DRAW_MODE_TRANS, NULL, 0, 0);
197 set_alpha_blender( );
198 draw_trans_sprite( bmp , ptr -> sprite , x - ptr -> sprite -> w / 2 , y - ptr -> sprite -> h /2 );
199 drawing_mode(DRAW_MODE_SOLID, NULL, 0, 0);
200
201 }
202 else
203 {
204 if( ptr -> sprite )
205 {
206 #ifdef DEBUGMODE
207 circle( bmp , x - ptr -> sprite -> w / 2 , y - ptr -> sprite -> h / 2 , 3 , makecol( 255 , 255 , 0 ) );
208 #endif
209 masked_blit( ptr -> sprite , bmp , 0 , 0 , x - ptr -> sprite -> w / 2 , y - ptr -> sprite -> h /2, ptr -> sprite -> w , ptr -> sprite -> h );
210 }
211 else
212 circle( bmp , x , y , 2 , ptr -> color );
213 }
214
215 ptr = ptr -> next ;
216 }
217
218 return TRUE;
219 }
220
221 int free_particle( PARTICLE_SYSTEM *psys , PARTICLE **ptr )
222 {
223 PARTICLE *next = NULL , *prev = NULL ;
224
225 next = (*ptr) -> next ;
226 prev = (*ptr) -> prev ;
227
228 if( next )
229 {
230 if( prev )
231 next -> prev = prev ;
232 else
233 {
234 next -> prev = NULL ;
235 psys -> list_start = next ;
236 }
237 }
238
239 if( prev )
240 {
241 if( next )
242 prev -> next = next ;
243 else
244 {
245 prev -> next = NULL ;
246 psys -> list_end = prev ;
247 }
248 }
249
250 Free( (*ptr) );
251
252 psys -> nb_particles -- ;
253
254 if( psys -> nb_particles <= 0 )
255 psys -> list_start = psys -> list_end = NULL ;
256
257 (*ptr) = next ;
258
259 return TRUE ;
260 } /* free_particle(...) */
261
262
263
264 int free_particle_system( PARTICLE_SYSTEM **psys)
265 {
266 PARTICLE *ptr = NULL , *ptr_next = NULL ;
267
268 ptr = (*psys) -> list_start ;
269
270 while( ptr )
271 {
272 ptr_next = ptr -> next ;
273 Free( ptr ) ;
274 ptr = ptr_next ;
275 }
276
277 Free( (*psys) );
278
279 return TRUE;
280 }
281