Skip to content

Commit 8cea5b3

Browse files
authored
Fix run loop; correctly integrate changes. (#36)
1 parent 9f34ea7 commit 8cea5b3

File tree

3 files changed

+73
-32
lines changed

3 files changed

+73
-32
lines changed

crates/.DS_Store

6 KB
Binary file not shown.

crates/processing_pyo3/src/graphics.rs

Lines changed: 52 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,38 @@
11
use bevy::prelude::Entity;
22
use processing::prelude::*;
3-
use pyo3::{exceptions::PyRuntimeError, prelude::*, types::PyAny};
3+
use pyo3::{exceptions::PyRuntimeError, prelude::*};
44

55
use crate::glfw::GlfwContext;
66

77
#[pyclass(unsendable)]
8-
pub struct Graphics {
8+
pub struct Surface {
9+
entity: Entity,
910
glfw_ctx: GlfwContext,
10-
surface: Entity,
11+
}
12+
13+
#[pymethods]
14+
impl Surface {
15+
pub fn poll_events(&mut self) -> bool {
16+
self.glfw_ctx.poll_events()
17+
}
18+
}
19+
20+
impl Drop for Surface {
21+
fn drop(&mut self) {
22+
let _ = surface_destroy(self.entity);
23+
}
24+
}
25+
26+
#[pyclass(unsendable)]
27+
pub struct Graphics {
28+
entity: Entity,
29+
pub surface: Surface,
30+
}
31+
32+
impl Drop for Graphics {
33+
fn drop(&mut self) {
34+
let _ = graphics_destroy(self.entity);
35+
}
1136
}
1237

1338
#[pymethods]
@@ -24,42 +49,53 @@ impl Graphics {
2449
let surface = surface_create(window_handle, display_handle, width, height, 1.0)
2550
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))?;
2651

27-
Ok(Self { glfw_ctx, surface })
52+
let surface = Surface {
53+
entity: surface,
54+
glfw_ctx,
55+
};
56+
57+
let graphics = graphics_create(surface.entity, width, height)
58+
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))?;
59+
60+
Ok(Self {
61+
entity: graphics,
62+
surface,
63+
})
2864
}
2965

3066
pub fn background(&self, args: Vec<f32>) -> PyResult<()> {
3167
let (r, g, b, a) = parse_color(&args)?;
3268
let color = bevy::color::Color::srgba(r, g, b, a);
33-
graphics_record_command(self.surface, DrawCommand::BackgroundColor(color))
69+
graphics_record_command(self.entity, DrawCommand::BackgroundColor(color))
3470
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
3571
}
3672

3773
pub fn fill(&self, args: Vec<f32>) -> PyResult<()> {
3874
let (r, g, b, a) = parse_color(&args)?;
3975
let color = bevy::color::Color::srgba(r, g, b, a);
40-
graphics_record_command(self.surface, DrawCommand::Fill(color))
76+
graphics_record_command(self.entity, DrawCommand::Fill(color))
4177
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
4278
}
4379

4480
pub fn no_fill(&self) -> PyResult<()> {
45-
graphics_record_command(self.surface, DrawCommand::NoFill)
81+
graphics_record_command(self.entity, DrawCommand::NoFill)
4682
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
4783
}
4884

4985
pub fn stroke(&self, args: Vec<f32>) -> PyResult<()> {
5086
let (r, g, b, a) = parse_color(&args)?;
5187
let color = bevy::color::Color::srgba(r, g, b, a);
52-
graphics_record_command(self.surface, DrawCommand::StrokeColor(color))
88+
graphics_record_command(self.entity, DrawCommand::StrokeColor(color))
5389
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
5490
}
5591

5692
pub fn no_stroke(&self) -> PyResult<()> {
57-
graphics_record_command(self.surface, DrawCommand::NoStroke)
93+
graphics_record_command(self.entity, DrawCommand::NoStroke)
5894
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
5995
}
6096

6197
pub fn stroke_weight(&self, weight: f32) -> PyResult<()> {
62-
graphics_record_command(self.surface, DrawCommand::StrokeWeight(weight))
98+
graphics_record_command(self.entity, DrawCommand::StrokeWeight(weight))
6399
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
64100
}
65101

@@ -75,7 +111,7 @@ impl Graphics {
75111
bl: f32,
76112
) -> PyResult<()> {
77113
graphics_record_command(
78-
self.surface,
114+
self.entity,
79115
DrawCommand::Rect {
80116
x,
81117
y,
@@ -87,25 +123,12 @@ impl Graphics {
87123
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
88124
}
89125

90-
pub fn run(&mut self, draw_fn: Option<Py<PyAny>>) -> PyResult<()> {
91-
loop {
92-
if !self.glfw_ctx.poll_events() {
93-
break;
94-
}
95-
96-
graphics_begin_draw(self.surface)
97-
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))?;
98-
99-
if let Some(ref draw) = draw_fn {
100-
Python::attach(|py| {
101-
draw.call0(py)
102-
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
103-
})?;
104-
}
126+
pub fn begin_draw(&self) -> PyResult<()> {
127+
graphics_begin_draw(self.entity).map_err(|e| PyRuntimeError::new_err(format!("{e}")))
128+
}
105129

106-
graphics_end_draw(self.surface).map_err(|e| PyRuntimeError::new_err(format!("{e}")))?;
107-
}
108-
Ok(())
130+
pub fn end_draw(&self) -> PyResult<()> {
131+
graphics_end_draw(self.entity).map_err(|e| PyRuntimeError::new_err(format!("{e}")))
109132
}
110133
}
111134

crates/processing_pyo3/src/lib.rs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
//! receiver.
88
//!
99
//! To allow Python users to create a similar experience, we provide module-level
10-
//! functions that forward to a singleton Graphics object behind the scenes.
10+
//! functions that forward to a singleton Graphics object bepub(crate) pub(crate) hind the scenes.
1111
mod glfw;
1212
mod graphics;
1313

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

1717
#[pymodule]
1818
fn processing(m: &Bound<'_, PyModule>) -> PyResult<()> {
@@ -40,7 +40,25 @@ fn size(module: &Bound<'_, PyModule>, width: u32, height: u32) -> PyResult<()> {
4040
#[pyfunction]
4141
#[pyo3(pass_module, signature = (draw_fn=None))]
4242
fn run(module: &Bound<'_, PyModule>, draw_fn: Option<Py<PyAny>>) -> PyResult<()> {
43-
get_graphics_mut(module)?.run(draw_fn)
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+
}
58+
59+
get_graphics(module)?.end_draw()?;
60+
}
61+
Ok(())
4462
}
4563

4664
#[pyfunction]

0 commit comments

Comments
 (0)