electronstudio / raylib-python-cffi

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

RLGL standalone example (using new defines.py and GLFW) #113

Closed ashleysommer closed 8 months ago

ashleysommer commented 9 months ago

There's a few things going on in this PR.

This started off to see if I could bump Raylib to 5.0 and get it to build, that worked fine and I bumped RayGUI to the corresponding v4.0 release. I started adding a couple of new examples too, in particular I wanted to port this one: https://github.com/raysan5/raylib/blob/master/examples/others/rlgl_standalone.c

I quickly discovered I needed access to functions, structs, and constants from GLFW, that are not included with regular Raylib-python build. The corresponding GLFW version is bundled with raylib-c at raylib-c/src/external/glfw/ so it was easy to add "glfw3.h" to the FFI compiler, and point the raylib parser at that location and extract the functions and structs.

It was then I found that DEFINEs from each library are not automatically exported in the same way Enums are. The raylib/defines.py file has a few manually added aliases from raylib.h, but none from rlgl.h or raymath.h and I needed DEFINEs from glfw3.h.

The defines are exported by the raylib parser in the same way that structs and enums are, so I added a new create_define_consts.py script that adds them to raylib/defines.py while preserving their typing (float, int, string) and preserving their bitwise operations (eg GLFW_HAT_LEFT_UP = GLFW_HAT_LEFT | GLFW_HAT_UP) and preserving float math (eg DEG2RAD = PI / 180.0). This now exports defines from raylib.h, raymath.h, rlgl.h, raygui.h, and glfw3.h to raylib/defines.py.

When generating function stubs in the raylib/__init__.pyi file for GLFW3 functions, I came across an issue where argument names for functions that take a callback have the callback signature incorrectly included in the argument name. This is because the raylib parser does not properly parse GLFW functions, so the argument names are not included in the glfw3.json file. I added code to detect when this occurs and rename the argument name to "callback".

Then I discovered you can add the -d APIDEF argument to the raylib header parser to give it a hint how to find and parse functions from other header files. I found this issue also affected raymath.h, raygui.h and physac,h, so with the right APIDEF declared for the parser on each of those, the parser can make a full functions dict in the JSON, that allows us to use proper argument names for RayMath, RayGUI, Physac, and GLFW3 functions. I needed to add an extra step to avoid using python reserved keywords as argument names (eg, raymath.h has some fn args with name from that breaks python).

Finally, I completed porting the rlgl_standalone demo, updated some other examples to match v5.0 changes, and added an audio playback example, and regenerated the docs.

electronstudio commented 9 months ago

Thanks for doing all that.

this really should be about 6 separate PRs (you can stack PRs on top of each other if necessary if one PR depends on work done in a previous PR).

I will still review it but it might take me a while in this format.

ashleysommer commented 9 months ago

Thanks. I realized after completing the work and making the large commit, it really should be in multiple PRs. But I haven't done stacked PRs in github before, so I didn't know if that would make the whole set even more difficult to manage.

ashleysommer commented 9 months ago

One easy win will be to split out the Raylib 5.0 (and corresponding RayGUI 4.0) update into a separate PR, that will be easy to review and merge. Then I can rebase this one onto that.

@electronstudio PR https://github.com/electronstudio/raylib-python-cffi/pull/114 is created, probably better to review that one first on its own.

ashleysommer commented 8 months ago

Thanks for merging this @electronstudio

One thing I forgot to mention, you'll notice there is a strange sed line added in make_docs.sh, this performs a complicated looking find-and-replace operation on the glfw3.json file. This is because raylib_parser tool includes the C comments from the glfw3.h file in the names of the Defines in the glfw3.json file. The sed operation detects and removes the comments from the names of the defines in glfw3.json.