Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Init lib update winit 0.30 #4

Merged
merged 18 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 12 additions & 24 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,42 +8,39 @@ edition = "2021"
[features]
default = []
egui_gui = ["egui-wgpu", "egui-winit", "egui"]
egui_extra = ["egui_gui", "egui_extras", "egui_plot"]
egui_extra = ["egui_gui", "egui_extras"]
egui_demo = ["egui_gui", "egui_demo_lib", "egui_demo_lib/syntect"]
egui_persistence = ["egui_gui", "egui_demo", "egui/persistence", "egui_demo_lib/serde"]
trace = ["wgpu/trace"]
wgpu_serde = ["wgpu/serde"]

[dependencies]
indexmap = "2.2"
pollster = "0.3"
image = "0.25"
bytemuck = { version = "1.16", features = ["derive"] }
wgpu = { version = "0.20", default-features = true, features = ["naga-ir"] }
winit = { version = "0.29", features = [] }
glam = "0.27"
wgpu = { version = "22.1.0", features = ["naga-ir"] }
winit = { version = "0.30" }
path-clean = "1.0.1"
notify = "6.1"
flume = "0.11"
log = "0.4"

# Optional Egui
egui = { version = "0.28", optional = true }
egui-wgpu = { version = "0.28", optional = true }
egui-winit = { version = "0.28", optional = true }
egui_extras = { version = "0.28", optional = true }
egui_demo_lib = { version = "0.28", optional = true }
egui_plot = { version = "0.28", optional = true }
egui = { version = "0.29", optional = true }
egui-wgpu = { version = "0.29", optional = true }
egui-winit = { version = "0.29", optional = true }
egui_extras = { version = "0.29", optional = true }
egui_demo_lib = { version = "0.29", optional = true }

[dev-dependencies]
egui_demo_lib = { version = "0.28" }
winit_input_helper = "0.16"
rapier2d = { version = "0.21.0", features = ["default", "debug-render"] }
rapier2d = { version = "0.22", features = ["default", "debug-render"] }
rand = "0.8"
glam = "0.29"

[lints.clippy]
blocks_in_conditions = "allow"
field_reassign_with_default = "allow"
self_named_constructors = "allow"

[profile.dev]
opt-level = 3
Expand Down Expand Up @@ -100,15 +97,6 @@ required-features = ["egui_gui", "egui_demo"]
name = "Egui Gui"
description = "Example that runs an egui demo app"

[[example]]
name = "fluid_sim"
path = "examples/fluid_sim/main.rs"
required-features = ["egui_gui"]

[package.metadata.example.fluid_sim]
name = "Fluid Sim"
description = "Example that runs a fluid simulation using glass"

[[example]]
name = "lines"
path = "examples/lines.rs"
Expand All @@ -121,7 +109,7 @@ description = "Example that draws lines"
[[example]]
name = "sand"
path = "examples/sand/main.rs"
required-features = ["egui_gui"]
required-features = []

[package.metadata.example.sand]
name = "Sand Sim"
Expand Down
36 changes: 6 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Glass

