Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

[7f9531]: explosion.c Maximize Restore History

Download this file

explosion.c    194 lines (161 with data), 5.4 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/* Asteroids3D - a first person game of blowing up asteroids
* Copyright (C) 2000 Stuart Mark Pomerantz <smp [at] psc edu>
* Copyright Š Jan Engelhardt <jengelh [at] linux01 gwdg de>, 2003 - 2005
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* Stuart Pomerantz
* 3935 Stonecliffe drive
* Monroeville, PA 15146
*/
#include <GL/glut.h>
#include <math.h>
#include <string.h>
#include "asteroids3D.h"
#include "vecops.h"
static void gen_rand_explosion_particles(int, int, double, int);
static void init_explosion_data(void);
static void init_explosion_dl(void);
static void init_explosion_winshield_particle_dl(void);
/* Explosion array, which is defined to be MAX_BLASTS > MAX_TORPS size since
there may be more than MAX_TORPS number of simultanious explosions, since the
explosions (currently) last longer then the torp ttl. */
Blast explosion[MAX_BLASTS];
int explosions_active_flag = 0; // are there explosions activated
int expl_dl[NUM_BLAST_TYPES], expl_winshield_particle_dl,
expl_winshield_particle_count = 0;
void init_explosions(void) {
init_explosion_data() ;
init_explosion_dl() ;
init_explosion_winshield_particle_dl() ;
return ;
}
static void init_explosion_data(void) {
int i ;
explosions_active_flag = 0 ;
memset(explosion, 0, sizeof(explosion));
for( i = 0 ; i < MAX_BLASTS ; i++ ) {
explosion[i].scalar = 1 ;
}
return ;
}
void activate_explosion(const Vector *ppos) {
int i ;
/*
* check for a free explosion slot.
* use the first free slot found. This code
* is almost exactly the same as the torp code.
*/
for( i = 0 ; i < MAX_BLASTS ; i++ ) {
if(!explosion[i].is_active)
break ;
}
if( i == MAX_BLASTS ) /* no free slot , so don't draw one */
return ;
/* set up explosion parameters */
explosion[i].is_active = 1 ;
explosion[i].pos = *ppos;
explosion[i].ttl = BLAST_TTL*tps + ticks ;
explosion[i].scalar = 1 ;
explosion[i].type = gen_rand_int(0,NUM_BLAST_TYPES-1) ;
if(!explosions_active_flag) {
explosions_active_flag = 1 ;
}
return ;
}
void process_explosion(void) {
int num_active_blasts = 0;
Blast *e;
int i;
expl_winshield_particle_count = 0;
if(!explosions_active_flag)
return;
for(i = 0; i < MAX_BLASTS; ++i) {
e = &explosion[i];
if(!e->is_active)
continue;
++num_active_blasts;
if(vec_dist(&pos, &e->pos) <= e->scalar)
++expl_winshield_particle_count;
e->scalar += dt * BLAST_ER;
if(ticks >= explosion[i].ttl)
e->is_active = 0;
}
if(num_active_blasts <= 0)
explosions_active_flag = 0;
return;
}
static void init_explosion_dl(void) {
int i ;
for( i = 0 ; i < NUM_BLAST_TYPES ; i++ ){
expl_dl[i] = glGenLists (1);
gen_rand_explosion_particles(15+i,15+i,0.5,expl_dl[i]) ;
}
return ;
}
static void gen_rand_explosion_particles(int ps, int ts, double pert, int dl) {
double phi, theta ;
int p_slices = ps ;
int t_slices = ts ;
double delta_p = M_PI / p_slices;
double delta_t = 2 * M_PI / t_slices;
int rows = p_slices - 1 ;
int cols = t_slices ;
int i, j ;
double q = pert ;
/*
* This is the same code that generates the randomly perturbed sphere which
* I turned into an asteroid. What was that you said about applying the
* same trick again and again, Dr. Wetzel ? :-)
*
*/
glNewList (dl, GL_COMPILE);
glPointSize( 1.0 ) ;
glBegin(GL_POINTS) ;
for( i = 0, phi = delta_p ; i < rows ; phi += delta_p, i++ ) {
double sinphi = sin(phi), cosphi = cos(phi);
for( j = 0, theta = 0 ; j < cols ; theta += delta_t, j++) {
glVertex3f( cos(theta) * sinphi + gen_rand_float(-q, q),
sin(theta) * sinphi + gen_rand_float(-q, q),
cosphi + gen_rand_float(-q, q));
}
}
glEnd() ;
glEndList ();
return ;
}
static void init_explosion_winshield_particle_dl(void) {
static const float emit_on[] = {0.0, 0.6, 0.0, 1.0};
static const float emit_off[] = {0.0, 0.0, 0.0, 1.0};
static const float ad[] = {0.0, 0.0, 0.0, 0.5};
double t;
expl_winshield_particle_dl = glGenLists (1);
glNewList (expl_winshield_particle_dl, GL_COMPILE);
glEnable(GL_BLEND) ;
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA) ;
glMaterialfv(GL_FRONT,GL_AMBIENT_AND_DIFFUSE,ad) ;
glMaterialfv(GL_FRONT,GL_EMISSION,emit_on) ;
glBegin(GL_TRIANGLE_FAN) ;
glVertex3f(0,0,0) ;
for(t = 0; t < 2 * M_PI + M_PI / 10; t += M_PI / 10) {
glVertex3f( 0.01*cos(t), 0.01*sin(t), 0) ;
}
glEnd() ;
glMaterialfv(GL_FRONT,GL_EMISSION,emit_off) ;
glDisable(GL_BLEND);
glEndList ();
return ;
}