summaryrefslogtreecommitdiff
path: root/tools/generate_raylib_enums.py
blob: 6701b7802fc86e1107b770ca3d4c58d5a0870d27 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
"""
Generates raylib enum bindings from C to s7
"""

import json
from os import path


# List of enums that will be included in the generated file
# Change this and re-run the script to add more enum bindings
INCLUDE_ENUMS = [
    "KeyboardKey",
]


def main():
    input_path = path.join(path.dirname(__file__), "..", "libs", "raylib", "parser", "output", "raylib_api.json")
    output_path = path.join(path.dirname(__file__), "..", "sources", "rl", "enums.c")
    with open(input_path, "r") as input_file:
        raylib_api = json.load(input_file)
    with open(output_path, "w") as output_file:
        def writelines(*lines):
            output_file.writelines(line + "\n" for line in lines)
        writelines(
            "// This file was automatically generated by tools/generate_raylib_enums.py",
            "// Any manual modifications will be overwritten when running the script again",
            "#include \"enums.h\"",
            "#include \"raylib.h\"",
            "",
            "void rl_register_enums(s7_scheme *s7) {",
        )
        for enum in raylib_api["enums"]:
            if enum["name"] not in INCLUDE_ENUMS:
                continue
            writelines(
                f"  // enum {enum['name']}",
                *[
                    f"  S7_DEFINE_ENUM(s7, {value['name']});"
                    for value in enum["values"]
                ],
            )
        writelines("}")


if __name__ == "__main__":
    main()