11use bevy::prelude::Entity;
22use processing::prelude::*;
3- use pyo3::{exceptions::PyRuntimeError, prelude::*, types::PyAny };
3+ use pyo3::{exceptions::PyRuntimeError, prelude::*};
44
55use 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
0 commit comments