Added Lua parser implementation and Lua BNF through macro_rules!;

Added a windowed application shell from a tutorial using wgpu;
This commit is contained in:
2026-06-10 02:27:51 +01:00
parent 6e7201bd4e
commit ce2bd0b360
4 changed files with 360 additions and 41 deletions
+56 -40
View File
@@ -1,16 +1,22 @@
#![recursion_limit = "256"]
mod lang;
mod neural;
// Credit of most code to https://sotrh.github.io/learn-wgpu/ since I come from OpenGl not wgpu
use std::sync::Arc;
use winit::{
application::ApplicationHandler, event::*, event_loop::{ActiveEventLoop, EventLoop}, keyboard::{KeyCode, PhysicalKey}, window::Window
};
use winit::dpi::{PhysicalPosition};
#[cfg(target_arch = "wasm32")]
use wasm_bindgen::prelude::*;
use winit::dpi::PhysicalPosition;
#[cfg(target_arch = "wasm32")]
use winit::platform::web::EventLoopExtWebSys;
use winit::{
application::ApplicationHandler,
event::*,
event_loop::{ActiveEventLoop, EventLoop},
keyboard::{KeyCode, PhysicalKey},
window::Window,
};
// This will store the state of our game
pub struct State {
@@ -76,7 +82,9 @@ impl State {
// Shader code in this tutorial assumes an sRGB surface texture. Using a different
// one will result in all the colors coming out darker. If you want to support non
// sRGB surfaces, you'll need to account for that when drawing to the frame.
let surface_format = surface_caps.formats.iter()
let surface_format = surface_caps
.formats
.iter()
.find(|f| f.is_srgb())
.copied()
.unwrap_or(surface_caps.formats[0]);
@@ -106,13 +114,15 @@ impl State {
vertex: wgpu::VertexState {
module: &shader1,
entry_point: Some("vs_main"), // 1.
buffers: &[], // 2.
buffers: &[], // 2.
compilation_options: wgpu::PipelineCompilationOptions::default(),
},
fragment: Some(wgpu::FragmentState { // 3.
fragment: Some(wgpu::FragmentState {
// 3.
module: &shader1,
entry_point: Some("fs_main"),
targets: &[Some(wgpu::ColorTargetState { // 4.
targets: &[Some(wgpu::ColorTargetState {
// 4.
format: config.format,
blend: Some(wgpu::BlendState::REPLACE),
write_mask: wgpu::ColorWrites::ALL,
@@ -133,12 +143,12 @@ impl State {
},
depth_stencil: None, // 1.
multisample: wgpu::MultisampleState {
count: 1, // 2.
mask: !0, // 3.
count: 1, // 2.
mask: !0, // 3.
alpha_to_coverage_enabled: false, // 4.
},
multiview_mask: None, // 5.
cache: None, // 6.
cache: None, // 6.
});
let shader2 = device.create_shader_module(wgpu::include_wgsl!("shader2.wgsl"));
@@ -156,13 +166,15 @@ impl State {
vertex: wgpu::VertexState {
module: &shader2,
entry_point: Some("vs_main"), // 1.
buffers: &[], // 2.
buffers: &[], // 2.
compilation_options: wgpu::PipelineCompilationOptions::default(),
},
fragment: Some(wgpu::FragmentState { // 3.
fragment: Some(wgpu::FragmentState {
// 3.
module: &shader2,
entry_point: Some("fs_main"),
targets: &[Some(wgpu::ColorTargetState { // 4.
targets: &[Some(wgpu::ColorTargetState {
// 4.
format: config.format,
blend: Some(wgpu::BlendState::REPLACE),
write_mask: wgpu::ColorWrites::ALL,
@@ -183,12 +195,12 @@ impl State {
},
depth_stencil: None, // 1.
multisample: wgpu::MultisampleState {
count: 1, // 2.
mask: !0, // 3.
count: 1, // 2.
mask: !0, // 3.
alpha_to_coverage_enabled: false, // 4.
},
multiview_mask: None, // 5.
cache: None, // 6.
cache: None, // 6.
});
Ok(Self {
@@ -206,7 +218,7 @@ impl State {
g: 0.2,
b: 0.3,
a: 1.0,
}
},
})
}
@@ -254,10 +266,14 @@ impl State {
anyhow::bail!("Lost device");
}
};
let view = output.texture.create_view(&wgpu::TextureViewDescriptor::default());
let mut encoder = self.device.create_command_encoder(&wgpu::CommandEncoderDescriptor {
label: Some("Render Encoder"),
});
let view = output
.texture
.create_view(&wgpu::TextureViewDescriptor::default());
let mut encoder = self
.device
.create_command_encoder(&wgpu::CommandEncoderDescriptor {
label: Some("Render Encoder"),
});
{
let mut render_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("Render Pass"),
@@ -304,8 +320,8 @@ impl State {
fn handle_mouse_moved(&mut self, position: PhysicalPosition<f64>) {
self.sky = wgpu::Color {
r: 0.3,
g: position.x/1000.0,
b: position.y/1000.0,
g: position.x / 1000.0,
b: position.y / 1000.0,
a: 1.0,
}
}
@@ -363,13 +379,15 @@ impl ApplicationHandler<State> for App {
// proxy to send the results to the event loop
if let Some(proxy) = self.proxy.take() {
wasm_bindgen_futures::spawn_local(async move {
assert!(proxy
.send_event(
State::new(window)
.await
.expect("Unable to create canvas!!!")
)
.is_ok())
assert!(
proxy
.send_event(
State::new(window)
.await
.expect("Unable to create canvas!!!")
)
.is_ok()
)
});
}
}
@@ -414,16 +432,14 @@ impl ApplicationHandler<State> for App {
}
}
}
WindowEvent::CursorMoved {
position: pos, ..
} => state.handle_mouse_moved(pos),
WindowEvent::CursorMoved { position: pos, .. } => state.handle_mouse_moved(pos),
WindowEvent::KeyboardInput {
event:
KeyEvent {
physical_key: PhysicalKey::Code(code),
state: key_state,
..
},
KeyEvent {
physical_key: PhysicalKey::Code(code),
state: key_state,
..
},
..
} => state.handle_key(event_loop, code, key_state.is_pressed()),
_ => {}
@@ -463,4 +479,4 @@ pub fn run_web() -> Result<(), wasm_bindgen::JsValue> {
run().unwrap_throw();
Ok(())
}
}