buffer_create

语法:

buffer_create(size, type, alignment)


参数 描述
大小 The size (in bytes) of the buffer.
type The type of buffer to create (see the constants list below).
alignment The byte alignment for the buffer


返回: Real(实数)


描述

You use this function to allocate a portion of memory as a buffer in your game, with the function returning the unique buffer id that should be stored in a variable and used for all further function calls to the buffer. The buffer can then be used to store different types of data (specified when you write to the buffer using the buffer_write function, with the following constants being used to define the buffer type:

常量 描述
buffer_fixed A buffer of fixed size.
buffer_grow A buffer that will "grow" dynamically as data is added
buffer_wrap A buffer where the data will "wrap". 当要添加的数据达到缓冲区大小的限制时,写入将回到缓冲区的开始位置,从那里将继续进行进一步的写入。
buffer_fast 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.
buffer_vbuffer This type of buffer is to be used as a vertex buffer only.



Apart from the buffer type, you will also have to set the byte alignment for the buffer. This value will vary depending on the data that you wish to store in the buffer, and in most cases a value of 1 is perfectly fine. However, be aware that for some operations a specific alignment is essential, and an incorrect alignment may cause errors (for further details on alignment see Buffers). The following is a general guide to show which values are most appropriate for each data type (when in doubt, use an alignment of 1):

NOTE: Byte alignment can be very important as the wrong choice may adversely affect performance.

NOTE: It's important that you remove any dynamically created resources like this from memory when you no longer need them to prevent memory leaks, so when you are finished with the buffer that you have created you should free it up again using buffer_delete.


例如:

player_buffer = buffer_create(16384, buffer_fixed, 2);

The above code allocates 16384 bytes of memory to a buffer and returns the index of that buffer, which is stored in the variable "player_buffer", for future use. The buffer is aligned to a two byte boundary.