Skip to content

Commit 9f34ea7

Browse files
authored
Refactor rendering crate to clarify primary Processing API (#34)
1 parent 553ec04 commit 9f34ea7

File tree

17 files changed

+1783
-698
lines changed

17 files changed

+1783
-698
lines changed

Cargo.lock

Lines changed: 106 additions & 81 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ path = "examples/rectangle.rs"
4242
name = "background_image"
4343
path = "examples/background_image.rs"
4444

45+
[[example]]
46+
name = "update_pixels"
47+
path = "examples/update_pixels.rs"
48+
4549
[profile.wasm-release]
4650
inherits = "release"
4751
opt-level = "z"

crates/processing_ffi/src/lib.rs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@ pub extern "C" fn processing_surface_resize(window_id: u64, width: u32, height:
7676
pub extern "C" fn processing_background_color(window_id: u64, color: Color) {
7777
error::clear_error();
7878
let window_entity = Entity::from_bits(window_id);
79-
error::check(|| record_command(window_entity, DrawCommand::BackgroundColor(color.into())));
79+
error::check(|| {
80+
graphics_record_command(window_entity, DrawCommand::BackgroundColor(color.into()))
81+
});
8082
}
8183

8284
/// Set the background image for the given window.
@@ -90,7 +92,9 @@ pub extern "C" fn processing_background_image(window_id: u64, image_id: u64) {
9092
error::clear_error();
9193
let window_entity = Entity::from_bits(window_id);
9294
let image_entity = Entity::from_bits(image_id);
93-
error::check(|| record_command(window_entity, DrawCommand::BackgroundImage(image_entity)));
95+
error::check(|| {
96+
graphics_record_command(window_entity, DrawCommand::BackgroundImage(image_entity))
97+
});
9498
}
9599

96100
/// Begins the draw for the given window.
@@ -102,7 +106,7 @@ pub extern "C" fn processing_background_image(window_id: u64, image_id: u64) {
102106
pub extern "C" fn processing_begin_draw(window_id: u64) {
103107
error::clear_error();
104108
let window_entity = Entity::from_bits(window_id);
105-
error::check(|| begin_draw(window_entity));
109+
error::check(|| graphics_begin_draw(window_entity));
106110
}
107111

108112
/// Flushes recorded draw commands for the given window.
@@ -114,7 +118,7 @@ pub extern "C" fn processing_begin_draw(window_id: u64) {
114118
pub extern "C" fn processing_flush(window_id: u64) {
115119
error::clear_error();
116120
let window_entity = Entity::from_bits(window_id);
117-
error::check(|| flush(window_entity));
121+
error::check(|| graphics_flush(window_entity));
118122
}
119123

120124
/// Ends the draw for the given window and presents the frame.
@@ -126,7 +130,7 @@ pub extern "C" fn processing_flush(window_id: u64) {
126130
pub extern "C" fn processing_end_draw(window_id: u64) {
127131
error::clear_error();
128132
let window_entity = Entity::from_bits(window_id);
129-
error::check(|| end_draw(window_entity));
133+
error::check(|| graphics_end_draw(window_entity));
130134
}
131135

132136
/// Shuts down internal resources with given exit code, but does *not* terminate the process.
@@ -151,7 +155,7 @@ pub extern "C" fn processing_set_fill(window_id: u64, r: f32, g: f32, b: f32, a:
151155
error::clear_error();
152156
let window_entity = Entity::from_bits(window_id);
153157
let color = bevy::color::Color::srgba(r, g, b, a);
154-
error::check(|| record_command(window_entity, DrawCommand::Fill(color)));
158+
error::check(|| graphics_record_command(window_entity, DrawCommand::Fill(color)));
155159
}
156160

157161
/// Set the stroke color.
@@ -165,7 +169,7 @@ pub extern "C" fn processing_set_stroke_color(window_id: u64, r: f32, g: f32, b:
165169
error::clear_error();
166170
let window_entity = Entity::from_bits(window_id);
167171
let color = bevy::color::Color::srgba(r, g, b, a);
168-
error::check(|| record_command(window_entity, DrawCommand::StrokeColor(color)));
172+
error::check(|| graphics_record_command(window_entity, DrawCommand::StrokeColor(color)));
169173
}
170174

171175
/// Set the stroke weight.
@@ -178,7 +182,7 @@ pub extern "C" fn processing_set_stroke_color(window_id: u64, r: f32, g: f32, b:
178182
pub extern "C" fn processing_set_stroke_weight(window_id: u64, weight: f32) {
179183
error::clear_error();
180184
let window_entity = Entity::from_bits(window_id);
181-
error::check(|| record_command(window_entity, DrawCommand::StrokeWeight(weight)));
185+
error::check(|| graphics_record_command(window_entity, DrawCommand::StrokeWeight(weight)));
182186
}
183187

184188
/// Disable fill for subsequent shapes.
@@ -191,7 +195,7 @@ pub extern "C" fn processing_set_stroke_weight(window_id: u64, weight: f32) {
191195
pub extern "C" fn processing_no_fill(window_id: u64) {
192196
error::clear_error();
193197
let window_entity = Entity::from_bits(window_id);
194-
error::check(|| record_command(window_entity, DrawCommand::NoFill));
198+
error::check(|| graphics_record_command(window_entity, DrawCommand::NoFill));
195199
}
196200

197201
/// Disable stroke for subsequent shapes.
@@ -204,7 +208,7 @@ pub extern "C" fn processing_no_fill(window_id: u64) {
204208
pub extern "C" fn processing_no_stroke(window_id: u64) {
205209
error::clear_error();
206210
let window_entity = Entity::from_bits(window_id);
207-
error::check(|| record_command(window_entity, DrawCommand::NoStroke));
211+
error::check(|| graphics_record_command(window_entity, DrawCommand::NoStroke));
208212
}
209213

210214
/// Draw a rectangle.
@@ -228,7 +232,7 @@ pub extern "C" fn processing_rect(
228232
error::clear_error();
229233
let window_entity = Entity::from_bits(window_id);
230234
error::check(|| {
231-
record_command(
235+
graphics_record_command(
232236
window_entity,
233237
DrawCommand::Rect {
234238
x,
@@ -318,15 +322,15 @@ pub extern "C" fn processing_image_resize(image_id: u64, new_width: u32, new_hei
318322
/// - buffer_len must equal width * height of the image.
319323
/// - This is called from the same thread as init.
320324
#[unsafe(no_mangle)]
321-
pub unsafe extern "C" fn processing_image_load_pixels(
325+
pub unsafe extern "C" fn processing_image_readback(
322326
image_id: u64,
323327
buffer: *mut Color,
324328
buffer_len: usize,
325329
) {
326330
error::clear_error();
327331
let image_entity = Entity::from_bits(image_id);
328332
error::check(|| {
329-
let colors = image_load_pixels(image_entity)?;
333+
let colors = image_readback(image_entity)?;
330334

331335
// Validate buffer size
332336
if colors.len() != buffer_len {

crates/processing_pyo3/src/glfw.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,8 @@ impl GlfwContext {
5858
self.glfw.poll_events();
5959

6060
for (_, event) in glfw::flush_messages(&self.events) {
61-
match event {
62-
WindowEvent::Close => return false,
63-
_ => {}
61+
if event == WindowEvent::Close {
62+
return false;
6463
}
6564
}
6665

crates/processing_pyo3/src/graphics.rs

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
use bevy::prelude::Entity;
22
use processing::prelude::*;
3-
use pyo3::exceptions::PyRuntimeError;
4-
use pyo3::prelude::*;
5-
use pyo3::types::PyAny;
3+
use pyo3::{exceptions::PyRuntimeError, prelude::*, types::PyAny};
64

75
use crate::glfw::GlfwContext;
86

@@ -16,8 +14,8 @@ pub struct Graphics {
1614
impl Graphics {
1715
#[new]
1816
pub fn new(width: u32, height: u32) -> PyResult<Self> {
19-
let glfw_ctx = GlfwContext::new(width, height)
20-
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))?;
17+
let glfw_ctx =
18+
GlfwContext::new(width, height).map_err(|e| PyRuntimeError::new_err(format!("{e}")))?;
2119

2220
init().map_err(|e| PyRuntimeError::new_err(format!("{e}")))?;
2321

@@ -32,43 +30,59 @@ impl Graphics {
3230
pub fn background(&self, args: Vec<f32>) -> PyResult<()> {
3331
let (r, g, b, a) = parse_color(&args)?;
3432
let color = bevy::color::Color::srgba(r, g, b, a);
35-
record_command(self.surface, DrawCommand::BackgroundColor(color))
33+
graphics_record_command(self.surface, DrawCommand::BackgroundColor(color))
3634
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
3735
}
3836

3937
pub fn fill(&self, args: Vec<f32>) -> PyResult<()> {
4038
let (r, g, b, a) = parse_color(&args)?;
4139
let color = bevy::color::Color::srgba(r, g, b, a);
42-
record_command(self.surface, DrawCommand::Fill(color))
40+
graphics_record_command(self.surface, DrawCommand::Fill(color))
4341
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
4442
}
4543

4644
pub fn no_fill(&self) -> PyResult<()> {
47-
record_command(self.surface, DrawCommand::NoFill)
45+
graphics_record_command(self.surface, DrawCommand::NoFill)
4846
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
4947
}
5048

5149
pub fn stroke(&self, args: Vec<f32>) -> PyResult<()> {
5250
let (r, g, b, a) = parse_color(&args)?;
5351
let color = bevy::color::Color::srgba(r, g, b, a);
54-
record_command(self.surface, DrawCommand::StrokeColor(color))
52+
graphics_record_command(self.surface, DrawCommand::StrokeColor(color))
5553
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
5654
}
5755

5856
pub fn no_stroke(&self) -> PyResult<()> {
59-
record_command(self.surface, DrawCommand::NoStroke)
57+
graphics_record_command(self.surface, DrawCommand::NoStroke)
6058
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
6159
}
6260

6361
pub fn stroke_weight(&self, weight: f32) -> PyResult<()> {
64-
record_command(self.surface, DrawCommand::StrokeWeight(weight))
62+
graphics_record_command(self.surface, DrawCommand::StrokeWeight(weight))
6563
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
6664
}
6765

68-
pub fn rect(&self, x: f32, y: f32, w: f32, h: f32, tl: f32, tr: f32, br: f32, bl: f32) -> PyResult<()> {
69-
record_command(
66+
pub fn rect(
67+
&self,
68+
x: f32,
69+
y: f32,
70+
w: f32,
71+
h: f32,
72+
tl: f32,
73+
tr: f32,
74+
br: f32,
75+
bl: f32,
76+
) -> PyResult<()> {
77+
graphics_record_command(
7078
self.surface,
71-
DrawCommand::Rect { x, y, w, h, radii: [tl, tr, br, bl] },
79+
DrawCommand::Rect {
80+
x,
81+
y,
82+
w,
83+
h,
84+
radii: [tl, tr, br, bl],
85+
},
7286
)
7387
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
7488
}
@@ -79,17 +93,17 @@ impl Graphics {
7993
break;
8094
}
8195

82-
begin_draw(self.surface)
96+
graphics_begin_draw(self.surface)
8397
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))?;
8498

8599
if let Some(ref draw) = draw_fn {
86100
Python::attach(|py| {
87-
draw.call0(py).map_err(|e| PyRuntimeError::new_err(format!("{e}")))
101+
draw.call0(py)
102+
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
88103
})?;
89104
}
90105

91-
end_draw(self.surface)
92-
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))?;
106+
graphics_end_draw(self.surface).map_err(|e| PyRuntimeError::new_err(format!("{e}")))?;
93107
}
94108
Ok(())
95109
}
@@ -108,7 +122,12 @@ fn parse_color(args: &[f32]) -> PyResult<(f32, f32, f32, f32)> {
108122
Ok((v, v, v, args[1] / 255.0))
109123
}
110124
3 => Ok((args[0] / 255.0, args[1] / 255.0, args[2] / 255.0, 1.0)),
111-
4 => Ok((args[0] / 255.0, args[1] / 255.0, args[2] / 255.0, args[3] / 255.0)),
125+
4 => Ok((
126+
args[0] / 255.0,
127+
args[1] / 255.0,
128+
args[2] / 255.0,
129+
args[3] / 255.0,
130+
)),
112131
_ => Err(PyRuntimeError::new_err("color requires 1-4 arguments")),
113132
}
114133
}

crates/processing_pyo3/src/lib.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@
1111
mod glfw;
1212
mod graphics;
1313

14-
use graphics::{get_graphics, get_graphics_mut, Graphics};
15-
use pyo3::prelude::*;
16-
use pyo3::types::PyAny;
14+
use graphics::{Graphics, get_graphics, get_graphics_mut};
15+
use pyo3::{prelude::*, types::PyAny};
1716

1817
#[pymodule]
1918
fn processing(m: &Bound<'_, PyModule>) -> PyResult<()> {
@@ -82,6 +81,16 @@ fn stroke_weight(module: &Bound<'_, PyModule>, weight: f32) -> PyResult<()> {
8281

8382
#[pyfunction]
8483
#[pyo3(pass_module, signature = (x, y, w, h, tl=0.0, tr=0.0, br=0.0, bl=0.0))]
85-
fn rect(module: &Bound<'_, PyModule>, x: f32, y: f32, w: f32, h: f32, tl: f32, tr: f32, br: f32, bl: f32) -> PyResult<()> {
84+
fn rect(
85+
module: &Bound<'_, PyModule>,
86+
x: f32,
87+
y: f32,
88+
w: f32,
89+
h: f32,
90+
tl: f32,
91+
tr: f32,
92+
br: f32,
93+
bl: f32,
94+
) -> PyResult<()> {
8695
get_graphics(module)?.rect(x, y, w, h, tl, tr, br, bl)
8796
}

crates/processing_render/src/error.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ pub enum ProcessingError {
88
AppAccess,
99
#[error("Error initializing tracing: {0}")]
1010
Tracing(#[from] tracing::subscriber::SetGlobalDefaultError),
11-
#[error("Window not found")]
12-
WindowNotFound,
11+
#[error("Surface not found")]
12+
SurfaceNotFound,
1313
#[error("Handle error: {0}")]
1414
HandleError(#[from] raw_window_handle::HandleError),
1515
#[error("Invalid window handle provided")]
@@ -20,4 +20,8 @@ pub enum ProcessingError {
2020
UnsupportedTextureFormat,
2121
#[error("Invalid argument: {0}")]
2222
InvalidArgument(String),
23+
#[error("Graphics not found")]
24+
GraphicsNotFound,
25+
#[error("Invalid entity")]
26+
InvalidEntity,
2327
}

0 commit comments

Comments
 (0)