SDL包含许多用于处理以SDL_RWops结构为中心的I / O的功能。它们中的许多与stdio I / O功能非常相似,以至于它们使用相同样式的字符串。例如,fopen vs SDLRWFromfile。
为什么会这样,我应该在标准库上使用SDL I / O功能吗? SDL函数更易于移植吗?
SDL_RWops
的兴趣在于它只是一个接口结构,带有用于读取/写入/查找/关闭的功能指针...
typedef struct SDL_RWops {
/** Seek to 'offset' relative to whence, one of stdio's whence values:
* SEEK_SET, SEEK_CUR, SEEK_END
* Returns the final offset in the data source.
*/
int (SDLCALL *seek)(struct SDL_RWops *context, int offset, int whence);
/** Read up to 'maxnum' objects each of size 'size' from the data
* source to the area pointed at by 'ptr'.
* Returns the number of objects read, or -1 if the read failed.
*/
int (SDLCALL *read)(struct SDL_RWops *context, void *ptr, int size, int maxnum);
/** Write exactly 'num' objects each of size 'objsize' from the area
* pointed at by 'ptr' to data source.
* Returns 'num', or -1 if the write failed.
*/
int (SDLCALL *write)(struct SDL_RWops *context, const void *ptr, int size, int num);
/** Close and free an allocated SDL_FSops structure */
int (SDLCALL *close)(struct SDL_RWops *context);
// ... there are other internal fields too
}
[例如,使用SDL_LoadBMP
时,SDL从文件句柄创建SDL_RWops
对象,但是您也可以从其他来源(例如,内存位置,例如对于不提供系统的系统)创建SDL_RWops
对象一个本机文件系统(Nintendo DS让人想到,即使R4或M3之类的自制链接程序推车通常也可以提供这样的服务)。
来自SDL_Video.h
:
/**
* Load a surface from a seekable SDL data source (memory or file.)
...
*/
extern DECLSPEC SDL_Surface * SDLCALL SDL_LoadBMP_RW(SDL_RWops *src, int freesrc);
/** Convenience macro -- load a surface from a file */
#define SDL_LoadBMP(file) SDL_LoadBMP_RW(SDL_RWFromFile(file, "rb"), 1)
因此SDL_LoadBMP
是一个调用SDL_RWFromFile(file, "rb")
的宏,该宏肯定使用标准库来创建文件的句柄,并创建一个SDL_RWop
对象,该对象的函数指针已初始化为现有read
,write
的标准库, seek
,close
功能。
在那种情况下,您可以将资产以字节数组的形式硬编码在可执行二进制文件中,然后在该内存上映射SDL_RWops
对象。
因此,对于SDL函数,您必须使用它们(即使隐藏了它们的使用)。但是,如果您还有其他资源文件(例如音频,配置文件)没有馈入SDL,则可以使用标准库来读取它们。