I've read some random tutorials about .dat files but none seem to help me out...
Also there were actually quite many
Like the thread topic says, I'm having problems reading .PNG files from .DAT files.
For starters, I'll load a WhiteBox.png into the .DAT file:
> dat -a -t PNG -bpp 32 test.dat WhiteBox.png
Writing test.dat...
> dat -l test.dat
Reading test.dat
- PNG - WHITEBOX_PNG - binary data (4374 bytes)
It's binary data..? What? Is it ok?
Before I've loaded BMP images from .DAT without any problems.
And when I "read" the .DAT file with BMP images inside it says:
Reading test.dat
- BMP - WHITEBOX_BMP - bitmap (640, 480, 24bit)
What's the difference if it says it's binary data? Can I still read/use it like bmp files?
Naturally this code reads and shows the BMP image without problems, but I guess there's some sort of trick to use PNG images from .DAT files ...or? some tricky way to go around this. Some epic loading trick with loadpng? I don't know. I'm lost in here.
Code:
1 | #include <allegro.h> |
2 | #include "loadpng.h" |
3 | |
4 | #define MODE GFX_AUTODETECT_WINDOWED |
5 | #define WIDTH 640 |
6 | #define HEIGHT 480 |
7 | #define WHITE makecol(255,255,255) |
8 | |
9 | //define objects in datfile |
10 | #define WHITEBOX_PNG 0 |
11 | |
12 | int main(void) |
13 | { |
14 | DATAFILE *data; |
15 | BITMAP *sprite; |
16 | |
17 | allegro_init(); |
18 | install_keyboard(); |
19 | install_timer(); |
20 | set_color_depth(16); |
21 | set_gfx_mode(MODE, WIDTH, HEIGHT, 0, 0); |
22 | |
23 | data = load_datafile("test.dat"); |
24 | |
25 | //grab sprite and store it in BITMAP |
26 | sprite = (BITMAP *)data[WHITEBOX_PNG].dat; |
27 | draw_sprite(screen, sprite, WIDTH/2-sprite->w/2,HEIGHT/2-sprite->h/2); |
28 | |
29 | //pause |
30 | while(!keypressed()); |
31 | |
32 | allegro_exit(); |
33 | return 0; |
34 | } |
35 | END_OF_MAIN() |
So any help on how to get the PNG images OUT of .DAT files would be appreciated. I'm struggling and after hitting my head on the wall for three hours... I can't figure this one out by myself.
Help me, please.
EDIT #1: Oh no, after I don't know how many searches I finally ran into something I could "use"...
http://www.allegro.cc/forums/thread/262390
So basically replace the whole datafile loading with load_png("datafile.dat#filename_png", NULL), but is this really the answer? Does this work with all the .DAT features like encryption?
The dat utility doesn't know what a PNG so it's treating it as simple binary data, and not as a BITMAP. I think loadpng comes with a plugin for Grabber that enables you to store PNGs in a datafile as BITMAPs. Another way to go would be to check if loadpng knows how to load PNGs from memory, or maybe try load_png("test.dat#WHITEBOX_PNG"). Did you look at the loadpng documentation?
Store the PNGs in the datafile with a type id of "PNG". Then you just need to call init_loadpng() before loading the datafile and it will Magically WorkTM.
Thanks for both answers guys, I kinda moved further ahead again
I don't know why I completely missed the actual example from \example with the loadpng files ( exdata.c - loading a PNG from a datafile (exdata.dat) ).
I think the code uses the id thingie you mentioned gnolam. Ok, I'm having one concern anymore, if I use this method, does it affect the image in any possible way? Or does it pop out from the .DAT file just the way I stored it in (referring to alpha channel and other minor things which I'm in love with now :p), don't want to loose them now. After so much work, heh.
And for Ron, nope, I didn't really look into the documentation that profoundly.
EDIT #1: Did some changes to the example and storet another PNG image into the .DAT file and yes, it kinda messes the picture. The example alpha.png which comes with loadpng turns into a box err... before it was this shiny, transparent ball thingie (can't descript it better) and now it's just some rainbow colored box. Definately something I don't want.
I really need the PNG files like "they were".
EDIT #2: So that leaves me with only one choice, to somehow save them as bitmaps but with grapper, now that's annoying. The program really needs some rework on the "easy to use"-department :p Guess it's time to try and figure this one out. I'll spend the next three hours with this
EDIT #3: gnolam, you don't happen to referring to this:
extern int loadpng_init(void); ?
The example alpha.png which comes with loadpng turns into a box err... before it was this shiny, transparent ball thingie (can't descript it better) and now it's just some rainbow colored box. Definately something I don't want.
In the grabber or in your program? You need 32 bit color for alpha transparency in Allegro, while you're setting a 16 bit mode in your code above.
And yes, I meant loadpng_init(). I'm sleep deprived and have spent the entire day staring at, and tweaking, code for a servo-mounted sonar. Which happens to use an init_foo() naming convention.
Oh wow, it's a miracle. It works.
Note(s) to self:
- REMEMBER TO SET THE ALPHA_BLENDER!
- Why do you keep using the bit depth 16?! It's 32! 32!!
And thanks guys, I was waaayyy off again. Simple things, simple things. The actual ID trick seemed to do the it, really didn't get into the extern int loadpng_init(void) just yet. Maybe that's just a good thing(?).
My eyes hurt from so much coding and trying to read my own wreck of a code... Oh well, it's time to get some sleep now.
So I'd say this is solved. I'll get back to it if it is not. Thanks again guys. And gnolam, you try getting some sleep.
Here is the code for those with similiar problems:
1 | #include <png.h> |
2 | #include <allegro.h> |
3 | #include "loadpng.h" |
4 | |
5 | #define DAT_PNG DAT_ID('P','N','G',' ') |
6 | |
7 | int main(void) |
8 | { |
9 | BITMAP *bmp; |
10 | DATAFILE *data; |
11 | int depth = 32; |
12 | |
13 | allegro_init(); |
14 | install_keyboard(); |
15 | |
16 | register_png_datafile_object(DAT_PNG); |
17 | |
18 | set_color_depth(depth); |
19 | set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0); |
20 | clear_bitmap(screen); |
21 | |
22 | data = load_datafile("exdata.dat"); |
23 | |
24 | bmp = (BITMAP *)data[0].dat; |
25 | |
26 | set_alpha_blender(); |
27 | clear_to_color(screen, makecol(255,255,255)); |
28 | draw_trans_sprite(screen, bmp, (SCREEN_W - bmp->w) / 2, (SCREEN_H - bmp->h) / 2); |
29 | |
30 | readkey(); |
31 | return 0; |
32 | } |
33 | |
34 | END_OF_MAIN() |