From a1aa251ce5056ccb1c584bd40cba5209df2afc8d Mon Sep 17 00:00:00 2001 From: shenjack <3695888@qq.com> Date: Sat, 25 Mar 2023 10:38:49 +0800 Subject: [PATCH] pyglet_rs making --- libs/Difficult_Rocket_rs/src/setup.py | 1 - libs/pyglet_rs/src/post_build.py | 41 +++++++++++++++++++++++++++ libs/pyglet_rs/src/setup.py | 39 +++++++++++++++++++++++++ libs/pyglet_rs/src/src/__init__.py | 10 +++++++ libs/pyglet_rs/src/src/lib.rs | 3 +- libs/pyglet_rs/src/src/sprite.rs | 31 +++++++++++++------- 6 files changed, 112 insertions(+), 13 deletions(-) create mode 100644 libs/pyglet_rs/src/post_build.py create mode 100644 libs/pyglet_rs/src/setup.py create mode 100644 libs/pyglet_rs/src/src/__init__.py diff --git a/libs/Difficult_Rocket_rs/src/setup.py b/libs/Difficult_Rocket_rs/src/setup.py index e4ca8b2..b3c4657 100644 --- a/libs/Difficult_Rocket_rs/src/setup.py +++ b/libs/Difficult_Rocket_rs/src/setup.py @@ -22,7 +22,6 @@ setup( author='shenjackyuanjie', author_email='3695888@qq.com', rust_extensions=[RustExtension(target="Difficult_Rocket_rs.Difficult_Rocket_rs", - # rust_version='2021', binding=Binding.PyO3)], zip_safe=False, ) diff --git a/libs/pyglet_rs/src/post_build.py b/libs/pyglet_rs/src/post_build.py new file mode 100644 index 0000000..ead64fd --- /dev/null +++ b/libs/pyglet_rs/src/post_build.py @@ -0,0 +1,41 @@ +# ------------------------------- +# Difficult Rocket +# Copyright © 2020-2023 by shenjackyuanjie 3695888@qq.com +# All rights reserved +# ------------------------------- +import os +import shutil +import warnings +import traceback + +package_path = 'pyglet_rs' +lib_path = '../lib' +build_path = 'build' + +if not os.path.exists(lib_path): + os.mkdir(lib_path) + +builds = os.listdir(build_path) +print(os.path.abspath('.')) + +try: + shutil.copy('src/__init__.py', os.path.join(lib_path, '__init__.py')) +except shutil.SameFileError: + traceback.print_exc() + +for build_dir in builds: + if not os.path.exists(os.path.join(build_path, build_dir, package_path)): + warnings.warn(f'package not found at {build_path}/{build_dir}') + continue + for file in os.listdir(os.path.join(build_path, build_dir, package_path)): + # file_name = os.path.join(lib_path, file.replace(package_path, f'{package_path}.{DR_runtime.DR_Rust_version}')) + file_name = os.path.join(lib_path, file) + shutil.rmtree(file_name, ignore_errors=True) + try: + shutil.copy(os.path.join(build_path, build_dir, package_path, file), file_name) + except (shutil.SameFileError, PermissionError): + # print(os.path.exists(os.path)) + print(os.listdir(lib_path)) + traceback.print_exc() + continue + print(os.path.abspath(file_name)) diff --git a/libs/pyglet_rs/src/setup.py b/libs/pyglet_rs/src/setup.py new file mode 100644 index 0000000..ba38dd8 --- /dev/null +++ b/libs/pyglet_rs/src/setup.py @@ -0,0 +1,39 @@ +# ------------------------------- +# Difficult Rocket +# Copyright © 2020-2023 by shenjackyuanjie 3695888@qq.com +# All rights reserved +# ------------------------------- +import os +import sys +import rtoml +import shutil +from setuptools import setup +from setuptools_rust import Binding, RustExtension + +sys.path.append('../../../') +sys.path.append(os.curdir) + +package_path = 'pyglet_rs' + +# 版本号从 cargo.toml 中读取 +with open(f'Cargo.toml', 'r') as f: + cargo_toml = rtoml.load(f) + version = cargo_toml['package']['version'] + +setup( + name='pyglet_rs', + version=version, + author='shenjackyuanjie', + author_email='3695888@qq.com', + rust_extensions=[RustExtension(target="pyglet_rs.pyglet_rs", + binding=Binding.PyO3)], + zip_safe=False, +) + +lib_path = '../lib' +build_path = 'build' + +if 'clean' in sys.argv: + shutil.rmtree(build_path, ignore_errors=True) + shutil.rmtree(f'{package_path}.egg-info', ignore_errors=True) + sys.exit(0) diff --git a/libs/pyglet_rs/src/src/__init__.py b/libs/pyglet_rs/src/src/__init__.py new file mode 100644 index 0000000..5387515 --- /dev/null +++ b/libs/pyglet_rs/src/src/__init__.py @@ -0,0 +1,10 @@ +# ------------------------------- +# Difficult Rocket +# Copyright © 2020-2023 by shenjackyuanjie 3695888@qq.com +# All rights reserved +# ------------------------------- + +from typing import TYPE_CHECKING + +if not TYPE_CHECKING: + from .pyglet_rs import * diff --git a/libs/pyglet_rs/src/src/lib.rs b/libs/pyglet_rs/src/src/lib.rs index 88c0672..c4e3c15 100644 --- a/libs/pyglet_rs/src/src/lib.rs +++ b/libs/pyglet_rs/src/src/lib.rs @@ -10,8 +10,7 @@ mod sprite; use pyo3::prelude::*; - -#[pymoudule] +#[pymodule] #[pyo3(name = "pyglet_rs")] fn module_init(_py: Python<'_>, m: &PyModule) -> PyResult<()> { m.add_class::()?; diff --git a/libs/pyglet_rs/src/src/sprite.rs b/libs/pyglet_rs/src/src/sprite.rs index 3f78ff3..e510564 100644 --- a/libs/pyglet_rs/src/src/sprite.rs +++ b/libs/pyglet_rs/src/src/sprite.rs @@ -6,12 +6,14 @@ * ------------------------------- */ - use pyo3::prelude::*; /// Instance of an on-screen image /// See the module documentation for usage. #[pyclass(name = "Sprite", subclass)] +#[pyo3(text_signature = "(img, x=0.0, y=0.0, z=0.0, \ + batch=None, group=None, \ + subpixel=False, program=None)")] pub struct Sprite { // render pub batch: Py, @@ -28,33 +30,42 @@ pub struct Sprite { // frame pub frame_index: u32, pub animation: Option>, + pub texture: Option>, pub paused: bool, // other - pub rgba: (i8, i8, i8, i8), + pub rgba: (u8, u8, u8, u8), } #[pymethods] impl Sprite { #[new] - #[pyo3(text_signature = "(img, x=0.0, y=0.0, z=0.0, \ - batch=None, group=None, \ - subpixel=False, program=None)")] - fn new(img: &PyAny, x: f64, y:, batch: &PyAny, group: &PyAny) -> Self { + fn new(py_: Python, img: &PyAny, x: f64, y: f64, z: f64, batch: &PyAny, group: &PyAny) -> Self { + // let img_class = PyModule::from_code(py_, "pyglet.image.Animation", "", "")?.getattr(); + let animation_class = + PyModule::import(py_, "pyglet.image.Animation").unwrap().getattr("Animation").unwrap(); + let mut texture: Option> = None; + let mut animation: Option> = None; + if img.is_instance(animation_class).unwrap() { + animation = Some(img.into()); + } else { + texture = Some(img.into()); + } Sprite { batch: batch.into(), - x, y, z, + x, + y, + z, scale: 1.0, scale_x: 1.0, scale_y: 1.0, visible: true, vertex_list: None, frame_index: 0, - animation: None, + animation, + texture, paused: false, rgba: (255, 255, 255, 255), group_class: group.into(), } } } -} -