Skip to content

Commit

Permalink
feat: resource sharing now fully working. with example
Browse files Browse the repository at this point in the history
  • Loading branch information
ElhamAryanpur committed Aug 28, 2023
1 parent e4c58d7 commit be915ed
Show file tree
Hide file tree
Showing 6 changed files with 300 additions and 86 deletions.
14 changes: 3 additions & 11 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,10 @@ path = "examples/shapes/cube.rs"
name = "rotate_around"
path = "examples/camera/rotate_around.rs"

# Utils
[[example]]
name = "fly_camera"
path = "examples/camera/fly_camera.rs"

# GUI
[[example]]
name = "hello_gui"
path = "examples/gui/hello_gui.rs"

[[example]]
name = "styling_gui"
path = "examples/gui/styling.rs"
name = "resource_sharing"
path = "examples/utils/resource_sharing.rs"

# Development ONLY
[[example]]
Expand Down
167 changes: 104 additions & 63 deletions examples/dev/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,80 +9,121 @@ use blue_engine::{
fn main() {
let mut engine = Engine::new().expect("win");

// ===============================
let texture = engine
.renderer
.build_texture(
"background",
TextureData::Path("resources/BlueLogoDiscord.png"),
blue_engine::TextureMode::Clamp,
)
.unwrap();

let first = square(
"main",
ObjectSettings::default(),
&mut engine.renderer,
&mut engine.objects,
);

//let triangle_id = triangle(Some("Triangleee"), &mut engine, camera).unwrap();
let window_size = engine.window.inner_size();
engine.objects.get_mut("main").unwrap().set_texture(texture);
engine
.objects
.get_mut("main")
.unwrap()
.set_position(-1f32, 0f32, 0f32);

uv_sphere(
"cube",
(18, 36, 1f32),
let second = square(
"alt",
ObjectSettings::default(),
&mut engine.renderer,
&mut engine.objects,
)
.unwrap();
// engine.objects.get_mut("cube").unwrap().scale(0.6, 0.6, 0.6);
);

engine
.objects
.get_mut("cube")
.get_mut("alt")
.unwrap()
.set_color(1f32, 0f32, 0f32, 1f32);
//cube.scale(0.3, 0.3, 0.3);

.reference_texture("main");
engine
.objects
.get_mut("monke")
.get_mut("alt")
.unwrap()
.set_color(0.051f32, 0.533f32, 0.898f32, 1f32);
//engine.objects[test].rotate(90f32, RotateAxis::Y);

/*let sphere_1 = uv_sphere(Some("SPHERE1"), &mut engine, (18, 36, 1f32)).unwrap();
engine.objects[sphere_1].scale(2f32, 2f32, 2f32);
engine.objects[sphere_1].set_color(0.051f32, 0.533f32, 0.898f32, 1f32);
let sphere_1 = uv_sphere(Some("SPHERE1"), &mut engine, (18, 36, 1f32)).unwrap();
engine.objects[sphere_1].position(2f32, 1f32, 0f32);
engine.objects[sphere_1].set_color(1.0f32, 0.5f32, 0.31f32, 1f32);
let sphere_2 = uv_sphere(Some("SPHERE2"), &mut engine, (18, 36, 1f32)).unwrap();
engine.objects[sphere_2].position(-2f32, 1f32, 0f32);
engine.objects[sphere_2].set_color(1.0f32, 0.5f32, 0.31f32, 1f32);
let sphere_3 = uv_sphere(Some("SPHERE3"), &mut engine, (18, 36, 1f32)).unwrap();
engine.objects[sphere_3].position(2f32, -1f32, 0f32);
engine.objects[sphere_3].set_color(1.0f32, 0.5f32, 0.31f32, 1f32);
let sphere_4 = uv_sphere(Some("SPHERE4"), &mut engine, (18, 36, 1f32)).unwrap();
engine.objects[sphere_4].position(-2f32, -1f32, 0f32);
engine.objects[sphere_4].set_color(1.0f32, 0.5f32, 0.31f32, 1f32); */

//let window_size = engine.window.inner_size();
/*let change_texture = engine
.renderer
.build_and_append_texture(
"name",
TextureData::Bytes(include_bytes!("resource/BlueLogoDiscord.png").to_vec()),
blue_engine::header::TextureMode::Clamp,
//blue_engine::header::TextureFormat::PNG,
)
.unwrap();*/

//let square = engine.get_object(square_id).unwrap();

//square.change_color(0.0, 0.0, 1.0, 0.7).unwrap();
//square.change_texture(change_texture);
//square.resize(100.0, 100.0, 0.0, window_size);

//let square = engine.objects.get_mut(square_id).unwrap();

//square.no_stretch_update(&mut engine.renderer, engine.window.inner_size()).unwrap();
//font.draw("Hello_World", (-100, 50), &mut engine).unwrap();

let radius = 10f32;
let start = std::time::SystemTime::now();
let mut rotation = 0f32;
let speed = -0.05;

let mut has_border = false;
let mut val = 0f32;
.set_position(1f32, 0f32, 0f32);

// ===============================
/*
//let triangle_id = triangle(Some("Triangleee"), &mut engine, camera).unwrap();
let window_size = engine.window.inner_size();
uv_sphere(
"cube",
(18, 36, 1f32),
&mut engine.renderer,
&mut engine.objects,
)
.unwrap();
// engine.objects.get_mut("cube").unwrap().scale(0.6, 0.6, 0.6);
engine
.objects
.get_mut("cube")
.unwrap()
.set_color(1f32, 0f32, 0f32, 1f32);
//cube.scale(0.3, 0.3, 0.3);
engine
.objects
.get_mut("monke")
.unwrap()
.set_color(0.051f32, 0.533f32, 0.898f32, 1f32);
//engine.objects[test].rotate(90f32, RotateAxis::Y);
/*let sphere_1 = uv_sphere(Some("SPHERE1"), &mut engine, (18, 36, 1f32)).unwrap();
engine.objects[sphere_1].scale(2f32, 2f32, 2f32);
engine.objects[sphere_1].set_color(0.051f32, 0.533f32, 0.898f32, 1f32);
let sphere_1 = uv_sphere(Some("SPHERE1"), &mut engine, (18, 36, 1f32)).unwrap();
engine.objects[sphere_1].position(2f32, 1f32, 0f32);
engine.objects[sphere_1].set_color(1.0f32, 0.5f32, 0.31f32, 1f32);
let sphere_2 = uv_sphere(Some("SPHERE2"), &mut engine, (18, 36, 1f32)).unwrap();
engine.objects[sphere_2].position(-2f32, 1f32, 0f32);
engine.objects[sphere_2].set_color(1.0f32, 0.5f32, 0.31f32, 1f32);
let sphere_3 = uv_sphere(Some("SPHERE3"), &mut engine, (18, 36, 1f32)).unwrap();
engine.objects[sphere_3].position(2f32, -1f32, 0f32);
engine.objects[sphere_3].set_color(1.0f32, 0.5f32, 0.31f32, 1f32);
let sphere_4 = uv_sphere(Some("SPHERE4"), &mut engine, (18, 36, 1f32)).unwrap();
engine.objects[sphere_4].position(-2f32, -1f32, 0f32);
engine.objects[sphere_4].set_color(1.0f32, 0.5f32, 0.31f32, 1f32); */
//let window_size = engine.window.inner_size();
/*let change_texture = engine
.renderer
.build_and_append_texture(
"name",
TextureData::Bytes(include_bytes!("resource/BlueLogoDiscord.png").to_vec()),
blue_engine::header::TextureMode::Clamp,
//blue_engine::header::TextureFormat::PNG,
)
.unwrap();*/
//let square = engine.get_object(square_id).unwrap();
//square.change_color(0.0, 0.0, 1.0, 0.7).unwrap();
//square.change_texture(change_texture);
//square.resize(100.0, 100.0, 0.0, window_size);
//let square = engine.objects.get_mut(square_id).unwrap();
//square.no_stretch_update(&mut engine.renderer, engine.window.inner_size()).unwrap();
//font.draw("Hello_World", (-100, 50), &mut engine).unwrap();
let radius = 10f32;
let start = std::time::SystemTime::now();
let mut rotation = 0f32;
let speed = -0.05;
let mut has_border = false;
let mut val = 0f32;
*/
engine
.update_loop(move |renderer, _window, objects, input, camera, plugins| {
/*let o =
Expand Down
65 changes: 65 additions & 0 deletions examples/utils/resource_sharing.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use blue_engine::{primitive_shapes::square, Engine, ObjectSettings, TextureData};

fn main() {
// Start the engine
let mut engine = Engine::new().expect("window not initialized");

// build a texture as an example of resource to be shared
let texture = engine
.renderer
.build_texture(
"background",
TextureData::Path("resources/BlueLogoDiscord.png"),
blue_engine::TextureMode::Clamp,
)
.unwrap();

// build your main object with the texture
square(
"main",
ObjectSettings::default(),
&mut engine.renderer,
&mut engine.objects,
)
.expect("Error during creation of main square");

// add the texture to the main object as normally would
engine
.objects
.get_mut("main")
.unwrap()
.set_texture(texture)
.expect("Error during inserting texture to the main square");
// set position to make it visible
engine
.objects
.get_mut("main")
.expect("Error during setting the position of the main square")
.set_position(-1.5f32, 0f32, 0f32);

// create another object where you want to get resources shared with
square(
"alt",
ObjectSettings::default(),
&mut engine.renderer,
&mut engine.objects,
)
.expect("Error during creation of alt square");

// here you can use `reference_texture` to reference the texture from the main object
engine
.objects
.get_mut("alt")
.expect("Error during copying texture of the main square")
.reference_texture("main");
// setting position again to make it visible
engine
.objects
.get_mut("alt")
.expect("Error during setting the position of the alt square")
.set_position(1.5f32, 0f32, 0f32);

engine
.update_loop(move |_, _, _, _, _, _| {})
.expect("Error during update loop");
}
1 change: 0 additions & 1 deletion src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,6 @@ pub enum PipelineData<T> {
Copy(String),
Data(T),
}
//? ADD IT TO ALL OF PIPELINE

/// Container for vertex and index buffer
#[derive(Debug)]
Expand Down
21 changes: 21 additions & 0 deletions src/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,27 @@ impl Object {

Ok(updated_buffer2.0)
}

// ============================= FOR COPY OF PIPELINES =============================
/// References another object's vertices
pub fn reference_vertices(&mut self, object_id: impl StringBuffer) {
self.pipeline.vertex_buffer = PipelineData::Copy(object_id.as_string());
}

/// References another object's shader
pub fn reference_shader(&mut self, object_id: impl StringBuffer) {
self.pipeline.shader = PipelineData::Copy(object_id.as_string());
}

/// References another object's texture
pub fn reference_texture(&mut self, object_id: impl StringBuffer) {
self.pipeline.texture = PipelineData::Copy(object_id.as_string());
}

/// References another object's uniform buffer
pub fn reference_uniform_buffer(&mut self, object_id: impl StringBuffer) {
self.pipeline.uniform = PipelineData::Copy(object_id.as_string());
}
}

#[derive(Debug)]
Expand Down
Loading

0 comments on commit be915ed

Please sign in to comment.