diff options
-rw-r--r-- | Cargo.lock | 147 | ||||
-rw-r--r-- | Cargo.toml | 1 | ||||
-rw-r--r-- | src/main.rs | 60 |
3 files changed, 200 insertions, 8 deletions
diff --git a/Cargo.lock b/Cargo.lock index e4ef8d4..b48c529 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -129,6 +129,20 @@ name = "bytemuck" version = "1.20.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8b37c88a63ffd85d15b406896cc343916d7cf57838a847b3a6f2ca5d39a5695a" +dependencies = [ + "bytemuck_derive", +] + +[[package]] +name = "bytemuck_derive" +version = "1.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bcfcc3cd946cb52f0bbfdbbcfa2f4e24f75ebb6c0e1002f7c25904fada18b9ec" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] [[package]] name = "bytes" @@ -244,7 +258,20 @@ checksum = "c07782be35f9e1140080c6b96f0d44b739e2278479f64e02fdab4e32dfd8b081" dependencies = [ "bitflags 1.3.2", "core-foundation 0.9.4", - "core-graphics-types", + "core-graphics-types 0.1.3", + "foreign-types", + "libc", +] + +[[package]] +name = "core-graphics" +version = "0.24.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa95a34622365fa5bbf40b20b75dba8dfa8c94c734aea8ac9a5ca38af14316f1" +dependencies = [ + "bitflags 2.6.0", + "core-foundation 0.10.0", + "core-graphics-types 0.2.0", "foreign-types", "libc", ] @@ -261,12 +288,29 @@ dependencies = [ ] [[package]] +name = "core-graphics-types" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3d44a101f213f6c4cdc1853d4b78aef6db6bdfa3468798cc1d9912f4735013eb" +dependencies = [ + "bitflags 2.6.0", + "core-foundation 0.10.0", + "libc", +] + +[[package]] name = "crossbeam-utils" version = "0.8.20" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "22ec99545bb0ed0ea7bb9b8e1e9122ea386ff8a48c0922e43f36d45ab09e0e80" [[package]] +name = "ctor-lite" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f791803201ab277ace03903de1594460708d2d54df6053f2d9e82f592b19e3b" + +[[package]] name = "cursor-icon" version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -300,6 +344,45 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f25c0e292a7ca6d6498557ff1df68f32c99850012b6ea401cf8daf771f22ff53" [[package]] +name = "drm" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "98888c4bbd601524c11a7ed63f814b8825f420514f78e96f752c437ae9cbb5d1" +dependencies = [ + "bitflags 2.6.0", + "bytemuck", + "drm-ffi", + "drm-fourcc", + "rustix", +] + +[[package]] +name = "drm-ffi" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "97c98727e48b7ccb4f4aea8cfe881e5b07f702d17b7875991881b41af7278d53" +dependencies = [ + "drm-sys", + "rustix", +] + +[[package]] +name = "drm-fourcc" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0aafbcdb8afc29c1a7ee5fbe53b5d62f4565b35a042a662ca9fecd0b54dae6f4" + +[[package]] +name = "drm-sys" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd39dde40b6e196c2e8763f23d119ddb1a8714534bf7d77fa97a65b0feda3986" +dependencies = [ + "libc", + "linux-raw-sys 0.6.5", +] + +[[package]] name = "equivalent" version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -320,12 +403,19 @@ name = "etch" version = "0.1.0" dependencies = [ "gilrs", + "softbuffer", "tracing", "tracing-subscriber", "winit", ] [[package]] +name = "fastrand" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "486f806e73c5707928240ddc295403b1b93c96a02038563881c4a2fd84b81ac4" + +[[package]] name = "fnv" version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -555,6 +645,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "78b3ae25bc7c8c38cec158d1f2757ee79e9b3740fbc7ccf0e59e4b08d793fa89" [[package]] +name = "linux-raw-sys" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2a385b1be4e5c3e362ad2ffa73c392e53f031eaa5b7d648e64cd87f27f6063d7" + +[[package]] name = "log" version = "0.4.22" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1065,7 +1161,7 @@ dependencies = [ "bitflags 2.6.0", "errno", "libc", - "linux-raw-sys", + "linux-raw-sys 0.4.14", "windows-sys 0.52.0", ] @@ -1182,6 +1278,38 @@ dependencies = [ ] [[package]] +name = "softbuffer" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "18051cdd562e792cad055119e0cdb2cfc137e44e3987532e0f9659a77931bb08" +dependencies = [ + "as-raw-xcb-connection", + "bytemuck", + "cfg_aliases", + "core-graphics 0.24.0", + "drm", + "fastrand", + "foreign-types", + "js-sys", + "log", + "memmap2", + "objc2", + "objc2-foundation", + "objc2-quartz-core", + "raw-window-handle", + "redox_syscall 0.5.7", + "rustix", + "tiny-xlib", + "wasm-bindgen", + "wayland-backend", + "wayland-client", + "wayland-sys", + "web-sys", + "windows-sys 0.59.0", + "x11rb", +] + +[[package]] name = "strict-num" version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1254,6 +1382,19 @@ dependencies = [ ] [[package]] +name = "tiny-xlib" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d52f22673960ad13af14ff4025997312def1223bfa7c8e4949d099e6b3d5d1c" +dependencies = [ + "as-raw-xcb-connection", + "ctor-lite", + "libloading", + "pkg-config", + "tracing", +] + +[[package]] name = "toml_datetime" version = "0.6.8" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1901,7 +2042,7 @@ dependencies = [ "cfg_aliases", "concurrent-queue", "core-foundation 0.9.4", - "core-graphics", + "core-graphics 0.23.2", "cursor-icon", "dpi", "js-sys", diff --git a/Cargo.toml b/Cargo.toml index e3c3f09..e2217f9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,6 +5,7 @@ edition = "2021" [dependencies] gilrs = "0.11.0" +softbuffer = "0.4.6" tracing = "0.1.41" tracing-subscriber = { version = "0.3.18", features = ["env-filter"] } winit = "0.30.5" diff --git a/src/main.rs b/src/main.rs index e9770e9..cdcb23d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,16 @@ -use std::time::{Duration, Instant}; +use std::{ + num::NonZeroU32, + rc::Rc, + time::{Duration, Instant}, +}; use gilrs::{Axis, GamepadId, Gilrs}; +use softbuffer::{Buffer, Context, Surface}; use tracing::level_filters::LevelFilter; use tracing_subscriber::EnvFilter; use winit::{ application::ApplicationHandler, + dpi::LogicalSize, event::{DeviceEvent, WindowEvent}, event_loop::{ControlFlow, EventLoop}, window::Window, @@ -25,6 +31,7 @@ fn main() { left_angle: 0.0, next_check: Instant::now(), }; + el.run_app(&mut etch); } @@ -44,9 +51,13 @@ struct DialState { right_y: f32, } -#[derive(Debug)] +struct SurfacedWindow { + window: Rc<Window>, + surface: Surface<Rc<Window>, Rc<Window>>, +} + struct Etch { - window: Option<Window>, + window: Option<SurfacedWindow>, gilrs: Gilrs, dial: DialState, @@ -55,10 +66,18 @@ struct Etch { } const DIAL_SENSETIVITY: f32 = 2.0; +const WIDTH: f32 = 640.0; +const HEIGHT: f32 = 480.0; impl ApplicationHandler for Etch { fn resumed(&mut self, event_loop: &winit::event_loop::ActiveEventLoop) { - self.window = Some(event_loop.create_window(Window::default_attributes()).unwrap()); + let window = Rc::new(event_loop.create_window(Window::default_attributes()).unwrap()); + window.set_resizable(false); + window.request_inner_size(LogicalSize::new(WIDTH, HEIGHT)); + + let ctx = Context::new(window.clone()).unwrap(); + let surface = Surface::new(&ctx, window.clone()).unwrap(); + self.window = Some(SurfacedWindow { window, surface }); } fn window_event( @@ -75,6 +94,7 @@ impl ApplicationHandler for Etch { WindowEvent::RedrawRequested => { let previous_dial = self.dial; + // Process gamepad events while let Some(gilrs::Event { id, event, time, .. }) = self.gilrs.next_event() @@ -95,6 +115,7 @@ impl ApplicationHandler for Etch { } } + // We check the state of the joystick at 20fps if self.next_check.elapsed() > Duration::from_millis(50) { let left_angle = xy_to_deg(self.dial.left_x, self.dial.left_y); @@ -105,7 +126,36 @@ impl ApplicationHandler for Etch { self.next_check = Instant::now(); } - self.window.as_ref().map(|w| w.request_redraw()); + let Some(surfaced) = self.window.as_mut() else { + tracing::warn!("self.window is None in Redraw!"); + return; + }; + + let (width, height) = { + let phys = surfaced.window.inner_size(); + (phys.width as usize, phys.height as usize) + }; + + let mut buffer = surfaced.surface.buffer_mut().unwrap(); + for idx in 0..width { + buffer[idx + (height / 2) * width] = 0xFF00FF00; + } + + buffer.present().unwrap(); + //surfaced.window.request_redraw(); + } + WindowEvent::Resized(phys) => { + tracing::trace!("resized window: {phys:?}"); + + if let Some(surfaced) = self.window.as_mut() { + surfaced + .surface + .resize( + NonZeroU32::new(phys.width).unwrap(), + NonZeroU32::new(phys.height).unwrap(), + ) + .unwrap(); + } } _ => (), } |