Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Load bitmap from zip file

This thread is locked; no one can reply to it. rss feed Print
Load bitmap from zip file
DarknessEyes
Member #15,611
May 2014

Im trying to convert my A4 project to A5 but im having some issues...
Been searching for hours and i cant load bitmaps directly from a zip file.

Can anyone post a small example please?

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Neil Roy
Member #2,229
April 2002
avatar

It's actually fairly simple to use with PhsyFS that's a file system addon, not a physics addon ;)

Here's what I have (trimmed out the error checking code)

   PHYSFS_init(NULL);
   if(!PHYSFS_mount("MyGame.zip", "/", 1)) {
      // do error stuff here
   }
   al_set_physfs_file_interface();

You'll need to #include <allegro5/allegro_physfs.h> as well for this.

Note, you can rename your file anything you wish, like "MyGame.dat", no need to have "zip" on there if you don't want it so obvious.

Once this is done, all file loading will be from that file. I have folders in my ZIP as well containing graphics, sounds etc... so when I load them I use something like...

ALLEGRO_BITMAP *myimage = NULL;
myimage = al_load_bitmap("Graphics/MyImage.png");

...and it will load the PNG "MyImage.png" from the "Graphics" folder inside your zip file.

Important, if you wish to access a file outside of the zip you need to al_set_standard_file_interface(); first, then do your normal file access, and when you're done, use al_set_physfs_file_interface(); to switch back to your ZIP.

Another way to do this, and probably a better one, is to use:

ALLEGRO_STATE state;
al_store_state(&state, ALLEGRO_STATE_NEW_FILE_INTERFACE);
al_set_standard_file_interface();

// do your file access here

al_restore_state(&state);

---
“I love you too.” - last words of Wanda Roy

DarknessEyes
Member #15,611
May 2014

#SelectExpand
1#include <allegro5/allegro.h> 2#include "allegro5/allegro_image.h" 3#include <physfs.h> 4#include <allegro5/allegro_physfs.h> 5#include <iostream> /// namespace std 6 7using namespace std; 8 9int main(int argc, char **argv) { 10 ALLEGRO_DISPLAY *display = NULL; 11 ALLEGRO_BITMAP *image = NULL; 12 if (!al_init()) { 13 cout << "Failed: al_init" << endl; 14 } 15 16 display = al_create_display(800,600); 17 if(!display) { 18 cout << "Failed: al_create_display" << endl; 19 } 20 21 PHYSFS_init(NULL); 22 if(!PHYSFS_mount("data.zip", "/", 1)) { 23 cout << "Failed: PHYSFS_mount - " << PHYSFS_getLastError() << endl; 24 } 25 al_set_physfs_file_interface(); 26 27 if (!PHYSFS_exists("image.bmp")) { 28 cout << "Failed: PHYSFS_exists - " << PHYSFS_getLastError() << endl; 29 } 30 else { 31 image = al_load_bitmap("image.bmp"); 32 } 33 34 if (!image) { 35 cout << "loading backup.bmp" << endl; 36 al_set_standard_file_interface(); 37 image = al_load_bitmap("backup.bmp"); 38 } 39 40 al_clear_to_color(al_map_rgb(0,0,0)); 41 al_draw_bitmap(image,0,0,0); 42 al_flip_display(); 43 44 al_rest(5.0); 45 46 al_destroy_display(display); 47 48 return 0; 49}

I have the following code to test.

All i get is:

Start loading backup.bmp <crash>

Thomas Fjellstrom
Member #476
June 2000
avatar

is the zip file actually where your program expects it?

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

DarknessEyes
Member #15,611
May 2014

It works!!! i forgot to add the al_init_image_addon() in the test code.

Lets see if it works in the real project now.

Neil Roy
Member #2,229
April 2002
avatar

Good to see you got it working. :)

---
“I love you too.” - last words of Wanda Roy

Eric Johnson
Member #14,841
January 2013
avatar

You may also want to incorporate ALLEGRO_PATH to set the working directory so that your archive can be read no matter what directory it was executed from (this comes in handy if you run the application binary from a command line from within a different directory). I used the following in one of my projects:

#SelectExpand
1 void Routines::setResourceArchive(void) { 2 3 // Get current working directory. 4 ALLEGRO_PATH *al_path = al_get_standard_path(ALLEGRO_RESOURCES_PATH); 5 6 // Switch to directory in which application was run from. 7 al_append_path_component(al_path, "./"); 8 al_change_directory(al_path_cstr(al_path, '/')); 9 10 // Remove path from memory, as the new directory has been reached. 11 al_destroy_path(al_path); 12 13 // @NOTE: evaluate() breaks the game loop if the supplied condition fails. 14 15 evaluate(PHYSFS_init(NULL)); 16 17 // Attempt to read contents of data.dat. 18 evaluate(PHYSFS_mount("data.dat", "/", 1)); 19 20 // All future loading calls will read from data.dat. 21 al_set_physfs_file_interface(); 22}

Go to: