FS2_Open
Open source remastering of the Freespace 2 engine
muzzleflash.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) Volition, Inc. 1999. All rights reserved.
3  *
4  * All source code herein is the property of Volition, Inc. You may not sell
5  * or otherwise commercially exploit the source or things you created based on the
6  * source.
7  *
8 */
9 
10 #include "globalincs/systemvars.h"
11 #include "graphics/2d.h"
12 #include "math/vecmat.h"
13 #include "object/object.h"
14 #include "parse/parselo.h"
15 #include "particle/particle.h"
16 #include "weapon/muzzleflash.h"
17 
18 
19 // ---------------------------------------------------------------------------------------------------------------------
20 // MUZZLE FLASH DEFINES/VARS
21 //
22 
23 // muzzle flash info - read from a table
24 typedef struct mflash_blob_info {
26  int anim_id;
27  float offset;
28  float radius;
29 
31  {
32  strcpy_s( name, mbi.name );
33  anim_id = mbi.anim_id;
34  offset = mbi.offset;
35  radius = mbi.radius;
36  }
37 
39  anim_id( -1 ),
40  offset( 0.0 ),
41  radius( 0.0 )
42  {
43  name[ 0 ] = '\0';
44  }
45 
46  void operator=( const mflash_blob_info& r )
47  {
48  strcpy_s( name, r.name );
49  anim_id = r.anim_id;
50  offset = r.offset;
51  radius = r.radius;
52  }
54 
55 typedef struct mflash_info {
59 
61  : used_this_level( 0 )
62  {
63  name[ 0 ] = '\0';
64  }
65 
66  mflash_info( const mflash_info& mi )
67  {
68  strcpy_s( name, mi.name );
69  used_this_level = mi.used_this_level;
70  blobs = mi.blobs;
71  }
72 
73  void operator=( const mflash_info& r )
74  {
75  strcpy_s( name, r.name );
76  used_this_level = r.used_this_level;
77  blobs = r.blobs;
78  }
79 } mflash_info;
80 
82 
83 
84 // ---------------------------------------------------------------------------------------------------------------------
85 // MUZZLE FLASH FUNCTIONS
86 //
87 
88 void parse_mflash_tbl(const char *filename)
89 {
90  uint i;
91 
92  try
93  {
94  read_file_text(filename, CF_TYPE_TABLES);
95  reset_parse();
96 
97  // header
98  required_string("#Muzzle flash types");
99 
100  while (optional_string("$Mflash:")) {
101  mflash_info mflash;
102  bool override_mflash = false;
103 
104  required_string("+name:");
106 
107  if (optional_string("+override"))
108  override_mflash = true;
109 
110  // read in all blobs
111  while (optional_string("+blob_name:")) {
112  mflash_blob_info mblob;
113 
115 
116  required_string("+blob_offset:");
117  stuff_float(&mblob.offset);
118 
119  required_string("+blob_radius:");
120  stuff_float(&mblob.radius);
121 
122  mflash.blobs.push_back(mblob);
123  }
124 
125  for (i = 0; i < Mflash_info.size(); i++) {
126  if (!stricmp(mflash.name, Mflash_info[i].name)) {
127  if (override_mflash) {
128  Mflash_info[i] = mflash;
129  }
130  break;
131  }
132  }
133 
134  // no matching name exists so add as new
135  if (i == Mflash_info.size()) {
136  Mflash_info.push_back(mflash);
137  }
138  // a mflash of the same name exists, don't add it again
139  else {
140  if (!override_mflash) {
141  Warning(LOCATION, "Muzzle flash \"%s\" already exists! Using existing entry instead.", mflash.name);
142  }
143  }
144  }
145 
146  // close
147  required_string("#end");
148  }
149  catch (const parse::ParseException& e)
150  {
151  mprintf(("TABLES: Unable to parse '%s'! Error message = %s.\n", filename, e.what()));
152  return;
153  }
154 }
155 
156 // initialize muzzle flash stuff for the whole game
158 {
159  // parse main table first
160  parse_mflash_tbl("mflash.tbl");
161 
162  // look for any modular tables
164 }
165 
167 {
168  if (index < 0)
169  return;
170 
171  Assert( index < (int)Mflash_info.size() );
172 
173  Mflash_info[index].used_this_level++;
174 }
175 
176 void mflash_page_in(bool load_all)
177 {
178  uint i, idx;
179  int num_frames, fps;
180 
181  // load up all anims
182  for ( i = 0; i < Mflash_info.size(); i++) {
183  // skip if it's not used
184  if ( !load_all && !Mflash_info[i].used_this_level )
185  continue;
186 
187  // blobs
188  int original_num_blobs = Mflash_info[i].blobs.size();
189  int original_idx = 1;
190  for ( idx = 0; idx < Mflash_info[i].blobs.size(); ) {
191  mflash_blob_info* mfbip = &Mflash_info[i].blobs[idx];
192  mfbip->anim_id = bm_load_either(mfbip->name, &num_frames, &fps, NULL, 1);
193  if ( mfbip->anim_id >= 0 ) {
195  ++idx;
196  }
197  else {
198  Warning(LOCATION, "Muzleflash \"%s\", blob [%d/%d]\nMuzzleflash blob \"%s\" not found! Deleting.",
199  Mflash_info[i].name, original_idx, original_num_blobs, Mflash_info[i].blobs[idx].name);
200  Mflash_info[i].blobs.erase( Mflash_info[i].blobs.begin() + idx );
201  }
202  ++original_idx;
203  }
204  }
205 }
206 
207 // initialize muzzle flash stuff for the level
209 {
210  uint i, idx;
211 
212  // reset all anim usage for this level
213  for ( i = 0; i < Mflash_info.size(); i++) {
214  for ( idx = 0; idx < Mflash_info[i].blobs.size(); idx++) {
215  Mflash_info[i].used_this_level = 0;
216  }
217  }
218 }
219 
220 // shutdown stuff for the level
222 {
223  uint i, idx;
224 
225  // release all anims
226  for ( i = 0; i < Mflash_info.size(); i++) {
227  // blobs
228  for ( idx = 0; idx < Mflash_info[i].blobs.size(); idx++) {
229  if ( Mflash_info[i].blobs[idx].anim_id < 0 )
230  continue;
231 
232  bm_release( Mflash_info[i].blobs[idx].anim_id );
233  Mflash_info[i].blobs[idx].anim_id = -1;
234  }
235  }
236 }
237 
238 // create a muzzle flash on the guy
239 void mflash_create(vec3d *gun_pos, vec3d *gun_dir, physics_info *pip, int mflash_type, object *local)
240 {
241  // mflash *mflashp;
242  mflash_info *mi;
243  mflash_blob_info *mbi;
245  uint idx;
246 
247  // standalone server should never create trails
249  return;
250  }
251 
252  // illegal value
253  if ( (mflash_type < 0) || (mflash_type >= (int)Mflash_info.size()) )
254  return;
255 
256  // create the actual animations
257  mi = &Mflash_info[mflash_type];
258 
259  if (local != NULL) {
260  for (idx = 0; idx < mi->blobs.size(); idx++) {
261  mbi = &mi->blobs[idx];
262 
263  // bogus anim
264  if (mbi->anim_id < 0)
265  continue;
266 
267  // fire it up
268  memset(&p, 0, sizeof(particle_info));
269  vm_vec_scale_add(&p.pos, gun_pos, gun_dir, mbi->offset);
270  vm_vec_zero(&p.vel);
271  //vm_vec_scale_add(&p.vel, &pip->rotvel, &pip->vel, 1.0f);
272  p.rad = mbi->radius;
273  p.type = PARTICLE_BITMAP;
274  p.optional_data = mbi->anim_id;
275  p.attached_objnum = OBJ_INDEX(local);
276  p.attached_sig = local->signature;
277  particle_create(&p);
278  }
279  } else {
280  for (idx = 0; idx < mi->blobs.size(); idx++) {
281  mbi = &mi->blobs[idx];
282 
283  // bogus anim
284  if (mbi->anim_id < 0)
285  continue;
286 
287  // fire it up
288  memset(&p, 0, sizeof(particle_info));
289  vm_vec_scale_add(&p.pos, gun_pos, gun_dir, mbi->offset);
290  vm_vec_scale_add(&p.vel, &pip->rotvel, &pip->vel, 1.0f);
291  p.rad = mbi->radius;
292  p.type = PARTICLE_BITMAP;
293  p.optional_data = mbi->anim_id;
294  p.attached_objnum = -1;
295  p.attached_sig = 0;
296  particle_create(&p);
297  }
298  }
299 }
300 
301 // lookup type by name
302 int mflash_lookup(char *name)
303 {
304  uint idx;
305 
306  // look it up
307  for (idx = 0; idx < Mflash_info.size(); idx++) {
308  if ( !stricmp(name, Mflash_info[idx].name) )
309  return idx;
310  }
311 
312  // couldn't find it
313  return -1;
314 }
#define MAX_FILENAME_LEN
Definition: pstypes.h:324
int i
Definition: multi_pxo.cpp:466
int mflash_lookup(char *name)
void mflash_level_close()
int Game_mode
Definition: systemvars.cpp:24
vec3d rotvel
Definition: physics.h:78
void vm_vec_scale_add(vec3d *dest, const vec3d *src1, const vec3d *src2, float k)
Definition: vecmat.cpp:266
void mflash_game_init()
char name[MAX_FILENAME_LEN]
Definition: muzzleflash.cpp:25
GLuint index
Definition: Glext.h:5608
void mflash_create(vec3d *gun_pos, vec3d *gun_dir, physics_info *pip, int mflash_type, object *local)
void mflash_mark_as_used(int index)
void _cdecl void void _cdecl void _cdecl Warning(char *filename, int line, SCP_FORMAT_STRING const char *format,...) SCP_FORMAT_STRING_ARGS(3
Assert(pm!=NULL)
Definition: pstypes.h:88
#define mprintf(args)
Definition: pstypes.h:238
struct mflash_info mflash_info
struct mflash_blob_info mflash_blob_info
void operator=(const mflash_blob_info &r)
Definition: muzzleflash.cpp:46
#define PARTICLE_BITMAP
Definition: particle.h:49
void bm_page_in_xparent_texture(int bitmapnum, int nframes)
Marks a textures as being used for level and is transparant.
Definition: bmpman.cpp:2514
int bm_release(int handle, int clear_render_targets)
Frees both a bitmap's data and it's associated slot.
Definition: bmpman.cpp:2603
int signature
Definition: object.h:145
void stuff_float(float *f)
Definition: parselo.cpp:2328
void mflash_page_in(bool load_all)
int optional_data
Definition: particle.h:67
GLdouble GLdouble GLdouble r
Definition: Glext.h:5337
unsigned int uint
Definition: pstypes.h:64
void mflash_level_init()
char * filename
vec3d vel
Definition: particle.h:63
void stuff_string(char *outstr, int type, int len, char *terminators)
Definition: parselo.cpp:1189
#define CF_TYPE_TABLES
Definition: cfile.h:50
int required_string(const char *pstr)
Definition: parselo.cpp:468
void parse_mflash_tbl(const char *filename)
Definition: muzzleflash.cpp:88
int optional_string(const char *pstr)
Definition: parselo.cpp:539
void read_file_text(const char *filename, int mode, char *processed_text, char *raw_text)
Definition: parselo.cpp:1995
vec3d pos
Definition: particle.h:62
int idx
Definition: multiui.cpp:761
#define vm_vec_zero(v)
Definition: vecmat.h:37
#define OBJ_INDEX(objp)
Definition: object.h:235
#define NOX(s)
Definition: pstypes.h:473
#define GM_STANDALONE_SERVER
Definition: systemvars.h:27
void reset_parse(char *text)
Definition: parselo.cpp:3305
GLuint const GLchar * name
Definition: Glext.h:5608
vec3d vel
Definition: physics.h:77
bool local
Definition: lua.cpp:4524
int attached_sig
Definition: particle.h:72
char name[MAX_FILENAME_LEN]
Definition: muzzleflash.cpp:56
void operator=(const mflash_info &r)
Definition: muzzleflash.cpp:73
SCP_vector< mflash_blob_info > blobs
Definition: muzzleflash.cpp:58
GLfloat GLfloat p
Definition: Glext.h:8373
#define F_NAME
Definition: parselo.h:34
#define LOCATION
Definition: pstypes.h:245
int parse_modular_table(const char *name_check, void(*parse_callback)(const char *filename), int path_type, int sort_type)
Definition: parselo.cpp:4205
int used_this_level
Definition: muzzleflash.cpp:57
int bm_load_either(const char *filename, int *nframes, int *fps, int *keyframe, int can_drop_frames, int dir_type)
Loads either animation (bm_load_animation) or still image (bm_load)
Definition: bmpman.cpp:1683
mflash_blob_info(const mflash_blob_info &mbi)
Definition: muzzleflash.cpp:30
SCP_vector< mflash_info > Mflash_info
Definition: muzzleflash.cpp:81
float rad
Definition: particle.h:65
mflash_info(const mflash_info &mi)
Definition: muzzleflash.cpp:66
particle * particle_create(particle_info *pinfo)
Definition: particle.cpp:105
int attached_objnum
Definition: particle.h:71
#define stricmp(s1, s2)
Definition: config.h:271
#define strcpy_s(...)
Definition: safe_strings.h:67