electronstudio / raylib-python-cffi

Python CFFI bindings for Raylib
http://electronstudio.github.io/raylib-python-cffi
Eclipse Public License 2.0
142 stars 29 forks source link

Examples #47

Open electronstudio opened 2 years ago

electronstudio commented 2 years ago

We should split the examples into ones that use the C API and ones that use the Python API.

We should add more examples.

We should test all the examples work, with both the static and dynamic binding.

JackWitherell commented 2 years ago

Shader examples that change uniforms shaders_fog.py shaders_texture_drawing.py shaders_texture_waves.py don't work due to the change of rl.UNIFORM_FLOAT being changed to rl.SHADER_UNIFORM_FLOAT in an earlier version. Otherwise they work fine.

SirNate0 commented 4 months ago

An example showing how to create a mesh in python code would be helpful. I don't whether there is one in normal raylib (as functions generating meshes in rmodels.c are already examples), but in Python it took me many hours to figure out (and required building a debug build of the library so I could figure out why I was getting all the SegFaults).

If you want, feel free to use my Gist here, based on the GenMeshCube code (though it does use numpy to handle the mesh data arrays, rather than attempting a more manual conversion).

electronstudio commented 4 months ago

Thanks. I will add some code to pyray to make it do theffi.from_buffer calls automatically when a buffer is used as an argument to a struct creator method (e.g. here Mesh()). I will also change your example to usearray.array instead of numpy.ndarray.

It would be nice if we could send Python lists to the struct creator and also if it didnt require maintaining references to the buffers to prevent garbage collection, but I cant see any easy way of doing that automatically. I guess we'd need Mesh() to return a proxy object that contains both the cdata struct and the references.

electronstudio commented 4 months ago

According to CFFI docs, a global_weakkeydict = weakref.WeakKeyDictionary() can be used to keep the references alive automatically. Unfortunately it's difficult to test if it's working correctly because errors caused by GC seem to be non-deterministic - I sometimes see them, but I can't force them to occur repeatably, so I don't know if they are all gone.

electronstudio commented 4 months ago

https://github.com/electronstudio/raylib-python-cffi/pull/119