Crash because of data file
yipster

I had a whole bunch of images and midi files, and decided to try using a data file. It's all fine and dandy when compiling, but crashes when it tries to 'draw_sprite()'.

I've commented what I think is relevant. As the code shown here was done in the heat of the moment, and I just did whatever popped into my brain for x's and o's. So any tips on fixing it up or efficiency is welcome.
[Note: I've attached everything in a rar]

Here is a snippet: (max post size is 64k :( so I just cut and pasted the relevant stuff)

1#include <allegro.h>
2 
3DATAFILE *data;
4BITMAP *x_pic;
5 
6 
7data = load_datafile ("game.dat");
8
9x_pic = (BITMAP *)data[0].dat;
10 
11else if(key[KEY_ENTER] && squares[location] != 1 && squares[location] != 2)
12 {
13 if(turn == 1)
14 {
15 draw_sprite( screen, x_pic, x, y);
16 squares[location] = turn;
17 count++;
18 }
19 else if(turn == 2)
20 {
21 draw_sprite( screen, o_pic, x, y);
22 squares[location] = turn;
23 count++;
24 }
25
26 check_win();
27 }

Michael Faerber

Have you checked if x_pic is zero? Have you properly initialized everything?

yipster

Well I've added

DATAFILE *data = NULL;
BITMAP *x_pic = NULL;

Still crashes when it tries to draw_sprite(). :(

BAF

Did oyu make sure data isn't null?

Kitty Cat

Are you sure the sprite is the first object in the datafile?

yipster
BAF said:

Did oyu make sure data isn't null?

Not sure what you mean. I've intialized data and made sure it was NULL. And then "data = load_datafile ("game.dat");"

Kitty Cat said:

Are you sure the sprite is the first object in the datafile?

Hmm...I used the grabber, and the 'x' picture is at the top. So unless the grabber index's the last item as '0' , then I'm pretty sure the 'x' picture is at the top.

scriptX

Maybe it's failing to load?

if (!(data = load_datafile ("game.dat"))) {
  printf("error loading data file!\n");
  return -1;
}

// --- or ---

data = load_datafile ("game.dat");
if (!data) {
  printf("error loading data file!\n");
  return -1;
}

BAF
Quote:

Not sure what you mean. I've intialized data and made sure it was NULL. And then "data = load_datafile ("game.dat");"

I meant what scriptX meant in the example he posted.

yipster

I did something like this to make sure and commented out all the game function calls. Compiled and ran, result: data file did not fail to load.

1int main(){
2 allegro_init();
3 install_keyboard();
4 install_sound(0,MIDI_AUTODETECT,0);
5 set_color_depth(16);
6 set_gfx_mode( GFX_AUTODETECT, 640, 480, 0, 0);
7 
8 data = load_datafile ("game.dat");
9 
10 if (!data) {
11 
12 textout_centre_ex( screen, font, "error loading", 320, 240, makecol( 255, 255, 255), makecol(0, 0, 0));
13 return -1;
14
15 
16 }
17
18 readkey();
19}

BAF

What is o_pic in your original source?

yipster

x_pic represents the bitmap for 'X'
o_pic represents the bitmap for 'O'

BAF

Is it trying to draw o_pic and failing because it is null? (I dont see anything regarding it in your source so I cant be sure).

yipster

At the start of the game, X goes first. So I'm pretty sure it has nothing to do with o_pic. As for the code posted at the beginning, there just snippets of what I thought was relevant. The entire source is attached.

Thread #581977. Printed from Allegro.cc