Functions for manipulating buffers and streams
Unsigned integer, 1 byte
Unsigned integer, 2 bytes
Unsigned integer, 4 bytes
Unsigned integer, 8 bytes
Signed integer, 1 byte
Signed integer, 2 bytes
Signed integer, 4 bytes
Signed integer, 8 bytes
Float, single precision, 4 bytes
Create a new data buffer containing a specified set of streams. A data buffer can contain one or more streams with typed data. This is useful for managing compound data, for instance a vertex buffer could contain separate streams for vertex position, color, normal etc.
element_count - The number of elements the buffer should hold
declaration - A table where each entry (table) describes a stream
name
: The name of the streamtype
: The data type of the streamcount
: The number of values each element should holdbuffer - the new buffer
How to create and initialize a buffer
function init(self)
local size = 128
self.image = buffer.create( size * size, { {name=hash("rgb"), type=buffer.VALUE_TYPE_UINT8, count=3 } })
self.imagestream = buffer.get_stream(self.image, hash("rgb"))
for y=0,self.height-1 do
for x=0,self.width-1 do
local index = y * self.width * 3 + x * 3 + 1
self.imagestream[index + 0] = self.r
self.imagestream[index + 1] = self.g
self.imagestream[index + 2] = self.b
end
end
Get a specified stream from a buffer.
buffer - the buffer to get the stream from
stream_name - the stream name
stream - the data stream
Copy a specified amount of data from one stream to another. The value type and size must match between source and destination streams. The source and destination streams can be the same.
dst - the destination stream
dstoffset - the offset to start copying data to (measured in value type)
src - the source data stream
srcoffset - the offset to start copying data from (measured in value type)
count - the number of values to copy (measured in value type)
How to update a texture of a sprite:
-- copy entire stream
local srcstream = buffer.get_stream(srcbuffer, hash("xyz"))
local dststream = buffer.get_stream(dstbuffer, hash("xyz"))
buffer.copy_stream(dststream, 0, srcstream, 0, #srcstream)
Copy all data streams from one buffer to another, element wise. Each of the source streams must have a matching stream in the destination buffer. The streams must match in both type and size. The source and destination buffer can be the same.
dst - the destination buffer
dstoffset - the offset to start copying data to
src - the source data buffer
srcoffset - the offset to start copying data from
count - the number of elements to copy
How to copy elements (e.g. vertices) from one buffer to another
-- copy entire buffer
buffer.copy_buffer(dstbuffer, 0, srcbuffer, 0, #srcbuffer)
-- copy last 10 elements to the front of another buffer
buffer.copy_buffer(dstbuffer, 0, srcbuffer, #srcbuffer - 10, 10)
Get a copy of all the bytes from a specified stream as a Lua string.
buffer - the source buffer
stream_name - the name of the stream
data - the buffer data as a Lua string
Creates or updates a metadata array entry on a buffer. The value type and count given when updating the entry should match those used when first creating it.
buf - the buffer to set the metadata on
metadata_name - name of the metadata entry
values - actual metadata, an array of numeric values
value_type - type of values when stored
How to set a metadata entry on a buffer
-- create a new metadata entry with three floats
buffer.set_metadata(buf, hash("somefloats"), {1.5, 3.2, 7.9}, buffer.VALUE_TYPE_FLOAT32)
-- ...
-- update to a new set of values
buffer.set_metadata(buf, hash("somefloats"), {-2.5, 10.0, 32.2}, buffer.VALUE_TYPE_FLOAT32)
Get a named metadata entry from a buffer along with its type.
buf - the buffer to get the metadata from
metadata_name - name of the metadata entry
values - table of metadata values or nil
if the entry does not exist
value_type - numeric type of values or nil
How to get a metadata entry from a buffer
-- retrieve a metadata entry named "somefloats" and its nomeric type
local values, type = buffer.get_metadata(buf, hash("somefloats"))
if metadata then print(#metadata.." values in 'somefloats'") end