saving bitmap into memory instead of a file


so I have some bitmaps and I would like to save them to the disk. However this is done while the game is running (autosave) and slows the game quite a lot (most of the time is burned in the IO). Therefore I spawn a secondary thread to save the bitmaps to the drive. The thread can't operate bitmaps as rich allegro data (because OSX doesn't like that) so I would need to pass the bitmpas as raw memory to the thread. Ideally as stringstreams or something.

So my question is when I have an allegro bitmap how do I dump it to a memory ?

This is probably quite silly. However I have played with ALLEGRO_FILE for a while and haven't figured out how to create one mapped to memory with appropriate size. Thanks for the help.

Thomas Fjellstrom

Take a look at the memfile addon and al_save_bitmap_f.

The memfile addon doesn't support dynamic sizing (which isn't TOO hard to implement, it just hasn't been a priority), so you'll have to create it with a large enough buffer.


Yeah I figured I could create ALLEGRO_FILE via al_open_memfile and then use the al_save_bitmap_f. However is there a way to figure out how big buffer to allocate for a given bitmap ? I searched the bitmap interface and hasn't found anything. Of course I can make some "good enough" estimates by taking the width and height and multipliing that by 4bytes or so, but I would prefer no to guess :)

Thomas Fjellstrom

That's a pretty good guess if you're saving a non-compressed bitmap, you may actually want to make it a bit larger than that. If you're saving it in a compressed form like jpeg or png, that guess should almost always work, since parts of the bitmap will almost always be compressed.

Thread #612339. Printed from