getDirectAccess method
Get direct access to internal arrays for performance-critical operations.
WARNING: The returned DirectBufferAccess contains views into native memory. These views are only valid until the buffer is invalidated (i.e., until the next call to Renderer.render()). Accessing the views after invalidation may cause crashes or undefined behavior.
Implementation
DirectBufferAccess getDirectAccess() {
_checkValid();
final w = width;
final h = height;
final len = w * h;
if (len == 0) {
throw StateError('Buffer has zero size');
}
// Get pointers with validation - match Go's null checking pattern
final charPtr = _bindings.bufferGetCharPtr(_ptr);
final fgPtr = _bindings.bufferGetFgPtr(_ptr);
final bgPtr = _bindings.bufferGetBgPtr(_ptr);
final attrPtr = _bindings.bufferGetAttributesPtr(_ptr);
// Validate pointers before creating TypedLists
if (charPtr == nullptr) {
throw StateError('Failed to get character pointer from buffer');
}
if (fgPtr == nullptr) {
throw StateError('Failed to get foreground pointer from buffer');
}
if (bgPtr == nullptr) {
throw StateError('Failed to get background pointer from buffer');
}
if (attrPtr == nullptr) {
throw StateError('Failed to get attributes pointer from buffer');
}
return DirectBufferAccess(
chars: charPtr.asTypedList(len),
foregrounds: fgPtr.asTypedList(len * 4), // 4 floats per Color
backgrounds: bgPtr.asTypedList(len * 4),
attributes: attrPtr.asTypedList(len),
width: w,
height: h,
);
}