Skip to content

Commit ec752bd

Browse files
refactor pyray into separate module
1 parent 8666f0c commit ec752bd

24 files changed

+2394
-2055
lines changed

.github/workflows/build.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ jobs:
8484
cd raylib-c
8585
mkdir build
8686
cd build
87-
cmake -DINCLUDE_EVERYTHING=on -DSUPPORT_FILEFORMAT_JPG -DWITH_PIC=on -DCMAKE_BUILD_TYPE=Release ..
87+
cmake -DINCLUDE_EVERYTHING=on -DSUPPORT_FILEFORMAT_JPG=on -DWITH_PIC=on -DCMAKE_BUILD_TYPE=Release ..
8888
make -j2
8989
sudo make install
9090
- name: Build raylib-python-cffi
@@ -131,7 +131,7 @@ jobs:
131131
cd raylib-c
132132
mkdir build
133133
cd build
134-
cmake -DINCLUDE_EVERYTHING=on -DSUPPORT_FILEFORMAT_JPG -DWITH_PIC=on -DCMAKE_BUILD_TYPE=Release ..
134+
cmake -DINCLUDE_EVERYTHING=on -DSUPPORT_FILEFORMAT_JPG=on -DWITH_PIC=on -DCMAKE_BUILD_TYPE=Release ..
135135
msbuild raylib.sln /target:raylib /property:Configuration=Release
136136
copy raylib\Release\raylib.lib ..\..
137137
cd ..\..

README.md

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,35 @@
11
# Python Bindings for Raylib 3.7
22

33
New CFFI API static bindings. Automatically generated to be as close as possible to
4-
original Raylib. Faster, fewer bugs and easier to maintain than ctypes.
4+
original Raylib. Faster, fewer bugs and easier to maintain than ctypes. Commercial-friendly license.
5+
Docstrings and auto-completion.
56

