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
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.
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.
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
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...
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.
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...
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.
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?
I unload the data file assuming I use it when I am just debugging here
1 | #include "FallingBall.h" |
2 | #include "FallingBallSetup.h" |
3 | |
4 | int 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 | } |
44 | END_OF_MAIN() |
that is the main program FallingBall.c
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?
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
[url http://www.geocities.com/jensen_305/FallingBall1.7r.zip]FallingBall[/url] ClickMe
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.
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.
No. It only frees the first bitmap. I highly doubt it is doing what you think/want it to do.
so how can I play the wave from the datafile?
and
how do I destroy the entire thing? *ball
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);
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");
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.
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)
Samples are of type SAMP, not WAV.
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
1 | BITMAP *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 | . |
11 | temp = 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
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.
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