Skip to content

Commit 0087610

Browse files
authored
refactor!: update render dependencies & cleanup the code (#70)
* chore: bump render deps versions * refactor: replace EventLoopWindowTarget by ActiveEventLoop * refactor: remove wasm32 target code * refactor: update window init using deprecated method I will further update it later, this is just enough for the crate to compile * refactor: add default compilation option to the state creation * feat: add new basic structures based on rust-windowing/winit#3626 (comment) * fix: revert an IDE-induced rename * fix: revert idem * feat: complete GfxState constructor * refactor: make GfxState::new synchronous using pollster * feat: impl ApplicationHandler for App * feat: add necessary update methods to gfx state * fix: add handle vertex building code * chore: cleanup imports & re-exports * refactor: move shader_data & camera modules * fix: update render examples * chore: add cusotm lints to honeycomb-render * fix: process some lints * fix: add back the possibility to run default examples
1 parent 6aea900 commit 0087610

19 files changed

+414
-457
lines changed

Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ bytemuck = "1.15.0"
4444
cgmath = "0.18.0"
4545
env_logger = "0.11.3"
4646
pollster = "0.3.0"
47-
smaa = "0.13.0"
48-
wgpu = "0.19.4"
49-
winit = "0.29.15"
47+
smaa = "0.14.0"
48+
wgpu = "0.20.0"
49+
winit = "0.30.0"
5050

5151
[profile.bench]
5252
debug = true
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
use honeycomb_render::{RenderParameters, Runner, SmaaMode};
1+
use honeycomb_render::{launch, RenderParameters, SmaaMode};
22

33
fn main() {
44
let render_params = RenderParameters {
55
smaa_mode: SmaaMode::Disabled,
66
..Default::default()
77
};
8-
Runner::default().run::<f32>(render_params, None);
8+
launch::<f32>(render_params, None);
99
}

honeycomb-examples/examples/render/default_smaa1x.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ fn main() {
55
smaa_mode: SmaaMode::Smaa1X,
66
..Default::default()
77
};
8-
Runner::default().run::<f32>(render_params, None);
8+
launch::<f32>(render_params, None);
99
}

honeycomb-examples/examples/render/splitsquaremap.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ fn main() {
77
..Default::default()
88
};
99
let map: CMap2<f32> = GridBuilder::split_unit_squares(4).build2().unwrap();
10-
Runner::default().run(render_params, Some(&map));
10+
launch(render_params, Some(&map));
1111
}

honeycomb-examples/examples/render/squaremap.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ fn main() {
77
..Default::default()
88
};
99
let map: CMap2<f32> = GridBuilder::unit_squares(4).build2().unwrap();
10-
Runner::default().run(render_params, Some(&map));
10+
launch(render_params, Some(&map));
1111
}

honeycomb-examples/examples/render/squaremap_parameterized.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ fn main() {
1212
.len_per_cell_y(3.0_f64)
1313
.build2()
1414
.unwrap();
15-
Runner::default().run(render_params, Some(&map));
15+
launch(render_params, Some(&map));
1616
}

honeycomb-examples/examples/render/squaremap_shift.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,5 @@ fn main() {
6060
let elapsed = now.elapsed();
6161
println!("I: Finished shifting in {}μs", elapsed.as_micros());
6262

63-
Runner::default().run(render_params, Some(&map));
63+
launch(render_params, Some(&map));
6464
}

honeycomb-examples/examples/render/squaremap_split_diff.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,5 @@ fn main() {
5757
let elapsed = now.elapsed();
5858
println!("I: Finished splitting in {}μs", elapsed.as_micros());
5959

60-
Runner::default().run(render_params, Some(&map));
60+
launch(render_params, Some(&map));
6161
}

honeycomb-examples/examples/render/squaremap_split_some.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,5 @@ fn main() {
5555
let elapsed = now.elapsed();
5656
println!("I: Finished splitting in {}μs", elapsed.as_micros());
5757

58-
Runner::default().run(render_params, Some(&map));
58+
launch(render_params, Some(&map));
5959
}

