Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » WAV in dat file trouble using with allegro

This thread is locked; no one can reply to it. rss feed Print
WAV in dat file trouble using with allegro
CIRCLE
Member #5,301
December 2004

I have been brushing up on my programming skills for the fun of it and it was either QBASIC or C/C++ and ALLEGRO. Well as you guessed I went with ALLEGRO. I went through an old program that I created called FallingBall and have added sounds and new graphics. I placed everthing inside a DAT file. with out the DAT file I can load everthing but inside the DAT file I can only load my background.

I am having problems with getting the wav file out and BMP's where I need to Grabframe.

Here is the BMP sides

     temp = load_bitmap("ball.bmp",NULL);
     for (n = 0;n<6;n++)
         ball[n] = grabframe(temp,17,17,0,0,1,n);
     destroy_bitmap(temp);
     temp = load_bitmap("explode.bmp",NULL);
     for (n = 0;n<6;n++)
         explode[n] = grabframe(temp,17,17,0,0,1,n);
     destroy_bitmap(temp);

for the WAV

sounds[1] = load_wav("explode.wav");            
sounds[2] = load_wav("explode2.wav");

simple stuff just can not get either from a dat file

#define BACKGROUND_BMP                   1        /* BMP  */
#define BALL_BMP                         2        /* BMP  */
#define BACKGROUND2_BMP                  0        /* BMP  */
#define EXPLODE2_WAV                     3        /* WAV  */
#define EXPLODE_WAV                      4        /* WAV  */

Also I have searched the forums and the examples seem to not be working.
if anyone would need source code in full I can easily link from my website the newest version.

-I edit a lot. Prepare thyself.

Billybob
Member #3,136
January 2003

Would you mind showing your code for loading from the dat file? Here's an example, if it helps any:

// Do this once to load the entire datafile, keep the datafile variable around
DATAFILE *datafile = load_datafile("data.dat");

// Example of how to get something out of the datafile
sounds[1] = datafile[EXPLODE_WAV].dat;

// At the end of your program
unload_datafile(datafile);

I don't use datafiles, so please excuse me if any of that is incorrect.

Oh and remember not to destroy the bitmaps, audio, etc you get out of the datafile. unload_datafile will take care of all the cleanup.

CIRCLE
Member #5,301
December 2004

in 3 different files since I keep everything separated so it may look funny set out but this is it put together.

You can get the FallingBall program by clicking FallingBall which is full of the source and compiled version of the game. That is my version 1.7r that is exactly what I am working with all sounds backgrounds ball are in that data file, explosion graphic excluded.

By the way I am not an artist so if you run the little game graphics will not be great but the game is coming along.

When I add the code to look like this
DATAFILE *data;
.
.
data = load_datafile("falling.dat");

int volume = 64;
int pan = 128;
int pitch = 2000;

