From eeb9f730ebc3adb73e196a5524cf2893af2d6e01 Mon Sep 17 00:00:00 2001
From: Chance <Caznix01@gmail.com>
Date: Mon, 24 Mar 2025 18:08:00 -0400
Subject: [PATCH] press esc to change bg color

---
 engine/src/core/render/ctx.rs | 25 +++++++++++++++++--------
 engine/src/core/render/mod.rs | 22 ++++++++++++++++++++++
 2 files changed, 39 insertions(+), 8 deletions(-)

diff --git a/engine/src/core/render/ctx.rs b/engine/src/core/render/ctx.rs
index d216c93..a854ebb 100644
--- a/engine/src/core/render/ctx.rs
+++ b/engine/src/core/render/ctx.rs
@@ -78,7 +78,7 @@ impl Vertex {
     }
 }
 
-const CUBE_VERTICES: &[Vertex] = &[
+static CUBE_VERTICES: &[Vertex] = &[
     Vertex {
         position: [-0.5, -0.5, 0.5],
         normal: [0.0, 0.0, 1.0],
@@ -235,6 +235,7 @@ pub struct WgpuCtx<'window> {
     uniform_buffer: wgpu::Buffer,
     vertex_buffer: wgpu::Buffer,
     start_time: Instant,
+    bg_color: wgpu::Color,
 }
 
 impl<'window> WgpuCtx<'window> {
@@ -346,6 +347,12 @@ impl<'window> WgpuCtx<'window> {
             render_pipeline,
             uniform_buffer,
             vertex_buffer,
+            bg_color: wgpu::Color {
+                r: 0.1,
+                g: 0.1,
+                b: 0.1,
+                a: 1.0,
+            },
             start_time: Instant::now(),
         })
     }
@@ -362,7 +369,7 @@ impl<'window> WgpuCtx<'window> {
     }
 
     pub fn draw(&mut self) {
-        let elapsed = self.start_time.elapsed().as_secs_f32();
+        let elapsed = self.start_time.elapsed().as_secs_f32() * 0.80f32;
         let model = Matrix4::from_angle_x(Rad(elapsed)) * Matrix4::from_angle_y(Rad(elapsed));
         let view = Matrix4::look_at_rh(
             Point3::new(0.0, 0.0, 3.0),
@@ -399,12 +406,7 @@ impl<'window> WgpuCtx<'window> {
                     view: &view_texture,
                     resolve_target: None,
                     ops: wgpu::Operations {
-                        load: wgpu::LoadOp::Clear(wgpu::Color {
-                            r: 0.1,
-                            g: 0.1,
-                            b: 0.1,
-                            a: 1.0,
-                        }),
+                        load: wgpu::LoadOp::Clear(self.bg_color),
                         store: wgpu::StoreOp::Store,
                     },
                 })],
@@ -428,4 +430,11 @@ impl<'window> WgpuCtx<'window> {
         self.queue.submit(Some(encoder.finish()));
         surface_texture.present();
     }
+
+    pub fn change_bg_color(&mut self, color: wgpu::Color) {
+        self.bg_color = color;
+    }
+    pub fn bg_color(&self) -> wgpu::Color {
+        self.bg_color.clone()
+    }
 }
diff --git a/engine/src/core/render/mod.rs b/engine/src/core/render/mod.rs
index 1e61c8a..6ad0cd4 100644
--- a/engine/src/core/render/mod.rs
+++ b/engine/src/core/render/mod.rs
@@ -42,6 +42,28 @@ impl ApplicationHandler for App<'_> {
                 debug!("Window closed, exiting");
                 std::process::exit(0)
             }
+            WindowEvent::KeyboardInput { device_id, event, is_synthetic } => {
+                match event.physical_key {
+                    winit::keyboard::PhysicalKey::Code(code) => {
+                        if event.state.is_pressed() == false {
+                            return;
+                        }
+                        if code == winit::keyboard::KeyCode::Escape {
+                            // event_loop.exit();
+                            debug!("Window closed, exiting");
+                            if let Some(ctx) = &mut self.ctx {
+                                match ctx.bg_color() {
+                                    wgpu::Color::WHITE => ctx.change_bg_color(wgpu::Color::BLACK),
+                                    wgpu::Color::BLACK => ctx.change_bg_color(wgpu::Color::WHITE),
+                                    _ => ctx.change_bg_color(wgpu::Color::WHITE),
+                            }
+                            // std::process::exit(0)
+                        }
+                    }
+                }
+                _ => {}
+            }
+            }
             WindowEvent::RedrawRequested => {
                 if let Some(ctx) = &mut self.ctx {
                     ctx.draw();