Skip to content

Commit 8e8a95c

Browse files
committed
Set asset directory and load assets from there
1 parent eeac492 commit 8e8a95c

File tree

5 files changed

+13
-15
lines changed

5 files changed

+13
-15
lines changed
66.8 KB
Loading

crates/processing_pyo3/src/graphics.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,11 @@ impl Drop for Graphics {
3838
#[pymethods]
3939
impl Graphics {
4040
#[new]
41-
pub fn new(width: u32, height: u32) -> PyResult<Self> {
41+
pub fn new(width: u32, height: u32, asset_path: &str) -> PyResult<Self> {
4242
let glfw_ctx =
4343
GlfwContext::new(width, height).map_err(|e| PyRuntimeError::new_err(format!("{e}")))?;
4444

45-
// TODO: pass in something to set the directory?
46-
init().map_err(|e| PyRuntimeError::new_err(format!("{e}")))?;
45+
init(Some(asset_path)).map_err(|e| PyRuntimeError::new_err(format!("{e}")))?;
4746

4847
let surface = glfw_ctx
4948
.create_surface(width, height, 1.0)

crates/processing_pyo3/src/lib.rs

Lines changed: 5 additions & 7 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::*};
15+
use pyo3::{exceptions::PyRuntimeError, prelude::*, types::PyString};
1616

1717
#[pymodule]
1818
fn processing(m: &Bound<'_, PyModule>) -> PyResult<()> {
@@ -27,10 +27,9 @@ fn processing(m: &Bound<'_, PyModule>) -> PyResult<()> {
2727
let abspath = path
2828
.getattr("abspath")?
2929
.call1(pyo3::types::PyTuple::new(py, &[dirname])?)?;
30+
let abspath = path.getattr("join")?.call1((abspath, "assets"))?;
3031

31-
println!("DEBUG MOMENT OF SUCCESS: {}", abspath);
32-
// TODO: Pass this into Graphics for App init
33-
32+
m.add("_root_dir", abspath)?;
3433
m.add_class::<Graphics>()?;
3534
m.add_function(wrap_pyfunction!(size, m)?)?;
3635
m.add_function(wrap_pyfunction!(run, m)?)?;
@@ -49,9 +48,8 @@ fn processing(m: &Bound<'_, PyModule>) -> PyResult<()> {
4948
#[pyfunction]
5049
#[pyo3(pass_module)]
5150
fn size(module: &Bound<'_, PyModule>, width: u32, height: u32) -> PyResult<()> {
52-
// would we get a directory here?
53-
54-
let graphics = Graphics::new(width, height)?;
51+
let asset_path: String = module.getattr("_root_dir")?.extract()?;
52+
let graphics = Graphics::new(width, height, asset_path.as_str())?;
5553
module.setattr("_graphics", graphics)?;
5654
Ok(())
5755
}

crates/processing_render/src/lib.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ pub fn surface_resize(graphics_entity: Entity, width: u32, height: u32) -> error
203203
})
204204
}
205205

206-
fn create_app() -> App {
206+
fn create_app(asset_path: Option<&str>) -> App {
207207
let mut app = App::new();
208208

209209
#[cfg(not(target_arch = "wasm32"))]
@@ -232,7 +232,7 @@ fn create_app() -> App {
232232
app.register_asset_source(
233233
"assets_directory",
234234
// TODO: set this path to the directory containing the main sketch file
235-
AssetSourceBuilder::platform_default("TODO/TODO/TODO/libprocessing/assets", None),
235+
AssetSourceBuilder::platform_default(asset_path.unwrap(), None),
236236
);
237237

238238
app.add_plugins(plugins);
@@ -265,14 +265,15 @@ fn set_app(app: App) {
265265

266266
/// Initialize the app, if not already initialized. Must be called from the main thread and cannot
267267
/// be called concurrently from multiple threads.
268+
/// asset_path is Optional because only python needs to use it.
268269
#[cfg(not(target_arch = "wasm32"))]
269-
pub fn init() -> error::Result<()> {
270+
pub fn init(asset_path: Option<&str>) -> error::Result<()> {
270271
setup_tracing()?;
271272
if is_already_init()? {
272273
return Ok(());
273274
}
274275

275-
let mut app = create_app();
276+
let mut app = create_app(asset_path);
276277
app.finish();
277278
app.cleanup();
278279
set_app(app);

examples/background_image.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ fn main() {
1919

2020
fn sketch() -> error::Result<()> {
2121
let mut glfw_ctx = GlfwContext::new(400, 400)?;
22-
init()?;
22+
init(None)?;
2323

2424
let width = 400;
2525
let height = 400;

0 commit comments

Comments
 (0)