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 | |
3 | DATAFILE *data; |
4 | BITMAP *x_pic; |
5 | |
6 | |
7 | data = load_datafile ("game.dat"); |
8 | |
9 | x_pic = (BITMAP *)data[0].dat; |
10 | |
11 | else 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 | } |
]]>
Have you checked if x_pic is zero? Have you properly initialized everything?
]]>Well I've added
Still crashes when it tries to draw_sprite().
]]>Did oyu make sure data isn't null?
]]>Are you sure the sprite is the first object in the datafile?
]]>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");"
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.
]]>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; }
]]>
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.
]]>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.
1 | int 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 | } |
]]>
What is o_pic in your original source?
]]>x_pic represents the bitmap for 'X'
o_pic represents the bitmap for 'O'
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).
]]>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.
]]>