C - SDL_image - >从自定义资源文件加载图像

时间:2012-09-27 21:44:51

标签: sdl jpeg sdl-image

我正在尝试从自定义资源文件中获取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修改时,我上了stdout:

这不是JPG文件,或JPG支持不可用。

我请求你的帮助,如果有人想要,我可以上传整个源代码,或者至少是资源文件的简化版本。

1 个答案:

答案 0 :(得分:0)

我最近遵循相同的SDL custom resource tutorial,扩展了其功能,允许将资源文件解压缩为原始文件格式。我想我遇到了与你有关的问题,可能会对这个问题有更深入的了解。

代码可以很好地打包并解压缩教程地址的特定文件类型,即BMP和WAV。它还可以打包和解压缩OGG文件,没有任何问题。但是,该过程会删除所有换行格式的TXT文件。在打包/解包过程中的某个地方,PNG文件已完全损坏。我没有尝试使用JPG,但它们可能会遇到与PNG相同的命运,如果在打包期间发生这种情况,那么这可以解释为什么你的JPG不会使用SDL_Rwops加载。

今晚我将用JPG进行测试,并通过校验和运行前后文件以查看是否存在任何差异。

相关问题