Skip to content

Commit f9a981f

Browse files
committed
Calling setup() function
1 parent 64ff895 commit f9a981f

File tree

2 files changed

+29
-21
lines changed

2 files changed

+29
-21
lines changed

crates/processing_pyo3/src/lib.rs

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ mod glfw;
1212
mod graphics;
1313

1414
use graphics::{Graphics, get_graphics, get_graphics_mut};
15-
use pyo3::{exceptions::PyRuntimeError, prelude::*, types::PyAny};
15+
use pyo3::{exceptions::PyRuntimeError, prelude::*, types::PyFunction};
1616

1717
#[pymodule]
1818
fn processing(m: &Bound<'_, PyModule>) -> PyResult<()> {
@@ -38,27 +38,16 @@ fn size(module: &Bound<'_, PyModule>, width: u32, height: u32) -> PyResult<()> {
3838
}
3939

4040
#[pyfunction]
41-
#[pyo3(pass_module, signature = (draw_fn=None))]
42-
fn run(module: &Bound<'_, PyModule>, draw_fn: Option<Py<PyAny>>) -> PyResult<()> {
43-
loop {
44-
{
45-
let mut graphics = get_graphics_mut(module)?;
46-
if !graphics.surface.poll_events() {
47-
break;
48-
}
49-
graphics.begin_draw()?;
50-
}
51-
52-
if let Some(ref draw) = draw_fn {
53-
Python::attach(|py| {
54-
draw.call0(py)
55-
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
56-
})?;
57-
}
41+
#[pyo3(pass_module)]
42+
fn run(module: &Bound<'_, PyModule>) -> PyResult<()> {
43+
Python::attach(|py| {
44+
let builtins = PyModule::import(py, "builtins")?;
45+
let locals = builtins.getattr("locals")?.call0()?;
46+
let setup_fn = locals.get_item("setup")?;
47+
setup_fn.call0()?;
5848

59-
get_graphics(module)?.end_draw()?;
60-
}
61-
Ok(())
49+
Ok(())
50+
})
6251
}
6352

6453
#[pyfunction]

rectangle.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from processing import *
2+
3+
def moon_func():
4+
print("Hello, Moon!")
5+
6+
def setup():
7+
print("HELLO")
8+
size(800, 600)
9+
10+
def draw():
11+
background(220)
12+
13+
fill(255, 0, 100)
14+
stroke(0)
15+
stroke_weight(2)
16+
rect(100, 100, 200, 150)
17+
18+
# TODO: this should happen implicitly on module load somehow
19+
run()

0 commit comments

Comments
 (0)