Reputation: 676
I'm trying to get an SDL_Surface* from a custom resource file. This custom resource file, is get with this code; http://content.gpwiki.org/index.php/C:Custom_Resource_Files
I packed a folder, wich contain a bitmap, a jpeg and a WAV sound.
I have a function returning a buffer, then with this buffer i'm able to load a surface using SDL_Rworps*.
It works fine when i'm tryin to get my BMP image, with SDL.
But my problem is to get the same effect with JPG and PNG using sdl_image.
Here are some code;
This function read the resource file (*resourcefilename) , and search for the file (*resourcename) we want to get. The last int param is a pointer handling the file size
char *GetBufferFromResource(char *resourcefilename, char *resourcename, int *filesize)
{
//Try to open the resource file in question
int fd = open(resourcefilename, O_RDONLY);
if (fd < 0){perror("Error opening resource file"); exit(1);}
//Make sure we're at the beginning of the file
lseek(fd, 0, SEEK_SET);
//Read the first INT, which will tell us how many files are in this resource
int numfiles;
read(fd, &numfiles, sizeof(int));
//Get the pointers to the stored files
int *filestart = (int *) malloc(sizeof(int) * numfiles); // this is probably wrong in the zip
read(fd, filestart, sizeof(int) * numfiles);
//Loop through the files, looking for the file in question
int filenamesize;
char *buffer;
int i;
for(i=0;i<numfiles;i++)
{
char *filename;
//Seek to the location
lseek(fd, filestart[i], SEEK_SET);
//Get the filesize value
read(fd, filesize, sizeof(int));
//Get the size of the filename string
read(fd, &filenamesize, sizeof(int));
//Size the buffer and read the filename
filename = (char *) malloc(filenamesize + 1);
read(fd, filename, filenamesize);
//Remember to terminate the string properly!
filename[filenamesize] = '\0';
//Compare to the string we're looking for
if (strcmp(filename, resourcename) == 0)
{
//Get the contents of the file
buffer = (char *) malloc(*filesize);
read(fd, buffer, *filesize);
free(filename);
break;
}
//Free the filename buffer
free(filename);
}
//Release memory
free(filestart);
//Close the resource file!
close(fd);
//Did we find the file within the resource that we were looking for?
if (buffer == NULL)
{
printf("Unable to find '%s' in the resource file!\n", resourcename);
exit(1);
}
//Return the buffer
return buffer;
}
Now that's my function returning a SDL_Surface* ( for BMP ) note that this function use SDL_Image "IMG_LoadBMP_RW()"
SDL_Surface *LoadBMP(char *resourcefilename, char *imagefilename){
//Get the image's buffer and size from the resource file
int filesize = 0;
char *buffer = GetBufferFromResource(resourcefilename, imagefilename, &filesize);
//Load the buffer into a surface using RWops
SDL_RWops *rw = SDL_RWFromMem(buffer, filesize);
if(IMG_isBMP(rw))
printf("This is a BMP file.\n");
else
printf("This is not a BMP file, or BMP support is not available.\n");
SDL_Surface *temp = IMG_LoadBMP_RW(rw);
free(buffer);
//Return our loaded image
printf("IMG size: %d x %d\n", temp->w, temp->h);
SDL_Surface *image;
image = SDL_DisplayFormat(temp);
SDL_FreeSurface(temp);
return image;
}
But when i try to use the same function, modified for JPG, i get on my stdout:
This is not a JPG file, or JPG support is not available.
I ask for your help, if someone want, i can upload entire source code, or at least a simplified version with the resource file.
Upvotes: 0
Views: 2964
Reputation: 1
I recently followed the same SDL custom resource tutorial, extending its functionality to allow unpacking of resource files into the original file formats. I think I'm having a problem which relates to yours, and may provide more insight into the issue.
The code works fine packing and unpacking the specific file types the tutorial addresses, namely BMP and WAV. It also packs and unpacks OGG files with no issues. However, the process does strip TXT files of all line break formatting. And somewhere in the packing / unpacking process PNG files are completely corrupted. I have not tried with JPG, but they could be suffering the same fate as the PNG, and if this happens during packing, then this could explain why your JPGs won't load using SDL_Rwops.
I will test with a JPG tonight, and run the before and after files through a checksum to see if there are any discrepancies.
Upvotes: 0