Buffer

Namespace: dmBuffer Language: C++ Type: Defold C++ File: buffer.h Source: engine/dlib/src/dmsdk/dlib/buffer.h Include: dmsdk/dlib/buffer.h

Buffer API for data buffers as the main way to communicate between systems.

API

dmBuffer::Copy

Type: FUNCTION Copies the data from one buffer to another buffer. The stream declaration needs to be the same in both buffers.

Parameters

Returns

Examples

dmBuffer::Result r = dmBuffer::Copy(buffer_a, buffer_b);

if (r == dmBuffer::RESULT_OK) {
    // success
} else {
    // handle error
}

dmBuffer::Create

Type: FUNCTION Creates a new HBuffer with a number of different streams.

Parameters

Returns

Examples

const dmBuffer::StreamDeclaration streams_decl[] = {
    {dmHashString64("position"), dmBuffer::VALUE_TYPE_FLOAT32, 3},
    {dmHashString64("texcoord0"), dmBuffer::VALUE_TYPE_UINT16, 2},
    {dmHashString64("color"), dmBuffer::VALUE_TYPE_UINT8, 4},
};
dmBuffer::HBuffer buffer = 0x0;
dmBuffer::Result r = dmBuffer::Create(1024, streams_decl, 3, &buffer);

if (r == dmBuffer::RESULT_OK) {
    // success
} else {
    // handle error
}

dmBuffer::Destroy

Type: FUNCTION Destroys a HBuffer and it’s streams.

Parameters

Examples

const dmBuffer::StreamDeclaration streams_decl[] = {
    {dmHashString64("position"), dmBuffer::VALUE_TYPE_FLOAT32, 3},
};
dmBuffer::HBuffer buffer = 0x0;
dmBuffer::Result r = dmBuffer::Create(4, streams_decl, 1, &buffer);

if (r == dmBuffer::RESULT_OK) {
    dmBuffer::Destroy(buffer);
} else {
    // handle error
}

dmBuffer::GetBytes

Type: FUNCTION Gets the buffer as a byte array. If the buffer is interleaved (default), a pointer to the whole memory is returned.

Parameters

Returns

Examples

uint8_t* bytes = 0x0;
uint32_t size = 0;

dmBuffer::Result r = dmBuffer::GetBytes(buffer, (void**)&bytes, &size);

if (r == dmBuffer::RESULT_OK) {
    for (int i = 0; i < size; ++i)
    {
        stream[i] = (uint8_t)(i & 0xFF);
    }
} else {
    // handle error
}

dmBuffer::GetContentVersion

Type: FUNCTION Gets the current update number

Parameters

Returns

dmBuffer::GetCount

Type: FUNCTION Get (struct) count for a buffer.

Parameters

Returns

Examples

uint32_t count = 0;
dmBuffer::Result r = dmBuffer::GetCount(buffer, &count);

if (r == dmBuffer::RESULT_OK) {
    printf("buffer %p has %d number of elements", buffer, count);
} else {
    // handle error
}

dmBuffer::GetMetaData

Type: FUNCTION Retrieve metadata entry information

Parameters

dmBuffer::GetResultString

Type: FUNCTION Converts result to string

Parameters

Returns

dmBuffer::GetSizeForValueType

Type: FUNCTION Gets the size of a value type

Parameters

Returns

dmBuffer::GetStream

Type: FUNCTION Get a stream from a buffer. Output stream is 16 byte aligned.

Parameters

Returns

Examples

float* positions = 0x0;
uint32_t size = 0;
uint32_t components = 0;
uint32_t stride = 0;
dmBuffer::Result r = dmBuffer::GetStream(buffer, dmHashString64("position"), (void**)&positions, &count, &components, &stride);

if (r == dmBuffer::RESULT_OK) {
    for (int i = 0; i < count; ++i)
    {
        for (int c = 0; c < components; ++c)
        {
             positions[c] *= 1.1f;
        }
        positions += stride;
    }
} else {
    // handle error
}

dmBuffer::GetStreamType

Type: FUNCTION Gets the stream type

Parameters

Returns

dmBuffer::GetValueTypeString

Type: FUNCTION Converts a value type to string

Parameters

Returns

dmBuffer::HBuffer

Type: TYPEDEF

typedef uint32_t HBuffer;

dmBuffer::IsBufferValid

Type: FUNCTION Checks if a handle is still valid

Parameters

Returns

dmBuffer::SetMetaData

Type: FUNCTION Create or update a new metadata entry with a number of values of a specific type. It will allocate space to store these values.

Parameters

Returns

dmBuffer::StreamDeclaration

Type: STRUCT Buffer stream declaration structure

Members

Examples

Declare a typical position stream:

const dmBuffer::StreamDeclaration streams_decl[] = {
    {dmHashString64("position"), dmBuffer::VALUE_TYPE_FLOAT32, 3}
};

dmBuffer::UpdateContentVersion

Type: FUNCTION Used to know if a buffer has been updated.

Parameters

Returns

dmBuffer::ValidateBuffer

Type: FUNCTION Validate a buffer and it’s streams.

Parameters

Examples

// Pass buffer to third party library that does operations on the buffer or streams.
ThirdPartyLib::PerformOperation(buffer);

r = dmBuffer::ValidateBuffer(buffer);
if (r == dmBuffer::RESULT_OK) {
    // buffer and streams are valid
} else {
    // the third party lib made the buffer invalid
}

Result

Type: ENUM Result enumeration.

Members

ValueType

Type: ENUM ValueType enumeration.

Members