fix build and basic_window example

some restructuring due to the recent changes to usingnamespace.

also cleaned up some deprecated stuff from raylib 3.7.

- appended the contents of raylib-wa.zig into raylib-zig.zig
- using raylib functions requires `const rl = @import("raylib");`
    (and accesing the identifiers inside rl, like `rl.InitWindow`)

only the basic_window example was updated, and it looks like it crashes
on keyboard inputs.

many thanks to @nektro :)
This commit is contained in:
Francisco Demartino 2022-01-08 04:20:11 -03:00
parent 3f19a5742a
commit 5e275e93df
14 changed files with 237 additions and 166 deletions

View file

@ -6,26 +6,27 @@ raylib.h in the working directory of this script and execute.
Tested with raylib version 3.0.0
"""
C_TO_ZIG = {
"bool": "bool",
"char": "u8",
"double": "f64",
"float": "f32",
"int": "c_int",
"long": "c_long",
"unsigned char": "u8",
"unsigned int": "c_uint",
}
# Some c types have a different size on different systems
# and zig knows that so we tell it to get the system specific size for us
def c_to_zig_type(t: str) -> str:
t = t.replace("const ", "")
if t == "float":
t = "f32"
if t == "double":
t = "f64"
if t == "int":
t = "c_int"
if t == "unsigned int":
t = "c_uint"
if t == "long":
t = "c_long"
if t == "char":
t = "u8"
if t == "unsigned char":
t = "u8"
return t
def c_to_zig_type(c: str) -> str:
c = c.replace("const ", "")
z = C_TO_ZIG.get(c)
if z is not None:
return z
return c
def fix_pointer(name: str, t: str):
@ -38,7 +39,7 @@ def fix_pointer(name: str, t: str):
t = pre + "const " + t
if t == "[*c]const void":
t = "*const c_void"
t = "*const anyopaque"
return name, t
@ -64,15 +65,42 @@ def parse_header(header_name: str, output_file: str, prefix: str):
header = open(header_name, mode="r")
zig_functions = []
zig_heads = []
zig_types = set()
leftover = ""
for line in header.readlines():
if line.startswith("typedef struct"):
zig_types.add(line.split(' ')[2])
elif line.startswith("typedef enum"):
# don't trip the general typedef case
pass
elif line.startswith("typedef "):
zig_types.add(line.split(' ')[2].replace(';', '').strip())
if not line.startswith(prefix):
continue
line = line.split(";", 1)[0]
if leftover:
line = leftover + line
leftover = ""
line = line.replace("* ", " *")
line = line.replace(",", ", ")
line = line.replace(" ", " ")
# each (.*) is some variable value
result = re.search(prefix + "(.*) (.*)start_arg(.*)end_arg(.*)", line.replace("(", "start_arg").replace(")", "end_arg"))
if result is None:
leftover += line
continue
# get whats in the (.*)'s
return_type = result.group(1)
func_name = result.group(2)
@ -95,13 +123,17 @@ def parse_header(header_name: str, output_file: str, prefix: str):
arg_type = c_to_zig_type(arg_type)
arg_name, arg_type = fix_pointer(arg_name, arg_type)
zig_types.add(arg_type)
zig_arguments.append(arg_name + ": " + arg_type) # put everything together
zig_arguments = ", ".join(zig_arguments)
zig_heads.append("pub extern fn " + func_name + "(" + zig_arguments + ") " + return_type + ";")
zigheader = open(output_file, mode="w")
print("usingnamespace @import(\"raylib-zig.zig\");\n", file=zigheader)
print("""const rl = @import("raylib-zig.zig");\n""", file=zigheader)
print("\n".join(sorted(f"const {t} = rl.{t};" for t in zig_types if ('*' not in t) and (t not in C_TO_ZIG.values()))), file=zigheader)
print("", file=zigheader)
print("\n".join(zig_heads), file=zigheader)
print("", file=zigheader)
print("\n".join(zig_functions), file=zigheader)