honeycomb-render/Cargo.toml

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@ keywords.workspace = true
1212
authors.workspace = true
1313
publish = true
1414

15-
[lib]
16-
crate-type = ["cdylib", "rlib"]
17-
1815
[dependencies]
1916
bytemuck = { workspace = true, features = ["derive"] }
2017
cfg-if.workspace = true
@@ -25,11 +22,3 @@ pollster.workspace = true
2522
smaa.workspace = true
2623
wgpu.workspace = true
2724
winit.workspace = true
28-
29-
[target.'cfg(target_arch = "wasm32")'.dependencies]
30-
console_error_panic_hook = "0.1.6"
31-
console_log = "1.0"
32-
wasm-bindgen = "0.2"
33-
wasm-bindgen-futures = "0.4"
34-
web-sys = { version = "0.3", features = ["Document", "Window", "Element"] }
35-
wgpu = { workspace = true, features = ["webgl"] }

honeycomb-render/src/handle.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,15 @@
55
66
// ------ IMPORTS
77

8-
use crate::representations::intermediates::{Entity, IntermediateFace};
9-
use crate::shader_data::Coords2Shader;
10-
use crate::SmaaMode;
8+
use crate::{
9+
representations::{
10+
intermediates::{Entity, IntermediateFace},
11+
shader_data::Coords2Shader,
12+
},
13+
MapRef, SmaaMode,
14+
};
1115
use honeycomb_core::{
12-
CMap2, CoordsFloat, DartIdentifier, EdgeIdentifier, Orbit2, OrbitPolicy, Vertex2, NULL_DART_ID,
16+
CoordsFloat, DartIdentifier, EdgeIdentifier, Orbit2, OrbitPolicy, Vertex2, NULL_DART_ID,
1317
};
1418

