Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » saving bitmap into memory instead of a file

This thread is locked; no one can reply to it. rss feed Print
saving bitmap into memory instead of a file
sleepywind
Member #14,665
October 2012

Hello,

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
Member #476
June 2000
avatar

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.

--
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

sleepywind
Member #14,665
October 2012

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
Member #476
June 2000
avatar

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.

--
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

Go to: