Skip to content

Commit 98a5715

Browse files
committed
[update] the runtime component API to have rendering builtin and update the primary demo.
1 parent f2fe4ea commit 98a5715

3 files changed

Lines changed: 49 additions & 74 deletions

File tree

lambda/src/core/component.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
use std::time::Duration;
22

3+
use super::{
4+
events::Events,
5+
render::{
6+
command::RenderCommand,
7+
RenderContext,
8+
},
9+
};
10+
311
/// The Component Interface for allowing Component based data structures
412
/// like the ComponentStack to store components with various purposes
513
/// and implementations to work together.
6-
pub trait Component<E> {
7-
fn on_attach(&mut self);
8-
fn on_detach(&mut self);
9-
fn on_event(&mut self, event: &E);
14+
pub trait Component {
15+
fn on_attach(&mut self, render_context: &mut RenderContext);
16+
fn on_detach(&mut self, render_context: &mut RenderContext);
17+
fn on_event(&mut self, event: Events);
18+
19+
/// When the application state should perform logic updates.
1020
fn on_update(&mut self, last_frame: &Duration);
11-
}
1221

13-
/// The interface for a Component that can be rendered.
14-
pub trait RenderableComponent<E>: Component<E> {
15-
fn on_renderer_attached(
16-
&mut self,
17-
render_context: &mut super::render::RenderContext,
18-
);
22+
/// When the application state should perform rendering.
1923
fn on_render(
2024
&mut self,
21-
render_context: &mut super::render::RenderContext,
22-
) -> Vec<super::render::command::RenderCommand>;
23-
fn on_renderer_detached(
24-
&mut self,
25-
render_context: &mut super::render::RenderContext,
26-
);
25+
render_context: &mut RenderContext,
26+
) -> Vec<RenderCommand>;
2727
}

lambda/src/runtimes/mod.rs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use lambda_platform::winit::{
1212
};
1313

