カスタム リソース ファイルから 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* ( for 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 がサポートされていません。
助けを求めます。誰かが望むなら、ソース コード全体、または少なくともリソース ファイルを含む簡略化されたバージョンをアップロードできます。