Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

arrays and pointers #2

Open
electronstudio opened this issue May 17, 2024 · 1 comment
Open

arrays and pointers #2

electronstudio opened this issue May 17, 2024 · 1 comment

Comments

@electronstudio
Copy link
Owner

It's impossible to tell the difference between an array and a pointer type in raylib.h. Currently (Camera3D *) is special cased to Camera3D, others are MemorySegment.

  1. Need to deal with array access properly.
  2. May be possible to special case everything by manually checking how Raylib uses them, but that's quite fragile.
  3. Better solution is to create overloaded methods, i.e. one method with pointer one with array.
  4. Need some way of dealing with 'out' pointers - pointers to primitives and non const chars. May just need example of FFI usage added to the docs.
@electronstudio
Copy link
Owner Author

C has a problem known as "array to pointer decay". C will automatically convert an array type and to a pointer type. Therefore the computer can't tell if a function parameter is an array of many items or a pointer to a single item. I don't know if modern C has solved this, but Raylib certainly hasn't. The info isn't in raylib_api.json. You just have to read the docs for each function to find out which it is.

So sometimes you will get a Java object that may represent one struct, or may represent an array of structs. You'll need to check the Raylib docs or the Javadoc for the function to know which it has given you.

If it's an array, you can use the getArrayElement() method to access the other structs. To create a new array there is allocateArray() method.

(I suppose we could provide List-like wrappers but to actually make them safe would require working out every function that uses arrays and what parameter it uses to store the size of the array. Nice to have but a luxury that users of C Raylib don't have, and requiring manual maintance work, so out of scope for now.)

Arrays of ints and floats are wrapped as IntBuffer and FloatBuffer. Note if you create your own you should use Raylib.createIntBuffer(1) to ensure you create a direct, native buffer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant