Skip to content

100+ official raylib C examples converted to Python / pyray #173

Closed

Description

@blep

I’ve converted over 100+ official raylib C examples into Python using pyray, via tool-assisted conversion.
I want to hand them over to the pyray community (due to my limited availability).

The repo containing the examples and a description of the process/tool used for the conversion is available here:
https://github.com/blep/pyray_examples. I made the repo because I wanted to share the conversion process, as I think it
would be valuable for other languages (and Copilot is still free for a few weeks).

124/174 (~71%) examples work without known issues.

I don't have the time to manage such a high visibility project nowadays, and want to hand it over to the community.
I already spent way more time than I initially planned on this. I started this initially just to have a few examples
for my personal project. But with the success, I decided to take the time to convert all the examples as I think it
will be a great boon for the Python/pyray communities, and get the tedious part of migrating the examples out of the
way.

Note that I'm a total newbie when it comes to raylib and pyray (though I've used similar libraries in the past).

  • Some examples have errors, some are clearly me not knowing how to use the pyray/CFFI bindings, but some seem to be
    binding issues. I've listed all the identified issues in the project. For example:

    • raylib_official_examples/shaders/shaders_deferred_render.py: Press key 1 (switch G buffer)
      texture = rl.Texture2D() 
      ffi.error: undefined struct/union name : struct Texture2D *
  • There is also some API that (IMHO) should be improved:

    • pyray.YELLOW is a tuple. Should be a namedtuple for convenience, so we could use .r, .g...
      There are also places where a rl.Color type is expected, and pyray.YELLOW is not a rl.Color instance.
      I don't know enough to say what should be done, but from a user P.O.V. it is confusing.
    • GUI style can return negative int32 value for get_color(), but color only accepts uint32, forcing a dirty work-around:
      # PORT: "0xffff_ffff &" is a work-around for converting the negative int32 to uint32
      rl.clear_background(rl.get_color(0xffff_ffff & rl.gui_get_style(rl.DEFAULT, rl.BACKGROUND_COLOR)))
    • rl.set_shader_value() only accepts a pointer for the value which makes it very painful/verbose to use

Please note that, due to my full-time job and other commitments, my response time may be delayed (two days or more).

Feel free to use or copy anything from the repository without waiting for my feedback, or I can transfer ownership...

Activity

  1. electronstudio commented on May 16, 2025

    @electronstudio
    Owner

    Thanks, this should save some time. But there isn't really any community, so I will have to review them all myself, which may take a while.

  2. electronstudio commented on May 16, 2025

    @electronstudio
    Owner

    ffi.error: undefined struct/union name : struct Texture2D *

    Texture2D in raylib.h is just a typedef for Texture. Python doesn't have any equivalent (that I know) way of having two types that are the same. So we just use Texture.

  3. blep commented on May 16, 2025

    @blep
    ContributorAuthor

    Thanks, replacing Texture2D by Texture worked for shaders_deferred_render.py.
    I'll fix the example with that problem today while I still have a few hours of free time.
    Would be nice to avoid exposing Texture2D to avoid the confusion (or just define Texture2D = Texture in python if possible).

  4. electronstudio commented on May 16, 2025

    @electronstudio
    Owner

    Yeah it seems CFFI handles them a bit strangely. They show up in list_types(), which is why pyray generates a Texture2D constructor, but then the constructor doesn't work because CFFI doesn't let you make them. So we should either not generate the Texture2D constructor, or special case it so it produces Texture. Or maybe actually better to just remove all the typedefs when we process raylib.h if CFFI doesn't handle them properly.

  5. electronstudio commented on May 16, 2025

    @electronstudio
    Owner

    pyray.YELLOW is a tuple. Should be a namedtuple for convenience,

    I'm not sure. Seems to be getting into the whole issue of how to wrap structs which is still open #157 .

    Lets say we did make a namedtuple Color. That would override the pyray.Color() constructor. So then you could only make namedtuples and not actual ctype Color structs.

    Better solution might be to do pyray.YELLOW = pyray.Color(253, 249, 0, 25) but I'm not sure if that would cause any other issues.

  6. electronstudio commented on May 16, 2025

    @electronstudio
    Owner

    There are also places where a rl.Color type is expected, and pyray.YELLOW is not a rl.Color instance.

    Remember the typehints are all fake basically. (There is no such class as Color, it only exists as a method name and a typehint.) They should be consistently fake though so if you've found stuff that fails to typecheck then we should fix it.

  7. electronstudio commented on May 16, 2025

    @electronstudio
    Owner

    GUI style can return negative int32 value for get_color(), but color only accepts uint32,

    RayGUI may be doing some bullshit like abusing the fact that a Color struct and an int are both 32 bits of memory. Will need to investigate further.

  8. electronstudio commented on May 16, 2025

    @electronstudio
    Owner

    rl.set_shader_value() only accepts a pointer for the value which makes it very painful/verbose to use

    There's an open issue for how we deal with 'out' pointers: #60

    This is actually a const pointer, so you're right it is not used for output and probably doesn't need to be a pointer. Perhaps we could convert automatically if the pointer is const. I think there are some other improvements that I wanted to do with const parameters but didn't because CFFI doesn't tell you what is const and what is not.

  9. electronstudio commented on May 17, 2025

    @electronstudio
    Owner

    It seems the reason SetShaderValue takes a pointer is so that the value can be subsequently changed without needing to call SetShaderValue again. So we can’t just automatically convert to a pointer, because we don’t know the expected lifetime the user needs the pointer to stick around and not be garbage collected. And users would probably be confused when they changed the values and found they didn’t update like they do in C.

    Pointers are a “painful/verbose” part of C but I can’t think of an easy way to hide them from the user, especially when interfacing to something as low level as shaders. Maybe someone could create some higher level wrapper / abstraction.

  10. Lcbx commented on May 19, 2025

    @Lcbx

    ffi.error: undefined struct/union name : struct Texture2D *

    Texture2D in raylib.h is just a typedef for Texture. Python doesn't have any equivalent (that I know) way of having two types that are the same. So we just use Texture.

    I had the same error with RenderTexture2D aka RenderTexture, was going to create an issue
    (encountered that problem while implementing a shadowmap).

    An approach to type aliases could be to create a function that calls the original type's constructor ;
    the hard part would be to identify the aliases (idk if there is a way to determine the original type in cffi).

    ( also long time no see -- it's been 4 years, time flies)

  11. electronstudio commented on Dec 10, 2025

    @electronstudio
    Owner

    I don't think anyone is ever going to have time to review these examples and add them to the repo, but they are undoubtedly useful, so perhaps solution is to include https://github.com/blep/pyray_examples as a submodule?

  12. electronstudio commented on Dec 15, 2025

    @electronstudio
    Owner

    added as submodule

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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions