Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Physfs problem (cont'd)

This thread is locked; no one can reply to it. rss feed Print
Physfs problem (cont'd)
xtheunknown0
Member #13,772
November 2011

Hello,

Continuing from http://www.allegro.cc/forums/thread/609005,
I've added

   al_set_physfs_file_interface();

   ALLEGRO_FILE *file;
   if (!(file = al_fopen("02.gif", "r"))) {
      al_draw_textf(font, al_map_rgb(255, 255, 255), 250, 350, 0, "fopen: Error loading gif");
      al_flip_display();
      al_rest(4.0);
   }

before the start of my last post on the old thread. This new code works, but again, I can't load the gif. Any suggestions, please?

TIA,
xtheunknown0

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

xtheunknown0
Member #13,772
November 2011

#SelectExpand
1#include <stdio.h> 2#include <allegro5/allegro.h> 3#include <allegro5/allegro_primitives.h> 4#include <allegro5/allegro_image.h> 5#include <allegro5/allegro_physfs.h> 6#include <physfs.h> 7#include <allegro5/allegro_font.h> 8#include <allegro5/allegro_ttf.h> 9 10static void show_image(ALLEGRO_BITMAP *bmp)/*{{{*/ 11{ 12 ALLEGRO_EVENT_QUEUE *queue; 13 ALLEGRO_EVENT event; 14 15 queue = al_create_event_queue(); 16 al_register_event_source(queue, al_get_keyboard_event_source()); 17 18 while (true) { 19 al_draw_bitmap(bmp, 0, 0, 0); 20 al_flip_display(); 21 al_wait_for_event(queue, &event); 22 if (event.type == ALLEGRO_EVENT_KEY_DOWN 23 && event.keyboard.keycode == ALLEGRO_KEY_ESCAPE) { 24 break; 25 } 26 } 27 28 al_destroy_event_queue(queue); 29}/*}}}*/ 30 31int main(int argc, const char *argv[]) 32{ 33 ALLEGRO_DISPLAY *display; 34 ALLEGRO_BITMAP *bmp; 35 ALLEGRO_FS_ENTRY *entry; 36 int i; 37 38 if (!al_init()) 39 return 1; 40 al_init_primitives_addon(); 41 al_init_font_addon(); 42 al_init_ttf_addon(); 43 44 display = al_create_display(640, 480); 45 if (!display) { 46 return 1; 47 } 48 49 al_init_image_addon(); 50 51 ALLEGRO_PATH *path = al_get_standard_path(ALLEGRO_RESOURCES_PATH); 52 53 al_append_path_component(path, "resources"); 54 al_change_directory(al_path_cstr(path, '/')); // change the working directory 55 ALLEGRO_FONT *font = al_load_ttf_font("DejaVuSans.ttf", 12, 0); 56 if (!font) { 57 al_draw_textf(font, al_map_rgb(255, 255, 255), 50, 1, 0, "Error loading font.\n"); 58 al_rest(4.0); 59 return 1; 60 } 61 62 63 64 al_remove_path_component(path, -1); 65 al_change_directory(al_path_cstr(path, '/')); // change the working directory 66 al_draw_textf(font, al_map_rgb(255, 255, 255), 200, 200, 0, "%s", al_path_cstr(path, '/')); 67 /* Set up PhysicsFS. */ 68 if (!PHYSFS_init(argv[0])) { 69 al_draw_textf(font, al_map_rgb(255, 255, 255), 50, 1, 0, "Error initialising physicsfs"); 70 al_rest(4.0); 71 return 1; 72 } 73 74 // This creates a ~/.allegro directory, which is very annoying to say the/*{{{*/ 75 // least - and no need for it in this example. 76 // if (!PHYSFS_setSaneConfig("allegro", "ex_physfs", NULL, 0, 0)) 77 // return 1;/*}}}*/ 78 if (!PHYSFS_addToSearchPath("./data/ex_physfs.zip", 1)) { 79 al_draw_textf(font, al_map_rgb(255, 255, 255), 50, 50, 0, "Error adding to search path."); 80 al_flip_display(); 81 al_rest(4.0); 82 return 1; 83 } 84 85 al_set_physfs_file_interface(); 86 87 ALLEGRO_FILE *file; 88 if (!(file = al_fopen("02.gif", "r"))) { 89 al_draw_textf(font, al_map_rgb(255, 255, 255), 250, 350, 0, "fopen: Error loading gif"); 90 al_flip_display(); 91 al_rest(4.0); 92 } 93 94 bmp = al_load_bitmap_f(file, ".gif"); 95 if (bmp) { 96 show_image(bmp); 97 al_rest(4.0); 98 al_destroy_bitmap(bmp); 99 } 100 else { 101 al_draw_textf(font, al_map_rgb(255, 255, 255), 50, 350, 0, "Error loading gif"); 102 al_flip_display(); 103 al_rest(4.0); 104 } 105 106 PHYSFS_deinit(); 107 al_destroy_path(path); 108 return 0; 109}

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

And what does that code print out?

I made a simplified test case :

#SelectExpand
1 2#include <allegro5/allegro.h> 3#include <allegro5/allegro_image.h> 4#include <allegro5/allegro_physfs.h> 5 6#include <physfs.h> 7 8#include <iostream> 9using std::cout; 10using std::endl; 11 12 13int main(int argc, const char *argv[]) 14{ 15 ALLEGRO_DISPLAY *display; 16 ALLEGRO_BITMAP *bmp; 17// ALLEGRO_FS_ENTRY *entry; 18// int i; 19 20 if (!al_init()) { 21 cout << "Failed to initialize allegro 5." << endl; 22 return 1; 23 } 24 25 display = al_create_display(640, 480); 26 if (!display) { 27 cout << "Failed to create display." << endl; 28 return 1; 29 } 30 31 al_init_image_addon(); 32 33 ALLEGRO_PATH *path = al_get_standard_path(ALLEGRO_RESOURCES_PATH); 34 al_change_directory(al_path_cstr(path, '/')); // change the working directory 35 cout << "Current working directory is " << al_get_current_directory() << endl; 36 al_destroy_path(path); 37 38 if (!PHYSFS_init(argv[0])) { 39 cout << "Failed to initialize PhysFS." << endl; 40 return 1; 41 } 42 43 if (!PHYSFS_addToSearchPath("./data/test.zip", 1)) { 44 cout << "Error adding ./data/test.zip to search path." << endl; 45 return 1; 46 } 47 48 const char* file_name = "test.gif"; 49 al_set_physfs_file_interface(); 50 51 ALLEGRO_FILE *file; 52 if (!(file = al_fopen(file_name, "r"))) { 53 cout << "Could not open " << file_name << " for reading." << endl; 54 return 1; 55 } 56 else { 57 cout << "Opened file " << file_name << " for reading." << endl; 58 } 59 60 bmp = al_load_bitmap_f(file, ".gif"); 61 if (bmp) { 62 cout << "Loaded " << file_name << " using al_load_bitmap_f." << endl; 63 al_clear_to_color(al_map_rgba(0,0,0,0)); 64 al_draw_bitmap(bmp , 0 , 0 , 0); 65 al_flip_display(); 66 al_rest(5.0); 67 al_destroy_bitmap(bmp); 68 } 69 else { 70 cout << "Failed to load " << file_name << " from ALLEGRO_FILE* using al_load_bitmap_f." << endl; 71 } 72 al_fclose(file); 73 74 PHYSFS_deinit(); 75 return 0; 76}

And here's a 7-zip of the source, exe, dlls, and data/test.zip file.

The gif in the zip file doesn't display properly, but if I do the exact same thing with a png instead, then it displays perfectly.

I think your gif is bad. Try loading it regularly (not using Physfs) and see if it is still NULL.

xtheunknown0
Member #13,772
November 2011

Well I've tried to load a png file instead. I got this from http://images2.wikia.nocookie.net/__cb20090331234637/uncyclopedia/images/8/80/Smiley.png

Here, I get a blank screen.

#SelectExpand
1#include <stdio.h> 2#include <allegro5/allegro.h> 3#include <allegro5/allegro_primitives.h> 4#include <allegro5/allegro_image.h> 5#include <allegro5/allegro_physfs.h> 6#include <physfs.h> 7#include <allegro5/allegro_font.h> 8#include <allegro5/allegro_ttf.h> 9 10int main(int argc, const char *argv[]) 11{ 12 ALLEGRO_DISPLAY *display; 13 ALLEGRO_BITMAP *bmp; 14 ALLEGRO_FS_ENTRY *entry; 15 int i; 16 17 if (!al_init()) 18 return 1; 19 al_init_primitives_addon(); 20 al_init_font_addon(); 21 al_init_ttf_addon(); 22 23 display = al_create_display(640, 480); 24 if (!display) { 25 return 1; 26 } 27 28 al_init_image_addon(); 29 30 ALLEGRO_PATH *path = al_get_standard_path(ALLEGRO_RESOURCES_PATH); 31 32 al_append_path_component(path, "resources"); 33 al_change_directory(al_path_cstr(path, '/')); // change the working directory 34 ALLEGRO_FONT *font = al_load_ttf_font("DejaVuSans.ttf", 12, 0); 35 if (!font) { 36 al_draw_textf(font, al_map_rgb(255, 255, 255), 50, 1, 0, "Error loading font.\n"); 37 al_rest(4.0); 38 return 1; 39 } 40 41 al_remove_path_component(path, -1); 42 al_change_directory(al_path_cstr(path, '/')); // change the working directory 43 ALLEGRO_FILE *file; 44 al_load_bitmap("./data/Smiley.png"); 45 al_rest(4.0); 46 al_draw_textf(font, al_map_rgb(255, 255, 255), 200, 200, 0, "%s", al_path_cstr(path, '/')); 47 al_destroy_path(path); 48 49 /* close_log(false); */ 50 return 0; 51}

A screenshot of the output from the previous post is attached.

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

You get a blank screen because you never flipped the display. Load the png, make sure it gave you a valid ALLGRO_BITMAP*, then add it to a zip file and try again.

Like I said, your gif is probably corrupt or invalid. Try loading it with al_load_bitmap instead, and see if it works.

Arthur Kalliokoski
Second in Command
February 2005
avatar

IIRC Matt explained in another thread that Allegro still doesn't support GIF, even though the patent expired years ago.

They all watch too much MSNBC... they get ideas.

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Matthew Leverton
Supreme Loser
January 1999
avatar

In A5, Only BMP, PCX, and TGA are guaranteed to be supported. Support for JPEG and PNG will probably have been enabled.

OS X and Windows will support GIF (the first frame) if the OS-native image loading libraries are being used.

Elias
Member #358
May 2000

IIRC Matt explained in another thread that Allegro still doesn't support GIF, even though the patent expired years ago.

We never cared about the patent, see my gif addon for A4 which was published when the patent was still active. It was up to the user of the addon to deal with the patent.

--
"Either help out or stop whining" - Evert

Arthur Kalliokoski
Second in Command
February 2005
avatar

Elias said:

We never cared about the patent

Allegro 3.12 faq.txt said:

Why can't Allegro read GIF files?

Unisys has a patent on the LZW compression algorithm that is used by
the GIF format. I want everything in Allegro to be freely usable
without any restrictions whatsoever, which means I can't include any
code that is subject to licensing or the payment of royalties.

They all watch too much MSNBC... they get ideas.

Elias
Member #358
May 2000

Quote:

Allegro 3.12 faq.txt said:

Well, 3.12 was before my time (as developer) :P

--
"Either help out or stop whining" - Evert

xtheunknown0
Member #13,772
November 2011

Solved. Thanks to everyone.

#SelectExpand
1#include <stdio.h> 2#include <allegro5/allegro.h> 3#include <allegro5/allegro_primitives.h> 4#include <allegro5/allegro_image.h> 5#include <allegro5/allegro_physfs.h> 6#include <physfs.h> 7#include <allegro5/allegro_font.h> 8#include <allegro5/allegro_ttf.h> 9 10int main(int argc, const char *argv[]) 11{ 12 ALLEGRO_DISPLAY *display; 13 ALLEGRO_BITMAP *bmp; 14 ALLEGRO_FS_ENTRY *entry; 15 int i; 16 17 if (!al_init()) 18 return 1; 19 al_init_primitives_addon(); 20 al_init_font_addon(); 21 al_init_ttf_addon(); 22 23 display = al_create_display(640, 480); 24 if (!display) { 25 return 1; 26 } 27 28 al_init_image_addon(); 29 ALLEGRO_PATH *path = al_get_standard_path(ALLEGRO_RESOURCES_PATH); 30 31 al_append_path_component(path, "resources"); 32 al_change_directory(al_path_cstr(path, '/')); // change the working directory 33 ALLEGRO_FONT *font = al_load_ttf_font("DejaVuSans.ttf", 12, 0); 34 if (!font) 35 return 1; 36 37 al_remove_path_component(path, -1); 38 al_append_path_component(path, "data"); 39 al_change_directory(al_path_cstr(path, '/')); // change the working directory 40 al_draw_textf(font, al_map_rgb(255, 255, 255), 200, 400, 0, "%s", al_path_cstr(path, '/')); 41 ALLEGRO_FILE *file; 42 43 /* Set up PhysicsFS. */ 44 if (!PHYSFS_init(argv[0])) 45 return 1; 46 47 if (!PHYSFS_addToSearchPath("ex_physfs.zip", 1)) { 48 return 1; 49 } 50 51 al_set_physfs_file_interface(); 52 53 if (!(bmp = al_load_bitmap("smiley.bmp"))) 54 return 1; 55 56 al_draw_bitmap(bmp, 0, 0, 0); 57 al_flip_display(); 58 al_rest(4.0); 59 al_destroy_path(path); 60 61 return 0; 62}

Go to: