buffer_create

该函数创建一个新的缓冲区并返回它。

该函数为缓冲区分配一部分内存,然后可用于存储不同类型的数据 (在使用 buffer_writebuffer_poke{ 写入缓冲区时指定 )} 或 buffer_fill 函数)。

以下常量可用于定义缓冲区类型:

缓冲区类型常量
常量描述
buffer_fixedA buffer of fixed size.
buffer_growA buffer that will "grow" dynamically as data is added
buffer_wrapA buffer where the data will "wrap". When the data being added reaches the limit of the buffer size, the overwrite will be placed back at the start of the buffer, and further writing will continue from that point.
buffer_fastA special "stripped" buffer that is extremely fast to read/write to. Can only be used with buffer_u8 data types, and must be 1 byte aligned.

除了缓冲区类型之外,您还必须为缓冲区设置字节对齐。此值将根据您希望存储在缓冲区中的数据而变化,并且在大多数情况下,值为1是完全可以的。但是,请注意,对于某些操作,特定的对齐是必不可少的,并且不正确的对齐可能会导致错误 (有关对齐的更多详细信息,请参见缓冲区)。以下是显示哪些值最适合每种数据类型的通用指南 (如有疑问,请使用1的对齐方式):

注意 字节对齐非常重要,因为错误的选择可能会对性能产生不利影响。

重要信息 您无法创建 2 GB(2,147,483,648 字节) 或更大的缓冲区。

注意 当不再需要此类动态创建的资源时,请务必将其从内存中删除,以防止内存泄漏,因此,当您使用完已创建的缓冲区时,应使用 buffer_delete 再次释放它。

 

语法:

buffer_create(size, type, alignment)

参数类型描述
sizeReal缓冲区的大小 (以字节为单位)。
type缓冲区类型常量要创建的缓冲区类型 (请参见上面的常量列表)。
alignmentReal缓冲区的字节对齐

 

返回:

Buffer

 

例子:

player_buffer = buffer_create(16384, buffer_fixed, 2);

上述代码将 16384 字节内存分配给缓冲区并返回该缓冲区,并将对其的引用存储在变量 player_buffer 中以供将来使用。缓冲区与两字节边界对齐。