사용자 정의 리소스 파일에서 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 지원을 사용할 수 없습니다.
다른 사람이 원하면 전체 소스 코드를 업로드하거나 리소스 파일과 함께 간단한 버전을 업로드 할 수 있습니다.