improve docs, make clear different APIs and static/dynamic

This commit is contained in:
electronstudio 2021-10-03 23:34:09 +01:00
parent 47c4d0d1b4
commit 9e17046408
49 changed files with 5467 additions and 17850 deletions

View file

@ -1,5 +1,5 @@
raylib.dynamic
==============
Dynamic Bindings
================
CFFI ABI dynamic bindings avoid the need to compile a C extension module. They now been moved to a separate module::
@ -9,7 +9,8 @@ CFFI ABI dynamic bindings avoid the need to compile a C extension module. They
There have been some weird failures with dynamic bindings and ctypes bindings before and often the
failures are silent
so you dont even know. Also the static bindings are faster. Therefore I personally recommend the static ones.
so you don't even know something has gone wrong and you don't get proper stacktraces. Also the static bindings are faster.
Therefore I personally recommend the static ones.
But the dynamic bindings have the advantage that you don't need to compile anything to install. You just need a Raylib DLL.
API is exactly the same as the static one documented here. (Therefore you can't have both modules installed at once.) The only difference is you can't do::
@ -18,25 +19,20 @@ API is exactly the same as the static one documented here. (Therefore you can't
Instead you have to do::
from raylib import raylib as rl
from raylib import rl
Then you access the functions with ``rl.`` prefix. See
Then you access the functions with ``rl.`` prefix.
See https://github.com/electronstudio/raylib-python-cffi/blob/master/dynamic/test_dynamic.py for an example.
If you use the ``rl.`` prefix then code will work on both static and dynamic bindings.
.. warning::
If you access functions via ``raylib.pyray`` then there is no difference at all, but be warned this hasn't been tested.
.. important::
If your system already has the Raylib library installed, you can set the environment variable ``USE_EXTERNAL_RAYLIB`` and it will
always be used instead of the bundled DLLs.
.. note::
If you write a program using the ``rl.`` prefix on all the functions and then you decide you want to use
that same program with the static binding instead of the dynamic, you don't have to remove the ``rl``,
you can just do::
import raylib as rl

View file

@ -51,7 +51,7 @@ ffi.cdef(open(MODULE / "raylib_modified.h").read().replace('RLAPI ', ''))
try:
raylib_fname = raylib_library_path()
raylib = ffi.dlopen(raylib_fname)
rl = ffi.dlopen(raylib_fname)
print('LOADED DYNAMICALLY SHARED LIB "{}"'.format(raylib_fname))
except Exception as e:
print(e)

1
dynamic/raylib/pyray.py Symbolic link
View file

@ -0,0 +1 @@
../../raylib/pyray.py

1
dynamic/raylib/pyray.pyi Symbolic link
View file

@ -0,0 +1 @@
../../raylib/pyray.pyi

View file

@ -2,14 +2,14 @@
This shows how to use the CFFI dynamic (ABI) binding. Note that is slower and more likely to run into silent errors and segfaults.
But it doesnt require any C compiler to build.
"""
from raylib import ffi, raylib as rl
from raylib import ffi, rl
from raylib.colors import *
rl.InitWindow(800, 450, b"Raylib dynamic binding test")
rl.SetTargetFPS(60)
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])
image = rl.LoadImage(b"examples/models/resources/heightmap.png")
image = rl.LoadImage(b"../examples/models/resources/heightmap.png")
texture = rl.LoadTextureFromImage(image)
mesh = rl.GenMeshHeightmap(image, [16, 8, 16])
model = rl.LoadModelFromMesh(mesh)

48
dynamic/test_pyray.py Normal file
View file

@ -0,0 +1,48 @@
"""
This shows how to use the Pyray wrapper around the static binding.
"""
from raylib.pyray import PyRay
from raylib.colors import *
pyray = PyRay()
pyray.init_window(800, 450, "Raylib texture test")
pyray.set_target_fps(60)
image = pyray.gen_image_color(800, 400, (0,0,0,255) )
texture = pyray.load_texture_from_image(image)
pyray.update_texture(texture, image.data)
camera = pyray.Camera3D([18.0, 16.0, 18.0], [0.0, 0.0, 0.0], [0.0, 1.0, 0.0], 45.0, 0)
image = pyray.load_image("../examples/models/resources/heightmap.png")
texture = pyray.load_texture_from_image(image)
mesh = pyray.gen_mesh_heightmap(image, (16, 8, 16))
model = pyray.load_model_from_mesh(mesh)
model.materials.maps[pyray.MATERIAL_MAP_DIFFUSE].texture = texture
pyray.unload_image(image)
pyray.set_camera_mode(camera, pyray.CAMERA_ORBITAL)
pos = pyray.get_mouse_position()
ray = pyray.get_mouse_ray(pos, camera)
rayhit = pyray.get_collision_ray_ground(ray, 0)
print(str(rayhit.position.x))
while not pyray.window_should_close():
pyray.update_camera(camera)
pyray.begin_drawing()
pyray.clear_background(RAYWHITE)
pyray.begin_mode_3d(camera)
pyray.draw_model(model, (-8.0, 0.0, -8.0), 1.0, RED)
pyray.draw_grid(20, 1.0)
pyray.end_mode_3d()
pyray.draw_text("This mesh should be textured", 190, 200, 20, VIOLET)
pyray.end_drawing()
pos = pyray.get_mouse_position()
ray = pyray.get_mouse_ray(pos, camera)
rayhit = pyray.get_collision_ray_ground(ray, 0)
#print(str(rayhit.position.x))
pyray.close_window()