Skip to content

UpdateTexture can't accept Buffer/Uint8Array, forcing workarounds #189

Description

@jensroth-git

Issue
The current Node.js bindings for raylib have a type mismatch in the UpdateTexture / UpdateTextureRec function. The TypeScript definition forces pixel data to be a number type (presumably a pointer in C), but JavaScript/TypeScript Buffers and Uint8Arrays can't be directly passed to this function.
This makes it unnecessarily complex, forcing workarounds like writing frames to disk as PPM files before loading them back as textures.

Suggested fix
Modify the node-raylib bindings to properly handle Buffer/Uint8Array data in the functions. The C++ binding should:
Accept a JavaScript Buffer/Uint8Array directly
Extract the raw memory pointer from the Node.js buffer using something like Node's node::Buffer::Data
Pass this pointer to the raylib C function

// Updated definition to support JavaScript data types
UpdateTexture(texture: Texture, pixels: Buffer | Uint8Array | number): void;

Technical implementation
In the C++ binding layer, you would need to add type checking similar to:

// In the UpdateTexture binding function
if (info[1]->IsArrayBufferView()) {
  // Handle Buffer/Uint8Array case
  v8::Local<v8::ArrayBufferView> view = info[1].As<v8::ArrayBufferView>();
  void* data = view->Buffer()->GetContents().Data();
  
  // Call the actual raylib function with the extracted pointer
  UpdateTexture(texture, data);
} else if (info[1]->IsNumber()) {
  // Handle existing number/pointer case for backward compatibility
  void* data = (void*)info[1]->IntegerValue(context).FromJust();
  UpdateTexture(texture, data);
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions