Loading from a zip file

Started by
9 comments, last by ChrisRoe 22 years, 5 months ago
Hi, I recently downloaded the zip loader from Flipcode''s code of the day. And all goes well, except. . . since you can''t use fread without a FILE pointer -. . . how would I read in headers and stuff? I''m trying to load a tga file from information from a char *x; .. help?
Advertisement
Well, I cheat slightly. I use a pack file, but extract the whole thing to a temporary directory when the app starts, and load everything from there. The app deletes it all when it ends. This means I can have FMOD play my wavs and MP3s exactly as they were before, and my MS3D model loader and BMP/TGA texture loaders don''t need to be rewritten.

It may not stop people nicking the textures (although a zip wouldn''t anyway...) but it achieves the main objective for me: A smaller, cleaner distrobution.

HTH.
-=aSe=-
You may have to be careful taking that approach, because (although not as common in the days of 8-Bejillion GB drives) some people play that fun little game called "my hard-drive is completely full"... they may have had just enough to install your game, then when they run it... BAM... windoze pops up some shite about no space on C or whatever.

Not Happy Jan.

So while it is easier to do what aSe suggested, you probably should look into doing it properly at some point.

Just to help out a bit (see, i''m not all hot air and negativity )... you say you can''t use fread without a FILE pointer... my advice is load all the data into memory, and simply use string manipulation functions (maintain your own pointer to where you are in the string to keep track for the next "read").

Now, enough procrastinating... time to actually do my project
If you can, try adding your files to your excutable as a resource. That way you can compress the entire exe file and the files stored in it.
Well, once you have everything loaded into a char array, you can just emulate what fread does.

// modified FILE*struct FPtrStruct {  char *base;  char *pos;}void FRead(void *buff, int size, int num, FPtrStruct stream){  memcpy(buff, stream.pos, num * size); //copy the data  stream.pos += num * size;  //increase the current position} 


This should basically do what you want, but you''ll probably want to add error checking if you''re going to use this approach. I have the base pointer in the structure sot that if the data was allocated via new it can be freed easily.

I hope that this at least gives you an idea!
jw
I''m not sure what your problem is, surely the zip-loading code must come with an equivalent of fread to read from the zip files? Otherwise what''s the point of the zip loader?

Other zip reading packages:
zziplib (can read from zip-archives and regular files transparently, as if the zip was a directory).

zipios++ (provides a C++ iostream like interface to zip-archives).

both use zlib for decompression, and neither requires the file to be temporarily unpacked (i.e. they read directly from the zip-archive and decompress ''on the fly'').

This probably didn''t answer your question though
Dactylos, maybe didn''t answer ChrisRoe''s question but certainly showed me a better way of doing it!

Thanks )

-=aSe=-
-=aSe=-
Here is what I''d do:

Make yourself your own .pak file format storing file names and size + data. Then, program a custom fopen function that open the pak file and extract the necessary info to fill a FILE pointer for a specified file inside the pack. Then, you should be able to use fread as you feel. (The only flaw is that you cannot use compressed data with this method)
Hi,

you said
"how would I read in headers and stuff? I''m trying to load a tga file from information from a char *x; .. help?"

so that means that you already have the tga file loaded into the memory if so then :-

x[0] is ignored.
x[1] should = 0
x[2] should = either 2 or 3
x[3] to x[12] will be the header

that means that the tga is alread loaded and you need to convert it so you would use it as a texture to convert the tga image you need to do this ..

BOOL loadtga(unsigned char *tga)
{
unsigned char *imageData = NULL;
int imageWidth, imageHeight;
int imageBits, size;

if (!tga) return FALSE;//

if (tga[1] != 0 || (tga[2] != 2 && tga[2] != 3))
return FALSE; // something is wrong this isnt a tga

imageWidth = tga[12] + tga[13] * 256;
imageHeight = tga[14] + tga[15] * 256;
imageBits = tga[16];

size = imageWidth * imageHeight *imageBits;

imageData = (unsigned char *)malloc(size);
// trying to copy the image contants into
// the var image for later processing.
memcpy(imageData,&tga[17],size);

// now you need to switch the BGRA to RGBA or just RGB.
for(int i =0;i < size;i+=4)
{
tmpcolor = imageData;
imageData = imageData[i+2];<br> imageData[i+2] = tmpcolor;<br> }<br><br>// you need to set the texture id that you want <br>// therefore i didnt bother defining it <img src="smile.gif" width=15 height=15 align=middle> <br><br> glBindTexture (GL_TEXTURE_2D, id);<br> glPixelStorei (GL_UNPACK_ALIGNMENT, 1);<br> glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);<br> glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);<br> glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);<br> glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);<br> glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);<br><br> glTexImage2D(GL_TEXTURE_2D,0,3 for RGB and 4 For RGBA,imageWidth,imageHeight,0,GL_RGB or GL_RGBA,GL_UNSIGNED_BYTE,imageData);<br><br> free (imageData);<br><br> return TRUE;<br>}<br><br>This code has been written here and hasnt been tested and i didnt even double check it.. i wrote this code to clear the way to deal with a tga file already loaded into the memory, i hope thats what your asking for <img src="smile.gif" width=15 height=15 align=middle>, if not sorry that i made a whole lota mess.<br><br>Dizman </i>
Hi everybody. I''m a russian programmer. Lets look source code of my demo which load WaveFront obj models with jpeg textures from zip archive. Any questions to proton2@mail.ru

Download it from
http://opengl.org.ru/proton/download/loader.zip

This topic is closed to new replies.

Advertisement