ppy / SDL3-CS

MIT License
46 stars 12 forks source link

Automatically generate macro functions #175

Open Susko3 opened 1 week ago

Susko3 commented 1 week ago

Currently, macro functions need to be manually updated (and added), see https://github.com/ppy/SDL3-CS/pull/174/commits/2590a7205eff1d1a928ce5f9a700f16339fbb157.

My idea for this

The manually written C# code would be the following, placed in the usual SDL_{name}.cs file:

public static partial class SDL3
{
    [Macro]
    [return: CastFrom("int")]
    public static partial SDL_PixelType SDL_PIXELTYPE([Cast("int")] SDL_PixelFormat X);
}

This will cause the source gen to generate the following code (this may be unnecessary):

    public static partial SDL_PixelType SDL_PIXELTYPE(SDL_PixelFormat X) => (SDL_PixelType)Unsafe_SDL_PIXELTYPE((int)X);

The python script would generate this C header file and add it to ClangSharp (the file is not saved to disk/repo):

#include <SDL3/SDL.h>

//      | taken from [CastFrom("int")]
//      ↓                        ↓ taken from [Cast("int")]
inline int Unsafe_SDL_PIXELTYPE(int X)
{
    return SDL_PIXELTYPE(X);
}

ClangSharp can convert inline functions to C# code, so calling the macro functions inside inline functions should work.

The generated C# code will be horrible, as it'll resolve all macros (and SDL code sometime nests a lot of macros).