Skip to content

Commit 85c8a18

Browse files
committed
Image class
1 parent 5e444b3 commit 85c8a18

File tree

3 files changed

+43
-11
lines changed

3 files changed

+43
-11
lines changed

crates/processing_pyo3/examples/background_image.py

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

3+
i = None
4+
35
def setup():
6+
global i
47
size(800, 600)
8+
i = image("images/logo.png")
9+
510

611
def draw():
7-
background(220)
8-
image("images/logo.png")
12+
global i
13+
background(220, 100, 24)
14+
# i = image("images/logo.png")
15+
background(i)
916

1017

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

crates/processing_pyo3/src/graphics.rs

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

26+
#[pyclass]
27+
#[derive(Clone, Debug)]
28+
pub struct Image {
29+
entity: Entity,
30+
}
31+
32+
// TODO: we lose the image if this is implemented, meaning that the image is getting dropped prematurely
33+
// impl Drop for Image {
34+
// fn drop(&mut self) {
35+
// let _ = image_destroy(self.entity);
36+
// }
37+
// }
38+
2639
#[pyclass(unsendable)]
2740
pub struct Graphics {
2841
entity: Entity,
@@ -71,6 +84,11 @@ impl Graphics {
7184
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
7285
}
7386

87+
pub fn background_image(&self, image: Image) -> PyResult<()> {
88+
graphics_record_command(self.entity, DrawCommand::BackgroundImage(image.entity))
89+
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
90+
}
91+
7492
pub fn fill(&self, args: Vec<f32>) -> PyResult<()> {
7593
let (r, g, b, a) = parse_color(&args)?;
7694
let color = bevy::color::Color::srgba(r, g, b, a);
@@ -124,10 +142,11 @@ impl Graphics {
124142
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
125143
}
126144

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}")))
145+
pub fn image(&self, file: &str) -> PyResult<Image> {
146+
match image_load(file) {
147+
Ok(image) => Ok(Image { entity: image }),
148+
Err(e) => Err(PyRuntimeError::new_err(format!("{e}"))),
149+
}
131150
}
132151

133152
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::<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)