1519
// ------ CONTENT
@@ -18,6 +22,7 @@ use honeycomb_core::{
1822
///
1923
/// The user can easily adjust parameters in this structure to obtain
2024
/// the preferred output style.
25+
#[derive(Clone, Copy)]
2126
pub struct RenderParameters {
2227
/// Anti-aliasing configuration.
2328
pub smaa_mode: SmaaMode,
@@ -44,7 +49,7 @@ impl Default for RenderParameters {
4449
}
4550

4651
pub struct CMap2RenderHandle<'a, T: CoordsFloat> {
47-
handle: &'a CMap2<T>,
52+
handle: MapRef<'a, T>,
4853
params: RenderParameters,
4954
intermediate_buffer: Vec<IntermediateFace<T>>,
5055
dart_construction_buffer: Vec<Coords2Shader>,
@@ -54,7 +59,7 @@ pub struct CMap2RenderHandle<'a, T: CoordsFloat> {
5459
}
5560

5661
impl<'a, T: CoordsFloat> CMap2RenderHandle<'a, T> {
57-
pub fn new(map: &'a CMap2<T>, params: Option<RenderParameters>) -> Self {
62+
pub fn new(map: MapRef<'a, T>, params: Option<RenderParameters>) -> Self {
5863
Self {
5964
handle: map,
6065
params: params.unwrap_or_default(),

honeycomb-render/src/lib.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//!
33
//! This crate implements a runner that can be used to display combinatorial maps.
44
//!
5-
//! It currently only supports 2D maps as the core library only implements these (as [CMap2])
5+
//! It currently only supports 2D maps as the core library only implements these (as [`honeycomb_core::CMap2`])
66
//!
77
//! ## Key bindings
88
//!
@@ -14,21 +14,33 @@
1414
//!
1515
//! Examples are available in the **honeycomb-examples** crate.
1616
17-
#[cfg(doc)]
18-
use honeycomb_core::CMap2;
17+
// ------ CUSTOM LINTS
18+
19+
// more lints
20+
#![warn(clippy::pedantic)]
21+
#![warn(missing_docs)]
22+
// some exceptions
23+
#![allow(clippy::module_name_repetitions)]
24+
#![allow(clippy::too_many_lines)]
25+
#![allow(clippy::cast_possible_truncation)]
26+
#![allow(clippy::cast_sign_loss)]
27+
#![allow(clippy::cast_precision_loss)]
1928

2029
// ------ MODULE DECLARATIONS
2130

22-
mod camera;
2331
mod handle;
2432
mod runner;
25-
mod shader_data;
2633
mod state;
2734

2835
mod representations;
2936

3037
// ------ RE-EXPORTS
3138

3239
pub use handle::RenderParameters;
33-
pub use runner::Runner;
40+
pub use runner::{launch, launch_async};
3441
pub use state::SmaaMode;
42+
43+
// ------ CONTENT
44+
45+
/// Convenience type alias
46+
type MapRef<'a, T> = &'a honeycomb_core::CMap2<T>;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
pub mod intermediates;
2+
pub mod shader_data;

honeycomb-render/src/shader_data.rs renamed to honeycomb-render/src/representations/shader_data.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
66
// ------ IMPORTS
77

8-
use crate::representations::intermediates::Entity;
98
use bytemuck::{Pod, Zeroable};
9+
10+
use crate::representations::intermediates::Entity;
1011
use honeycomb_core::{CoordsFloat, Vertex2};
1112

1213
// ------ CONTENT

honeycomb-render/src/runner.rs

Lines changed: 34 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -4,145 +4,46 @@
44
55
// ------ IMPORTS
66

7-
use winit::event::{Event, WindowEvent};
87
use winit::event_loop::EventLoop;
9-
use winit::window::Window;
108

11-
use crate::state::State;
12-
use crate::RenderParameters;
13-
use honeycomb_core::{CMap2, CoordsFloat};
9+
use crate::{state::App, MapRef, RenderParameters};
10+
use honeycomb_core::CoordsFloat;
1411

1512
// ------ CONTENT
1613

17-
cfg_if::cfg_if! {
18-
if #[cfg(target_arch = "wasm32")] {
19-
pub type MapRef<'a, T> = &'static CMap2<T>;
20-
} else {
21-
pub type MapRef<'a, T> = &'a CMap2<T>;
22-
}
14+
#[allow(clippy::missing_panics_doc)]
15+
/// Main rendering function.
16+
///
17+
/// # Arguments
18+
///
19+
/// - `render_params: RenderParameters` -- Render parameterization.
20+
/// - `map: Option<&CMap2>` -- Optional reference to the map that should be rendered
21+
///
22+
/// If no reference is passed to the method, a hardcoded example will be rendered instead.
23+
///
24+
/// # Example
25+
///
26+
/// Because the renderer uses the core and utils crates, examples are provided as standalone
27+
/// files rather than in the doc. You can find them in the project's repository, under the
28+
/// `honeycomb-examples` crate and run them using `cargo`:
29+
///
30+
/// ```shell
31+
/// cargo run --example <EXAMPLE>
32+
/// ```
33+
///
34+
pub fn launch<T: CoordsFloat>(render_params: RenderParameters, map: Option<MapRef<'_, T>>) {
35+
// enable custom env logging
36+
env_logger::init();
37+
// build app & event loop
38+
let mut app = App::new(render_params, map);
39+
let event_loop = EventLoop::new().unwrap();
40+
let _ = event_loop.run_app(&mut app);
2341
}
2442

25-
const TARGET_FPS: f32 = 240.;
26-
27-
/// This yields an approximate 240 FPS
28-
const MS_PER_FRAME: u128 = (1000. / TARGET_FPS) as u128;
29-
30-
async fn inner<T: CoordsFloat>(
31-
event_loop: EventLoop<()>,
32-
window: Window,
33-
render_params: RenderParameters,
34-
map: Option<MapRef<'_, T>>,
43+
/// UNIMPLEMENTED
44+
pub async fn launch_async<T: CoordsFloat>(
45+
_render_params: RenderParameters,
46+
_map: Option<MapRef<'_, T>>,
3547
) {
36-
let mut state = if let Some(val) = map {
37-
State::new(&window, render_params, val).await
38-
} else {
39-
State::new_test(&window, render_params).await
40-
};
41-
println!("I: Press F1 to quit");
42-
event_loop
43-
.run(move |event, target| {
44-
// process events
45-
match event {
46-
Event::WindowEvent {
47-
window_id,
48-
event: wevent,
49-
} => {
50-
if window_id == state.window().id() && !state.input(&wevent, target) {
51-
match wevent {
52-
WindowEvent::Resized(new_size) => state.resize(Some(new_size)),
53-
WindowEvent::RedrawRequested => {
54-
let start = std::time::Instant::now();
55-
state.update();
56-
match state.render() {
57-
Ok(_) => {}
58-
Err(wgpu::SurfaceError::Lost) => state.resize(None),
59-
Err(wgpu::SurfaceError::OutOfMemory) => target.exit(), // kill if OOM
60-
Err(e) => eprintln!("{:?}", e),
61-
};
62-
// put a hard cap on the rendering speed
63-
std::thread::sleep(std::time::Duration::from_millis(
64-
(MS_PER_FRAME - start.elapsed().as_millis()) as u64,
65-
));
66-
}
67-
WindowEvent::CloseRequested => target.exit(),
68-
_ => {}
69-
};
70-
}
71-
}
72-
Event::AboutToWait => {
73-
state.window().request_redraw();
74-
}
75-
_ => {}
76-
}
77-
})
78-
.unwrap();
79-
}
80-
81-
/// Main rendering structure
82-
pub struct Runner {
83-
event_loop: EventLoop<()>,
84-
window: Window,
85-
}
86-
87-
impl Runner {
88-
/// Main rendering method.
89-
///
90-
/// # Arguments
91-
///
92-
/// - `render_params: RenderParameters` -- Render parameterization.
93-
/// - `map: Option<&CMap2>` -- Optionnal reference to the map that should be rendered
94-
///
95-
/// If no reference is passed to the method, a hardcoded example will be rendered instead.
96-
///
97-
/// # Example
98-
///
99-
/// Because the renderer uses the core and utils crates, examples are provided as standalone
100-
/// files rather than in the doc. You can run them using the following command:
101-
///
102-
/// ```shell
103-
/// cargo run --example <EXAMPLE>
104-
/// ```
105-
///
106-
pub fn run<T: CoordsFloat>(self, render_params: RenderParameters, map: Option<MapRef<'_, T>>) {
107-
cfg_if::cfg_if! {
108-
if #[cfg(target_arch = "wasm32")] {
109-
std::panic::set_hook(Box::new(console_error_panic_hook::hook));
110-
console_log::init().expect("could not initialize logger");
111-
wasm_bindgen_futures::spawn_local(inner(self.event_loop, self.window, render_params, map));
112-
} else {
113-
env_logger::init();
114-
pollster::block_on(inner(self.event_loop, self.window, render_params, map));
115-
}
116-
}
117-
}
118-
119-
/// UNIMPLEMENTED
120-
pub async fn run_async(&self) {
121-
unimplemented!()
122-
}
123-
}
124-
125-
impl Default for Runner {
126-
fn default() -> Self {
127-
let event_loop = EventLoop::new().unwrap();
128-
#[allow(unused_mut)]
129-
let mut builder = winit::window::WindowBuilder::new();
130-
#[cfg(target_arch = "wasm32")]
131-
{
132-
use wasm_bindgen::JsCast;
133-
use winit::platform::web::WindowBuilderExtWebSys;
134-
let canvas = web_sys::window()
135-
.unwrap()
136-
.document()
137-
.unwrap()
138-
.get_element_by_id("wasm-example")
139-
.unwrap()
140-
.dyn_into::<web_sys::HtmlCanvasElement>()
141-
.unwrap();
142-
builder = builder.with_canvas(Some(canvas));
143-
}
144-
let window = builder.build(&event_loop).unwrap();
145-
146-
Self { event_loop, window }
147-
}
48+
unimplemented!()
14849
}

0 commit comments

Comments
 (0)