QOL docs, colors, version number, example imports

This commit is contained in:
richard 2021-10-04 15:56:29 +01:00
parent 9e17046408
commit 50c2fb12e2
13 changed files with 102 additions and 66 deletions

View file

@ -12,20 +12,20 @@ statically link and use in non-free / proprietary / commercial projects!
# Installation # Installation
We distribute a statically linked binary Raylib wheel:
python3 -m pip install raylib python3 -m pip install raylib
Problems may be caused by out of date pip: If it doesn't work, first make sure you have latest pip installed:
python3 -m pip install --upgrade pip python3 -m pip install --upgrade pip
Some platforms that _should_ be available: Windows 10 x64, MacOS 10.15 x64, Linux Ubuntu1804 x64. On most platforms it should install a binary wheel (Windows 10 x64, MacOS 10.15 x64, Linux Ubuntu1804 x64).
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. 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.
using homebrew, apt, etc. using homebrew, apt, etc.
[If it doesn't work, build from source](BUILDING.md) [If it doesn't work, you can build manually.](BUILDING.md)
## Dynamic binding version
There is now a separate dynamic version of this binding: There is now a separate dynamic version of this binding:
@ -36,8 +36,7 @@ There is now a separate dynamic version of this binding:
# How to use # How to use
There are two different ways of using this binding. You only need to pick one method, but you There are two APIs, you can use either or both:
can combine two methods in one program if you want to.
### If you are familiar with C coding and the Raylib C library and you want to use an exact copy of the C API ### If you are familiar with C coding and the Raylib C library and you want to use an exact copy of the C API
@ -63,7 +62,7 @@ Work in progress:
# Performance # Performance
For fastest permformance use Pypy rather than standard python. For fastest performance use Pypy rather than standard python.
Every call to C is costly, so it's slightly faster if you use Python data structures and functions when calculating Every call to C is costly, so it's slightly faster if you use Python data structures and functions when calculating
in your update loop in your update loop

View file

