• Advertisement
Sign in to follow this  

[C++]File packer

This topic is 3517 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Alright , like i said on my previous thread , im working on something sort of engine(well,more like a framework). I decided to create a simple utility that reads a file byte-by-byte , and then saves the file entry in a buffer(vector char array). Now , i would like to know , how can i use the loaded file to actually.. load the packed memory block. Example:

Packer p("block.bin");

std::vector<char> buf = p.getData("Background.tga"); //copy data to buffer("buf")

//Now , buf holds every byte of "background.tga" texture.
//How can i actually use the array to load the file?
//Resource texture manager , takes the following parameters to load a file:
// "Filename","TextureName"

Any ideas?

Share this post


Link to post
Share on other sites
Advertisement
Could you be more clear, I am having a hard time following. You are wondering how to actually get the data from the file?

Share this post


Link to post
Share on other sites
You change your resource manager so it doesn't need a filename, or it doesn't try to load from a file. All your resource manager will be doing at the moment is loading the file into memory and then parsing it surely?

Share this post


Link to post
Share on other sites
Thanks for the replies..

@CmpDev:
Exactly , but... my problem is that i can't figure out how should
i read the file from the memory block.

The article only explains the packing procedure , what i want is to read
a packed file , and then load its contents.


@Evil Steve:

Well , if i do this , then i will
have to change every single resource handling function.
And not only.

For example , to store texture i will have to follow the following procedure:

--PACKING--
*Read texture file.tga
*Copy pixels
*Pack the pixels only


--ACCESSING DATA--
*Locate entry in buffer
*Copy from *entry_position_in_memory -> *entry_size
*do whatever with the pixels


Then , i will have to do the same for audio/config files etc...

Im trying to create something like ...hmm...a virtual directory...with...lol...virtual files??
Not sure if that thing exists , but that's what im after.

**PS:Don't shoot me now , wait 'till i find a solution..lol**

Share this post


Link to post
Share on other sites
As always , when i ask questions , i do my own research , and always posting back the solution , because other users might be interested.. :P.

I found out that im doing it the WRONG way.

What i actually need , is a library like zLib or physFS for example.

Anyway ,
anyone knows where i can i find a small zLib or physFS example
streaming a file ?

I don't think its valid to unzip each file & then load its contents.

Share this post


Link to post
Share on other sites
Quote:
Original post by 3Dgonewild
Well , if i do this , then i will
have to change every single resource handling function.
And not only.

For example , to store texture i will have to follow the following procedure:

--PACKING--
*Read texture file.tga
*Copy pixels
*Pack the pixels only


--ACCESSING DATA--
*Locate entry in buffer
*Copy from *entry_position_in_memory -> *entry_size
*do whatever with the pixels


Then , i will have to do the same for audio/config files etc...
Your resource manager should handle the reading of files from disk, and your resource handling functions should just be given a (constant) pointer and length. Alternatively, you could have a Resource class that encapsulates that, and has a Read(size_t nBytes, void* pBuffer) function or similar.
That way your resource manager can be easily changed to read individual files or a memory archive.

Quote:
Original post by 3Dgonewild
I found out that im doing it the WRONG way.

What i actually need , is a library like zLib or physFS for example.

Anyway ,
anyone knows where i can i find a small zLib or physFS example
streaming a file ?

I don't think its valid to unzip each file & then load its contents.
That'll still just extract the file to memory, so your resource handling functions will still need to take a pointer and a size.

Basically, giving each resource handler a file handle is A Bad Thing, and limits you substantially.

Share this post


Link to post
Share on other sites
I think this might be what Evil Steve was hinting at, but couldn't you just add another function to your resource manager / handler that knows how to handle memory buffers representing specific file types (like LoadBMPFromBuf(const char* pBMPData))? Also along with the link CmpDev provided, you might want to look at the related article at the bottom of the page (Displaying a Bitmap from a Custom Resource File using SDL_RWops). It uses a function SDL_RWFromMem() to load a memory block as a custom resource, which can be used by other functions to load specific texture formats onto surfaces. (This is however a strictly SDL based method).

Share this post


Link to post
Share on other sites
@Evil Steve:

Oh well , then , i guess i will code my own stream reader/writer
since zlib doesn't make it easier.

I had in mind something like this:

zlib->addArchive("backgrounds.zip");
zlib->addArchive("more backgrounds.zip");
zlib->select("backgrounds.zip");
engine.resources.texture.add("BG0",zlib->getStream("bg.tga"));


Share this post


Link to post
Share on other sites
Quote:
Original post by 3Dgonewild
@Evil Steve:

Oh well , then , i guess i will code my own stream reader/writer
since zlib doesn't make it easier.

I had in mind something like this:
*** Source Snippet Removed ***
You can't get a FILE* handle to a file inside a zip file, because the file doesn't exist - it's just a compressed block of data inside another file. You'll need to extract the file into memory, and then deal with that buffer.
If zlib does allow you to do that, it'll be just giving you the FILE* of the zip file itself, possibly seek()ed to the start of the compressed file data.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement