我試圖從自定義資源文件中獲取SDL_Surface *。 這個自定義資源文件是用這段代碼得到的; http://content.gpwiki.org/index.php/C:Custom_Resource_FilesC - SDL_image - >從自定義資源文件加載圖像
我打包了一個文件夾,其中包含一個位圖,一個jpeg和一個WAV聲音。
我有一個函數返回一個緩衝區,然後用這個緩衝區我可以在使用SDL_Rworps加載表面*。
當我試着讓我的BMP圖像,與SDL它工作正常。
但我的問題是讓以JPG和PNG使用sdl_image同樣的效果。
這是一些代碼;
該函數讀取資源文件(* resourcefilename),並搜索我們想要獲得的文件(* resourcename)。最後INT參數是一個指針處理的文件大小
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;
}
現在,這是我的函數返回一個SDL_Surface *(對於BMP)注意,此功能使用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;
}
但是,當我嘗試使用相同的功能,修改爲JPG,我得到我的標準輸出:
這不是JPG文件,或JPG的支持不可用。
我請求你們的幫助,如果有人願意,我可以上傳完整的源代碼,或者至少一個簡化版本的資源文件。