Skip to content

Commit 7010b95

Browse files
committed
[update] component & runtime implementations to be Result driven, update package versions.
1 parent bf0ff44 commit 7010b95

File tree

14 files changed

+211
-72
lines changed

14 files changed

+211
-72
lines changed

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/lambda-rs-platform/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "lambda-rs-platform"
33
description = "Platform implementations for lambda-rs"
4-
version = "2023.1.28"
4+
version = "2023.1.29"
55
edition = "2021"
66
resolver = "2"
77
license = "MIT"

crates/lambda-rs-platform/src/winit/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//! Winit wrapper to easily construct cross platform windows
2+
13
use winit::{
24
dpi::{
35
LogicalSize,

crates/lambda-rs/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "lambda-rs"
33
description = "A framework for building cross platform graphics & compute applications."
4-
version = "2023.1.28"
4+
version = "2023.1.29"
55
edition = "2021"
66
license = "MIT"
77
keywords = ["graphics", "compute"]

crates/lambda-rs/examples/push_constants.rs

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@ use lambda::{
2828
ResourceId,
2929
},
3030
runtime::start_runtime,
31-
runtimes::ApplicationRuntimeBuilder,
31+
runtimes::{
32+
application::ComponentResult,
33+
ApplicationRuntime,
34+
ApplicationRuntimeBuilder,
35+
},
3236
};
3337
use lambda_platform::{
3438
gfx::{
@@ -129,8 +133,11 @@ pub struct PushConstantsExample {
129133
height: u32,
130134
}
131135

132-
impl Component for PushConstantsExample {
133-
fn on_attach(&mut self, render_context: &mut lambda::render::RenderContext) {
136+
impl Component<ComponentResult, String> for PushConstantsExample {
137+
fn on_attach(
138+
&mut self,
139+
render_context: &mut lambda::render::RenderContext,
140+
) -> Result<ComponentResult, String> {
134141
let render_pass = RenderPassBuilder::new().build(render_context);
135142
let push_constant_size = std::mem::size_of::<PushConstant>() as u32;
136143

@@ -193,13 +200,22 @@ impl Component for PushConstantsExample {
193200
self.render_pass = Some(render_context.attach_render_pass(render_pass));
194201
self.render_pipeline = Some(render_context.attach_pipeline(pipeline));
195202
self.mesh = Some(mesh);
203+
204+
return Ok(ComponentResult::Success);
196205
}
197206

198-
fn on_detach(&mut self, render_context: &mut lambda::render::RenderContext) {
207+
fn on_detach(
208+
&mut self,
209+
render_context: &mut lambda::render::RenderContext,
210+
) -> Result<ComponentResult, String> {
199211
println!("Detaching component");
212+
return Ok(ComponentResult::Success);
200213
}
201214

202-
fn on_event(&mut self, event: lambda::events::Events) {
215+
fn on_event(
216+
&mut self,
217+
event: lambda::events::Events,
218+
) -> Result<ComponentResult, String> {
203219
// Only handle resizes.
204220
match event {
205221
lambda::events::Events::Window { event, issued_at } => match event {
@@ -211,13 +227,18 @@ impl Component for PushConstantsExample {
211227
_ => {}
212228
},
213229
_ => {}
214-
}
230+
};
231+
return Ok(ComponentResult::Success);
215232
}
216233

217234
/// Update the frame number every frame.
218-
fn on_update(&mut self, last_frame: &std::time::Duration) {
235+
fn on_update(
236+
&mut self,
237+
last_frame: &std::time::Duration,
238+
) -> Result<ComponentResult, String> {
219239
self.last_frame = *last_frame;
220240
self.frame_number += 1;
241+
return Ok(ComponentResult::Success);
221242
}
222243

223244
fn on_render(

crates/lambda-rs/examples/triangle.rs

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ use lambda::{
2020
RenderContext,
2121
},
2222
runtime::start_runtime,
23-
runtimes::ApplicationRuntimeBuilder,
23+
runtimes::{
24+
application::ComponentResult,
25+
ApplicationRuntimeBuilder,
26+
},
2427
};
2528

2629
pub struct DemoComponent {
@@ -32,8 +35,11 @@ pub struct DemoComponent {
3235
height: u32,
3336
}
3437

35-
impl Component for DemoComponent {
36-
fn on_attach(&mut self, render_context: &mut RenderContext) {
38+
impl Component<ComponentResult, String> for DemoComponent {
39+
fn on_attach(
40+
&mut self,
41+
render_context: &mut RenderContext,
42+
) -> Result<ComponentResult, String> {
3743
println!("Attached the demo component to the renderer");
3844
let render_pass =
3945
render_pass::RenderPassBuilder::new().build(&render_context);
@@ -50,11 +56,20 @@ impl Component for DemoComponent {
5056
self.render_pipeline_id = Some(render_context.attach_pipeline(pipeline));
5157

5258
println!("Attached the DemoComponent.");
59+
return Ok(ComponentResult::Success);
5360
}
5461

55-
fn on_detach(self: &mut DemoComponent, render_context: &mut RenderContext) {}
62+
fn on_detach(
63+
self: &mut DemoComponent,
64+
render_context: &mut RenderContext,
65+
) -> Result<ComponentResult, String> {
66+
return Ok(ComponentResult::Success);
67+
}
5668

57-
fn on_event(self: &mut DemoComponent, event: Events) {
69+
fn on_event(
70+
self: &mut DemoComponent,
71+
event: Events,
72+
) -> Result<ComponentResult, String> {
5873
match event {
5974
Events::Runtime { event, issued_at } => match event {
6075
lambda::events::RuntimeEvent::Shutdown => {
@@ -101,16 +116,21 @@ impl Component for DemoComponent {
101116
}
102117
},
103118
_ => {}
104-
}
119+
};
120+
return Ok(ComponentResult::Success);
105121
}
106122

107-
fn on_update(self: &mut DemoComponent, last_frame: &std::time::Duration) {
123+
fn on_update(
124+
self: &mut DemoComponent,
125+
last_frame: &std::time::Duration,
126+
) -> Result<ComponentResult, String> {
108127
match last_frame.as_millis() > 20 {
109128
true => {
110129
println!("[WARN] Last frame took {}ms", last_frame.as_millis());
111130
}
112131
false => {}
113-
}
132+
};
133+
return Ok(ComponentResult::Success);
114134
}
115135
fn on_render(
116136
self: &mut DemoComponent,

crates/lambda-rs/examples/triangles.rs

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ use lambda::{
2323
RenderContext,
2424
},
2525
runtime::start_runtime,
26-
runtimes::ApplicationRuntimeBuilder,
26+
runtimes::{
27+
application::ComponentResult,
28+
ApplicationRuntimeBuilder,
29+
},
2730
};
2831

2932
pub struct TrianglesComponent {
@@ -37,8 +40,11 @@ pub struct TrianglesComponent {
3740
position: (f32, f32),
3841
}
3942

40-
impl Component for TrianglesComponent {
41-
fn on_attach(&mut self, render_context: &mut RenderContext) {
43+
impl Component<ComponentResult, String> for TrianglesComponent {
44+
fn on_attach(
45+
&mut self,
46+
render_context: &mut RenderContext,
47+
) -> Result<ComponentResult, String> {
4248
let render_pass =
4349
render_pass::RenderPassBuilder::new().build(&render_context);
4450

@@ -56,9 +62,15 @@ impl Component for TrianglesComponent {
5662
self.render_pipeline = Some(render_context.attach_pipeline(pipeline));
5763

5864
println!("Attached the DemoComponent.");
65+
return Ok(ComponentResult::Success);
5966
}
6067

61-
fn on_detach(&mut self, _render_context: &mut RenderContext) {}
68+
fn on_detach(
69+
&mut self,
70+
_render_context: &mut RenderContext,
71+
) -> Result<ComponentResult, String> {
72+
return Ok(ComponentResult::Success);
73+
}
6274

6375
fn on_render(
6476
&mut self,
@@ -139,7 +151,7 @@ impl Component for TrianglesComponent {
139151
return commands;
140152
}
141153

142-
fn on_event(&mut self, event: Events) {
154+
fn on_event(&mut self, event: Events) -> Result<ComponentResult, String> {
143155
match event {
144156
Events::Runtime { event, issued_at } => match event {
145157
lambda::events::RuntimeEvent::Shutdown => {
@@ -180,16 +192,21 @@ impl Component for TrianglesComponent {
180192
_ => {}
181193
},
182194
_ => {}
183-
}
195+
};
196+
return Ok(ComponentResult::Success);
184197
}
185198

186-
fn on_update(&mut self, last_frame: &std::time::Duration) {
199+
fn on_update(
200+
&mut self,
201+
last_frame: &std::time::Duration,
202+
) -> Result<ComponentResult, String> {
187203
match last_frame.as_millis() > 20 {
188204
true => {
189205
println!("[WARN] Last frame took {}ms", last_frame.as_millis());
190206
}
191207
false => {}
192-
}
208+
};
209+
return Ok(ComponentResult::Success);
193210
}
194211
}
195212

crates/lambda-rs/src/component.rs

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
use std::time::Duration;
1+
use std::{
2+
fmt::Debug,
3+
time::Duration,
4+
};
25

36
use crate::{
47
events::Events,
@@ -11,15 +14,29 @@ use crate::{
1114
/// The Component Interface for allowing Component based data structures
1215
/// like the ComponentStack to store components with various purposes
1316
/// and implementations to work together.
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);
17+
pub trait Component<R, E>
18+
where
19+
R: Sized + Debug,
20+
E: Sized + Debug,
21+
{
22+
/// The attach function is called when the component is added to the
23+
/// component data storage a runtime is using.
24+
fn on_attach(&mut self, render_context: &mut RenderContext) -> Result<R, E>;
25+
26+
/// The detach function is called when the component is removed from the
27+
/// component data storage a runtime is using.
28+
fn on_detach(&mut self, render_context: &mut RenderContext) -> Result<R, E>;
29+
30+
/// The event function is called every time an event is received from
31+
/// the windowing system/event loop.
32+
fn on_event(&mut self, event: Events) -> Result<R, E>;
1833

1934
/// The update function is called every frame and is used to update
2035
/// the state of the component.
21-
fn on_update(&mut self, last_frame: &Duration);
36+
fn on_update(&mut self, last_frame: &Duration) -> Result<R, E>;
2237

38+
/// Render commands returned from this function will be executed
39+
/// by the renderer immediately.
2340
fn on_render(
2441
&mut self,
2542
render_context: &mut RenderContext,

crates/lambda-rs/src/events.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//! Event definitions for lambda runtimes and applications.
2+
13
use std::time::Instant;
24

35
/// events generated by kernel interactions with the component.
@@ -20,6 +22,7 @@ pub enum WindowEvent {
2022
pub enum RuntimeEvent {
2123
Initialized,
2224
Shutdown,
25+
ComponentPanic { message: String },
2326
}
2427

2528
/// Exports the winit virtual key codes to this namespace for convenience.
@@ -29,14 +32,17 @@ pub use lambda_platform::winit::winit_exports::VirtualKeyCode as VirtualKey;
2932
/// the windowing system.
3033
#[derive(Debug, Clone)]
3134
pub enum Key {
35+
/// Emitted when a key is pressed.
3236
Pressed {
3337
scan_code: u32,
3438
virtual_key: Option<VirtualKey>,
3539
},
40+
/// Emitted when a key is released.
3641
Released {
3742
scan_code: u32,
3843
virtual_key: Option<VirtualKey>,
3944
},
45+
/// Emitted when a modifier key is pressed.
4046
ModifierPressed {
4147
modifier: u32,
4248
virtual_key: VirtualKey,
@@ -56,34 +62,34 @@ pub enum Button {
5662
/// windowing system. The coordinates are in logical pixels.
5763
#[derive(Debug, Clone)]
5864
pub enum Mouse {
65+
/// Emitted when the mouse cursor is moved within the window.
5966
Moved {
6067
x: f64,
6168
y: f64,
6269
dx: f64,
6370
dy: f64,
6471
device_id: u32,
6572
},
66-
Scrolled {
67-
device_id: u32,
68-
},
73+
/// Emitted when the mouse wheel is scrolled.
74+
Scrolled { device_id: u32 },
75+
/// Emitted when a mouse button is pressed.
6976
Pressed {
7077
x: f64,
7178
y: f64,
7279
button: Button,
7380
device_id: u32,
7481
},
82+
/// Emitted when a mouse button is released.
7583
Released {
7684
x: f64,
7785
y: f64,
7886
button: Button,
7987
device_id: u32,
8088
},
81-
LeftWindow {
82-
device_id: u32,
83-
},
84-
EnteredWindow {
85-
device_id: u32,
86-
},
89+
/// Emitted when the mouse cursor leaves the window.
90+
LeftWindow { device_id: u32 },
91+
/// Emitted when the mouse cursor enters the window.
92+
EnteredWindow { device_id: u32 },
8793
}
8894

8995
/// Generic Event Enum which encapsulates all possible events that will be

crates/lambda-rs/src/math/matrix.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ use super::{
99

1010
// -------------------------------- MATRIX -------------------------------------
1111

12+
/// Matrix trait which defines the basic operations that can be performed on a
13+
/// matrix. Lambda currently implements this trait for f32 arrays of arrays
14+
/// for any size.
1215
pub trait Matrix<V: Vector> {
1316
fn add(&self, other: &Self) -> Self;
1417
fn subtract(&self, other: &Self) -> Self;

0 commit comments

Comments
 (0)