type hints for raylib.static

This commit is contained in:
richard 2021-08-28 13:35:55 +01:00
parent aa5659f272
commit b0a8448573
7 changed files with 2753 additions and 79 deletions

View file

@ -1,76 +0,0 @@
from raylib.richlib import *
WIDTH=1200
HEIGHT=800
CAMERA=pyray.CAMERA_FIRST_PERSON
player = Box((-5, 0, -5), (1, 1, 1), 'green', True)
o = Vector([-5,-0,-5])
td = False
def init():
camera.position = (0.0, 4, 7)
camera.target = (100,100,-100)
camera.up = (0, 1, 0)
camera.fovy = 60
#rl.UpdateCamera(camera)
pyray.set_camera_mode(camera[0], pyray.CAMERA_FIRST_PERSON)
#rl.UpdateCamera(camera)
def update():
if keyboard.right:
player.pos.x += 0.1
elif keyboard.left:
player.pos.x -= 0.1
elif keyboard.up:
player.pos.y += 0.1
elif keyboard.down:
player.pos.y -= 0.1
elif keyboard.l:
player.pos.z += 0.1
elif keyboard.p:
player.pos.z-= 0.1
if pyray.is_key_pressed(pyray.KEY_Z):
global td
td = not td
def draw3d():
if td:
#pyray.draw_plane((0, 0, 0), (300,300), DARKRED)
pyray.draw_grid(10, 1)
pyray.draw_plane((player.x, 0, player.z), (1,1), GRAY)
pyray.draw_ray([o,[0,0,1]],BLUE)
pyray.draw_ray([o,[0,1,0]],GREEN)
pyray.draw_ray([o,[1,0,0]],RED)
player.draw()
def draw2dbackground():
clear('white')
origin = rl.GetWorldToScreen(o, camera[0])
rl.DrawText(b"0", int(origin.x), int(origin.y), 20, BLACK)
for i in range (0,11):
xa = rl.GetWorldToScreen((o.x+i,o.y,o.z), camera[0])
ya = rl.GetWorldToScreen((o.x,o.y+i,o.z), camera[0])
za = rl.GetWorldToScreen((o.x,o.y,o.z+i), camera[0])
pyray.draw_text(str(i), int(xa.x), int(xa.y), 20, RED)
pyray.draw_text(str(i), int(ya.x), int(ya.y), 20, GREEN)
if td:
pyray.draw_text(str(i), int(za.x), int(za.y), 20, BLUE)
pyray.draw_text(f"X: {int(player.x+5)}", 10, 10, 30, RED)
pyray.draw_text(f"Y: {int(player.y)}", 10, 50, 30, GREEN)
if td:
pyray.draw_text(f"Z: {int(player.z+5)}", 10, 110, 30, BLUE)
run()

81
create_stub_static.py Normal file
View file

@ -0,0 +1,81 @@
from raylib.static import rl, ffi
from inspect import ismethod, getmembers, isbuiltin
import inflection, sys
print("""from typing import Any
class struct: ...
""")
def ctype_to_python_type(t):
if t == '_Bool':
return "bool"
elif t == 'void':
return 'None'
elif t == "long":
return "int"
elif t == "double":
return "float"
elif "char *" in t:
return "str"
elif "char" in t:
return "str" # not sure about this one
elif "*" in t:
return "Any"
elif t.startswith("struct"):
return t.replace("struct ","")
elif t.startswith("unsigned"):
return t.replace("unsigned ", "")
else:
return t
for name, attr in getmembers(rl):
uname = name
if isbuiltin(attr) or str(type(attr)) == "<class '_cffi_backend.__FFIFunctionWrapper'>":
sig = ""
for i, arg in enumerate(ffi.typeof(attr).args):
param_name = arg.cname.replace("struct", "").replace("char *", "str").replace("*",
"_pointer").replace(
" ", "")
param_type = ctype_to_python_type(arg.cname)
sig += f"{param_name}_{i}: {param_type},"
return_type = ffi.typeof(attr).result.cname
print(
f'def {uname}({sig}) -> {ctype_to_python_type(return_type)}:\n """{attr.__doc__}"""\n ...')
elif str(type(attr)) == "<class '_cffi_backend._CDataBase'>":
return_type = ffi.typeof(attr).result.cname
print(
f'def {uname}(*args) -> {ctype_to_python_type(return_type)}:\n """VARARG FUNCTION - MAY NOT BE SUPPORTED BY CFFI"""\n ...')
else:
#print("*****", str(type(attr)))
print(f"{name}: {str(type(attr))[8:-2]}") # this isolates the type
for struct in ffi.list_types()[0]:
print("processing", struct, file=sys.stderr)
if ffi.typeof(struct).kind == "struct":
# if ffi.typeof(struct).fields is None:
# print("weird empty struct, skipping", file=sys.stderr)
# break
print(f"{struct}: struct")
# sig = ""
# for arg in ffi.typeof(struct).fields:
# sig += ", " + arg[0]
# print(f" def __init__(self{sig}):")
#
# for arg in ffi.typeof(struct).fields:
# print(f" self.{arg[0]}={arg[0]}")
elif ffi.typeof(struct).kind == "enum":
print(f"{struct}: int")
else:
print("ERROR UNKNOWN TYPE", ffi.typeof(struct), file=sys.stderr)

View file

@ -1,5 +1,6 @@
#!/usr/bin/bash
python3 create_stub.py > raylib/pyray.pyi
python3 create_stub_pyray.py > raylib/pyray.pyi
python3 create_stub_static.py >raylib/static/__init__.pyi
cd docs-src
make clean ; make html ; cp -a _build/html/. ../docs/

View file

@ -1 +1,4 @@
__version__ = "3.7.0.post2"
__version__ = "3.7.0.post3"
from raylib.static import *
from raylib.pyray import PyRay

2665
raylib/static/__init__.pyi Normal file

File diff suppressed because it is too large Load diff

View file

@ -3,7 +3,7 @@ This shows how to use the CFFI static (API) binding. It should be fast and code
C code.
"""
from raylib.static import *
from raylib import *
InitWindow(800, 450, b"Raylib static texture test")
SetTargetFPS(60)