Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Save to Memory  (Read 3429 times)

0 Members and 1 Guest are viewing this topic.

Tukimitzu

  • Full Member
  • ***
  • Posts: 117
  • Anti-Hero Member
    • View Profile
Save to Memory
« on: August 20, 2016, 02:10:58 am »
Hello all,
I have a bunch of images, fonts, sounds and musics that I want to pack into a single binary file. I know there are loadFromMemory functions, but how to I write these objects in the memory in the first place?

In an ideal world I would have something like this:

// rscMap is a std::map<string, X*> where X can be sf::Sound, sf::Texture, etc...
std::ofstream file("my-pack", ios_base::binary);
file << rscMap.size();
for (auto resource : rscMap){
  file << resource.first;
  file << sizeof(*resource.second);
  file << *resouce.second;
}

If there is a library that already does that I would like to know as well.
Thanks!

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Save to Memory
« Reply #1 on: August 20, 2016, 03:18:45 am »
To load from memory, the only thing you need is to have the file copied to memory.

Read the file using normal c++ file operations, copy it - with byte accuracy - to somewhere in memory, most probably in the heap. Then, you simply pass a pointer, which points to the beginning of that memory, and the size of the file/memory to loadFromMemory.

If you store multiple files into one (the file would just be all the files concatenated), after loading that file into memory, you will need pointers to the beginning of each 'file' inside that block (as well as their sizes).
Instead, you may want to just load the parts of the file that you need separately (obviously, you don't have to load an entire file).

Note that the file in memory must be in exactly the same format as the file. That is, it must have all headers and meta-data. It reads the memory as if it was a file.


If you're looking to save many files into one using SFML, you could use SFML to save the individual file temporarily and then copy that file onto the end of the final 'bundle' file.
You could also use this method to save to memory. That is, save to file temporarily, load it into memory, then delete the temporary file.
« Last Edit: August 20, 2016, 03:22:18 am by Hapax »
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Tukimitzu

  • Full Member
  • ***
  • Posts: 117
  • Anti-Hero Member
    • View Profile
Re: Save to Memory
« Reply #2 on: August 21, 2016, 02:48:19 am »
Ah, of course! It's more trivial than I was thinking.

Read the file using normal c++ file operations, copy it - with byte accuracy....

That's all I needed to read.

Thanks!