Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Crash because of data file

This thread is locked; no one can reply to it. rss feed Print
Crash because of data file
yipster
Member #7,168
April 2006
avatar

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
Member #4,800
July 2004
avatar

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

--
"The basic of informatics is Microsoft Office." - An informatics teacher in our school
"Do you know Linux?" "Linux? Isn't that something for visually impaired people?"

yipster
Member #7,168
April 2006
avatar

Well I've added

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

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

BAF
Member #2,981
December 2002
avatar

Did oyu make sure data isn't null?

Kitty Cat
Member #2,815
October 2002
avatar

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

--
"Do not meddle in the affairs of cats, for they are subtle and will pee on your computer." -- Bruce Graham

yipster
Member #7,168
April 2006
avatar

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
Member #6,574
November 2005
avatar

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
Member #2,981
December 2002
avatar

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
Member #7,168
April 2006
avatar

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
Member #2,981
December 2002
avatar

What is o_pic in your original source?

yipster
Member #7,168
April 2006
avatar

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

BAF
Member #2,981
December 2002
avatar

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
Member #7,168
April 2006
avatar

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.

Go to: