buffer_create_from_vertex_buffer

此函数分配一部分内存作为游戏中的缓冲区,其中填充了之前创建的 顶点缓冲区 中的数据。

该函数返回对缓冲区的引用,该引用应存储在变量中并用于对缓冲区的所有进一步函数调用。该函数获取要使用的顶点缓冲区的引用 (例如,由函数 vertex_create_buffer 返回),并使用以下常量来定义缓冲区类型:

Buffer Type Constant
常量描述
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的对齐方式):

注意 顶点缓冲区是 1 字节对齐的,但您可以根据您想要如何处理数据来创建具有任何对齐方式的缓冲区,因为顶点数据只是原始内存复制到缓冲区。

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

注意 用于创建新缓冲区的顶点缓冲区也不会从内存中删除,当不再需要时,您应该使用函数 vertex_delete_buffer

 

语法:

buffer_create_from_vertex_buffer(vertex_buffer, type, alignment)

参数类型描述
vertex_bufferVertex Buffer对要使用的顶点缓冲区的引用。
typeBuffer Type Constant要创建的缓冲区类型(请参见上面的常量列表)。
alignmentReal缓冲器的字节对齐

 

返回:

Buffer

 

例子:

player_buffer = buffer_create_from_vertex_buffer(model_buffer, buffer_grow, 1);

上述代码将内存分配给缓冲区,然后将给定顶点缓冲区中的数据复制到其中,返回存储在变量 player_buffer 中的新缓冲区,以供将来使用。