2012-09-27 3 views
0

사용자 정의 리소스 파일에서 SDL_Surface *를 가져 오려고합니다. 이 사용자 지정 리소스 파일은이 코드로 가져옵니다. http://content.gpwiki.org/index.php/C:Custom_Resource_FilesC - SDL_image -> 사용자 정의 리소스 파일에서 이미지로드

비트 맵, jpeg 및 WAV 사운드가 포함 된 폴더를 압축했습니다.

버퍼를 반환하는 함수가 있는데이 버퍼를 사용하여 SDL_Rworps *를 사용하여 서페이스를로드 할 수 있습니다.

SDL과 함께 BMP 이미지를 얻으 려 할 때 제대로 작동합니다.

하지만 내 문제는 sdl_image를 사용하여 JPG와 PNG로 동일한 효과를 얻는 것입니다.

다음은 몇 가지 코드입니다.

이 함수는 리소스 파일 (* resourcefilename)을 읽고 얻고 자하는 파일 (* resourcename)을 검색합니다. 마지막 INT의 PARAM주의

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; 
} 

지금 그 (BMP 용) SDL_Surface의 *를 돌려 내 함수의 파일 크기를 처리 포인터이다이 기능을 사용 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로 테스트하고 체크섬을 통해 이전 및 이후 파일을 실행하여 불일치가 있는지 확인합니다.