Skip to content

Commit b60009a

Browse files
committed
[add] input to update the position for one of the triangles.
1 parent 98a5715 commit b60009a

1 file changed

Lines changed: 96 additions & 75 deletions

File tree

tools/triangles_demo/src/main.rs

Lines changed: 96 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
use lambda::{
22
core::{
3-
component::{
4-
Component,
5-
RenderableComponent,
6-
},
3+
component::Component,
74
events::{
85
Events,
6+
KeyEvent,
7+
VirtualKey,
98
WindowEvent,
109
},
1110
render::{
@@ -22,6 +21,7 @@ use lambda::{
2221
VirtualShader,
2322
},
2423
viewport,
24+
RenderContext,
2525
},
2626
runtime::start_runtime,
2727
},
@@ -35,72 +35,12 @@ pub struct TrianglesComponent {
3535
render_pipeline: Option<lambda::core::render::ResourceId>,
3636
width: u32,
3737
height: u32,
38+
animation_scalar: f32,
39+
position: (f32, f32),
3840
}
3941

40-
impl Component<Events> for TrianglesComponent {
41-
fn on_attach(&mut self) {
42-
println!("Attached the DemoComponent.");
43-
}
44-
45-
fn on_detach(&mut self) {}
46-
47-
fn on_event(&mut self, event: &lambda::core::events::Events) {
48-
match event {
49-
Events::Runtime { event, issued_at } => match event {
50-
lambda::core::events::RuntimeEvent::Shutdown => {
51-
println!("Shutting down the runtime");
52-
}
53-
_ => {}
54-
},
55-
Events::Window { event, issued_at } => match event {
56-
WindowEvent::Resize { width, height } => {
57-
println!("Window resized to {}x{}", width, height);
58-
self.width = *width;
59-
self.height = *height;
60-
}
61-
WindowEvent::Close => {
62-
println!("Window closed");
63-
}
64-
},
65-
_ => {}
66-
}
67-
}
68-
69-
fn on_update(&mut self, last_frame: &std::time::Duration) {
70-
match last_frame.as_millis() > 20 {
71-
true => {
72-
println!("[WARN] Last frame took {}ms", last_frame.as_millis());
73-
}
74-
false => {}
75-
}
76-
}
77-
}
78-
79-
#[repr(C)]
80-
#[derive(Debug, Copy, Clone)]
81-
pub struct PushConstant {
82-
color: [f32; 4],
83-
pos: [f32; 2],
84-
scale: [f32; 2],
85-
}
86-
87-
pub fn push_constants_to_bytes(push_constants: &PushConstant) -> &[u32] {
88-
let bytes = unsafe {
89-
let size_in_bytes = std::mem::size_of::<PushConstant>();
90-
let size_in_u32 = size_in_bytes / std::mem::size_of::<u32>();
91-
let ptr = push_constants as *const PushConstant as *const u32;
92-
std::slice::from_raw_parts(ptr, size_in_u32)
93-
};
94-
95-
return bytes;
96-
}
97-
98-
/// Implement rendering for the component.
99-
impl RenderableComponent<Events> for TrianglesComponent {
100-
fn on_renderer_attached(
101-
&mut self,
102-
render_context: &mut lambda::core::render::RenderContext,
103-
) {
42+
impl Component for TrianglesComponent {
43+
fn on_attach(&mut self, render_context: &mut RenderContext) {
10444
let render_pass =
10545
render_pass::RenderPassBuilder::new().build(&render_context);
10646

@@ -116,19 +56,25 @@ impl RenderableComponent<Events> for TrianglesComponent {
11656

11757
self.render_pass = Some(render_context.attach_render_pass(render_pass));
11858
self.render_pipeline = Some(render_context.attach_pipeline(pipeline));
59+
60+
println!("Attached the DemoComponent.");
11961
}
12062

63+
fn on_detach(&mut self, _render_context: &mut RenderContext) {}
64+
12165
fn on_render(
12266
&mut self,
12367
_render_context: &mut lambda::core::render::RenderContext,
12468
) -> Vec<RenderCommand> {
12569
let viewport =
12670
viewport::ViewportBuilder::new().build(self.width, self.height);
12771

72+
let (x, y) = self.position;
73+
12874
let triangle_data = &[
12975
PushConstant {
130-
color: [1.0, 0.0, 0.0, 1.0],
131-
pos: [0.0, 0.0],
76+
color: [1.0, 1.0, 0.0, 1.0],
77+
pos: [x, y],
13278
scale: [0.3, 0.3],
13379
},
13480
PushConstant {
@@ -173,6 +119,8 @@ impl RenderableComponent<Events> for TrianglesComponent {
173119
},
174120
];
175121

122+
// Upload triangle data into the the GPU at the vertex stage of the pipeline
123+
// before requesting to draw each triangle.
176124
for triangle in triangle_data {
177125
commands.push(RenderCommand::PushConstants {
178126
pipeline: render_pipeline.clone(),
@@ -188,12 +136,83 @@ impl RenderableComponent<Events> for TrianglesComponent {
188136
return commands;
189137
}
190138

191-
fn on_renderer_detached(
192-
&mut self,
193-
_render_context: &mut lambda::core::render::RenderContext,
194-
) {
195-
println!("Detached the demo component from the renderer");
139+
fn on_event(&mut self, event: Events) {
140+
match event {
141+
Events::Runtime { event, issued_at } => match event {
142+
lambda::core::events::RuntimeEvent::Shutdown => {
143+
println!("Shutting down the runtime");
144+
}
145+
_ => {}
146+
},
147+
Events::Window { event, issued_at } => match event {
148+
WindowEvent::Resize { width, height } => {
149+
println!("Window resized to {}x{}", width, height);
150+
self.width = width;
151+
self.height = height;
152+
}
153+
WindowEvent::Close => {
154+
println!("Window closed");
155+
}
156+
},
157+
Events::Component { event, issued_at } => todo!(),
158+
Events::Keyboard { event, issued_at } => match event {
159+
KeyEvent::KeyPressed {
160+
scan_code,
161+
virtual_key,
162+
} => match virtual_key {
163+
Some(VirtualKey::W) => {
164+
self.position.1 += 0.01;
165+
}
166+
Some(VirtualKey::S) => {
167+
self.position.1 -= 0.01;
168+
}
169+
Some(VirtualKey::A) => {
170+
self.position.0 -= 0.01;
171+
}
172+
Some(VirtualKey::D) => {
173+
self.position.0 += 0.01;
174+
}
175+
_ => {}
176+
},
177+
_ => {}
178+
},
179+
_ => {}
180+
}
196181
}
182+
183+
fn on_update(&mut self, last_frame: &std::time::Duration) {
184+
match self.animation_scalar {
185+
0.0..=0.5 => self.animation_scalar += last_frame.as_secs_f32(),
186+
0.5..=1.0 => self.animation_scalar -= last_frame.as_secs_f32(),
187+
_ => self.animation_scalar = 0.0,
188+
}
189+
190+
match last_frame.as_millis() > 20 {
191+
true => {
192+
println!("[WARN] Last frame took {}ms", last_frame.as_millis());
193+
}
194+
false => {}
195+
}
196+
}
197+
}
198+
199+
#[repr(C)]
200+
#[derive(Debug, Copy, Clone)]
201+
pub struct PushConstant {
202+
color: [f32; 4],
203+
pos: [f32; 2],
204+
scale: [f32; 2],
205+
}
206+
207+
pub fn push_constants_to_bytes(push_constants: &PushConstant) -> &[u32] {
208+
let bytes = unsafe {
209+
let size_in_bytes = std::mem::size_of::<PushConstant>();
210+
let size_in_u32 = size_in_bytes / std::mem::size_of::<u32>();
211+
let ptr = push_constants as *const PushConstant as *const u32;
212+
std::slice::from_raw_parts(ptr, size_in_u32)
213+
};
214+
215+
return bytes;
197216
}
198217

199218
impl Default for TrianglesComponent {
@@ -227,6 +246,8 @@ impl Default for TrianglesComponent {
227246
render_pipeline: None,
228247
width: 800,
229248
height: 600,
249+
animation_scalar: 0.0,
250+
position: (0.0, 0.0),
230251
};
231252
}
232253
}

0 commit comments

Comments
 (0)