Skip to content

Commit 3a8d764

Browse files
add pythonic wrapper, pyray
1 parent 18f3fdf commit 3a8d764

File tree

7 files changed

+113
-56
lines changed

7 files changed

+113
-56
lines changed

raylib/richlib/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"""
33
__version__ = '0.1'
44

5-
from ..static import ffi, rl
5+
from ..static import ffi, rl, prl
66
#from ..dynamic import ffi, raylib as rl
77
from ..colors import *
88

raylib/static/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,3 @@
55

66

77
print("RAYLIB STATIC LOADED")
8-

raylib/static/pyray.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
from . import rl, ffi
2+
from ..colors import *
3+
from inspect import ismethod,getmembers,isbuiltin
4+
import inflection
5+
6+
class PyRay:
7+
def pointer(self, struct):
8+
return ffi.addressof(struct)
9+
10+
pyray = PyRay()
11+
12+
13+
def makefunc(a):
14+
#print("makefunc ",a)
15+
def func(self, *args):
16+
modified_args = []
17+
for arg in args:
18+
#print(arg, type(arg))
19+
if type(arg) == str:
20+
encoded = arg.encode('utf-8')
21+
modified_args.append(encoded)
22+
else:
23+
modified_args.append(arg)
24+
return a(*modified_args)
25+
return func
26+
27+
def makeStructHelper(struct):
28+
def func(self, *args):
29+
return ffi.new(f"struct {struct} *", args)[0]
30+
return func
31+
32+
33+
for name, attr in getmembers(rl):
34+
print(name, attr)
35+
uname = inflection.underscore(name).replace('3_d','_3d').replace('2_d','_2d')
36+
if isbuiltin(attr):
37+
#print(attr)
38+
#print(dir(attr))
39+
#print(dir(attr.__repr__))
40+
f = makefunc(attr)
41+
setattr(PyRay, uname, f)
42+
#def wrap(*args):
43+
# print("call to ",attr)
44+
#setattr(PyRay, uname, lambda *args: print("call to ",attr))
45+
else:
46+
setattr(PyRay, name, attr)
47+
48+
for struct in ('Vector2','Vector3','Vector4','Camera2D', 'Camera3D', 'Quaternion', 'Color'):
49+
f = makeStructHelper(struct)
50+
setattr(PyRay, struct, f)

test_dynamic.py

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,37 @@
11
"""
2-
This shows how to use the CFFI dynamic (ABI) binding. Note that is slower and more likely to run into silent errors.
2+
This shows how to use the CFFI dynamic (ABI) binding. Note that is slower and more likely to run into silent errors and segfaults.
33
But it doesnt require any C compiler to build.
44
"""
5-
import pathlib
6-
MODULE = pathlib.Path(__file__).parent
7-
85
from raylib.dynamic import ffi, raylib as rl
96
from raylib.colors import *
107

11-
rl.InitWindow(800,450,b"Raylib dynamic binding test")
8+
rl.InitWindow(800, 450, b"Raylib dynamic binding test")
129
rl.SetTargetFPS(60)
1310

14-
camera = ffi.new("struct Camera3D *", [[ 18.0, 16.0, 18.0 ], [ 0.0, 0.0, 0.0 ], [ 0.0, 1.0, 0.0 ], 45.0, 0 ])
15-
16-
imageFile = str(MODULE / "examples/models/resources/heightmap.png").encode('utf-8')
17-
image = rl.LoadImage(imageFile)
11+
camera = ffi.new("struct Camera3D *", [[18.0, 16.0, 18.0], [0.0, 0.0, 0.0], [0.0, 1.0, 0.0], 45.0, 0])
12+
image = rl.LoadImage(b"examples/models/resources/heightmap.png")
1813
texture = rl.LoadTextureFromImage(image)
19-
mesh = rl.GenMeshHeightmap(image, [ 16, 8, 16 ])
14+
mesh = rl.GenMeshHeightmap(image, [16, 8, 16])
2015
model = rl.LoadModelFromMesh(mesh)
21-
print(model.materials) # SHOULD BE A pointer to a 'struct Material' but some is NULL pointer to 'Material' ?
22-
16+
print(model.materials) # SHOULD BE A pointer to a 'struct Material' but some is NULL pointer to 'Material' ?
2317
model.materials.maps[rl.MAP_DIFFUSE].texture = texture
24-
mapPosition = ( -8.0, 0.0, -8.0 )
2518

2619
rl.UnloadImage(image)
27-
2820
rl.SetCameraMode(camera[0], rl.CAMERA_ORBITAL)
2921

30-
3122
while not rl.WindowShouldClose():
3223
rl.UpdateCamera(camera)
3324
rl.BeginDrawing()
3425
rl.ClearBackground(RAYWHITE)
3526
rl.BeginMode3D(camera[0])
36-
rl.DrawModel(model, mapPosition, 1.0, RED)
27+
rl.DrawModel(model, (-8.0, 0.0, -8.0), 1.0, RED)
3728
rl.DrawGrid(20, 1.0)
3829
rl.EndMode3D()
3930
rl.DrawText(b"This mesh should be textured", 190, 200, 20, VIOLET)
4031
rl.EndDrawing()
4132
rl.CloseWindow()
4233

43-
4434
"""
4535
Previously this failed to work in the same place the ctypes binding fails, accessing
4636
materials of a model. I though it was because Python can't dynamically tell the difference between a pointer and an array.
47-
"""
37+
"""