67
[Full documentation](http://electronstudio.github.io/raylib-python-cffi)
78

8-
# License (updated)
9+
# Quickstart
910

10-
The bindings are now under the Eclipse Public License, so you are free to
11-
statically link and use in non-free / proprietary / commercial projects!
11+
`pip3 install raylib`
1212

13-
# Installation
13+
from pyray import *
14+
init_window(800, 450, "Hello")
15+
while not window_should_close():
16+
begin_drawing()
17+
clear_background(WHITE)
18+
draw_text("Hello world", 190, 200, 20, VIOLET)
19+
end_drawing()
20+
close_window()
1421

15-
python3 -m pip install raylib
1622

17-
If it doesn't work, first make sure you have latest pip installed:
23+
# Installation
24+
25+
First make sure you have latest pip installed:
1826

1927
python3 -m pip install --upgrade pip
2028

29+
Then install
30+
31+
python3 -m pip install raylib
32+
2133
On most platforms it should install a binary wheel (Windows 10 x64, MacOS 10.15 x64, Linux Ubuntu1804 x64).
2234

2335
If yours isn't available then pip will attempt to build from source, in which case you will need to have Raylib development libs installed, e.g.
@@ -51,18 +63,23 @@ Use [the Python API](https://electronstudio.github.io/raylib-python-cffi/pyray.h
5163

5264
# RLZero
5365

54-
Work in progress:
66+
A related library (that is a work in progress!):
5567

5668
[A simplified API for Raylib for use in education and to enable beginners to create 3d games](https://github.com/electronstudio/rlzero)
5769

5870
# Help wanted
5971

60-
* converting more examples from C to python
61-
* testing and building on more platforms
72+
* Converting more examples from C to Python
73+
* Testing on more platforms
74+
75+
# License (updated)
76+
77+
The bindings are now under the Eclipse Public License, so you are free to
78+
statically link and use in non-free / proprietary / commercial projects!
6279

6380
# Performance
6481

65-
For fastest performance use Pypy rather than standard python.
82+
For fastest performance use Pypy rather than standard Python.
6683

6784
Every call to C is costly, so it's slightly faster if you use Python data structures and functions when calculating
6885
in your update loop

create_stub_pyray.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ def ctype_to_python_type(t):
4444

4545
print("""from typing import Any
4646
47-
class PyRay:
48-
def pointer(self, struct):
49-
return ffi.addressof(struct)
47+
48+
def pointer(struct):
49+
...
5050
""")
5151

5252

@@ -69,7 +69,7 @@ def pointer(self, struct):
6969
param_name = list(p)[i]
7070

7171
param_type = ctype_to_python_type(arg.cname)
72-
sig += f", {param_name}: {param_type}"
72+
sig += f"{param_name}: {param_type},"
7373

7474
return_type = ffi.typeof(attr).result.cname
7575

@@ -79,15 +79,15 @@ def pointer(self, struct):
7979
description = json_object['description']
8080

8181
print(
82-
f' def {uname}(self{sig}) -> {ctype_to_python_type(return_type)}:\n """{description}"""\n ...')
82+
f'def {uname}({sig}) -> {ctype_to_python_type(return_type)}:\n """{description}"""\n ...')
8383

8484
elif str(type(attr)) == "<class '_cffi_backend._CDataBase'>":
8585
return_type = ffi.typeof(attr).result.cname
8686
print(
87-
f' def {uname}(self, *args) -> {ctype_to_python_type(return_type)}:\n """VARARG FUNCTION - MAY NOT BE SUPPORTED BY CFFI"""\n ...')
87+
f'def {uname}(*args) -> {ctype_to_python_type(return_type)}:\n """VARARG FUNCTION - MAY NOT BE SUPPORTED BY CFFI"""\n ...')
8888
else:
8989
#print("*****", str(type(attr)))
90-
print(f" {name}: {str(type(attr))[8:-2]}") # this isolates the type
90+
print(f"{name}: {str(type(attr))[8:-2]}") # this isolates the type
9191

9292
for struct in ffi.list_types()[0]:
9393
print("processing", struct, file=sys.stderr)
@@ -99,18 +99,18 @@ def pointer(self, struct):
9999
if ffi.typeof(struct).fields is None:
100100
print("weird empty struct, skipping", file=sys.stderr)
101101
break
102-
print(f" class {struct}:")
103-
print(f' """ comment """')
102+
print(f"class {struct}:")
103+
print(f' """ struct """')
104104
sig = ""
105105
for arg in ffi.typeof(struct).fields:
106106
sig += ", " + arg[0]
107-
print(f" def __init__(self{sig}):")
107+
print(f" def __init__(self{sig}):")
108108

109109
for arg in ffi.typeof(struct).fields:
110-
print(f" self.{arg[0]}={arg[0]}")
110+
print(f" self.{arg[0]}={arg[0]}")
111111

112112
elif ffi.typeof(struct).kind == "enum":
113-
print(f" {struct}: int")
113+
print(f"{struct}: int")
114114
else:
115115
print("ERROR UNKNOWN TYPE", ffi.typeof(struct), file=sys.stderr)
116116

dynamic/README.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ See https://github.com/electronstudio/raylib-python-cffi/blob/master/dynamic/tes
2727

2828
If you use the ``rl.`` prefix then code will work on both static and dynamic bindings.
2929

30-
.. warning::
30+
.. tip::
3131

32-
If you access functions via ``raylib.pyray`` then there is no difference at all, but be warned this hasn't been tested.
32+
If you access functions via ``import pyray`` then there is no difference at all, but be warned this hasn't been tested much.
3333

3434

3535
.. important::

dynamic/raylib/__init__.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,29 @@ def so_name():
5858
except Exception as e:
5959
print(e)
6060

61-
from .pyray import PyRay
61+
LIGHTGRAY =( 200, 200, 200, 255 )
62+
GRAY =( 130, 130, 130, 255 )
63+
DARKGRAY =( 80, 80, 80, 255 )
64+
YELLOW =( 253, 249, 0, 255 )
65+
GOLD =( 255, 203, 0, 255 )
66+
ORANGE =( 255, 161, 0, 255 )
67+
PINK =( 255, 109, 194, 255 )
68+
RED =( 230, 41, 55, 255 )
69+
MAROON =( 190, 33, 55, 255 )
70+
GREEN =( 0, 228, 48, 255 )
71+
LIME =( 0, 158, 47, 255 )
72+
DARKGREEN =( 0, 117, 44, 255 )
73+
SKYBLUE =( 102, 191, 255, 255 )
74+
BLUE =( 0, 121, 241, 255 )
75+
DARKBLUE =( 0, 82, 172, 255 )
76+
PURPLE =( 200, 122, 255, 255 )
77+
VIOLET =( 135, 60, 190, 255 )
78+
DARKPURPLE =( 112, 31, 126, 255 )
79+
BEIGE =( 211, 176, 131, 255 )
80+
BROWN =( 127, 106, 79, 255 )
81+
DARKBROWN =( 76, 63, 47, 255 )
82+
WHITE =( 255, 255, 255, 255 )
83+
BLACK =( 0, 0, 0, 255 )
84+
BLANK =( 0, 0, 0, 0 )
85+
MAGENTA =( 255, 0, 255, 255 )
86+
RAYWHITE =( 245, 245, 245, 255 )

dynamic/raylib/pyray.py

Lines changed: 0 additions & 1 deletion
This file was deleted.

dynamic/raylib/pyray.pyi

Lines changed: 0 additions & 1 deletion
This file was deleted.

dynamic/setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"Programming Language :: Python :: 3.6",
3131
"Programming Language :: Python :: 3.7",
3232
],
33-
packages=["raylib"],
33+
packages=["raylib", "pyray"],
3434
include_package_data=True,
3535
install_requires=["cffi>=1.14.5","inflection"],
3636
)

