Skip to content

Commit

Permalink
WIP migrate libs
Browse files Browse the repository at this point in the history
  • Loading branch information
Maticzpl committed Nov 10, 2023
1 parent ec3f971 commit 226f6f1
Show file tree
Hide file tree
Showing 24 changed files with 1,110 additions and 715 deletions.
1,553 changes: 965 additions & 588 deletions Cargo.lock

Large diffs are not rendered by default.

30 changes: 18 additions & 12 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,31 @@ crate-type = ["cdylib", "rlib"]
[dependencies]
cgmath = "0.18.0"
rand = "0.8.5"
getrandom = { version = "0.2.10", features = ["js"]}
regex = "1.7.1"
getrandom = { version = "0.2.11", features = ["js"]}
regex = "1.10.2"
#syn = "2.0.15"
#quote = "1.0.26"
winit = "0.28.6"
winit = { version = "0.29.3", features = ["rwh_06"] }
env_logger = "0.10.0"
log = "0.4.19"
wgpu = "0.17.0"
wgpu_glyph = "0.21.0"
log = "0.4.20"
wgpu = "0.18.0"
# web-sys = { version = "0.3.65", features = ["GpuComputePassTimestampWrites"]}
# glyphon = "0.3.0"
glyphon = { git = "https://github.com/grovesNL/glyphon.git" } # Version supporting wgpu 0.18 not on crates.io yet
cfg-if = "1.0.0"
pollster = "0.3.0"
instant = "0.1.12"
bytemuck = { version = "1.13.1", features = ["derive"] }
bytemuck = { version = "1.14.0", features = ["derive"] }
#serde = "1.0.171"
#directories = "5.0.1"
rust_bresenham = "0.1.8"