test_pyray.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""
2+
This shows how to use the Pyray wrapper around the static binding.
3+
"""
4+
from raylib.static.pyray import pyray as prl
5+
from raylib.colors import *
6+
7+
prl.init_window(800, 450, "Raylib texture test")
8+
prl.set_target_fps(60)
9+
10+
camera = prl.Camera3D([18.0, 16.0, 18.0], [0.0, 0.0, 0.0], [0.0, 1.0, 0.0], 45.0, 0)
11+
image = prl.load_image("examples/models/resources/heightmap.png")
12+
texture = prl.load_texture_from_image(image)
13+
mesh = prl.gen_mesh_heightmap(image, (16, 8, 16))
14+
model = prl.load_model_from_mesh(mesh)
15+
model.materials.maps[prl.MAP_DIFFUSE].texture = texture
16+
17+
prl.unload_image(image)
18+
prl.set_camera_mode(camera, prl.CAMERA_ORBITAL)
19+
20+
while not prl.window_should_close():
21+
prl.update_camera(prl.pointer(camera))
22+
prl.begin_drawing()
23+
prl.clear_background(RAYWHITE)
24+
prl.begin_mode_3d(camera)
25+
prl.draw_model(model, (-8.0, 0.0, -8.0), 1.0, RED)
26+
prl.draw_grid(20, 1.0)
27+
prl.end_mode_3d()
28+
prl.draw_text("This mesh should be textured", 190, 200, 20, VIOLET)
29+
prl.end_drawing()
30+
prl.close_window()

test_richlib.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from raylib.richlib import *
2+
from raylib.static.pyray import pyray
23

34
WIDTH=800
45
HEIGHT=640
@@ -14,6 +15,7 @@
1415
castle.scale.x=1
1516
castle.scale.z=1
1617

18+
1719
#def init():
1820
# set_camera_mode(camera, CAMERA_FIRST_PERSON)
1921

@@ -39,12 +41,15 @@ def draw3d():
3941
enemy_sphere.draw()
4042
player.draw()
4143
castle.draw()
42-
rl.DrawGrid(10, 10)
44+
pyray.draw_grid(10, 10)
4345

4446

4547
def draw2d():
46-
rl.DrawText(b"Move player with cursors to collide", 220, 40, 20, GRAY)
47-
rl.DrawFPS(10, 10)
48+
pyray.draw_text("Move player with cursors to collide", 220, 40, 20, (255,0,0,255))
49+
rl.DrawText(b"Move player with cursors to collide", 220, 80, 20, (255,0,0,255))
50+
#prl.DrawFPS(10, 10)
51+
#pyray.foo(10,10)
52+
pyray.draw_fps(20,20)
4853

4954

5055
run()

test_static.py

Lines changed: 16 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,31 @@
1-
from raylib.static import *
1+
"""
2+
This shows how to use the CFFI static (API) binding. It should be fast and code be as close as possible to original
3+
C code.
4+
"""
25

6+
from raylib.static import *
37

4-
InitWindow(800,450,b"Raylib texture test")
8+
InitWindow(800, 450, b"Raylib static texture test")
59
SetTargetFPS(60)
610

7-
print(Vector3(1,1,1))
8-
9-
camera = ffi.new("struct Camera3D *", [[ 18.0, 16.0, 18.0 ], [ 0.0, 0.0, 0.0 ], [ 0.0, 1.0, 0.0 ], 45.0, 0 ])
10-
11-
12-
image = LoadImage(b"examples/models/resources/heightmap.png") # Load heightmap image (RAM)
13-
print(image)
14-
texture = LoadTextureFromImage(image) # Convert image to texture (VRAM)
15-
16-
mesh = GenMeshHeightmap(image, ( 16, 8, 16 )) # Generate heightmap mesh (RAM and VRAM)
17-
print(mesh)
18-
model = LoadModelFromMesh(mesh) # Load model from generated mesh
19-
20-
print(model.meshes)
21-
print(model.meshes[1])
22-
23-
print(model.materials)
24-
print(model.materialCount)
25-
print(model.materials[0].maps[rl.MAP_DIFFUSE])
26-
27-
model.materials.maps[MAP_DIFFUSE].texture = texture # Set map diffuse texture
28-
print(model.materials[0].maps[rl.MAP_DIFFUSE].value)
29-
30-
31-
mapPosition = ( -8.0, 0.0, -8.0 ) # Define model position
32-
33-
UnloadImage(image) # Unload heightmap image from RAM, already uploaded to VRAM
34-
35-
SetCameraMode(camera[0], CAMERA_ORBITAL) # Set an orbital camera mode
11+
camera = ffi.new("struct Camera3D *", [[18.0, 16.0, 18.0], [0.0, 0.0, 0.0], [0.0, 1.0, 0.0], 45.0, 0])
12+
image = LoadImage(b"examples/models/resources/heightmap.png")
13+
texture = LoadTextureFromImage(image)
14+
mesh = GenMeshHeightmap(image, (16, 8, 16))
15+
model = LoadModelFromMesh(mesh)
16+
model.materials.maps[MAP_DIFFUSE].texture = texture
3617

18+
UnloadImage(image)
19+
SetCameraMode(camera[0], CAMERA_ORBITAL)
3720

3821
while not WindowShouldClose():
3922
UpdateCamera(camera)
4023
BeginDrawing()
4124
ClearBackground(RAYWHITE)
4225
BeginMode3D(camera[0])
43-
DrawModel(model, mapPosition, 1.0, RED)
26+
DrawModel(model, (-8.0, 0.0, -8.0), 1.0, RED)
4427
DrawGrid(20, 1.0)
4528
EndMode3D()
4629
DrawText(b"This mesh should be textured", 190, 200, 20, VIOLET)
4730
EndDrawing()
48-
CloseWindow()
31+
CloseWindow()

0 commit comments

Comments
 (0)