".dat" files? what, who, were and why?
jaime barrachina

I've recently started whith C++ (and with programming, come to that) and everyday I find surprises, today, the winner is the ".dat" file. While cheking out the code to make a RPG's map I come to find that all the images and data concerning them are in a misterious file, sealed off from mortal eyes, called "data.dat".

So, what exactly is a .dat file? what does it do, how does it work, and how can I open and edit it? Plese escuse my incredibly basic question but as ignorance ain't a crime (yet) I think I'll do my best to put a remedy to it, if any of you can help me out, I'd be realy thankfull cause its really frustrating to go trught the page of code labeled map.cpp only to find lonelly drawing functions there (which come to it I still don't fully understand xD)

Cheers and thanks! ;)

Arthur Kalliokoski

You're talking about Allegro? If you've downloaded & installed it, check out the manuals in /allegro/docs/html or /allegro/docs/text etc. In the tools section there's a "grabber.exe" program that'll show you what's in the .dat files & import/export bitmaps, wave files, etc.


It is basically a compressed file, like a tar.gz or a .zip. You can either load it all in memory, or load just chuncks as you need them. Check grabber.txt and datafile.txt somewhere in Allegro source to learn more about the technical details.

jaime barrachina

Oks thanks people :)
(wow that was quick, 5 minutes since i posted O.O )


Like others have said, the .dat files are generally produced by grabber (in Allegro programs, anyways). This isn't always the case, though. the .dat extension could be used for virtually anything. Someone might want to serialize their classes out into a random-access .dat file (in short - a simple database), which is much different than a compressed file that could contain several medias.

Johan Halmén

The Allegro dat files don't have to be compressed. If they aren't, they are there only to keep everything in as few file as possible. You could have your 100 sprites in 100 bmp files or in one dat file.

jaime barrachina

Mmm, ive managed to open the file whith the grabber and it shows a kind of bunched up images compresed into a single drawing, pieces and bits of trees, fences, grass, ect. I supose the program somehow cuts the picture like if it were on a grid, how does it do that? Anyone could post a simple example of the code needed to slice my rectangular pizza? Txs a bunch people, i dont know wht i'd do whithout this forum (Rott in some forgoten room, surrounded by empty coke cans and pizza boxes i supose)


Kitty Cat

That's called a tileset. It's a single big image that holds many different tiles and/or sprites used for a specific area of a game. Typically, each tile/sprite is a fixed size (16x16 or 32x32 usually), so you just need to use create_sub_bitmap at 16 or 32 pixel intervals to grab each one.

Johan Halmén

i dont know wht i'd do whithout this forum

You would be having a life. Like we would.

Johan Halmén said:

You would be having a life. Like we would.

And still you let your children in here? :)

jaime barrachina

I supose xDDD


The following is an assumption:

You are looking at my datafile that I created. The bitmap itself is a complete set of tiles. There are simple ways of accessing which tile I need at any point. The bitmap is arranged as 256x1024 or 8x32 of 32x32 pixel tiles.

#define TILESIZE        32
#define SHIFT           5 // 2^SHIFT = TILESIZE
#define TILE_SET_WIDTH  8

void draw_tile( BITMAP *buffer, BITMAP *tiles, int p, int x, int y )
    int tx = ( p % TILE_SET_WIDTH ) << SHIFT;
    int ty = ( p / TILE_SET_WIDTH ) << SHIFT;

    blit( tiles, buffer, tx, ty, x, y, TILESIZE, TILESIZE );

Let's say that you want to draw the tile at 2 rows down and 4 from left at screen position 128x128. That would be tile ( 2 * TILE_SET_WIDTH ) + 4 = 20;

draw_tile( buffer, (BITMAP*)datafile[ BMP_TILES ].dat, 20, 128, 128 );

20 is broken down in the function as

tx = ( 20 % 8 ) << 5 = 128
ty = ( 20 / 8 ) << 5 = 64

So the section of the bitmap with be drawn like:

blit( tiles, buffer, 128, 64, 128, 128, 32, 32 );

Or for a grid

4void draw_map( BITMAP *buffer )
6 for ( int j = 0; j < SCREEN_GRID_HEIGHT; j++ )
7 {
8 for ( int i = 0; i < SCREEN_GRID_WIDTH; i++ )
9 {
10 int p = get_from_map( i, j );
12 draw_tile( buffer, (BITMAP*)datafile[ BMP_TILES ].dat, p, i << SHIFT, j << SHIFT );
13 }
14 }


If you need to do special things to the tiles such as transparency, translucency, etc. This will not be possible. You would then need to separate the entire tile bitmap into 256 bitmaps. This can be done easily enough in the grabber program.

1. Read in a bitmap
2. Grab from Grid
3. Set the tile size you need
4. Enter the name such as TILES

You will end up with 256 tiles named TILES000 to TILES255

Or you could do it yourself in a function

1BITMAP *tiles[ 256 ];
3int tilefy( BITMAP *bitmap )
5 for ( int i = 0; i < 256; i++ )
6 {
7 if ( tiles[ i ] )
8 {
9 destroy_bitmap( tiles[ i ] );
10 tiles[ i ] = NULL;
11 }
13 tiles[ i ] = create_bitmap( TILESIZE, TILESIZE );
14 if ( !tiles[ i ] )
15 {
16 return -1;
17 }
19 blit( bitmap, tiles[ i ], ( i % TILE_SET_WIDTH ) << SHIFT, ( i / TILE_SET_WIDTH ) << SHIFT, 0, 0, TILESIZE, TILESIZE );
21 }
23 return 0;

jaime barrachina

TXS for all the replies!! I'll try and see if anithing comes out xD
Cheers and txs again!!

Thread #562611. Printed from Allegro.cc