making sprite rs

This commit is contained in:
shenjack 2023-03-25 20:03:15 +08:00
parent 29b9c4be59
commit 7800f0009b
3 changed files with 101 additions and 11 deletions

1
DR.py
View File

@ -52,7 +52,6 @@ def main() -> None:
from libs.pyglet_rs import Sprite, get_version_str from libs.pyglet_rs import Sprite, get_version_str
print('pyglet_rs import success') print('pyglet_rs import success')
print('pyglet_rs available:', get_version_str()) print('pyglet_rs available:', get_version_str())
except ImportError as e: except ImportError as e:
print('pyglet_rs import error') print('pyglet_rs import error')

View File

@ -0,0 +1 @@
cargo fmt;python3.8 .\setup.py build; python .\post_build.py

View File

@ -6,6 +6,7 @@
* ------------------------------- * -------------------------------
*/ */
use pyo3::intern;
use pyo3::prelude::*; use pyo3::prelude::*;
/// Instance of an on-screen image /// Instance of an on-screen image
@ -16,6 +17,7 @@ use pyo3::prelude::*;
subpixel=False, program=None)")] subpixel=False, program=None)")]
pub struct Sprite { pub struct Sprite {
// render // render
pub subpixel: bool,
pub batch: Py<PyAny>, pub batch: Py<PyAny>,
pub group_class: Py<PyAny>, pub group_class: Py<PyAny>,
// view // view
@ -29,6 +31,9 @@ pub struct Sprite {
pub vertex_list: Option<Vec<()>>, pub vertex_list: Option<Vec<()>>,
// frame // frame
pub frame_index: u32, pub frame_index: u32,
pub next_dt: f64,
#[pyo3(get)]
pub program: Option<Py<PyAny>>,
pub animation: Option<Py<PyAny>>, pub animation: Option<Py<PyAny>>,
pub texture: Option<Py<PyAny>>, pub texture: Option<Py<PyAny>>,
pub paused: bool, pub paused: bool,
@ -38,20 +43,104 @@ pub struct Sprite {
#[pymethods] #[pymethods]
impl Sprite { impl Sprite {
/// python code:
/// 366:
/// if isinstance(img, image.Animation):
/// self._animation = img
/// self._texture = img.frames[0].image.get_texture()
/// self._next_dt = img.frames[0].duration
/// if self._next_dt:
/// clock.schedule_once(self._animate, self._next_dt)
/// else:
/// self._texture = img.get_texture()
/// 375:
/// if not program:
/// if isinstance(img, image.TextureArrayRegion):
/// self._program = get_default_array_shader()
/// else:
/// self._program = get_default_shader()
/// else:
/// self._program = program
/// 383:
/// self._batch = batch or graphics.get_default_batch()
/// self._user_group = group
/// self._group = self.group_class(self._texture, blend_src, blend_dest, self.program, group)
/// self._subpixel = subpixel
/// 387:
/// self._create_vertex_list()
#[new] #[new]
fn new(py_: Python, img: &PyAny, x: f64, y: f64, z: f64, batch: &PyAny, group: &PyAny) -> Self { fn new(
// let img_class = PyModule::from_code(py_, "pyglet.image.Animation", "", "")?.getattr(); py_: Python,
let animation_class = img: &PyAny,
PyModule::import(py_, "pyglet.image.Animation").unwrap().getattr("Animation").unwrap(); x: f64,
let mut texture: Option<Py<PyAny>> = None; y: f64,
let mut animation: Option<Py<PyAny>> = None; z: f64,
batch: &PyAny,
group: &PyAny,
subpixel: bool,
program_: &PyAny,
) -> Self {
let animation_class = PyModule::import(py_, "pyglet.image.Animation")
.unwrap()
.getattr("Animation")
.unwrap();
let texture;
let mut next_dt = 0.0;
let mut animation = None;
let mut program = program_;
// 366
if img.is_instance(animation_class).unwrap() { if img.is_instance(animation_class).unwrap() {
animation = Some(img.into()); animation = Some(img.into());
texture = img
.getattr(intern!(img.py(), "frames"))
.unwrap()
.get_item(0)
.unwrap()
.getattr(intern!(img.py(), "image"))
.unwrap()
.call_method0(intern!(img.py(), "get_texture"))
.unwrap();
let _next_dt = img
.getattr(intern!(img.py(), "frames"))
.unwrap()
.get_item(0)
.unwrap()
.getattr(intern!(img.py(), "duration"));
next_dt = match _next_dt {
Ok(v) => v.extract().unwrap(),
Err(_) => 0.0,
}
// 372
} else { } else {
texture = Some(img.into()); texture = img.call_method0(intern!(img.py(), "get_texture")).unwrap();
}
// 375
if !program.is_true().unwrap() {
let texture_array_region_class =
PyModule::import(py_, "pyglet.image.TextureArrayRegion")
.unwrap()
.getattr("TextureArrayRegion")
.unwrap();
if img.is_instance(texture_array_region_class).unwrap() {
// self._program = get_default_array_shader()
let get_default_array_shader = PyModule::import(py_, "pyglet.sprite")
.unwrap()
.getattr("get_default_array_shader")
.unwrap();
program = get_default_array_shader.call0().unwrap();
} else {
// self._program = get_default_shader()
let get_default_shader = PyModule::import(py_, "pyglet.sprite")
.unwrap()
.getattr("get_default_shader")
.unwrap();
program = get_default_shader.call0().unwrap();
}
} }
Sprite { Sprite {
subpixel,
batch: batch.into(), batch: batch.into(),
group_class: group.into(),
x, x,
y, y,
z, z,
@ -61,11 +150,12 @@ impl Sprite {
visible: true, visible: true,
vertex_list: None, vertex_list: None,
frame_index: 0, frame_index: 0,
animation, next_dt,
texture, program: Some(program.into()),
animation: animation,
texture: Some(texture.into()),
paused: false, paused: false,
rgba: (255, 255, 255, 255), rgba: (255, 255, 255, 255),
group_class: group.into(),
} }
} }
} }