Skip to content

Commit 27e78b9

Browse files
committed
Set asset directory and load assets from there
1 parent caaf028 commit 27e78b9

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
@@ -204,7 +204,7 @@ pub fn surface_resize(graphics_entity: Entity, width: u32, height: u32) -> error
204204
})
205205
}
206206

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

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

239239
app.add_plugins(plugins);
@@ -266,14 +266,15 @@ fn set_app(app: App) {
266266

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

276-
let mut app = create_app();
277+
let mut app = create_app(asset_path);
277278
app.finish();
278279
app.cleanup();
279280
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)