1414
use crate::core::{
15-
component::RenderableComponent,
15+
component::Component,
1616
events::{
1717
ComponentEvent,
1818
Events,
@@ -35,7 +35,7 @@ pub struct GenericRuntimeBuilder {
3535
app_name: String,
3636
render_context_builder: RenderContextBuilder,
3737
window_builder: WindowBuilder,
38-
components: Vec<Box<dyn RenderableComponent<Events>>>,
38+
components: Vec<Box<dyn Component>>,
3939
}
4040

4141
impl GenericRuntimeBuilder {
@@ -78,7 +78,7 @@ impl GenericRuntimeBuilder {
7878
}
7979

8080
/// Attach a component to the current runnable.
81-
pub fn with_component<T: Default + RenderableComponent<Events> + 'static>(
81+
pub fn with_component<T: Default + Component + 'static>(
8282
self,
8383
configure_component: impl FnOnce(Self, T) -> (Self, T),
8484
) -> Self {
@@ -97,13 +97,13 @@ impl GenericRuntimeBuilder {
9797
let window = self.window_builder.build(&mut event_loop);
9898

9999
let component_stack = self.components;
100-
let render_api = self.render_context_builder.build(&window);
100+
let render_context = self.render_context_builder.build(&window);
101101

102102
return GenericRuntime {
103103
name,
104104
event_loop,
105105
window,
106-
render_context: render_api,
106+
render_context,
107107
component_stack,
108108
};
109109
}
@@ -116,7 +116,7 @@ pub struct GenericRuntime {
116116
name: String,
117117
event_loop: Loop<Events>,
118118
window: Window,
119-
component_stack: Vec<Box<dyn RenderableComponent<Events>>>,
119+
component_stack: Vec<Box<dyn Component>>,
120120
render_context: RenderContext,
121121
}
122122

@@ -283,22 +283,18 @@ impl Runtime for GenericRuntime {
283283
RuntimeEvent::Initialized => {
284284
println!("[INFO] Initializing all of the components for the runtime: {}", name);
285285
for component in &mut component_stack {
286-
component.on_attach();
287-
component
288-
.on_renderer_attached(active_render_context.as_mut().unwrap());
286+
component.on_attach(active_render_context.as_mut().unwrap());
289287
}
290288
}
291289
RuntimeEvent::Shutdown => {
292290
for component in &mut component_stack {
293-
component.on_detach();
294-
component
295-
.on_renderer_detached(active_render_context.as_mut().unwrap());
291+
component.on_detach(active_render_context.as_mut().unwrap());
296292
}
297293
}
298294
},
299295
_ => {
300296
for component in &mut component_stack {
301-
component.on_event(&lambda_event);
297+
component.on_event(lambda_event.clone());
302298
}
303299
}
304300
},

tools/lambda_rs_demo/src/main.rs

Lines changed: 23 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
1-
use std::rc::Rc;
2-
31
use lambda::{
42
core::{
5-
component::{
6-
Component,
7-
RenderableComponent,
8-
},
3+
component::Component,
94
events::{
105
ComponentEvent,
116
Events,
127
KeyEvent,
13-
RuntimeEvent,
148
WindowEvent,
159
},
1610
render::{
@@ -24,6 +18,7 @@ use lambda::{
2418
VirtualShader,
2519
},
2620
viewport,
21+
RenderContext,
2722
},
2823
runtime::start_runtime,
2924
},
@@ -39,14 +34,29 @@ pub struct DemoComponent {
3934
height: u32,
4035
}
4136

42-
impl Component<Events> for DemoComponent {
43-
fn on_attach(&mut self) {
37+
impl Component for DemoComponent {
38+
fn on_attach(&mut self, render_context: &mut RenderContext) {
39+
println!("Attached the demo component to the renderer");
40+
let render_pass =
41+
render_pass::RenderPassBuilder::new().build(&render_context);
42+
43+
let pipeline = pipeline::RenderPipelineBuilder::new().build(
44+
render_context,
45+
&render_pass,
46+
&self.vertex_shader,
47+
&self.triangle_vertex,
48+
);
49+
50+
// Attach the render pass and pipeline to the render context
51+
self.render_pass_id = Some(render_context.attach_render_pass(render_pass));
52+
self.render_pipeline_id = Some(render_context.attach_pipeline(pipeline));
53+
4454
println!("Attached the DemoComponent.");
4555
}
4656

47-
fn on_detach(self: &mut DemoComponent) {}
57+
fn on_detach(self: &mut DemoComponent, render_context: &mut RenderContext) {}
4858

49-
fn on_event(self: &mut DemoComponent, event: &lambda::core::events::Events) {
59+
fn on_event(self: &mut DemoComponent, event: Events) {
5060
match event {
5161
Events::Runtime { event, issued_at } => match event {
5262
lambda::core::events::RuntimeEvent::Shutdown => {
@@ -57,8 +67,8 @@ impl Component<Events> for DemoComponent {
5767
Events::Window { event, issued_at } => match event {
5868
WindowEvent::Resize { width, height } => {
5969
println!("Window resized to {}x{}", width, height);
60-
self.width = *width;
61-
self.height = *height;
70+
self.width = width;
71+
self.height = height;
6272
}
6373
WindowEvent::Close => {
6474
println!("Window closed");
@@ -103,30 +113,6 @@ impl Component<Events> for DemoComponent {
103113
false => {}
104114
}
105115
}
106-
}
107-
108-
/// Implement rendering for the component.
109-
impl RenderableComponent<Events> for DemoComponent {
110-
fn on_renderer_attached(
111-
&mut self,
112-
render_context: &mut lambda::core::render::RenderContext,
113-
) {
114-
println!("Attached the demo component to the renderer");
115-
let render_pass =
116-
render_pass::RenderPassBuilder::new().build(&render_context);
117-
118-
let pipeline = pipeline::RenderPipelineBuilder::new().build(
119-
render_context,
120-
&render_pass,
121-
&self.vertex_shader,
122-
&self.triangle_vertex,
123-
);
124-
125-
// Attach the render pass and pipeline to the render context
126-
self.render_pass_id = Some(render_context.attach_render_pass(render_pass));
127-
self.render_pipeline_id = Some(render_context.attach_pipeline(pipeline));
128-
}
129-
130116
fn on_render(
131117
self: &mut DemoComponent,
132118
_render_context: &mut lambda::core::render::RenderContext,
@@ -159,13 +145,6 @@ impl RenderableComponent<Events> for DemoComponent {
159145
RenderCommand::EndRenderPass,
160146
];
161147
}
162-
163-
fn on_renderer_detached(
164-
self: &mut DemoComponent,
165-
_render_context: &mut lambda::core::render::RenderContext,
166-
) {
167-
println!("Detached the demo component from the renderer");
168-
}
169148
}
170149

171150
impl DemoComponent {}

0 commit comments

Comments
 (0)