2012-09-27 51 views
0

我試圖從自定義資源文件中獲取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的支持不可用。

我請求你們的幫助,如果有人願意,我可以上傳完整的源代碼,或者至少一個簡化版本的資源文件。

回答

0

我最近遵循同樣的SDL custom resource tutorial,擴展其功能,使的資源文件解壓縮到原始文件格式。我想我遇到了一個與你有關的問題,並且可能會提供更多關於此問題的信息。

的代碼工作正常,打包和解包的特定文件類型教程地址,即BMP和WAV。它還打包並解包OGG文件,沒有任何問題。但是,該過程不會去除所有換行格式的TXT文件。並且在打包/解包過程中的某個地方PNG文件被完全損壞。我還沒有使用過JPG格式,但它們可能會遭受與PNG相同的命運,如果在打包過程中發生這種情況,那麼這可以解釋爲什麼您的JPG格式不會使用SDL_Rwops加載。

我將與JPG今晚進行測試,並通過校驗和前後運行的文件,看看是否有任何差異。