为什么要在stdio函数上使用SDL I / O函数?

SDL包含许多用于处理以SDL_RWops结构为中心的I / O的功能。它们中的许多与stdio I / O函数非常相似,以至于它们使用相同样式的字符串。例如,fopen vs SDLRWFromfile。

为什么会这样,我的目标是在标准库上使用SDL I / O功能吗? SDL功能更易于移植吗?

lxyyxl001 回答:为什么要在stdio函数上使用SDL I / O函数?

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 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对象,该对象的函数指针已初始化为现有的标准库{ {1}},readwriteseek函数。

在这种情况下,您可以将资产以字节数组的形式硬编码在可执行二进制文件中,然后在该内存上映射一个close对象。

因此,对于SDL函数,必须使用它们(即使隐藏了它们的使用)。但是,如果您还有其他资源文件(例如音频,配置文件)没有馈入SDL,则可以使用标准库来读取它们。

本文链接:https://www.f2er.com/3163839.html

大家都在问