[target.'cfg(target_arch = "wasm32")'.dependencies]
console_error_panic_hook = "0.1.6"
console_error_panic_hook = "0.1.7"
console_log = "1.0.0"
wgpu = { version = "0.17.0", features = ["webgl"]}
wasm-bindgen = "0.2.87"
wasm-bindgen-futures = "0.4.30"
wgpu = { version = "0.18.0", features = ["webgl"]}
wasm-bindgen = "0.2.88"
wasm-bindgen-futures = "0.4.38"
web-sys = { version = "0.3.64", features = [
"Document",
"Window",
Expand All @@ -51,7 +53,11 @@ path = "lib/proc_macros"
[profile.dev]
opt-level = 0

[profile.release]
[profile.devfast] # cargo run --profile devfast
inherits = "dev"
opt-level = 3
debug = true

[profile.release]
opt-level = 3
#lto = true
2 changes: 1 addition & 1 deletion rust-toolchain
Original file line number Diff line number Diff line change
@@ -1 +1 @@
nightly-2023-06-25
stable
69 changes: 28 additions & 41 deletions src/input/event_handling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@ use std::rc::Rc;
use cgmath::{Matrix4, Transform, Vector2, Vector3, Vector4};
use instant::Instant;
use log::error;
use wgpu_glyph::ab_glyph::{Point, Rect};
use winit::dpi::{PhysicalPosition, PhysicalSize};
use winit::event::ElementState::Pressed;
use winit::event::MouseScrollDelta::{LineDelta, PixelDelta};
use winit::event::{Event, KeyboardInput, MouseButton, VirtualKeyCode, WindowEvent};
use winit::event_loop::{ControlFlow, EventLoop};
use winit::event::{Event, MouseButton, WindowEvent};
use winit::event_loop::EventLoop;
use winit::keyboard::PhysicalKey;

use crate::input::events::invoker::InputEventInvoker;
use crate::rendering::gui::game_gui::GameGUI;
use crate::rendering::renderer::Renderer;
use crate::rendering::Core;
use crate::rendering::{Core, Rect};
use crate::simulation::sim::{Simulation, WINH, WINW, XRES, YRES};

pub struct InputData {
pub mouse_buttons: HashMap<MouseButton, bool>,
pub prev_mouse_buttons: HashMap<MouseButton, bool>,
pub keys: HashMap<VirtualKeyCode, bool>,
pub prev_keys: HashMap<VirtualKeyCode, bool>,
pub keys: HashMap<PhysicalKey, bool>,
pub prev_keys: HashMap<PhysicalKey, bool>,
pub mouse_pos: PhysicalPosition<f64>,
pub scroll: f32,

Expand All @@ -32,13 +32,13 @@ pub struct InputData {
}

impl InputData {
pub fn key_pressed(&self, key: &VirtualKeyCode) -> bool {
pub fn key_pressed(&self, key: &PhysicalKey) -> bool {
self.keys.get(key).is_some()
}
pub fn key_just_pressed(&self, key: &VirtualKeyCode) -> bool {
pub fn key_just_pressed(&self, key: &PhysicalKey) -> bool {
self.keys.get(key).is_some() && self.prev_keys.get(key).is_none()
}
pub fn key_just_released(&self, key: &VirtualKeyCode) -> bool {
pub fn key_just_released(&self, key: &PhysicalKey) -> bool {
self.keys.get(key).is_none() && self.prev_keys.get(key).is_some()
}
pub fn mouse_pressed(&self, button: &MouseButton) -> bool {
Expand All @@ -52,7 +52,7 @@ impl InputData {
}
}

pub fn handle_events(
pub async fn handle_events(
event_loop: EventLoop<()>,
mut input: InputData,
mut sim: Simulation,
Expand All @@ -62,7 +62,7 @@ pub fn handle_events(
) {
let invoker = InputEventInvoker::new();

event_loop.run(move |event, _, flow| {
event_loop.run(move |event, event_loop_window_target| {
let core = rendering_core.borrow();
let win_id = core.window.id();
let size = core.window_size;
Expand All @@ -76,7 +76,7 @@ pub fn handle_events(
} if win_id == window_id => {
match ev {
WindowEvent::CloseRequested => {
flow.set_exit();
event_loop_window_target.exit();
}
WindowEvent::MouseInput { button, state, .. } => {
if state == Pressed {
Expand Down Expand Up @@ -104,41 +104,28 @@ pub fn handle_events(
input.mouse_pos.x = pos.x;
input.mouse_pos.y = pos.y;
}
WindowEvent::KeyboardInput {
input:
KeyboardInput {
virtual_keycode: Some(key),
state,
..
},
..
} => {
WindowEvent::KeyboardInput { event, .. } => {
// println!("{:?} k-s {}",key,_scan);
if state == Pressed {
input.keys.insert(key, true);
if event.state == Pressed {
input.keys.insert(event.physical_key, true);
}
else {
input.keys.remove(&key);
input.keys.remove(&event.physical_key);
}
}
WindowEvent::Resized { 0: size } => {
ren.resize(size);
}
WindowEvent::ScaleFactorChanged { new_inner_size, .. } => {
ren.resize(*new_inner_size);
}
WindowEvent::RedrawRequested => match ren.render(&sim, &mut gui) {
Ok(_) => {}
Err(wgpu::SurfaceError::Lost) => ren.resize(size),
Err(wgpu::SurfaceError::OutOfMemory) => event_loop_window_target.exit(),
Err(e) => error!("{:?}", e)
},
_ => {}
}
}
Event::RedrawRequested(window_id) if window_id == win_id => {
match ren.render(&sim, &mut gui) {
Ok(_) => {}
Err(wgpu::SurfaceError::Lost) => ren.resize(size),
Err(wgpu::SurfaceError::OutOfMemory) => flow.set_exit(),
Err(e) => error!("{:?}", e)
}
}
Event::MainEventsCleared => {
Event::AboutToWait => {
// TODO: Clean this up
let win_size: PhysicalSize<u32>;
{
Expand Down Expand Up @@ -201,16 +188,16 @@ pub fn handle_events(
cursor_y = cursor_y.clamp(hs, YRES - hs - (gui.brush_size % 2) as usize);
input.cursor_pos = Vector2::new(cursor_x, cursor_y);

gui.cursor = Rect {
min: Point {
gui.cursor = (
Vector2 {
x: (cursor_x - hs) as f32,
y: (cursor_y - hs) as f32
},
max: Point {
Vector2 {
x: (cursor_x - hs) as f32 + gui.brush_size as f32,
y: (cursor_y - hs) as f32 + gui.brush_size as f32
}
};
);

input.scroll = 0.0;

Expand All @@ -223,7 +210,7 @@ pub fn handle_events(
core.window.request_redraw();
}
}
_ => *flow = ControlFlow::Poll
_ => ()
}
});
}
8 changes: 4 additions & 4 deletions src/input/events/do_brush_size.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use winit::event::VirtualKeyCode;
use winit::keyboard::{KeyCode, PhysicalKey};

use crate::input::event_handling::InputData;
use crate::input::events::input_event::{AnyKey, InputEvent, KeyEvent, KeyState, LogicalOperator};
Expand All @@ -16,12 +16,12 @@ impl InputEvent for DoBrushSize {
fn default_keys(&self) -> Vec<KeyEvent> {
vec![
KeyEvent {
key: AnyKey::Keyboard(VirtualKeyCode::LShift),
key: AnyKey::Keyboard(PhysicalKey::Code(KeyCode::ShiftLeft)),
state: KeyState::Held,
combine_previous: None
},
KeyEvent {
key: AnyKey::Keyboard(VirtualKeyCode::LShift),
key: AnyKey::Keyboard(PhysicalKey::Code(KeyCode::ShiftLeft)),
state: KeyState::NotHeld,
combine_previous: Some(LogicalOperator::Or)
},
Expand All @@ -37,7 +37,7 @@ impl InputEvent for DoBrushSize {
) {
if input.scroll != 0.0 {
let mut speed = 1;
if input.key_pressed(&VirtualKeyCode::LShift) {
if input.key_pressed(&PhysicalKey::Code(KeyCode::ShiftLeft)) {
speed = 2;
}
gui.brush_size =
Expand Down
4 changes: 2 additions & 2 deletions src/input/events/do_camera_center.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use cgmath::{Vector2, Zero};
use winit::event::VirtualKeyCode;
use winit::keyboard::{KeyCode, PhysicalKey};

use crate::input::event_handling::InputData;
use crate::input::events::input_event::{AnyKey, InputEvent, KeyEvent, KeyState};
Expand All @@ -16,7 +16,7 @@ impl InputEvent for DoCameraCenter {

fn default_keys(&self) -> Vec<KeyEvent> {
vec![KeyEvent {
key: AnyKey::Keyboard(VirtualKeyCode::L),
key: AnyKey::Keyboard(PhysicalKey::Code(KeyCode::KeyL)),
state: KeyState::Pressed,
combine_previous: None
}]
Expand Down
4 changes: 2 additions & 2 deletions src/input/events/do_grid_size.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use winit::event::VirtualKeyCode;
use winit::keyboard::{KeyCode, PhysicalKey};

use crate::input::event_handling::InputData;
use crate::input::events::input_event::{AnyKey, InputEvent, KeyEvent, KeyState};
Expand All @@ -15,7 +15,7 @@ impl InputEvent for DoGridSize {

fn default_keys(&self) -> Vec<KeyEvent> {
vec![KeyEvent {
key: AnyKey::Keyboard(VirtualKeyCode::G),
key: AnyKey::Keyboard(PhysicalKey::Code(KeyCode::KeyG)),
state: KeyState::Held,
combine_previous: None
}]
Expand Down
4 changes: 2 additions & 2 deletions src/input/events/do_pause.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use winit::event::VirtualKeyCode;
use winit::keyboard::{KeyCode, PhysicalKey};

use crate::input::event_handling::InputData;
use crate::input::events::input_event::{AnyKey, InputEvent, KeyEvent, KeyState};
Expand All @@ -15,7 +15,7 @@ impl InputEvent for DoPause {

fn default_keys(&self) -> Vec<KeyEvent> {
vec![KeyEvent {
key: AnyKey::Keyboard(VirtualKeyCode::Space),
key: AnyKey::Keyboard(PhysicalKey::Code(KeyCode::Space)),
state: KeyState::Pressed,
combine_previous: None
}]
Expand Down
13 changes: 5 additions & 8 deletions src/input/events/do_tick.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use std::cell::Cell;

use instant::Instant;
use winit::event::VirtualKeyCode;
use winit::keyboard::{KeyCode, PhysicalKey};

use crate::input::event_handling::InputData;
use crate::input::events::input_event::{AnyKey, InputEvent, KeyEvent, KeyState, LogicalOperator};
Expand All @@ -19,22 +16,22 @@ impl InputEvent for DoTick {
fn default_keys(&self) -> Vec<KeyEvent> {
vec![
KeyEvent {
key: AnyKey::Keyboard(VirtualKeyCode::F),
key: AnyKey::Keyboard(PhysicalKey::Code(KeyCode::KeyF)),
state: KeyState::Pressed,
combine_previous: None
},
KeyEvent {
key: AnyKey::Keyboard(VirtualKeyCode::V),
key: AnyKey::Keyboard(PhysicalKey::Code(KeyCode::KeyV)),
state: KeyState::Pressed,
combine_previous: Some(LogicalOperator::Or)
},
KeyEvent {
key: AnyKey::Keyboard(VirtualKeyCode::N),
key: AnyKey::Keyboard(PhysicalKey::Code(KeyCode::KeyN)),
state: KeyState::Pressed,
combine_previous: Some(LogicalOperator::Or)
},
KeyEvent {
key: AnyKey::Keyboard(VirtualKeyCode::J),
key: AnyKey::Keyboard(PhysicalKey::Code(KeyCode::KeyJ)),
state: KeyState::Pressed,
combine_previous: Some(LogicalOperator::Or)
},
Expand Down
6 changes: 4 additions & 2 deletions src/input/events/do_zoom.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use cgmath::{Matrix4, Vector3};
use winit::event::VirtualKeyCode;
use winit::keyboard::PhysicalKey;

use crate::input::event_handling::InputData;
use crate::input::events::input_event::{AnyKey, InputEvent, KeyEvent, KeyState};
Expand All @@ -16,7 +16,9 @@ impl InputEvent for DoZoom {

fn default_keys(&self) -> Vec<KeyEvent> {
vec![KeyEvent {
key: AnyKey::Keyboard(VirtualKeyCode::LControl),
key: AnyKey::Keyboard(PhysicalKey::Code(
winit::keyboard::KeyCode::ControlLeft
)),
state: KeyState::Held,
combine_previous: None
}]
Expand Down
5 changes: 3 additions & 2 deletions src/input/events/input_event.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use winit::event::{MouseButton, VirtualKeyCode};
use winit::event::MouseButton;
use winit::keyboard::PhysicalKey;

use crate::input::event_handling::InputData;
use crate::rendering::gui::game_gui::GameGUI;
Expand All @@ -22,7 +23,7 @@ pub enum LogicalOperator {
}

pub enum AnyKey {
Keyboard(VirtualKeyCode),
Keyboard(PhysicalKey),
Mouse(MouseButton)
}

Expand Down
5 changes: 1 addition & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#![feature(core_intrinsics)]

extern crate core;

mod input;
Expand All @@ -10,7 +8,6 @@ use std::collections::HashMap;
use std::rc::Rc;

use cgmath::{Vector2, Vector4, Zero};
use rust_bresenham::Bresenham;
#[cfg(target_arch = "wasm32")]
use wasm_bindgen::prelude::*;
use winit::dpi::PhysicalPosition;
Expand Down Expand Up @@ -69,5 +66,5 @@ pub async fn run() {
));

let rendering_core = ren.rendering_core.clone();
handle_events(event_loop, input, sim, ren, gui, rendering_core);
handle_events(event_loop, input, sim, ren, gui, rendering_core).await;
}
1 change: 0 additions & 1 deletion src/rendering/gui/components/fps_display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::rc::{Rc, Weak};

use cgmath::Vector2;
use wgpu::Color;
use wgpu_glyph::FontId;

use crate::define_component;
use crate::rendering::gui::components::label::Label;
Expand Down
Loading

0 comments on commit 226f6f1

Please sign in to comment.