![Apache](https://img.shields.io/badge/license-Apache-blue.svg)
![CI](https://github.com/hakolao/glass/workflows/CI/badge.svg)

Expand All @@ -11,48 +12,23 @@ to skip annoying _wgpu_ boilerplate, _winit_ boilerplate and _window_ organizati
render or compute pipelines and organize your app how you like.

Example:

```rust
fn main() {
Glass::new(MyApp, GlassConfig::default()).run();
// Or if you want to avoid Device dependent Option<T> types within your app
Glass::new_and_run(GlassConfig::default(), |e, context| MyApp::new(context));
Glass::run(GlassConfig::default(), |_| Box::new(YourApp))
}

// Organize your app in anyway you like
struct MyApp;
struct YourApp;

// Implement methods that you need (to render or read inputs)
impl GlassApp for MyApp {}
impl GlassApp for YourApp {}
```

See `example` folder for more.

```rust
pub trait GlassApp {
/// Run at start
fn start(&mut self, _event_loop: &EventLoop<()>, _context: &mut GlassContext) {}
/// Run on each event received from winit
fn input(
&mut self,
_context: &mut GlassContext,
_event_loop: &EventLoopWindowTarget<()>,
_event: &Event<()>,
) {
}
/// Run each frame
fn update(&mut self, _context: &mut GlassContext) {}
/// Run each frame for each window after update
fn render(&mut self, _context: &GlassContext, _render_data: RenderData) {}
/// Run each frame for each window after post processing
fn after_render(&mut self, _context: &GlassContext) {}
/// Run each frame last
fn end_of_frame(&mut self, _context: &mut GlassContext) {}
/// Run at exit
fn end(&mut self, _context: &mut GlassContext) {}
}
```

# For whom

- People who want to learn rendering
- People annoyed at complexities of game engines, and wanting to have more control over their app
- People who wish to go back to the roots of coding (simplicity, and no magic)
75 changes: 43 additions & 32 deletions examples/egui_gui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,29 @@ use glass::{
window::GlassWindow, Glass, GlassApp, GlassConfig, GlassContext, GlassError, RenderData,
};
use wgpu::{CommandBuffer, CommandEncoder, StoreOp, TextureView};
use winit::{event::Event, event_loop::EventLoopWindowTarget};
use winit::{event::WindowEvent, event_loop::ActiveEventLoop, window::WindowId};

fn main() -> Result<(), GlassError> {
Glass::new_and_run(GlassConfig::default(), |event_loop, context| {
Glass::run(GlassConfig::default(), |_| {
Box::new(GuiApp {
gui: GuiState::new(event_loop, context),
gui: None,
})
})
}

impl GlassApp for GuiApp {
fn input(
fn start(&mut self, event_loop: &ActiveEventLoop, context: &mut GlassContext) {
self.gui = Some(GuiState::new(event_loop, context));
}

fn window_input(
&mut self,
context: &mut GlassContext,
_event_loop: &EventLoopWindowTarget<()>,
event: &Event<()>,
_event_loop: &ActiveEventLoop,
window_id: WindowId,
event: &WindowEvent,
) {
update_egui_with_winit_event(self, context, event);
update_egui_with_winit_event(self, context, window_id, event);
}

fn render(
Expand All @@ -36,7 +41,7 @@ impl GlassApp for GuiApp {
}

struct GuiApp {
gui: GuiState,
gui: Option<GuiState>,
}

struct GuiState {
Expand All @@ -48,21 +53,23 @@ struct GuiState {
}

impl GuiState {
fn new(event_loop: &EventLoopWindowTarget<()>, context: &mut GlassContext) -> GuiState {
fn new(event_loop: &ActiveEventLoop, context: &mut GlassContext) -> GuiState {
let ctx = egui::Context::default();
let pixels_per_point = context.primary_render_window().window().scale_factor() as f32;
let egui_winit = egui_winit::State::new(
ctx.clone(),
ViewportId::ROOT,
event_loop,
Some(pixels_per_point),
None,
Some(context.device().limits().max_texture_dimension_2d as usize),
);
let renderer = egui_wgpu::Renderer::new(
context.device(),
GlassWindow::default_surface_format(),
None,
1,
true,
);
GuiState {
egui_ctx: ctx,
Expand All @@ -74,27 +81,27 @@ impl GuiState {
}
}

fn update_egui_with_winit_event(app: &mut GuiApp, context: &mut GlassContext, event: &Event<()>) {
match event {
Event::WindowEvent {
window_id,
event,
..
} => {
let gui = &mut app.gui;
if let Some(window) = context.render_window(*window_id) {
let EventResponse {
consumed,
repaint,
} = gui.egui_winit.on_window_event(window.window(), event);
gui.repaint = repaint;
// Skip input if event was consumed by egui
if consumed {
return;
}
}
fn update_egui_with_winit_event(
app: &mut GuiApp,
context: &mut GlassContext,
window_id: WindowId,
event: &WindowEvent,
) {
let gui = &mut app.gui;
if let Some(window) = context.render_window(window_id) {
let EventResponse {
consumed,
repaint,
} = gui
.as_mut()
.unwrap()
.egui_winit
.on_window_event(window.window(), event);
gui.as_mut().unwrap().repaint = repaint;
// Skip input if event was consumed by egui
if consumed {
return;
}
_ => {}
}
}

Expand Down Expand Up @@ -124,7 +131,7 @@ fn render_egui(
egui_winit,
ui_app,
..
} = &mut app.gui;
} = &mut app.gui.as_mut().unwrap();
let raw_input = egui_winit.take_egui_input(window.window());
let FullOutput {
shapes,
Expand Down Expand Up @@ -162,7 +169,7 @@ fn render_egui(

// Render
{
let mut render_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
let render_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: None,
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
view: &view,
Expand All @@ -178,7 +185,11 @@ fn render_egui(
});
// Here you would render your scene
// Render Egui
renderer.render(&mut render_pass, &*clipped_primitives, &screen_descriptor);
renderer.render(
&mut render_pass.forget_lifetime(),
&*clipped_primitives,
&screen_descriptor,
);
}

for id in &textures_delta.free {
Expand Down
Loading
Loading