dynamic/test_pyray.py

Lines changed: 0 additions & 1 deletion
This file was deleted.

dynamic/test_pyray.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
"""
2+
This shows how to use the Pyray wrapper around the static binding.
3+
"""
4+
5+
import pyray as pr
6+
7+
pr.init_window(800, 450, "Raylib texture test")
8+
pr.set_target_fps(60)
9+
10+
image = pr.gen_image_color(800, 400, (0,0,0,255) )
11+
texture = pr.load_texture_from_image(image)
12+
pr.update_texture(texture, image.data)
13+
14+
camera = pr.Camera3D([18.0, 16.0, 18.0], [0.0, 0.0, 0.0], [0.0, 1.0, 0.0], 45.0, 0)
15+
image = pr.load_image("examples/models/resources/heightmap.png")
16+
texture = pr.load_texture_from_image(image)
17+
mesh = pr.gen_mesh_heightmap(image, (16, 8, 16))
18+
model = pr.load_model_from_mesh(mesh)
19+
model.materials.maps[pr.MATERIAL_MAP_DIFFUSE].texture = texture
20+
21+
pr.unload_image(image)
22+
pr.set_camera_mode(camera, pr.CAMERA_ORBITAL)
23+
24+
pos = pr.get_mouse_position()
25+
ray = pr.get_mouse_ray(pos, camera)
26+
rayhit = pr.get_collision_ray_ground(ray, 0)
27+
print(str(rayhit.position.x))
28+
29+
while not pr.window_should_close():
30+
pr.update_camera(camera)
31+
pr.begin_drawing()
32+
pr.clear_background(pr.RAYWHITE)
33+
pr.begin_mode_3d(camera)
34+
pr.draw_model(model, (-8.0, 0.0, -8.0), 1.0, pr.RED)
35+
pr.draw_grid(20, 1.0)
36+
pr.end_mode_3d()
37+
pr.draw_text("This mesh should be textured", 190, 200, 20, pr.VIOLET)
38+
pr.end_drawing()
39+
40+
pos = pr.get_mouse_position()
41+
ray = pr.get_mouse_ray(pos, camera)
42+
rayhit = pr.get_collision_ray_ground(ray, 0)
43+
#print(str(rayhit.position.x))
44+
45+
pr.close_window()

0 commit comments

Comments
 (0)