From 3d39d0a04f21a140b51438e43f7882e4d4a30e08 Mon Sep 17 00:00:00 2001 From: dani Date: Sat, 1 Jul 2023 17:33:50 +0000 Subject: [PATCH] stuff --- Cargo.lock | 11 +++++++++ Cargo.toml | 3 ++- examples/test.rs | 4 ++-- src/window.rs | 58 +++++++++++++++++++++++++++++++++++------------- 4 files changed, 57 insertions(+), 19 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 887dc03..bee9f4f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -612,6 +612,16 @@ dependencies = [ "libc", ] +[[package]] +name = "measure_time" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56220900f1a0923789ecd6bf25fbae8af3b2f1ff3e9e297fc9b6b8674dd4d852" +dependencies = [ + "instant", + "log", +] + [[package]] name = "memchr" version = "2.5.0" @@ -1180,6 +1190,7 @@ name = "skunk2d" version = "0.1.0" dependencies = [ "gif", + "measure_time", "num", "pixels", "rand", diff --git a/Cargo.toml b/Cargo.toml index 37cd2ea..0f9ea80 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,4 +10,5 @@ pixels = "0.13.0" winit = "0.28.6" gif = "0.12.0" rand = "0.8.5" -num = "0.4.0" \ No newline at end of file +num = "0.4.0" +measure_time = "0.8.2" \ No newline at end of file diff --git a/examples/test.rs b/examples/test.rs index cc291b4..bde7e60 100644 --- a/examples/test.rs +++ b/examples/test.rs @@ -1,7 +1,7 @@ use skunk2d::*; -const WIDTH: i32 = 1280; -const HEIGHT: i32 = 720; +const WIDTH: i32 = 1280 / 2; +const HEIGHT: i32 = 720 / 2; struct World { img: Image, diff --git a/src/window.rs b/src/window.rs index b1902c7..d6ea829 100644 --- a/src/window.rs +++ b/src/window.rs @@ -1,19 +1,27 @@ -use pixels::{Pixels, SurfaceTexture}; -use winit::dpi::LogicalSize; -use winit::event::{ElementState, Event as WinitEvent, WindowEvent, MouseButton as WinitMouseButton}; -use winit::event_loop::{ControlFlow, EventLoop}; -use winit::window::{WindowBuilder}; use crate::image::Image; use crate::vec2::Vec2; +use pixels::{Pixels, SurfaceTexture}; use rand::Rng; +use winit::dpi::LogicalSize; use winit::event::VirtualKeyCode::Escape; +use winit::event::{ + ElementState, Event as WinitEvent, MouseButton as WinitMouseButton, WindowEvent, +}; +use winit::event_loop::{ControlFlow, EventLoop}; +use winit::window::WindowBuilder; pub enum MouseButton { - Left, Right, Middle, WheelUp, WheelDown, Forward, Back + Left, + Right, + Middle, + WheelUp, + WheelDown, + Forward, + Back, } pub enum Event { - MouseClick(MouseButton, Vec2) + MouseClick(MouseButton, Vec2), } pub trait Game { @@ -25,7 +33,7 @@ pub trait Game { pub struct WindowState { palette: [[u8; 4]; 256], - mouse_pos: Vec2 + mouse_pos: Vec2, } impl WindowState { @@ -51,7 +59,7 @@ impl WindowState { } } -pub fn run(width: i32, height: i32) { +pub fn run(width: i32, height: i32) { let event_loop = EventLoop::new(); let window = { let size = LogicalSize::new(width as f64, height as f64); @@ -59,27 +67,37 @@ pub fn run(width: i32, height: i32) { .with_title("Skunk 2D") .with_inner_size(size) .with_min_inner_size(size) - //.with_fullscreen(Option::Some(Fullscreen::Borderless(None))) + .with_decorations(false) + .with_maximized(true) .build(&event_loop) .unwrap() }; + //todo: replace Pixels with custom thingie (startup time slow because wgpu?) let mut pixels = { let window_size = window.inner_size(); let surface_texture = SurfaceTexture::new(window_size.width, window_size.height, &window); Pixels::new(width as u32, height as u32, surface_texture).unwrap() }; - let mut screen = Image::new(Vec2{x: width, y: height}); + let mut screen = Image::new(Vec2 { + x: width, + y: height, + }); let mut window_state = WindowState::new(); let mut game = T::new(&mut window_state); event_loop.run(move |event, _, control_flow| { match event { WinitEvent::NewEvents(_) => {} - WinitEvent::WindowEvent { event: window_event, .. } => { + WinitEvent::WindowEvent { + event: window_event, + .. + } => { match window_event { - WindowEvent::Resized(_) => {} + WindowEvent::Resized(size) => { + pixels.resize_surface(size.width, size.height).unwrap(); + } WindowEvent::Moved(_) => {} WindowEvent::CloseRequested => {} WindowEvent::Destroyed => {} @@ -98,18 +116,26 @@ pub fn run(width: i32, height: i32) { WindowEvent::ModifiersChanged(_) => {} WindowEvent::Ime(_) => {} WindowEvent::CursorMoved { position, .. } => { - window_state.mouse_pos = Vec2{x: position.x as i32, y: position.y as i32} + let (x, y) = pixels + .window_pos_to_pixel((position.x as f32, position.y as f32)) + .unwrap_or_else(|pos| pixels.clamp_pixel_pos(pos)); + window_state.mouse_pos = Vec2 { + x: x as i32, + y: y as i32, + } } WindowEvent::CursorEntered { .. } => {} WindowEvent::CursorLeft { .. } => {} WindowEvent::MouseWheel { .. } => {} - WindowEvent::MouseInput { button, state, .. } => { + WindowEvent::MouseInput { button, state, .. } => { let b = match button { WinitMouseButton::Left => MouseButton::Left, WinitMouseButton::Right => MouseButton::Right, WinitMouseButton::Middle => MouseButton::Middle, //todo: handle other mousebuttons - WinitMouseButton::Other(b) => match b { _ => MouseButton::Back } + WinitMouseButton::Other(b) => match b { + _ => MouseButton::Back, + }, }; match state { ElementState::Pressed => {