Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Loading image from .dat file

This thread is locked; no one can reply to it. rss feed Print
Loading image from .dat file
moodle
Member #11,648
January 2010

So, I'm pretty much stuck with this for quite a while now

I'm trying to put all my images in a .dat file but grabber doesn't work in Vista, so I got dat for linux and put all the images in a .dat file using linux. The only problem is that I can't get the images to be loaded in the game. It compiles fine, but the images don't show up on the screen.
My code for loading the images is pretty much this:

#SelectExpand
1DATAFILE *data; 2BITMAP *background; 3packfile_password("password"); 4data = load_datafile("dat1.dat"); 5packfile_password(NULL); 6background = (BITMAP*) data[BACKGROUND_BMP].dat

Is something wrong with my code?

William Labbett
Member #4,486
March 2004
avatar

I've a feeling it would help if you showed more code.

Also, there should be a ';' on the last statement but I guess you cut and pasted a bit inaccurately missing the last character.

I don't think anyone can diagnose your problem from the code you posted because it all seems okay.

moodle
Member #11,648
January 2010

What else would you like to see? I've attached my main file just in case

William Labbett
Member #4,486
March 2004
avatar

Sorry moodle.

I can tell by your code that you know more about it all than me. You need someone else to help.

Just wondered if you'd called allegro_init() before loading the datafile.

EDIT : something tells me you're problem is because you made the dat file on linux though.

OnlineCop
Member #7,919
October 2006
avatar

This is what I've seen some do:

#SelectExpand
1void init_players(void) 2{ 3 DATAFILE *pb; 4 unsigned int i, j; 5 6 pb = load_datafile_object(PCX_DATAFILE, "USCHRS_PCX"); 7 8 if (!pb) 9 { 10 printf("Could not load character graphics!"); 11 exit(0); 12 } 13 14 for (i = 0; i < MAX_CHARACTERS; i++) 15 { 16 for (j = 0; j < MAX_FRAMES_PER_CHAR; j++) 17 { 18 blit((BITMAP *) pb->dat, frames[i][j], j * ENTITY_W, i * ENTITY_H, 0, 0, ENTITY_W, ENTITY_H); 19 } 20 } 21 22 unload_datafile_object(pb); 23}

It may not be the way your dat file is set up, but it may help. :)

moodle
Member #11,648
January 2010

@OnlineCop
Since I'm using global compression, the load_datafile_object will be slow, and I'll only try this after I think there's no more chance of figuring this out.

I mean, my problem doesn't seem to be with loading the file. I've added an if statement to print something if I wasn't able to load it, and it didn't print anything. So I have no idea of wtf is going on..

OnlineCop
Member #7,919
October 2006
avatar

I was more referring to line 18...

William Labbett
Member #4,486
March 2004
avatar

What do you get if you check background after this line :-

BITMAP *background = (BITMAP*) data[BACKGROUND_BMP].dat;//load_bitmap("background.bmp", NULL);

eg

if(background == NULL)
{
   printf("background is NULL.\n");
   exit(-1);
}

OnlineCop
Member #7,919
October 2006
avatar

I dunno; you only sent main.cpp, and we need

#SelectExpand
1#include "common.h" 2#include "includes.h" 3#include "gamebasics.h" 4#include "dat.h"

;)

moodle
Member #11,648
January 2010

@William
I've tried doing this....and the game runs just the same, without printing or exiting..

@OnlineCop
I can send the other files, but I don't think they're important..They have the classes I wrote(and there are quite a few :P) and dat.h is the header created when I used dat on linux. I've attached it, and if you want I could attach my classes later.

EDIT: Ok, nevermind it...I guess the problem was when I created the .dat file in linux. Thanks anyways

William Labbett
Member #4,486
March 2004
avatar

What about if you try :-

blit(background, screen, 0, 0, 0, 0, background->w, background->h);
readkey();

...right after you load it.

Again I'm probably underestimating your abilities but I wondered if there might be some other error in your code which would mean the bitmap's getting drawn somewhere off-screen.

moodle
Member #11,648
January 2010

@William
Nah, I've managed to make the image appear properly

Now, since my images will be loaded by many different classes in different files, I was trying to put my DATAFILE as a global variable, but my compiler is complaining(probably about the packfile_password()- not 100% sure about this.

I declared my DATAFILE as an extern variable on my gamebasics.h file, and I'm doing this in the .cpp:

#SelectExpand
1packfile_password("password"); 2DATAFILE* data = load_datafile("data.dat"); 3packfile_password(NULL);

And the error I get is: error:
expected constructor, destructor, or type conversion before '(' token

This error pops up on the 2 lines where I'm using packfile_password..Any clues?

OnlineCop
Member #7,919
October 2006
avatar

If you want DATAFILE* data to be global, shouldn't this work fine?

DATAFILE* data = NULL;

...

void that_function(void)
{
  ...
  packfile_password("password");
  data = load_datafile("data.dat");


  ...
}

moodle
Member #11,648
January 2010

Yes, it should :P
But it isn't :-/

Some of the images appear just fine, and others are completely screwed up. And now the game stops working all of a sudden..
I think I'll go to bed now, and try to fix this tomorrow

cwl157
Member #11,639
January 2010

If the grabber won't work you can use the dat.exe command line tool to add files to a dat file.

Also, i ran into the same problem. Is this a C program or C++ program? I just wrote a game in C and put all my images into a dat file and ran into the same thing. To fix it i had to load all the images for all the sprites before i setup the sprite data like x, y, etc. This is assuming the BITMAP images for the sprites are separate from the other sprite data. It isn't as organized but its the only thing i found to fix it. If you are using C like i did, i used malloc to create the sprites, my only guess is somehow calling this before loading an image was somehow messing it up. So by loading all the images first and then using malloc to create the sprites fixed it.

LennyLen
Member #5,313
December 2004
avatar

moodle said:

I'm trying to put all my images in a .dat file but grabber doesn't work in Vista

Grabber runs just fine with Vista.

Go to: