diff --git a/crates/processing_pyo3/examples/background_image.py b/crates/processing_pyo3/examples/background_image.py index 632dcd9..3cdaa55 100644 --- a/crates/processing_pyo3/examples/background_image.py +++ b/crates/processing_pyo3/examples/background_image.py @@ -1,11 +1,14 @@ from processing import * def setup(): + global i size(800, 600) + i = image("images/logo.png") + def draw(): - background(220) - image("images/logo.png") + background(220, 100, 24) + background(i) # TODO: this should happen implicitly on module load somehow diff --git a/crates/processing_pyo3/src/graphics.rs b/crates/processing_pyo3/src/graphics.rs index 5c4c173..00245f8 100644 --- a/crates/processing_pyo3/src/graphics.rs +++ b/crates/processing_pyo3/src/graphics.rs @@ -23,6 +23,18 @@ impl Drop for Surface { } } +#[pyclass] +#[derive(Debug)] +pub struct Image { + entity: Entity, +} + +impl Drop for Image { + fn drop(&mut self) { + let _ = image_destroy(self.entity); + } +} + #[pyclass(unsendable)] pub struct Graphics { entity: Entity, @@ -71,6 +83,11 @@ impl Graphics { .map_err(|e| PyRuntimeError::new_err(format!("{e}"))) } + pub fn background_image(&self, image: &Image) -> PyResult<()> { + graphics_record_command(self.entity, DrawCommand::BackgroundImage(image.entity)) + .map_err(|e| PyRuntimeError::new_err(format!("{e}"))) + } + pub fn fill(&self, args: Vec) -> PyResult<()> { let (r, g, b, a) = parse_color(&args)?; let color = bevy::color::Color::srgba(r, g, b, a); @@ -124,10 +141,11 @@ impl Graphics { .map_err(|e| PyRuntimeError::new_err(format!("{e}"))) } - pub fn image(&self, file: &str) -> PyResult<()> { - let image = image_load(file).unwrap(); - graphics_record_command(self.entity, DrawCommand::BackgroundImage(image)) - .map_err(|e| PyRuntimeError::new_err(format!("{e}"))) + pub fn image(&self, file: &str) -> PyResult { + match image_load(file) { + Ok(image) => Ok(Image { entity: image }), + Err(e) => Err(PyRuntimeError::new_err(format!("{e}"))), + } } pub fn push_matrix(&self) -> PyResult<()> { diff --git a/crates/processing_pyo3/src/lib.rs b/crates/processing_pyo3/src/lib.rs index 97c7b8d..3e5d07a 100644 --- a/crates/processing_pyo3/src/lib.rs +++ b/crates/processing_pyo3/src/lib.rs @@ -11,14 +11,15 @@ mod glfw; mod graphics; -use graphics::{Graphics, get_graphics, get_graphics_mut}; -use pyo3::{exceptions::PyRuntimeError, prelude::*}; +use graphics::{Graphics, Image, get_graphics, get_graphics_mut}; +use pyo3::{exceptions::PyRuntimeError, prelude::*, types::PyTuple}; use std::env; #[pymodule] fn processing(m: &Bound<'_, PyModule>) -> PyResult<()> { m.add_class::()?; + m.add_class::()?; m.add_function(wrap_pyfunction!(size, m)?)?; m.add_function(wrap_pyfunction!(run, m)?)?; m.add_function(wrap_pyfunction!(background, m)?)?; @@ -98,8 +99,13 @@ fn run(module: &Bound<'_, PyModule>) -> PyResult<()> { #[pyfunction] #[pyo3(pass_module, signature = (*args))] -fn background(module: &Bound<'_, PyModule>, args: Vec) -> PyResult<()> { - get_graphics(module)?.background(args) +fn background(module: &Bound<'_, PyModule>, args: &Bound<'_, PyTuple>) -> PyResult<()> { + let first = args.get_item(0)?; + if first.is_instance_of::() { + get_graphics(module)?.background_image(&*first.extract::>()?) + } else { + get_graphics(module)?.background(args.extract()?) + } } #[pyfunction] @@ -150,6 +156,6 @@ fn rect( #[pyfunction] #[pyo3(pass_module, signature = (image_file))] -fn image(module: &Bound<'_, PyModule>, image_file: &str) -> PyResult<()> { +fn image(module: &Bound<'_, PyModule>, image_file: &str) -> PyResult { get_graphics(module)?.image(image_file) }