electronstudio / jaylib-ffm

Other
4 stars 1 forks source link

arrays and pointers #2

Open electronstudio opened 2 months ago

electronstudio commented 2 months ago

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 commented 2 months ago

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.