Skip to content

Commit 437a521

Browse files
authored
Merge pull request #2 from tychedelia/pyo3/image_class
Take image by ref.
2 parents 85c8a18 + 9a66a40 commit 437a521

File tree

3 files changed

+8
-13
lines changed

3 files changed

+8
-13
lines changed

crates/processing_pyo3/examples/background_image.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
from processing import *
22

3-
i = None
4-
53
def setup():
64
global i
75
size(800, 600)
86
i = image("images/logo.png")
97

108

119
def draw():
12-
global i
1310
background(220, 100, 24)
14-
# i = image("images/logo.png")
1511
background(i)
1612

1713

crates/processing_pyo3/src/graphics.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,16 @@ impl Drop for Surface {
2424
}
2525

2626
#[pyclass]
27-
#[derive(Clone, Debug)]
27+
#[derive(Debug)]
2828
pub struct Image {
2929
entity: Entity,
3030
}
3131

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-
// }
32+
impl Drop for Image {
33+
fn drop(&mut self) {
34+
let _ = image_destroy(self.entity);
35+
}
36+
}
3837

3938
#[pyclass(unsendable)]
4039
pub struct Graphics {
@@ -84,7 +83,7 @@ impl Graphics {
8483
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
8584
}
8685

87-
pub fn background_image(&self, image: Image) -> PyResult<()> {
86+
pub fn background_image(&self, image: &Image) -> PyResult<()> {
8887
graphics_record_command(self.entity, DrawCommand::BackgroundImage(image.entity))
8988
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
9089
}

crates/processing_pyo3/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ fn run(module: &Bound<'_, PyModule>) -> PyResult<()> {
102102
fn background(module: &Bound<'_, PyModule>, args: &Bound<'_, PyTuple>) -> PyResult<()> {
103103
let first = args.get_item(0)?;
104104
if first.is_instance_of::<Image>() {
105-
get_graphics(module)?.background_image(first.extract::<Image>()?)
105+
get_graphics(module)?.background_image(&*first.extract::<PyRef<Image>>()?)
106106
} else {
107107
get_graphics(module)?.background(args.extract()?)
108108
}

0 commit comments

Comments
 (0)