4 // particles or explosion engine
11 #include <gl\gl.h> // Header File For The OpenGL32 Library
12 #include <gl\glu.h> // Header File For The GLu32 Library
13 #include <gl\glaux.h> // Header File For The Glaux Library
16 #include "particles.h"
18 static ParticleList
*particle_set
[MAX_PARTICLE_SET
];
19 static int particle_index
= 0;
24 ParticleList
*CreateParticleList(void) {
26 ParticleList
*result
= (ParticleList
*)malloc(
27 sizeof(ParticleList
));
33 result
->state
= DEAD_STATE
;
37 } // end of the function
42 void DestroyParticleList(ParticleList
*list
)
46 } // end of the function
51 static void New_Speed(float d
[3], float speed
)
60 r
= (tmp
* ((float)rand())/((float)RAND_MAX
)) - speed
;
63 r
= (tmp
* ((float)rand())/((float)RAND_MAX
)) - speed
;
66 r
= (tmp
* ((float)rand())/((float)RAND_MAX
)) - speed
;
73 } // end of the function
77 // - just set the position and turn it on
79 void Set_Explosion(ParticleList
*list
, float x
, float y
)
83 list
->life
= MAX_PARTICLE_LIFE
;
85 list
->state
= ALIVE_STATE
;
89 for (i
= 0; i
< MAX_PARTICLES
; i
++)
91 list
->particles
[i
].p_id
= i
;
93 list
->particles
[i
].p_pos
[0] = x
;
94 list
->particles
[i
].p_pos
[1] = 1.0f
;
95 list
->particles
[i
].p_pos
[2] = y
;
97 list
->particles
[i
].p_color
[0] = 1.0f
;
98 list
->particles
[i
].p_color
[1] = 0.1f
;
99 list
->particles
[i
].p_color
[2] = 0.1f
;
101 list
->particles
[i
].p_state
= ALIVE_STATE
;
104 New_Speed(list
->particles
[i
].p_speed
, PARTICLE_SPEED
);
109 } // end of the function
114 void Draw_Particles(ParticleList
*list
)
118 if (list
->state
== ALIVE_STATE
)
121 glDisable(GL_LIGHTING
);
127 for(i
=0; i
< MAX_PARTICLES
; i
++)
129 if (list
->particles
[i
].p_state
== DEAD_STATE
)
132 glColor3fv(list
->particles
[i
].p_color
);
133 glVertex3fv(list
->particles
[i
].p_pos
);
141 glEnable(GL_LIGHTING
);
146 } // end of the function
152 void Build_ParticleSet(void)
156 for (i
= 0; i
< MAX_PARTICLE_SET
; i
++)
158 particle_set
[i
] = CreateParticleList();
163 } // end of the function
166 // Destroy_Particles(void)
168 void Destroy_ParticleSet(void)
172 for (i
= 0; i
< MAX_PARTICLE_SET
; i
++)
174 DestroyParticleList(particle_set
[i
]);
177 } // end of the function
182 void SetExplosion(float x
, float y
)
184 Set_Explosion(particle_set
[particle_index
], x
, y
);
187 if (particle_index
>= MAX_PARTICLE_SET
)
190 } // end of the function
196 void Anim_Particles(ParticleList
*list
)
202 if (list
->state
== ALIVE_STATE
)
206 for(i
=0; i
< MAX_PARTICLES
; i
++)
209 for (j
= 0; j
< 3; j
++)
211 list
->particles
[i
].p_pos
[j
] +=
212 list
->particles
[i
].p_speed
[j
];
214 // kill if this one hits the ground
215 if (list
->particles
[i
].p_pos
[1] < 0.0f
)
216 list
->particles
[i
].p_state
= DEAD_STATE
;
220 list
->particles
[i
].p_color
[j
] -= dv
;
222 if (list
->particles
[i
].p_color
[j
] < 0.0f
)
223 list
->particles
[i
].p_color
[j
] = 0.0f
;
233 if (list
->life
< 0) {
236 list
->state
= DEAD_STATE
;
242 } // end of the function
247 void AnimateExplosions(void)
251 for (i
= 0; i
< MAX_PARTICLE_SET
; i
++)
253 if (particle_set
[i
]->state
== DEAD_STATE
)
256 Anim_Particles(particle_set
[i
]);
258 } // end of the functino
260 } // end of the function
264 // Now draw the entire set
266 void DrawExplosions(void)
270 for (i
= 0; i
< MAX_PARTICLE_SET
; i
++)
272 if (particle_set
[i
]->state
== DEAD_STATE
)
275 Draw_Particles(particle_set
[i
]);
277 } // end of the functino
279 } // end of the function