stuff
This commit is contained in:
parent
b313c473ac
commit
3d39d0a04f
|
@ -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",
|
||||
|
|
|
@ -11,3 +11,4 @@ winit = "0.28.6"
|
|||
gif = "0.12.0"
|
||||
rand = "0.8.5"
|
||||
num = "0.4.0"
|
||||
measure_time = "0.8.2"
|
|
@ -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,
|
||||
|
|
|
@ -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<i32>)
|
||||
MouseClick(MouseButton, Vec2<i32>),
|
||||
}
|
||||
|
||||
pub trait Game {
|
||||
|
@ -25,7 +33,7 @@ pub trait Game {
|
|||
|
||||
pub struct WindowState {
|
||||
palette: [[u8; 4]; 256],
|
||||
mouse_pos: Vec2<i32>
|
||||
mouse_pos: Vec2<i32>,
|
||||
}
|
||||
|
||||
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 window = {
|
||||
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_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<T : Game + 'static>(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 => {
|
||||
|
|
Loading…
Reference in New Issue