Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ workspace = true
default = ["wayland"]
wayland = ["processing_render/wayland"]
x11 = ["processing_render/x11"]
python = ["processing_render/python"]

[workspace]
resolver = "3"
Expand Down
2 changes: 1 addition & 1 deletion crates/processing_pyo3/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ x11 = ["processing/x11"]

[dependencies]
pyo3 = "0.27.0"
processing = { workspace = true, features = ["python"] }
processing = { workspace = true }
bevy = { workspace = true }
glfw = { version = "0.60.0"}

Expand Down
Comment thread
tychedelia marked this conversation as resolved.
Outdated
Binary file not shown.
2 changes: 1 addition & 1 deletion crates/processing_pyo3/src/graphics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl Graphics {

let mut config = Config::new();
config.set(ConfigKey::AssetRootPath, asset_path.to_string());
init(Some(config)).map_err(|e| PyRuntimeError::new_err(format!("{e}")))?;
init(config).map_err(|e| PyRuntimeError::new_err(format!("{e}")))?;

let surface = glfw_ctx
.create_surface(width, height, 1.0)
Expand Down
1 change: 0 additions & 1 deletion crates/processing_render/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ workspace = true
default = []
wayland = ["bevy/wayland"]
x11 = ["bevy/x11"]
python = []

[dependencies]
bevy = { workspace = true }
Expand Down
18 changes: 14 additions & 4 deletions crates/processing_render/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,33 @@
//!
//! To add a new Config just add a new enum with associated value

use bevy::prelude::Resource;
use std::collections::HashMap;

#[derive(Hash, Eq, PartialEq)]
#[derive(Clone, Hash, Eq, PartialEq)]
pub enum ConfigKey {
AssetRootPath,
}

// TODO: Consider Box<dyn Any> instead of String
pub type ConfigMap = HashMap<ConfigKey, String>;
#[derive(Resource)]
pub struct Config {
map: ConfigMap,
map: HashMap<ConfigKey, String>,
}

impl Clone for Config {
fn clone(&self) -> Self {
Config {
map: self.map.clone(),
}
}
}

impl Config {
pub fn new() -> Self {
// TODO consider defaults
Config {
map: ConfigMap::new(),
map: HashMap::new(),
}
}

Expand Down
16 changes: 11 additions & 5 deletions crates/processing_render/src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
//! It can be created from raw pixel data, loaded from disk, resized, and read back to CPU memory.
use std::path::PathBuf;

#[cfg(feature = "python")]
use bevy::asset::{AssetPath, io::AssetSourceId};
use bevy::{
asset::{
LoadState, RenderAssetUsages, handle_internal_asset_events, io::embedded::GetAssetServer,
AssetPath, LoadState, RenderAssetUsages, handle_internal_asset_events,
io::{AssetSourceId, embedded::GetAssetServer},
},
ecs::{entity::EntityHashMap, system::RunSystemOnce},
prelude::*,
Expand All @@ -25,6 +24,7 @@ use bevy::{
};
use half::f16;

use crate::config::{Config, ConfigKey};
use crate::error::{ProcessingError, Result};

pub struct ImagePlugin;
Expand Down Expand Up @@ -140,10 +140,16 @@ pub fn from_handle(
}

pub fn load(In(path): In<PathBuf>, world: &mut World) -> Result<Entity> {
#[cfg(feature = "python")]
let path = AssetPath::from_path_buf(path).with_source(AssetSourceId::from("assets_directory"));
let config = world.resource_mut::<Config>();
let path: AssetPath = match config.get(ConfigKey::AssetRootPath) {
Some(_) => {
AssetPath::from_path_buf(path).with_source(AssetSourceId::from("assets_directory"))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This name ("assets_directory") might be confusing for contributors coming from bevy (as that's canonically assets/) but I can't think of a better name off the top of my head.

}
None => AssetPath::from_path_buf(path),
};

let handle: Handle<bevy::image::Image> = world.get_asset_server().load(path);

while let LoadState::Loading = world.get_asset_server().load_state(&handle) {
world.run_system_once(handle_internal_asset_events).unwrap();
}
Expand Down
17 changes: 6 additions & 11 deletions crates/processing_render/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@ use std::{cell::RefCell, num::NonZero, path::PathBuf, sync::OnceLock};

use config::*;

#[cfg(feature = "python")]
use bevy::asset::io::AssetSourceBuilder;
#[cfg(not(target_arch = "wasm32"))]
use bevy::log::tracing_subscriber;
use bevy::{
app::{App, AppExit},
asset::AssetEventSystems,
asset::{AssetEventSystems, io::AssetSourceBuilder},
prelude::*,
render::render_resource::{Extent3d, TextureFormat},
};
Expand Down Expand Up @@ -207,9 +205,11 @@ pub fn surface_resize(graphics_entity: Entity, width: u32, height: u32) -> error
})
}

fn create_app(_config: Config) -> App {
fn create_app(config: Config) -> App {
let mut app = App::new();

app.insert_resource(config.clone());

#[cfg(not(target_arch = "wasm32"))]
let plugins = DefaultPlugins
.build()
Expand All @@ -232,9 +232,7 @@ fn create_app(_config: Config) -> App {
..default()
});

#[cfg(feature = "python")]
{
let asset_path = _config.get(ConfigKey::AssetRootPath).unwrap();
if let Some(asset_path) = config.get(ConfigKey::AssetRootPath) {
app.register_asset_source(
"assets_directory",
AssetSourceBuilder::platform_default(asset_path, None),
Expand Down Expand Up @@ -271,11 +269,8 @@ fn set_app(app: App) {

/// Initialize the app, if not already initialized. Must be called from the main thread and cannot
/// be called concurrently from multiple threads.
/// asset_path is Optional because only python needs to use it.
#[cfg(not(target_arch = "wasm32"))]
pub fn init(config: Option<Config>) -> error::Result<()> {
let config = config.unwrap_or_default();

pub fn init(config: Config) -> error::Result<()> {
setup_tracing()?;
if is_already_init()? {
return Ok(());
Expand Down
2 changes: 1 addition & 1 deletion examples/background_image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fn main() {

fn sketch() -> error::Result<()> {
let mut glfw_ctx = GlfwContext::new(400, 400)?;
init(None)?;
init(Config::default())?;

let width = 400;
let height = 400;
Expand Down
2 changes: 1 addition & 1 deletion examples/rectangle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fn main() {

fn sketch() -> error::Result<()> {
let mut glfw_ctx = GlfwContext::new(400, 400)?;
init(None)?;
init(Config::default())?;

let width = 400;
let height = 400;
Expand Down
2 changes: 1 addition & 1 deletion examples/transforms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn main() {

fn sketch() -> error::Result<()> {
let mut glfw_ctx = GlfwContext::new(400, 400)?;
init(None)?;
init(Config::default())?;

let surface = glfw_ctx.create_surface(400, 400, 1.0)?;
let graphics = graphics_create(surface, 400, 400)?;
Expand Down
2 changes: 1 addition & 1 deletion examples/update_pixels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fn main() {

fn sketch() -> error::Result<()> {
let mut glfw_ctx = GlfwContext::new(100, 100)?;
init(None)?;
init(Config::default())?;

let width = 100;
let height = 100;
Expand Down
Loading