sounds[1] = data[EXPLODE_WAV].dat;
instead of this which loads out side of datafile (which I don't want)
sounds[1] = load_wav("explode.wav");
and I play the wav like this
play_sample(sounds[1], volume, pan, pitch, FALSE);

I get a windows error right when the sound would apply.
FallingBall.c

-I edit a lot. Prepare thyself.

Kauhiz
Member #4,798
July 2004

Quote:

sounds[1] = datafile[EXPLODE_WAV].dat;

If you have DATAFILE *data; that should besounds[1] = data[EXPLODE_WAV].dat; Also, how is sounds defined? It better not be SAMPLE *sounds[1], or you'll have an array overflow...

---
It's Ridge Racer! RIIIIIDGE RAAAAACER!

CIRCLE
Member #5,301
December 2004

Another typo found but not in the code itself just what I typed on the forum.

it is defined like
SAMPLE *sounds[5];

Should it not be? But what about the error I get when playing the sound from the data file

dat -a -t WAV falling.dat explode.wav

Is how the sounds were put in. Don't know if that makes a difference.

-I edit a lot. Prepare thyself.

Kauhiz
Member #4,798
July 2004

The sounds define is ok, I just thought it was a bit odd that you used sounds[1] in the example, but nvm. When does it crash, loading or playing? For play_sample I'd pass 0 for looping instead of FALSE, but that's just personal preference, it shouldn't matter, I guess. I can't think of anything else right now...

---
It's Ridge Racer! RIIIIIDGE RAAAAACER!

CIRCLE
Member #5,301
December 2004

Well first I will say that when you click a ball it explodes and plays a sound. Everything loads and you start playing but as soon as you click a ball you get kicked out with a windows error with out the sound even starting.

Tried this aswell and didn't work

play_sample(data[EXPLODE_WAV].dat, volume, pan, pitch, FALSE);

and this which I think is the samething just adding a line of code.

sounds[1] = data[EXPLODE_WAV].dat;
play_sample(sounds[1], volume, pan, pitch, FALSE);

and this which eliminates more lines of code.

play_sample(load_wav(data[EXPLODE_WAV].dat), volume, pan, pitch, 0);

but as well does not work but compiles perfectly but the old way works fine when playing the same file just not in a datafile.

play_sample(load_wav("explode.wav"), volume, pan, pitch, 0);

I should add that either of the 4 ways compiles with 0 errors 0 warnings.

-I edit a lot. Prepare thyself.

Kauhiz
Member #4,798
July 2004

Quote:

play_sample(load_wav(data[EXPLODE_WAV].dat), volume, pan, pitch, 0);

That has got to be wrong. I must admit that I'm a bit rusty with samples, since I don't use allegro for sound. Hmm... where do you unload the datafile?

---
It's Ridge Racer! RIIIIIDGE RAAAAACER!

CIRCLE
Member #5,301
December 2004

I unload the data file assuming I use it when I am just debugging here

1#include "FallingBall.h"
2#include "FallingBallSetup.h"
3 
4int main()
5{
6 setup();
7 loadsprites();
8 setupscreen();
9 BITMAP *buffer = create_video_bitmap(SCREEN_W,SCREEN_H);
10 BITMAP *buffer2 = create_video_bitmap(SCREEN_W,SCREEN_H);
11 active_page = buffer2;
12 startup();
13 setupsound();
14
15 while(!key[KEY_ESC] && lives > 0)
16 {
17 multicheck();
18 singlecheck();
19 movemouse();
20 mousehold();
21 drawback();
22 checkdead();
23
24 show_video_bitmap(active_page);
25 if (active_page == buffer)
26 active_page = buffer2;
27 else
28 active_page = buffer;
29 clear_to_color(active_page, makecol(0, 0, 0));
30 rest(1);
31
32 }
33 unload_datafile(data); // <----------unloads here when I use it.
34 destroy_bitmap(buffer);
35 destroy_bitmap(buffer2);
36 destroy_bitmap(*explode);
37 destroy_bitmap(background);
38 destroy_bitmap(active_page);
39 destroy_bitmap(temp);
40 destroy_bitmap(*ball);
41
42 exitall();
43}
44END_OF_MAIN()

that is the main program FallingBall.c

-I edit a lot. Prepare thyself.

gnolam
Member #2,030
March 2002
avatar

Quote:

play_sample(load_wav("explode.wav"), volume, pan, pitch, 0);

That's a nasty memory leak.

And where's the rest of the code? It's not in the header files, I hope...

Oh, and is the datafile header up to date?

--
Move to the Democratic People's Republic of Vivendi Universal (formerly known as Sweden) - officially democracy- and privacy-free since 2008-06-18!

CIRCLE
Member #5,301
December 2004

Yes I just restarted the program after a 2 year stop. Everything down to out dated code has been redone.

textprintf to textprintf_ex that kind of thing.

oh yes its all in headerfiles 2 to be exact...but then this isn't about how everything is set up and the memory that may or may not leak. Sorry but I am trying to focus on one problem at a time.

the 3rd post from the top has a link that you can dl to get the answers

but you want all of it then ok

#SelectExpand
1//#include "FallingBall.h" 2#include "allegro.h" 3#define MODE GFX_AUTODETECT_FULLSCREEN 4#define WIDTH 640 5#define HEIGHT 480 6#define BallSpeed 1 7#define BallSize 16//((SCREEN_W + SCREEN_H) / 10) / 10 8#define MouseSpeed 2 9#define BACKGROUND_BMP 1 /* BMP */ 10#define BALL_BMP 2 /* BMP */ 11#define BACKGROUND2_BMP 0 /* BMP */ 12#define EXPLODE2_WAV 3 /* WAV */ 13#define EXPLODE_WAV 4 /* WAV */ 14 15struct tagBall { 16 int x; 17 int y; 18 int speed; 19 int color; 20 int size; 21} Balls[100]; 22struct expd { 23 int x; 24 int y; 25 int proc; 26} explodes[100]; 27struct Floor { 28 int y; 29} Floors; 30int i; 31int r; 32int expd; 33BITMAP *explode[9]; 34BITMAP *ball[9]; 35BITMAP *background; 36BITMAP *active_page; 37BITMAP *temp; 38DATAFILE *data; 39int expact; 40int speed; 41int mouseh; 42int n; 43int gameover = 0; 44int dead[100]; 45int lives = 10; 46int score = 0; 47int n; 48int mousex; 49int mousey; 50SAMPLE *sounds[5]; 51int volume = 64; 52int pan = 128; 53int pitch = 2000; 54void mousehold(); 55void dropball(); 56void moveball(); 57void movefloor(); 58void checkdead(); 59void getinput(); 60void setupscreen(); 61void startup(); 62void multicheck(); 63void singlecheck(); 64void exitall(); 65void setup(); 66void setupsound(); 67//#include "FallingBallSetup.h" 68void setup() 69{ 70 allegro_init(); 71 set_color_depth(16); 72 install_mouse(); 73 install_keyboard(); 74 install_timer(); 75 srand(time(NULL)); 76} 77 78void setupsound() 79{ 80 if (install_sound(DIGI_AUTODETECT, MIDI_NONE, "") != 0) 81 { 82 allegro_message("Error initilizing the sound system"); 83 return; 84 } 85 sounds[1] = load_wav("explode.wav"); 86 87 sounds[2] = load_wav("explode2.wav"); 88} 89 90void drawback() 91{ 92 rectfill(active_page,0,Floors.y,SCREEN_W,SCREEN_H,makecol(255,0,0)); 93 textprintf_ex(active_page,font,0,0,makecol(255,255,255),-1,"Lives: %i Score: %i",lives,score); 94 //show_video_bitmap(active_page); 95} 96 97void movemouse() 98{ 99 circle(active_page,mouse_x,mouse_y,BallSize / 5,makecol(255,255,0)); 100} 101 102void mousehold() 103{ 104 set_mouse_range(0 + (BallSize / 5),0 + (BallSize),SCREEN_W - (BallSize / 5),Floors.y - (BallSize / 5)); 105 if (mouse_b & 1 && mouseh <= 50) 106 { 107 mouseh++; 108 return; 109 } 110 if (mouseh >= 50) 111 { 112 mousex = mouse_x; 113 mousey = mouse_y; 114 while(mouse_b & 1) 115 { 116 textprintf_ex(screen,font,SCREEN_W/2 - (4*19),SCREEN_H/2,makecol(255,255,255),-1,"Dont Hold The Mouse"); 117 } 118 position_mouse(mousex,mousey); 119 } 120 if(!mouse_b & 1) 121 { 122 mouseh = 0; 123 } 124} 125 126void moveball() 127{ 128 //Test to see where actual click area is. Made due to an error found in the program. 129 if (mouse_x > Balls[n].x && mouse_x < Balls[n].x + BallSize && mouse_y > Balls[n].y && mouse_y < Balls[n].y + BallSize) 130 rect(active_page,Balls[n].x,Balls[n].y,Balls[n].x + BallSize,Balls[n].y + BallSize,makecol(255,0,0)); 131 draw_sprite(active_page,ball[i/2],Balls[n].x,Balls[n].y); 132 Balls[n].y = Balls[n].y + (BallSpeed);//Balls.speed; 133 return; 134} 135 136void exploded(void) 137{ 138 draw_sprite(active_page,explode[expd/4],explodes[r].x,explodes[r].y); 139 expd++; 140} 141 142void checkdead() 143{ 144 if (mouse_x > Balls[n].x && mouse_x < Balls[n].x + BallSize && mouse_y > Balls[n].y && mouse_y < Balls[n].y + BallSize && mouse_b & 1) 145 { 146 //exploded(); 147 // going to use this in the explosion which is not yet set up 148 expd = 0; 149 explodes[n].x = Balls[n].x; 150 explodes[n].y = Balls[n].y; 151 r = n; 152 play_sample(sounds[1], volume, pan, pitch, FALSE); 153 exploded(); 154 dead[n] = 1; 155 score += 10; 156 movefloor(); 157 } 158 if (Balls[n].y > Floors.y - BallSize) 159 { 160 expd = 0; 161 explodes[n].x = Balls[n].x; 162 explodes[n].y = Balls[n].y; 163 r = n; 164 play_sample(sounds[2], volume, pan, 500, FALSE); 165 exploded(); 166 Balls[n].y = 0 + BallSize; 167 dropball(); 168 //if (!key[KEY_ALT]) //Testing uses 169 lives--; 170 } 171 172 if (dead[n] != 0) 173 { 174 dropball(); 175 dead[n] = 0; 176 } 177} 178 179void movefloor() 180{ 181 Floors.y = Floors.y - 3; 182} 183 184void dropball() 185{ 186 Balls[n].y = 0 + BallSize; 187 Balls[n].x = rand() % (SCREEN_W - 16); 188 return; 189} 190 191void setupscreen() 192{ 193 int ret = set_gfx_mode(MODE, WIDTH, HEIGHT, 0, 0); 194 if (ret != 0) 195 { 196 allegro_message(allegro_error); 197 return; 198 } 199} 200 201BITMAP *grabframe(BITMAP *source, int width, int height, int startx, int starty, int columns, int frame) 202{ 203 BITMAP *temp = create_bitmap(width,height); 204 int x = startx + (frame ) * width; 205 //int y = starty + (frame ) * height; 206 blit(source,temp,x,0,0,0,width,height); 207 return temp; 208} 209 210void loadsprites(void) 211{ 212 // Load our ball(s) into memory 213 temp = load_bitmap("ball.bmp",NULL); 214 for (n = 0;n<6;n++) 215 ball[n] = grabframe(temp,17,17,0,0,1,n); 216 destroy_bitmap(temp); 217 temp = load_bitmap("explode.bmp",NULL); 218 for (n = 0;n<6;n++) 219 explode[n] = grabframe(temp,17,17,0,0,1,n); 220 destroy_bitmap(temp); 221 222} 223 224void startup(void) 225{ 226 set_mouse_speed(MouseSpeed,MouseSpeed); 227 Floors.y = SCREEN_H; 228 data = load_datafile("falling.dat"); 229 while(!key[KEY_SPACE]) 230 { 231 textprintf_ex(active_page,font,SCREEN_W/2 - (4*19),SCREEN_H/2,makecol(255,255,255),-1,"Get Ready!!!"); 232 textprintf_ex(active_page,font,SCREEN_W/2 - (4*19),SCREEN_H/2+10,makecol(255,255,255),-1,"Press Space to Start!"); 233 show_video_bitmap(active_page); 234 } 235} 236 237void multicheck(void) 238{ 239 /* Slows down the animation to half speed by allowing certain frames 240 of the animation to be played more then once*/ 241 draw_sprite(active_page,data[BACKGROUND2_BMP].dat,0,0); 242 for (n = 0;n < (score/100) + 1;n++) 243 { 244 checkdead(); 245 moveball(); 246 } 247} 248 249void singlecheck() 250{ 251 /* Slows down the animation to half speed by allowing certain frames 252 of the animation to be played more then once*/ 253 i++; 254 if (i >= 12) 255 { 256 i = 0; 257 } 258 if (expd >= 24) 259 { 260 expd = 0; 261 } 262 if (expd >= 1) 263 { 264 exploded(); 265 } 266} 267 268void exitall() 269{ 270 textprintf_ex(screen,font,0,10,makecol(255,255,255),-1,"Last Score was %i Press ESC to Exit",score); 271 while(!key[KEY_ESC]); 272 allegro_exit(); 273} 274 275int main() 276{ 277 setup(); 278 loadsprites(); 279 setupscreen(); 280 BITMAP *buffer = create_video_bitmap(SCREEN_W,SCREEN_H); 281 BITMAP *buffer2 = create_video_bitmap(SCREEN_W,SCREEN_H); 282 active_page = buffer2; 283 startup(); 284 setupsound(); 285 286 while(!key[KEY_ESC] && lives > 0) 287 { 288 multicheck(); 289 singlecheck(); 290 movemouse(); 291 mousehold(); 292 drawback(); 293 checkdead(); 294 295 show_video_bitmap(active_page); 296 if (active_page == buffer) 297 active_page = buffer2; 298 else 299 active_page = buffer; 300 clear_to_color(active_page, makecol(0, 0, 0)); 301 rest(1); 302 303 } 304 unload_datafile(data); 305 destroy_bitmap(buffer); 306 destroy_bitmap(buffer2); 307 destroy_bitmap(*explode); 308 destroy_bitmap(background); 309 destroy_bitmap(active_page); 310 destroy_bitmap(temp); 311 destroy_bitmap(*ball); 312 313 exitall(); 314} 315END_OF_MAIN();

[url http://www.geocities.com/jensen_305/FallingBall1.7r.zip]FallingBall[/url] ClickMe

-I edit a lot. Prepare thyself.

BAF
Member #2,981
December 2002
avatar

That's some ugly code, but what the hell are you doing calling destroy_bitmap on *explode and *ball? That isn't going to work right.

CIRCLE
Member #5,301
December 2004

ugly and works.
don't know most of this code is 2 years old and if it is not broke don't fix it or rewrite it is what I heard.

But eventually I will end up rewriting the entire thing but for now just want to get this part setup. That one thing at a time thing again sorry just dont want to get off the subject.

-I edit a lot. Prepare thyself.

BAF
Member #2,981
December 2002
avatar

No. It only frees the first bitmap. I highly doubt it is doing what you think/want it to do.

CIRCLE
Member #5,301
December 2004

so how can I play the wave from the datafile?

and

how do I destroy the entire thing? *ball

-I edit a lot. Prepare thyself.

BAF
Member #2,981
December 2002
avatar

You have to loop the bitmaps and destroy each one.

for(int i = 0; i < number_of_bitmaps_in_ball; ++i)
    destroy_bitmap(ball<i>);

And to play a wave from a datafile, you have to make sure its loaded correctly and just play it.

DATAFILE *data = load_datafile(my datafile);
if(!data)
    allegro_message("data file failed to load");

play_sample((SAMPLE *)data[MY_WAVE].dat, volume, pan, pitch, 0);
unload_data_file(data);

CIRCLE
Member #5,301
December 2004

I inserted the

play_sample((SAMPLE *)data[EXPLODE_WAV].dat, volume, pan, pitch, 0);

and got the same error.

DATAFILE *data
.
.
data = load_datafile("falling.dat");

still not working.

get an error "initializer element is not constant " when used like

DATAFILE *data = load_datafile("falling.dat");

-I edit a lot. Prepare thyself.

BAF
Member #2,981
December 2002
avatar

What is the error you get? Either the datafile isn't loading right, or the header file is out of date, referring to the wrong position in the datafile.

CIRCLE
Member #5,301
December 2004

I just added the WAV's and then -h the header out and put it up like such

#define BACKGROUND_BMP                   1        /* BMP  */
#define BALL_BMP                         2        /* BMP  */
#define BACKGROUND2_BMP                  0        /* BMP  */
#define EXPLODE2_WAV                     3        /* WAV  */
#define EXPLODE_WAV                      4        /* WAV  */

let me start the dat file from scratch.

BMP's are loaded like this
dat -a -t BMP -bpp 16 falling.dat ball.bmp

WAV's are loaded like this
dat -a -t WAV falling.dat explode.wav

anything wrong?

here is the output I get from -l falling.dat

C:\Dev-Cpp\bin>dat -l falling.dat
Reading falling.dat
- BMP  - BACKGROUND2_BMP              - bitmap (640x480, 16 bit)
- BMP  - BACKGROUND_BMP               - bitmap (640x480, 16 bit)
- BMP  - BALL_BMP                     - bitmap (102x17, 16 bit)
- WAV  - EXPLODE2_WAV                 - binary data (11600 bytes)
- WAV  - EXPLODE_WAV                  - binary data (71902 bytes)

-I edit a lot. Prepare thyself.

gnolam
Member #2,030
March 2002
avatar

Samples are of type SAMP, not WAV.

--
Move to the Democratic People's Republic of Vivendi Universal (formerly known as Sweden) - officially democracy- and privacy-free since 2008-06-18!

CIRCLE
Member #5,301
December 2004

Hmm will try

Well then it seems to work now.

one of those small mistakes. Thanks a lot of all the help.

added

stop_sample((SAMPLE *)data[EXPLODE_WAV].dat);
stop_sample((SAMPLE *)data[EXPLODE2_WAV].dat);
unload_datafile(data);

since you can exit in the middle of a sound which gave an error just thought I would also have to stop the sounds before exit. Thanks again.

also took the advice and added

    for(i = 0; i < 6; ++i)
    destroy_bitmap(explode<i>);
    for(i = 0; i < 6; ++i)
    destroy_bitmap(ball<i>);

thanks for that to

now that the sound is there here is the second thing and I posted about this in 2004 having a problem grabbing from a datafile

1BITMAP *grabframe(BITMAP *source, int width, int height, int startx, int starty, int columns, int frame)
2{
3 BITMAP *temp = create_bitmap(width,height);
4 int x = startx + (frame ) * width;
5 //int y = starty + (frame ) * height;
6 blit(source,temp,x,0,0,0,width,height);
7 return temp;
8}
9.
10.
11temp = load_bitmap("ball.bmp",NULL);
12 for (n = 0;n<6;n++)
13 ball[n] = grabframe(temp,17,17,0,0,1,n);
14 destroy_bitmap(temp);
15 temp = load_bitmap("explode.bmp",NULL);
16 for (n = 0;n<6;n++)
17 explode[n] = grabframe(temp,17,17,0,0,1,n);
18 destroy_bitmap(temp);

I am sure this can be edited the same way to pull from the datafile aswell but the same technique seems not to work with BITMAP instead of SAMPLE

i tried

temp = load_bitmap((BITMAP *)data[EXPLODE_BMP].dat, NULL);

but that didn't seem to work on got this warning.
[Warning] passing arg 1 of `load_bitmap' from incompatible pointer type

     temp = load_bitmap(data[EXPLODE_BMP].dat, NULL);

compiles but error right away

ball[n] = grabframe((BITMAP *)data[EXPLODE_BMP].dat,17,17,0,0,1,n);

and 

ball[n] = grabframe(data[EXPLODE_BMP].dat,17,17,0,0,1,n);

also give the same error or so it seems

If only the book "Game Programming All in One" talked more about datafiles then just 2 pages about using BMP's :)

Enough programming for now back to my "Sword of Truth" series

-I edit a lot. Prepare thyself.

BAF
Member #2,981
December 2002
avatar

You don't want to load the bitmap, as its already the bitmap. Just do mybitmap = (BITMAP *)data[EXLPODE].dat;

However, EXPLODE2_WAV is a WAV, not a bitmap, so good luck with that.

CIRCLE
Member #5,301
December 2004

Hmm another instance of typing it wrong in the forum but either way point got across will try that and see what happens.

Well tried it and error right on start just like all the other attempts.

BITMAP *grabframe(BITMAP *source, int width, int height, int startx, int starty, int columns, int frame) 
{
           BITMAP *temp = create_bitmap(width,height);
           int x = startx + (frame ) * width;
           //int y = starty + (frame ) * height;
           blit(source,temp,x,0,0,0,width,height);
           return temp;
}     
.
.
temp = (BITMAP *)data[BALL_BMP].dat;//load_bitmap("ball.bmp",NULL);
     for (n = 0;n<6;n++)
         ball[n] = grabframe(temp,17,17,0,0,1,n);
     destroy_bitmap(temp);

I am starting to think it is the data file that has something wrong with it again like how I did the sound.

dat -a -t BMP -bpp 16 falling.dat ball.bmp

is what is used to put it in side the file.

But I noticed -g x y w h

NOTE
I fixed the problem by putting the load_datafile command before all the loading ::)

-I edit a lot. Prepare thyself.

Go to: