refactor pyray into separate module

This commit is contained in:
richard 2021-10-08 04:47:58 +01:00
parent 8666f0cff8
commit ec752bdab7
24 changed files with 2394 additions and 2055 deletions

18
tests/color_test.py Normal file
View file

@ -0,0 +1,18 @@
import pyray
pyray.init_window(800, 400, 'demo')
white = pyray.get_color(0xFFFFFFFF)
red = pyray.get_color(0xFF0000FF)
green = pyray.get_color(0x00FF00FF)
blue = pyray.get_color(0x0000FFFF)
while not pyray.window_should_close():
pyray.begin_drawing()
pyray.clear_background(white)
pyray.draw_rectangle(0, 0, 100, 100, red)
pyray.draw_rectangle(100, 100, 100, 100, green)
pyray.draw_rectangle(200, 200, 100, 100, blue)
pyray.end_drawing()
pyray.close_window()

8
tests/hello_world.py Normal file
View file

@ -0,0 +1,8 @@
from pyray import *
init_window(800, 450, "Hello")
while not window_should_close():
begin_drawing()
clear_background(WHITE)
draw_text("Hello world", 190, 200, 20, VIOLET)
end_drawing()
close_window()

45
tests/test_pyray.py Normal file
View file

@ -0,0 +1,45 @@
"""
This shows how to use the Pyray wrapper around the static binding.
"""
import pyray as pr
pr.init_window(800, 450, "Raylib texture test")
pr.set_target_fps(60)
image = pr.gen_image_color(800, 400, (0,0,0,255) )
texture = pr.load_texture_from_image(image)
pr.update_texture(texture, image.data)
camera = pr.Camera3D([18.0, 16.0, 18.0], [0.0, 0.0, 0.0], [0.0, 1.0, 0.0], 45.0, 0)
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
pr.unload_image(image)
pr.set_camera_mode(camera, pr.CAMERA_ORBITAL)
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))
while not pr.window_should_close():
pr.update_camera(camera)
pr.begin_drawing()
pr.clear_background(pr.RAYWHITE)
pr.begin_mode_3d(camera)
pr.draw_model(model, (-8.0, 0.0, -8.0), 1.0, pr.RED)
pr.draw_grid(20, 1.0)
pr.end_mode_3d()
pr.draw_text("This mesh should be textured", 190, 200, 20, pr.VIOLET)
pr.end_drawing()
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))
pr.close_window()

View file

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

View file

@ -0,0 +1,47 @@
"""
This shows how to use the Pyray wrapper around the static binding.
"""
from raylib import pyray
pr = pyray.PyRay()
pr.init_window(800, 450, "Raylib texture test")
pr.set_target_fps(60)
image = pr.gen_image_color(800, 400, (0,0,0,255) )
texture = pr.load_texture_from_image(image)
pr.update_texture(texture, image.data)
camera = pr.Camera3D([18.0, 16.0, 18.0], [0.0, 0.0, 0.0], [0.0, 1.0, 0.0], 45.0, 0)
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
pr.unload_image(image)
pr.set_camera_mode(camera, pr.CAMERA_ORBITAL)
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))
while not pr.window_should_close():
pr.update_camera(camera)
pr.begin_drawing()
pr.clear_background(pr.RAYWHITE)
pr.begin_mode_3d(camera)
pr.draw_model(model, (-8.0, 0.0, -8.0), 1.0, pr.RED)
pr.draw_grid(20, 1.0)
pr.end_mode_3d()
pr.draw_text("This mesh should be textured", 190, 200, 20, pr.VIOLET)
pr.end_drawing()
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))
pr.close_window()

31
tests/test_static.py Normal file
View file

@ -0,0 +1,31 @@
"""
This shows how to use the CFFI static (API) binding. It should be fast and code be as close as possible to original
C code.
"""
from raylib import *
InitWindow(800, 450, b"Raylib static texture test")
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 = LoadImage(b"examples/models/resources/heightmap.png")
texture = LoadTextureFromImage(image)
mesh = GenMeshHeightmap(image, (16, 8, 16))
model = LoadModelFromMesh(mesh)
model.materials.maps[MATERIAL_MAP_DIFFUSE].texture = texture
UnloadImage(image)
SetCameraMode(camera[0], CAMERA_ORBITAL)
while not WindowShouldClose():
UpdateCamera(camera)
BeginDrawing()
ClearBackground(RAYWHITE)
BeginMode3D(camera[0])
DrawModel(model, (-8.0, 0.0, -8.0), 1.0, RED)
DrawGrid(20, 1.0)
EndMode3D()
DrawText(b"This mesh should be textured", 190, 200, 20, VIOLET)
EndDrawing()
CloseWindow()

View file

@ -0,0 +1,37 @@
"""
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, rl, colors
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")
texture = rl.LoadTextureFromImage(image)
mesh = rl.GenMeshHeightmap(image, [16, 8, 16])
model = rl.LoadModelFromMesh(mesh)
print(model.materials) # SHOULD BE A pointer to a 'struct Material' but some is NULL pointer to 'Material' ?
model.materials.maps[rl.MATERIAL_MAP_DIFFUSE].texture = texture
rl.UnloadImage(image)
rl.SetCameraMode(camera[0], rl.CAMERA_ORBITAL)
while not rl.WindowShouldClose():
rl.UpdateCamera(camera)
rl.BeginDrawing()
rl.ClearBackground(colors.RAYWHITE)
rl.BeginMode3D(camera[0])
rl.DrawModel(model, (-8.0, 0.0, -8.0), 1.0, colors.RED)
rl.DrawGrid(20, 1.0)
rl.EndMode3D()
rl.DrawText(b"This mesh should be textured", 190, 200, 20, colors.VIOLET)
rl.EndDrawing()
rl.CloseWindow()
"""
Previously this failed to work in the same place the ctypes binding fails, accessing
materials of a model. I though it was because Python can't dynamically tell the difference between a pointer and an array.
"""

View file

@ -0,0 +1,32 @@
"""
This shows how to use the CFFI static (API) binding. It should be fast and code be as close as possible to original
C code.
"""
import raylib as rl
rl.InitWindow(800, 450, b"Raylib static binding test with prefix")
rl.SetTargetFPS(60)
camera = rl.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")
texture = rl.LoadTextureFromImage(image)
mesh = rl.GenMeshHeightmap(image, [16, 8, 16])
model = rl.LoadModelFromMesh(mesh)
print(model.materials) # SHOULD BE A pointer to a 'struct Material' but some is NULL pointer to 'Material' ?
model.materials.maps[rl.MATERIAL_MAP_DIFFUSE].texture = texture
rl.UnloadImage(image)
rl.SetCameraMode(camera[0], rl.CAMERA_ORBITAL)
while not rl.WindowShouldClose():
rl.UpdateCamera(camera)
rl.BeginDrawing()
rl.ClearBackground(rl.RAYWHITE)
rl.BeginMode3D(camera[0])
rl.DrawModel(model, (-8.0, 0.0, -8.0), 1.0, rl.RED)
rl.DrawGrid(20, 1.0)
rl.EndMode3D()
rl.DrawText(b"This mesh should be textured", 190, 200, 20, rl.VIOLET)
rl.EndDrawing()
rl.CloseWindow()