Initial Import
[glAntsMech.git] / glants_mech / win32 / glAntsV05 / particles.cpp
blobf28555c8bed6a3502fc92e2f5552934f4c99cf43
1 //
2 // particles.cpp
3 //
4 // particles or explosion engine
5 //
6 #include <windows.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <math.h>
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
15 #include "bot.h"
16 #include "particles.h"
18 static ParticleList *particle_set[MAX_PARTICLE_SET];
19 static int particle_index = 0;
21 //
22 // Create Particle
24 ParticleList *CreateParticleList(void) {
26 ParticleList *result = (ParticleList *)malloc(
27 sizeof(ParticleList));
29 result->x = 0;
30 result->y = 0;
32 result->life = 0;
33 result->state = DEAD_STATE;
35 return result;
37 } // end of the function
40 // Destroy List
42 void DestroyParticleList(ParticleList *list)
44 RELEASE_OBJECT(list);
46 } // end of the function
49 // New_Speed
51 static void New_Speed(float d[3], float speed)
54 float x, y, z;
55 float r;
56 float tmp;
58 tmp = 2.0f * speed;
60 r = (tmp * ((float)rand())/((float)RAND_MAX)) - speed;
61 x = r;
63 r = (tmp * ((float)rand())/((float)RAND_MAX)) - speed;
64 y = r;
66 r = (tmp * ((float)rand())/((float)RAND_MAX)) - speed;
67 z = r;
69 d[0] = x;
70 d[1] = y;
71 d[2] = z;
73 } // end of the function
76 // Set Explosion
77 // - just set the position and turn it on
79 void Set_Explosion(ParticleList *list, float x, float y)
81 int i;
83 list->life = MAX_PARTICLE_LIFE;
85 list->state = ALIVE_STATE;
86 list->x = x;
87 list->y = y;
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;
103 // Set the speed
104 New_Speed(list->particles[i].p_speed, PARTICLE_SPEED);
106 } // end of the for
109 } // end of the function
112 // Draw_Particles
114 void Draw_Particles(ParticleList *list)
116 int i=0;
118 if (list->state == ALIVE_STATE)
121 glDisable(GL_LIGHTING);
123 glPushMatrix();
125 glBegin(GL_POINTS);
127 for(i =0; i < MAX_PARTICLES; i++)
129 if (list->particles[i].p_state == DEAD_STATE)
130 continue;
132 glColor3fv(list->particles[i].p_color);
133 glVertex3fv(list->particles[i].p_pos);
135 } // end of the for
137 glEnd();
139 glPopMatrix();
141 glEnable(GL_LIGHTING);
144 } // end of the if
146 } // end of the function
150 // Wrapper Functions
152 void Build_ParticleSet(void)
154 int i;
156 for (i = 0; i < MAX_PARTICLE_SET; i++)
158 particle_set[i] = CreateParticleList();
159 } // end of the for
161 particle_index = 0;
163 } // end of the function
166 // Destroy_Particles(void)
168 void Destroy_ParticleSet(void)
170 int i = 0;
172 for (i = 0; i < MAX_PARTICLE_SET; i++)
174 DestroyParticleList(particle_set[i]);
175 } // end of the of r
177 } // end of the function
180 // SetExplosion
182 void SetExplosion(float x, float y)
184 Set_Explosion(particle_set[particle_index], x, y);
186 particle_index++;
187 if (particle_index >= MAX_PARTICLE_SET)
188 particle_index = 0;
190 } // end of the function
194 // AnimateExplisions
196 void Anim_Particles(ParticleList *list)
198 int i = 0;
199 int j = 0;
200 float dv = 0;
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;
218 dv = 1.0f / 500.0f;
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;
225 } // end of the for
227 } // end of the for
230 // subtract life
231 list->life--;
233 if (list->life < 0) {
235 list->life = 0;
236 list->state = DEAD_STATE;
238 } // end of the if
240 } // end of big if
242 } // end of the function
245 // AnimateExplosions
247 void AnimateExplosions(void)
249 int i = 0;
251 for (i = 0; i < MAX_PARTICLE_SET; i++)
253 if (particle_set[i]->state == DEAD_STATE)
254 continue;
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)
268 int i = 0;
270 for (i = 0; i < MAX_PARTICLE_SET; i++)
272 if (particle_set[i]->state == DEAD_STATE)
273 continue;
275 Draw_Particles(particle_set[i]);
277 } // end of the functino
279 } // end of the function