@ -22,29 +22,28 @@ The API is *still the same as Raylib*, so you should still reply on `the officia
Example program: Example program:
.. code-block:: .. code-block::
from raylib.pyray import PyRay import raylib
from raylib.colors import *
pyray = PyRay() pr = raylib.PyRay()
pyray.init_window(800, 450, "Hello Pyray") pr.init_window(800, 450, "Hello Pyray")
pyray.set_target_fps(60) pr.set_target_fps(60)
camera = pyray.Camera3D([18.0, 16.0, 18.0], [0.0, 0.0, 0.0], [0.0, 1.0, 0.0], 45.0, 0) camera = pr.Camera3D([18.0, 16.0, 18.0], [0.0, 0.0, 0.0], [0.0, 1.0, 0.0], 45.0, 0)
pyray.set_camera_mode(camera, pyray.CAMERA_ORBITAL) pr.set_camera_mode(camera, pr.CAMERA_ORBITAL)
while not pyray.window_should_close(): while not pr.window_should_close():
pyray.update_camera(camera) pr.update_camera(camera)
pyray.begin_drawing() pr.begin_drawing()
pyray.clear_background(RAYWHITE) pr.clear_background(pr.RAYWHITE)
pyray.begin_mode_3d(camera) pr.begin_mode_3d(camera)
pyray.draw_grid(20, 1.0) pr.draw_grid(20, 1.0)
pyray.end_mode_3d() pr.end_mode_3d()
pyray.draw_text("Hello world", 190, 200, 20, VIOLET) pr.draw_text("Hello world", 190, 200, 20, pr.VIOLET)
pyray.end_drawing() pr.end_drawing()
pyray.close_window() pr.close_window()
See also https://github.com/electronstudio/raylib-python-cffi/blob/master/test_pyray.py See also https://github.com/electronstudio/raylib-python-cffi/blob/master/test_pyray.py

View file

@ -22,6 +22,7 @@ import itertools
import os import os
import pathlib import pathlib
import platform import platform
from .version import __version__
MODULE = pathlib.Path(__file__).parent MODULE = pathlib.Path(__file__).parent
@ -52,6 +53,6 @@ ffi.cdef(open(MODULE / "raylib_modified.h").read().replace('RLAPI ', ''))
try: try:
raylib_fname = raylib_library_path() raylib_fname = raylib_library_path()
rl = ffi.dlopen(raylib_fname) rl = ffi.dlopen(raylib_fname)
print('LOADED DYNAMICALLY SHARED LIB "{}"'.format(raylib_fname)) print('LOADED DYNAMICALLY SHARED LIB {} {}'.format(__version__, raylib_fname))
except Exception as e: except Exception as e:
print(e) print(e)

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

@ -0,0 +1 @@
../../version.py

View file

@ -1,6 +1,7 @@
import pathlib import pathlib
from setuptools import setup from setuptools import setup
from setuptools.dist import Distribution from setuptools.dist import Distribution
from version import __version__
# The directory containing this file # The directory containing this file
HERE = pathlib.Path(__file__).parent HERE = pathlib.Path(__file__).parent
@ -12,7 +13,7 @@ README = (HERE / "README.rst").read_text()
# This call to setup() does all the work # This call to setup() does all the work
setup( setup(
name="raylib_dynamic", name="raylib_dynamic",
version="3.7.0.post6", version=__version__,
description="Python CFFI bindings for Raylib DLL version", description="Python CFFI bindings for Raylib DLL version",
long_description=README, long_description=README,
long_description_content_type="text/x-rst", long_description_content_type="text/x-rst",

1
dynamic/version.py Symbolic link
View file

@ -0,0 +1 @@
../version.py

View file

@ -18,5 +18,7 @@ from raylib.colors import *
import cffi import cffi
import sys import sys
from raylib.pyray import PyRay from raylib.pyray import PyRay
from .version import __version__
print("RAYLIB STATIC "+__version__+" LOADED", file=sys.stderr)
print("RAYLIB STATIC LOADED", file=sys.stderr)

View file

@ -15,6 +15,8 @@
# Assumes raylib, GL, etc are all already installed as system libraries. We dont distribute them. # Assumes raylib, GL, etc are all already installed as system libraries. We dont distribute them.
# Raylib must be installed and compiled with: cmake -DWITH_PIC=ON -DSHARED=ON -DSTATIC=ON .. # Raylib must be installed and compiled with: cmake -DWITH_PIC=ON -DSHARED=ON -DSTATIC=ON ..
# We use /usr/local/lib/libraylib.a to ensure we link to static version
from cffi import FFI from cffi import FFI
import os import os
import platform import platform
@ -27,7 +29,7 @@ def build_linux():
ffibuilder.cdef(open("raylib/raylib_modified.h").read().replace('RLAPI ', '')) ffibuilder.cdef(open("raylib/raylib_modified.h").read().replace('RLAPI ', ''))
ffibuilder.set_source("raylib._raylib_cffi", ffibuilder.set_source("raylib._raylib_cffi",
""" """
#include "../../raylib/raylib.h" #include "raylib.h"
""", """,
extra_link_args=['/usr/local/lib/libraylib.a','-lm', '-lpthread', '-lGLU', '-lGL', '-lrt', '-lm', '-ldl', '-lX11', '-lpthread'], extra_link_args=['/usr/local/lib/libraylib.a','-lm', '-lpthread', '-lGLU', '-lGL', '-lrt', '-lm', '-ldl', '-lX11', '-lpthread'],
libraries=['GL','m','pthread', 'dl', 'rt', 'X11'] libraries=['GL','m','pthread', 'dl', 'rt', 'X11']
@ -55,7 +57,7 @@ def build_mac():
""" """
#include "../../raylib/raylib.h" // the C header of the library, supplied by us here #include "../../raylib/raylib.h" // the C header of the library, supplied by us here
""", """,
extra_link_args=['-lraylib', '-framework', 'OpenGL', '-framework', 'Cocoa', '-framework', 'IOKit', '-framework', 'CoreFoundation', '-framework', 'CoreVideo'], extra_link_args=['/usr/local/lib/libraylib.a', '-framework', 'OpenGL', '-framework', 'Cocoa', '-framework', 'IOKit', '-framework', 'CoreFoundation', '-framework', 'CoreVideo'],
) )
if __name__ == "__main__": if __name__ == "__main__":

View file

@ -21,6 +21,33 @@ class PyRay:
def pointer(self, struct): def pointer(self, struct):
return ffi.addressof(struct) return ffi.addressof(struct)
LIGHTGRAY =( 200, 200, 200, 255 )
GRAY =( 130, 130, 130, 255 )
DARKGRAY =( 80, 80, 80, 255 )
YELLOW =( 253, 249, 0, 255 )
GOLD =( 255, 203, 0, 255 )
ORANGE =( 255, 161, 0, 255 )
PINK =( 255, 109, 194, 255 )
RED =( 230, 41, 55, 255 )
MAROON =( 190, 33, 55, 255 )
GREEN =( 0, 228, 48, 255 )
LIME =( 0, 158, 47, 255 )
DARKGREEN =( 0, 117, 44, 255 )
SKYBLUE =( 102, 191, 255, 255 )
BLUE =( 0, 121, 241, 255 )
DARKBLUE =( 0, 82, 172, 255 )
PURPLE =( 200, 122, 255, 255 )
VIOLET =( 135, 60, 190, 255 )
DARKPURPLE =( 112, 31, 126, 255 )
BEIGE =( 211, 176, 131, 255 )
BROWN =( 127, 106, 79, 255 )
DARKBROWN =( 76, 63, 47, 255 )
WHITE =( 255, 255, 255, 255 )
BLACK =( 0, 0, 0, 255 )
BLANK =( 0, 0, 0, 0 )
MAGENTA =( 255, 0, 255, 255 )
RAYWHITE =( 245, 245, 245, 255 )
def makefunc(a): def makefunc(a):
#print("makefunc ",a, ffi.typeof(a).args) #print("makefunc ",a, ffi.typeof(a).args)

1
raylib/version.py Symbolic link
View file

@ -0,0 +1 @@
../version.py

View file

@ -1,6 +1,7 @@
import pathlib import pathlib
from setuptools import setup from setuptools import setup
from setuptools.dist import Distribution from setuptools.dist import Distribution
from version import __version__
# The directory containing this file # The directory containing this file
HERE = pathlib.Path(__file__).parent HERE = pathlib.Path(__file__).parent
@ -16,7 +17,7 @@ class BinaryDistribution(Distribution):
# This call to setup() does all the work # This call to setup() does all the work
setup( setup(
name="raylib", name="raylib",
version="3.7.0.post6", version=__version__,
description="Python CFFI bindings for Raylib", description="Python CFFI bindings for Raylib",
long_description=README, long_description=README,
long_description_content_type="text/markdown", long_description_content_type="text/markdown",

View file

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

1
version.py Normal file
View file

@ -0,0 +1 @@
__version__ = "3.7.0.post7"