我正在尝试从自定义资源文件中获取 SDL_Surface*。这个自定义资源文件,是用这个代码得到的; http://content.gpwiki.org/index.php/C:Custom_Resource_Files
我打包了一个文件夹,其中包含一个位图、一个 jpeg 和一个 WAV 声音。
我有一个函数返回一个缓冲区,然后通过这个缓冲区,我可以使用 SDL_Rworps* 加载一个表面。
当我尝试使用 SDL 获取我的 BMP 图像时,它工作正常。
但我的问题是使用 sdl_image 来获得与 JPG 和 PNG 相同的效果。
这是一些代码;
该函数读取资源文件 (*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 支持不可用。
我请求您的帮助,如果有人需要,我可以上传整个源代码,或者至少是带有资源文件的简化版本。