tutorial.h
), because three sound effects
have been added to it to support Section 2. There will also be another update
to support Section 3 soon.
Since the code is getting large, I will not show it in its entirety unnecessarily. So, at the top, add
#define BOOM_DELAY 5and add this class before the
projectile
class
class explosion : public sprite { int frame,delay,magnitude; public: explosion(fix _X,fix _Y,int mag) : sprite(_X,_Y,(RLE_SPRITE*)data[TUT_BOOM1].dat) { magnitude=mag; frame=0; delay=0; } virtual void draw(BITMAP*dest) { draw_rle_sprite(dest,image,X-image->w/2,Y-image->h/2); } virtual bool animate() { if ((delay++)>BOOM_DELAY) { if ((frame++)>magnitude) return TRUE; image=(RLE_SPRITE*)data[TUT_BOOM1+frame].dat; delay=0; } return FALSE; } };Here, the
magnitude
field tells how many frames of the
explosions should be shown, and the draw()
method is
overridden to show the explosion with X,Y representing the center
position instead of the top-left position of the sprites, mostly because
our explosion sprites have different sizes but they are all centered.
To make it easy for the code to choose which graphics to show, we have
ordered the sprites (in the data file) by numbering each BOOM
sprite, thus making it easy to choose the correct image by just adding the
frame number we want to BOOM1
, and showing that. Since the
Grabber automatically sorts the data file, this is very handy for animations
where several frames has to be shown in sequence.
Now, we need an explosion to be activated when a bomb hits the ground,
so we will need to change the projectile
class to:
class projectile : public sprite { fix DX,DY; int force; public: projectile(fix _X,fix _Y,fix _DX,fix _DY,RLE_SPRITE*img,int power) : sprite(_X,_Y,img) { DX=_DX; DY=_DY; force=power; } virtual bool animate() { move(DX,DY); DY+=GRAVITY; if (Y+image->h>=MAX_Y) { sprites.push_back(new explosion(X+image->w/2,Y+image->h,force/2)); return TRUE; } else return FALSE; } };When a bomb hits the ground now, it will spawn an explosion with a magnitude that is one half of the assigned explosive power (which we set to 5 for a bomb, so the magnitude will be 2).
install_keyboard();
(or wherever) in main()
, insert
install_sound(DIGI_AUTODETECT,MIDI_NONE,NULL);to install the Allegro Sound System. Now, we will trigger a neat explosion sound whenever an explosion is set off. Change the
explosion
constructor to read:
explosion(fix _X,fix _Y,int mag) : sprite(_X,_Y,(RLE_SPRITE*)data[TUT_BOOM1].dat) { magnitude=mag; frame=0; delay=0; stop_sample((SAMPLE*)data[TUT_BANG0].dat); play_sample((SAMPLE*)data[TUT_BANG0].dat,255,(256*(int)X)/SCREEN_W,1000,FALSE); }Here, we stop any previously playing explosion sounds (otherwise the result might get *very* loud and distorted, especially considering the quantity of bombs we are able to pour out), then start a new one. The loudness is here 255 (max), the pan (0-255) is set according to the X position of the explosion for those lucky enough to have a stereo sound card, and the frequency is normalized (1000), and it should not loop. (The
int
typecast in the pan calculation is for avoiding a fixed-point overflow.)
For fun, you may try the other two sound effects I put in the datafile,
TUT_BANG1
and TUT_BANG2
, but these were sampled
from videos by a friend, so they are probably copyrighted.
If you have any suggestions for improvement so far, send them to ovek@arcticnet.no