Skip to content

Commit f967a3e

Browse files
catilactychedelia
andauthored
[processing_pyo3] Image class (#54)
Co-authored-by: charlotte 🌸 <charlotte.c.mcelwain@gmail.com>
1 parent 5e444b3 commit f967a3e

File tree

3 files changed

+38
-11
lines changed

3 files changed

+38
-11
lines changed

crates/processing_pyo3/examples/background_image.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
from processing import *
22

33
def setup():
4+
global i
45
size(800, 600)
6+
i = image("images/logo.png")
7+
58

69
def draw():
7-
background(220)
8-
image("images/logo.png")
10+
background(220, 100, 24)
11+
background(i)
912

1013

1114
# TODO: this should happen implicitly on module load somehow

crates/processing_pyo3/src/graphics.rs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,18 @@ impl Drop for Surface {
2323
}
2424
}
2525

26+
#[pyclass]
27+
#[derive(Debug)]
28+
pub struct Image {
29+
entity: Entity,
30+
}
31+
32+
impl Drop for Image {
33+
fn drop(&mut self) {
34+
let _ = image_destroy(self.entity);
35+
}
36+
}
37+
2638
#[pyclass(unsendable)]
2739
pub struct Graphics {
2840
entity: Entity,
@@ -71,6 +83,11 @@ impl Graphics {
7183
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
7284
}
7385

86+
pub fn background_image(&self, image: &Image) -> PyResult<()> {
87+
graphics_record_command(self.entity, DrawCommand::BackgroundImage(image.entity))
88+
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
89+
}
90+
7491
pub fn fill(&self, args: Vec<f32>) -> PyResult<()> {
7592
let (r, g, b, a) = parse_color(&args)?;
7693
let color = bevy::color::Color::srgba(r, g, b, a);
@@ -124,10 +141,11 @@ impl Graphics {
124141
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
125142
}
126143

127-
pub fn image(&self, file: &str) -> PyResult<()> {
128-
let image = image_load(file).unwrap();
129-
graphics_record_command(self.entity, DrawCommand::BackgroundImage(image))
130-
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
144+
pub fn image(&self, file: &str) -> PyResult<Image> {
145+
match image_load(file) {
146+
Ok(image) => Ok(Image { entity: image }),
147+
Err(e) => Err(PyRuntimeError::new_err(format!("{e}"))),
148+
}
131149
}
132150

133151
pub fn push_matrix(&self) -> PyResult<()> {

crates/processing_pyo3/src/lib.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@
1111
mod glfw;
1212
mod graphics;
1313

14-
use graphics::{Graphics, get_graphics, get_graphics_mut};
15-
use pyo3::{exceptions::PyRuntimeError, prelude::*};
14+
use graphics::{Graphics, Image, get_graphics, get_graphics_mut};
15+
use pyo3::{exceptions::PyRuntimeError, prelude::*, types::PyTuple};
1616

1717
use std::env;
1818

1919
#[pymodule]
2020
fn processing(m: &Bound<'_, PyModule>) -> PyResult<()> {
2121
m.add_class::<Graphics>()?;
22+
m.add_class::<Image>()?;
2223
m.add_function(wrap_pyfunction!(size, m)?)?;
2324
m.add_function(wrap_pyfunction!(run, m)?)?;
2425
m.add_function(wrap_pyfunction!(background, m)?)?;
@@ -98,8 +99,13 @@ fn run(module: &Bound<'_, PyModule>) -> PyResult<()> {
9899

99100
#[pyfunction]
100101
#[pyo3(pass_module, signature = (*args))]
101-
fn background(module: &Bound<'_, PyModule>, args: Vec<f32>) -> PyResult<()> {
102-
get_graphics(module)?.background(args)
102+
fn background(module: &Bound<'_, PyModule>, args: &Bound<'_, PyTuple>) -> PyResult<()> {
103+
let first = args.get_item(0)?;
104+
if first.is_instance_of::<Image>() {
105+
get_graphics(module)?.background_image(&*first.extract::<PyRef<Image>>()?)
106+
} else {
107+
get_graphics(module)?.background(args.extract()?)
108+
}
103109
}
104110

105111
#[pyfunction]
@@ -150,6 +156,6 @@ fn rect(
150156

151157
#[pyfunction]
152158
#[pyo3(pass_module, signature = (image_file))]
153-
fn image(module: &Bound<'_, PyModule>, image_file: &str) -> PyResult<()> {
159+
fn image(module: &Bound<'_, PyModule>, image_file: &str) -> PyResult<Image> {
154160
get_graphics(module)?.image(image_file)
155161
}

0 commit comments

Comments
 (0)