pyglet_rs making

This commit is contained in:
shenjack 2023-03-25 10:38:49 +08:00
parent 71f0def9e9
commit a1aa251ce5
6 changed files with 112 additions and 13 deletions

View File

@ -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,
)

View File

@ -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))

View File

@ -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)

View File

@ -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 *

View File

@ -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::<sprite::Sprite>()?;

View File

@ -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<PyAny>,
@ -28,33 +30,42 @@ pub struct Sprite {
// frame
pub frame_index: u32,
pub animation: Option<Py<PyAny>>,
pub texture: Option<Py<PyAny>>,
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<Py<PyAny>> = None;
let mut animation: Option<Py<PyAny>> = 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(),
}
}
}
}