This commit is contained in:
dani 2023-07-01 17:33:50 +00:00
parent b313c473ac
commit 3d39d0a04f
4 changed files with 57 additions and 19 deletions

11
Cargo.lock generated
View File

@ -612,6 +612,16 @@ dependencies = [
"libc", "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]] [[package]]
name = "memchr" name = "memchr"
version = "2.5.0" version = "2.5.0"
@ -1180,6 +1190,7 @@ name = "skunk2d"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"gif", "gif",
"measure_time",
"num", "num",
"pixels", "pixels",
"rand", "rand",

View File

@ -11,3 +11,4 @@ winit = "0.28.6"
gif = "0.12.0" gif = "0.12.0"
rand = "0.8.5" rand = "0.8.5"
num = "0.4.0" num = "0.4.0"
measure_time = "0.8.2"

View File

@ -1,7 +1,7 @@
use skunk2d::*; use skunk2d::*;
const WIDTH: i32 = 1280; const WIDTH: i32 = 1280 / 2;
const HEIGHT: i32 = 720; const HEIGHT: i32 = 720 / 2;
struct World { struct World {
img: Image, img: Image,

View File

@ -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::image::Image;
use crate::vec2::Vec2; use crate::vec2::Vec2;
use pixels::{Pixels, SurfaceTexture};
use rand::Rng; use rand::Rng;
use winit::dpi::LogicalSize;
use winit::event::VirtualKeyCode::Escape; 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 { pub enum MouseButton {
Left, Right, Middle, WheelUp, WheelDown, Forward, Back Left,
Right,
Middle,
WheelUp,
WheelDown,
Forward,
Back,
} }
pub enum Event { pub enum Event {
MouseClick(MouseButton, Vec2<i32>) MouseClick(MouseButton, Vec2<i32>),
} }
pub trait Game { pub trait Game {
@ -25,7 +33,7 @@ pub trait Game {
pub struct WindowState { pub struct WindowState {
palette: [[u8; 4]; 256], palette: [[u8; 4]; 256],
mouse_pos: Vec2<i32> mouse_pos: Vec2<i32>,
} }
impl WindowState { impl WindowState {
@ -51,7 +59,7 @@ impl WindowState {
} }
} }
pub fn run<T : Game + 'static>(width: i32, height: i32) { pub fn run<T: Game + 'static>(width: i32, height: i32) {
let event_loop = EventLoop::new(); let event_loop = EventLoop::new();
let window = { let window = {
let size = LogicalSize::new(width as f64, height as f64); let size = LogicalSize::new(width as f64, height as f64);
@ -59,27 +67,37 @@ pub fn run<T : Game + 'static>(width: i32, height: i32) {
.with_title("Skunk 2D") .with_title("Skunk 2D")
.with_inner_size(size) .with_inner_size(size)
.with_min_inner_size(size) .with_min_inner_size(size)
//.with_fullscreen(Option::Some(Fullscreen::Borderless(None))) .with_decorations(false)
.with_maximized(true)
.build(&event_loop) .build(&event_loop)
.unwrap() .unwrap()
}; };
//todo: replace Pixels with custom thingie (startup time slow because wgpu?)
let mut pixels = { let mut pixels = {
let window_size = window.inner_size(); let window_size = window.inner_size();
let surface_texture = SurfaceTexture::new(window_size.width, window_size.height, &window); let surface_texture = SurfaceTexture::new(window_size.width, window_size.height, &window);
Pixels::new(width as u32, height as u32, surface_texture).unwrap() 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 window_state = WindowState::new();
let mut game = T::new(&mut window_state); let mut game = T::new(&mut window_state);
event_loop.run(move |event, _, control_flow| { event_loop.run(move |event, _, control_flow| {
match event { match event {
WinitEvent::NewEvents(_) => {} WinitEvent::NewEvents(_) => {}
WinitEvent::WindowEvent { event: window_event, .. } => { WinitEvent::WindowEvent {
event: window_event,
..
} => {
match window_event { match window_event {
WindowEvent::Resized(_) => {} WindowEvent::Resized(size) => {
pixels.resize_surface(size.width, size.height).unwrap();
}
WindowEvent::Moved(_) => {} WindowEvent::Moved(_) => {}
WindowEvent::CloseRequested => {} WindowEvent::CloseRequested => {}
WindowEvent::Destroyed => {} WindowEvent::Destroyed => {}
@ -98,7 +116,13 @@ pub fn run<T : Game + 'static>(width: i32, height: i32) {
WindowEvent::ModifiersChanged(_) => {} WindowEvent::ModifiersChanged(_) => {}
WindowEvent::Ime(_) => {} WindowEvent::Ime(_) => {}
WindowEvent::CursorMoved { position, .. } => { 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::CursorEntered { .. } => {}
WindowEvent::CursorLeft { .. } => {} WindowEvent::CursorLeft { .. } => {}
@ -109,7 +133,9 @@ pub fn run<T : Game + 'static>(width: i32, height: i32) {
WinitMouseButton::Right => MouseButton::Right, WinitMouseButton::Right => MouseButton::Right,
WinitMouseButton::Middle => MouseButton::Middle, WinitMouseButton::Middle => MouseButton::Middle,
//todo: handle other mousebuttons //todo: handle other mousebuttons
WinitMouseButton::Other(b) => match b { _ => MouseButton::Back } WinitMouseButton::Other(b) => match b {
_ => MouseButton::Back,
},
}; };
match state { match state {
ElementState::Pressed => { ElementState::Pressed => {