From 9adaead25d931428bae14383fbb44ec5577946af Mon Sep 17 00:00:00 2001 From: Alex Butler Date: Tue, 24 Oct 2023 00:35:40 +0100 Subject: [PATCH 1/2] Update dev-dependencies --- gfx-glyph/Cargo.toml | 8 +- gfx-glyph/examples/depth.rs | 148 +++++++------- gfx-glyph/examples/paragraph.rs | 243 +++++++++++------------ gfx-glyph/examples/performance.rs | 165 ++++++++------- gfx-glyph/examples/pre_positioned.rs | 98 ++++----- gfx-glyph/examples/varied.rs | 242 +++++++++++----------- glyph-brush/Cargo.toml | 6 +- glyph-brush/examples/draw_cache_guts.rs | 254 ++++++++++++------------ glyph-brush/examples/opengl.rs | 247 +++++++++++------------ 9 files changed, 706 insertions(+), 705 deletions(-) diff --git a/gfx-glyph/Cargo.toml b/gfx-glyph/Cargo.toml index 1e857c3..237262d 100644 --- a/gfx-glyph/Cargo.toml +++ b/gfx-glyph/Cargo.toml @@ -20,8 +20,8 @@ log = "0.4" cgmath = "0.18" env_logger = { version = "0.10", default-features = false } gfx_device_gl = "0.16" -glutin = "0.30.3" -glutin-winit = "0.3" -old_school_gfx_glutin_ext = "0.31" +glutin = "0.31" +glutin-winit = "0.4" +old_school_gfx_glutin_ext = { path = "../../old-school-gfx-glutin-ext" } spin_sleep = "1" -winit = "0.28" +winit = "0.29" diff --git a/gfx-glyph/examples/depth.rs b/gfx-glyph/examples/depth.rs index 49196ad..fe10d4b 100644 --- a/gfx-glyph/examples/depth.rs +++ b/gfx-glyph/examples/depth.rs @@ -10,14 +10,16 @@ use glutin_winit::GlWindow; use init::init_example; use std::error::Error; use winit::{ - event::{Event, KeyboardInput, VirtualKeyCode, WindowEvent}, + event::{Event, KeyEvent, WindowEvent}, event_loop::ControlFlow, + keyboard::{Key, NamedKey}, }; fn main() -> Result<(), Box> { init_example("depth"); - let event_loop = winit::event_loop::EventLoop::new(); + let event_loop = winit::event_loop::EventLoop::new()?; + event_loop.set_control_flow(ControlFlow::Poll); let title = "gfx_glyph example - resize to see multi-text layout"; let window_builder = winit::window::WindowBuilder::new() .with_title(title) @@ -50,87 +52,89 @@ fn main() -> Result<(), Box> { let mut loop_helper = spin_sleep::LoopHelper::builder().build_with_target_rate(250.0); let mut view_size = window.inner_size(); - event_loop.run(move |event, _, control_flow| { - *control_flow = ControlFlow::Poll; - + event_loop.run(move |event, elwt| { match event { + Event::AboutToWait => window.request_redraw(), Event::WindowEvent { event, .. } => match event { - WindowEvent::KeyboardInput { - input: - KeyboardInput { - virtual_keycode: Some(VirtualKeyCode::Escape), + WindowEvent::CloseRequested + | WindowEvent::KeyboardInput { + event: + KeyEvent { + logical_key: Key::Named(NamedKey::Escape), .. }, .. - } - | WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit, - _ => (), - }, - Event::MainEventsCleared => { - // handle resizes - let w_size = window.inner_size(); - if view_size != w_size { - window.resize_surface(&gl_surface, &gl_context); - old_school_gfx_glutin_ext::resize_views( - w_size, - &mut color_view, - &mut depth_view, + } => elwt.exit(), + WindowEvent::RedrawRequested => { + // handle resizes + let w_size = window.inner_size(); + if view_size != w_size { + window.resize_surface(&gl_surface, &gl_context); + old_school_gfx_glutin_ext::resize_views( + w_size, + &mut color_view, + &mut depth_view, + ); + view_size = w_size; + } + + encoder.clear(&color_view, [0.02, 0.02, 0.02, 1.0]); + encoder.clear_depth(&depth_view, 1.0); + + let (width, height) = (w_size.width as f32, w_size.height as f32); + + // first section is queued, and therefore drawn, first with lower z + glyph_brush.queue( + Section::default() + .add_text( + Text::new("On top") + .with_scale(95.0) + .with_color([0.8, 0.8, 0.8, 1.0]) + .with_z(0.2) + .with_font_id(italic_font), + ) + .with_screen_position((width / 2.0, 100.0)) + .with_bounds((width, height - 100.0)) + .with_layout(Layout::default().h_align(HorizontalAlign::Center)), ); - view_size = w_size; - } - encoder.clear(&color_view, [0.02, 0.02, 0.02, 1.0]); - encoder.clear_depth(&depth_view, 1.0); - - let (width, height) = (w_size.width as f32, w_size.height as f32); - - // first section is queued, and therefore drawn, first with lower z - glyph_brush.queue( - Section::default() - .add_text( - Text::new("On top") - .with_scale(95.0) - .with_color([0.8, 0.8, 0.8, 1.0]) - .with_z(0.2) - .with_font_id(italic_font), - ) - .with_screen_position((width / 2.0, 100.0)) - .with_bounds((width, height - 100.0)) - .with_layout(Layout::default().h_align(HorizontalAlign::Center)), - ); - - // 2nd section is drawn last but with higher z, - // draws are subject to depth testing - glyph_brush.queue( - Section::default() - .add_text( - Text::new(&include_str!("lipsum.txt").replace("\n\n", "").repeat(10)) + // 2nd section is drawn last but with higher z, + // draws are subject to depth testing + glyph_brush.queue( + Section::default() + .add_text( + Text::new( + &include_str!("lipsum.txt").replace("\n\n", "").repeat(10), + ) .with_scale(30.0) .with_color([0.05, 0.05, 0.1, 1.0]) .with_z(1.0), - ) - .with_bounds((width, height)), - ); - - glyph_brush - .use_queue() - // Enable depth testing with default less-equal drawing and update the depth buffer - .depth_target(&depth_view) - .draw(&mut encoder, &color_view) - .unwrap(); - - encoder.flush(&mut device); - gl_surface.swap_buffers(&gl_context).unwrap(); - device.cleanup(); - - if let Some(rate) = loop_helper.report_rate() { - window.set_title(&format!("{title} - {rate:.0} FPS")); - } + ) + .with_bounds((width, height)), + ); - loop_helper.loop_sleep(); - loop_helper.loop_start(); - } + glyph_brush + .use_queue() + // Enable depth testing with default less-equal drawing and update the depth buffer + .depth_target(&depth_view) + .draw(&mut encoder, &color_view) + .unwrap(); + + encoder.flush(&mut device); + gl_surface.swap_buffers(&gl_context).unwrap(); + device.cleanup(); + + if let Some(rate) = loop_helper.report_rate() { + window.set_title(&format!("{title} - {rate:.0} FPS")); + } + + loop_helper.loop_sleep(); + loop_helper.loop_start(); + } + _ => (), + }, _ => (), } - }); + })?; + Ok(()) } diff --git a/gfx-glyph/examples/paragraph.rs b/gfx-glyph/examples/paragraph.rs index 605f23c..980e46e 100644 --- a/gfx-glyph/examples/paragraph.rs +++ b/gfx-glyph/examples/paragraph.rs @@ -23,11 +23,9 @@ use std::{ io::{self, Write}, }; use winit::{ - event::{ - ElementState, Event, KeyboardInput, ModifiersState, MouseScrollDelta, VirtualKeyCode, - WindowEvent, - }, + event::{ElementState, Event, KeyEvent, Modifiers, MouseScrollDelta, WindowEvent}, event_loop::ControlFlow, + keyboard::{Key, NamedKey}, }; const MAX_FONT_SIZE: f32 = 2000.0; @@ -35,7 +33,8 @@ const MAX_FONT_SIZE: f32 = 2000.0; fn main() -> Result<(), Box> { init_example("paragraph"); - let event_loop = winit::event_loop::EventLoop::new(); + let event_loop = winit::event_loop::EventLoop::new()?; + event_loop.set_control_flow(ControlFlow::Poll); let title = "gfx_glyph example - scroll to size, type to modify, ctrl-scroll \ to gpu zoom, ctrl-shift-scroll to gpu rotate"; let window_builder = winit::window::WindowBuilder::new() @@ -70,45 +69,39 @@ fn main() -> Result<(), Box> { let mut loop_helper = spin_sleep::LoopHelper::builder().build_with_target_rate(250.0); let mut view_size = window.inner_size(); - let mut modifiers = ModifiersState::default(); - - event_loop.run(move |event, _, control_flow| { - *control_flow = ControlFlow::Poll; + let mut modifiers = Modifiers::default(); + event_loop.run(move |event, elwt| { match event { + Event::AboutToWait => window.request_redraw(), Event::WindowEvent { event, .. } => match event { WindowEvent::ModifiersChanged(new_mods) => modifiers = new_mods, + WindowEvent::CloseRequested => elwt.exit(), WindowEvent::KeyboardInput { - input: - KeyboardInput { - virtual_keycode: Some(VirtualKeyCode::Escape), - .. - }, - .. - } - | WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit, - WindowEvent::KeyboardInput { - input: - KeyboardInput { + event: + KeyEvent { + logical_key, state: ElementState::Pressed, - virtual_keycode: Some(VirtualKeyCode::Back), .. }, .. - } => { - text.pop(); - } - WindowEvent::ReceivedCharacter(c) => { - if c != '\u{7f}' && c != '\u{8}' { - text.push(c); + } => match logical_key { + Key::Named(NamedKey::Escape) => elwt.exit(), + Key::Named(NamedKey::Backspace) => { + text.pop(); } - } + key => { + if let Some(str) = key.to_text() { + text.push_str(str); + } + } + }, WindowEvent::MouseWheel { delta: MouseScrollDelta::LineDelta(_, y), .. } => { - let ctrl = modifiers.ctrl(); - let shift = modifiers.shift(); + let ctrl = modifiers.state().control_key(); + let shift = modifiers.state().shift_key(); if ctrl && shift { if y > 0.0 { angle += 0.02 * PI32; @@ -152,117 +145,117 @@ fn main() -> Result<(), Box> { } } } - _ => (), - }, - Event::MainEventsCleared => { - // handle resizes - let w_size = window.inner_size(); - if view_size != w_size { - window.resize_surface(&gl_surface, &gl_context); - old_school_gfx_glutin_ext::resize_views( - w_size, - &mut color_view, - &mut depth_view, - ); - view_size = w_size; - } - - encoder.clear(&color_view, [0.02, 0.02, 0.02, 1.0]); - - let (width, height, ..) = color_view.get_dimensions(); - let (width, height) = (f32::from(width), f32::from(height)); - let scale = font_size * window.scale_factor() as f32; + WindowEvent::RedrawRequested => { + // handle resizes + let w_size = window.inner_size(); + if view_size != w_size { + window.resize_surface(&gl_surface, &gl_context); + old_school_gfx_glutin_ext::resize_views( + w_size, + &mut color_view, + &mut depth_view, + ); + view_size = w_size; + } - // The section is all the info needed for the glyph brush to render a 'section' of text. - let section = gfx_glyph::Section::default() - .add_text( - Text::new(&text) - .with_scale(scale) - .with_color([0.9, 0.3, 0.3, 1.0]), - ) - .with_bounds((width / 3.15, height)); + encoder.clear(&color_view, [0.02, 0.02, 0.02, 1.0]); - // Adds a section & layout to the queue for the next call to `use_queue().draw(..)`, - // this can be called multiple times for different sections that want to use the - // same font and gpu cache. - // This step computes the glyph positions, this is cached to avoid unnecessary - // recalculation. - glyph_brush.queue(§ion); + let (width, height, ..) = color_view.get_dimensions(); + let (width, height) = (f32::from(width), f32::from(height)); + let scale = font_size * window.scale_factor() as f32; - use gfx_glyph::*; - glyph_brush.queue( - Section::default() + // The section is all the info needed for the glyph brush to render a 'section' of text. + let section = gfx_glyph::Section::default() .add_text( Text::new(&text) .with_scale(scale) - .with_color([0.3, 0.9, 0.3, 1.0]), + .with_color([0.9, 0.3, 0.3, 1.0]), ) - .with_screen_position((width / 2.0, height / 2.0)) - .with_bounds((width / 3.15, height)) - .with_layout( - Layout::default() - .h_align(HorizontalAlign::Center) - .v_align(VerticalAlign::Center), - ), - ); + .with_bounds((width / 3.15, height)); - glyph_brush.queue( - Section::default() - .add_text( - Text::new(&text) - .with_scale(scale) - .with_color([0.3, 0.3, 0.9, 1.0]), - ) - .with_screen_position((width, height)) - .with_bounds((width / 3.15, height)) - .with_layout( - Layout::default() - .h_align(HorizontalAlign::Right) - .v_align(VerticalAlign::Bottom), - ), - ); + // Adds a section & layout to the queue for the next call to `use_queue().draw(..)`, + // this can be called multiple times for different sections that want to use the + // same font and gpu cache. + // This step computes the glyph positions, this is cached to avoid unnecessary + // recalculation. + glyph_brush.queue(§ion); - // Rotation - let offset = - Matrix4::from_translation(Vector3::new(-width / 2.0, -height / 2.0, 0.0)); - let rotation = offset.inverse_transform().unwrap() - * Matrix4::from_angle_z(Rad(angle)) - * offset; + use gfx_glyph::*; + glyph_brush.queue( + Section::default() + .add_text( + Text::new(&text) + .with_scale(scale) + .with_color([0.3, 0.9, 0.3, 1.0]), + ) + .with_screen_position((width / 2.0, height / 2.0)) + .with_bounds((width / 3.15, height)) + .with_layout( + Layout::default() + .h_align(HorizontalAlign::Center) + .v_align(VerticalAlign::Center), + ), + ); - // Default projection - let projection: Matrix4 = gfx_glyph::default_transform(&color_view).into(); + glyph_brush.queue( + Section::default() + .add_text( + Text::new(&text) + .with_scale(scale) + .with_color([0.3, 0.3, 0.9, 1.0]), + ) + .with_screen_position((width, height)) + .with_bounds((width / 3.15, height)) + .with_layout( + Layout::default() + .h_align(HorizontalAlign::Right) + .v_align(VerticalAlign::Bottom), + ), + ); - // Here an example transform is used as a cheap zoom out (controlled with ctrl-scroll) - let zoom = Matrix4::from_scale(zoom); + // Rotation + let offset = + Matrix4::from_translation(Vector3::new(-width / 2.0, -height / 2.0, 0.0)); + let rotation = offset.inverse_transform().unwrap() + * Matrix4::from_angle_z(Rad(angle)) + * offset; - // Combined transform - let transform = zoom * projection * rotation; + // Default projection + let projection: Matrix4 = gfx_glyph::default_transform(&color_view).into(); - // Finally once per frame you want to actually draw all the sections you've submitted - // with `queue` calls. - // - // Note: Drawing in the case the text is unchanged from the previous frame (a common case) - // is essentially free as the vertices are reused & gpu cache updating interaction - // can be skipped. - glyph_brush - .use_queue() - .transform(transform) - .draw(&mut encoder, &color_view) - .unwrap(); + // Here an example transform is used as a cheap zoom out (controlled with ctrl-scroll) + let zoom = Matrix4::from_scale(zoom); - encoder.flush(&mut device); - gl_surface.swap_buffers(&gl_context).unwrap(); - device.cleanup(); + // Combined transform + let transform = zoom * projection * rotation; - if let Some(rate) = loop_helper.report_rate() { - window.set_title(&format!("{title} - {rate:.0} FPS")); - } + // Finally once per frame you want to actually draw all the sections you've submitted + // with `queue` calls. + // + // Note: Drawing in the case the text is unchanged from the previous frame (a common case) + // is essentially free as the vertices are reused & gpu cache updating interaction + // can be skipped. + glyph_brush + .use_queue() + .transform(transform) + .draw(&mut encoder, &color_view) + .unwrap(); + + encoder.flush(&mut device); + gl_surface.swap_buffers(&gl_context).unwrap(); + device.cleanup(); + + if let Some(rate) = loop_helper.report_rate() { + window.set_title(&format!("{title} - {rate:.0} FPS")); + } - loop_helper.loop_sleep(); - loop_helper.loop_start(); - } - Event::LoopDestroyed => println!(), + loop_helper.loop_sleep(); + loop_helper.loop_start(); + } + _ => (), + }, _ => (), } - }); + })?; + Ok(()) } diff --git a/gfx-glyph/examples/performance.rs b/gfx-glyph/examples/performance.rs index 084bc7e..8b8ddfc 100644 --- a/gfx-glyph/examples/performance.rs +++ b/gfx-glyph/examples/performance.rs @@ -10,8 +10,9 @@ use glutin_winit::GlWindow; use init::init_example; use std::{env, error::Error}; use winit::{ - event::{ElementState, Event, KeyboardInput, MouseScrollDelta, VirtualKeyCode, WindowEvent}, + event::{ElementState, Event, KeyEvent, MouseScrollDelta, WindowEvent}, event_loop::ControlFlow, + keyboard::{Key, NamedKey}, }; const MAX_FONT_SIZE: f32 = 4000.0; @@ -27,7 +28,8 @@ fn main() -> Result<(), Box> { return Ok(()); } - let event_loop = winit::event_loop::EventLoop::new(); + let event_loop = winit::event_loop::EventLoop::new()?; + event_loop.set_control_flow(ControlFlow::Poll); let title = "gfx_glyph rendering 30,000 glyphs - scroll to size, type to modify"; let window_builder = winit::window::WindowBuilder::new() .with_title(title) @@ -58,36 +60,30 @@ fn main() -> Result<(), Box> { let mut loop_helper = spin_sleep::LoopHelper::builder().build_without_target_rate(); let mut view_size = window.inner_size(); - event_loop.run(move |event, _, control_flow| { - *control_flow = ControlFlow::Poll; - + event_loop.run(move |event, elwt| { match event { + Event::AboutToWait => window.request_redraw(), Event::WindowEvent { event, .. } => match event { + WindowEvent::CloseRequested => elwt.exit(), WindowEvent::KeyboardInput { - input: - KeyboardInput { - virtual_keycode: Some(VirtualKeyCode::Escape), - .. - }, - .. - } - | WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit, - WindowEvent::KeyboardInput { - input: - KeyboardInput { + event: + KeyEvent { + logical_key, state: ElementState::Pressed, - virtual_keycode: Some(VirtualKeyCode::Back), .. }, .. - } => { - text.pop(); - } - WindowEvent::ReceivedCharacter(c) => { - if c != '\u{7f}' && c != '\u{8}' { - text.push(c); + } => match logical_key { + Key::Named(NamedKey::Escape) => elwt.exit(), + Key::Named(NamedKey::Backspace) => { + text.pop(); } - } + key => { + if let Some(str) = key.to_text() { + text.push_str(str); + } + } + }, WindowEvent::MouseWheel { delta: MouseScrollDelta::LineDelta(_, y), .. @@ -100,68 +96,69 @@ fn main() -> Result<(), Box> { }; font_size = font_size.clamp(1.0, MAX_FONT_SIZE); } - _ => (), - }, - Event::MainEventsCleared => { - // handle resizes - let w_size = window.inner_size(); - if view_size != w_size { - window.resize_surface(&gl_surface, &gl_context); - old_school_gfx_glutin_ext::resize_views( - w_size, - &mut color_view, - &mut depth_view, - ); - view_size = w_size; - } + WindowEvent::RedrawRequested => { + // handle resizes + let w_size = window.inner_size(); + if view_size != w_size { + window.resize_surface(&gl_surface, &gl_context); + old_school_gfx_glutin_ext::resize_views( + w_size, + &mut color_view, + &mut depth_view, + ); + view_size = w_size; + } - encoder.clear(&color_view, [0.02, 0.02, 0.02, 1.0]); - - let (width, height, ..) = color_view.get_dimensions(); - let (width, height) = (f32::from(width), f32::from(height)); - let scale = PxScale::from(font_size * window.scale_factor() as f32); - - // The section is all the info needed for the glyph brush to render a 'section' of text. - let section = Section::default() - .add_text( - Text::new(&text) - .with_scale(scale) - .with_color([0.8, 0.8, 0.8, 1.0]), - ) - .with_bounds((width, height)) - .with_layout( - Layout::default().line_breaker(BuiltInLineBreaker::AnyCharLineBreaker), - ); - - // Adds a section & layout to the queue for the next call to `use_queue().draw(..)`, - // this can be called multiple times for different sections that want to use the - // same font and gpu cache. - // This step computes the glyph positions, this is cached to avoid unnecessary - // recalculation. - glyph_brush.queue(§ion); - - // Finally once per frame you want to actually draw all the sections you've - // submitted with `queue` calls. - // - // Note: Drawing in the case the text is unchanged from the previous frame - // (a common case) is essentially free as the vertices are reused & gpu cache - // updating interaction can be skipped. - glyph_brush - .use_queue() - .draw(&mut encoder, &color_view) - .unwrap(); - - encoder.flush(&mut device); - gl_surface.swap_buffers(&gl_context).unwrap(); - device.cleanup(); - - if let Some(rate) = loop_helper.report_rate() { - window.set_title(&format!("{title} - {rate:.0} FPS")); - } + encoder.clear(&color_view, [0.02, 0.02, 0.02, 1.0]); + + let (width, height, ..) = color_view.get_dimensions(); + let (width, height) = (f32::from(width), f32::from(height)); + let scale = PxScale::from(font_size * window.scale_factor() as f32); + + // The section is all the info needed for the glyph brush to render a 'section' of text. + let section = Section::default() + .add_text( + Text::new(&text) + .with_scale(scale) + .with_color([0.8, 0.8, 0.8, 1.0]), + ) + .with_bounds((width, height)) + .with_layout( + Layout::default().line_breaker(BuiltInLineBreaker::AnyCharLineBreaker), + ); + + // Adds a section & layout to the queue for the next call to `use_queue().draw(..)`, + // this can be called multiple times for different sections that want to use the + // same font and gpu cache. + // This step computes the glyph positions, this is cached to avoid unnecessary + // recalculation. + glyph_brush.queue(§ion); + + // Finally once per frame you want to actually draw all the sections you've + // submitted with `queue` calls. + // + // Note: Drawing in the case the text is unchanged from the previous frame + // (a common case) is essentially free as the vertices are reused & gpu cache + // updating interaction can be skipped. + glyph_brush + .use_queue() + .draw(&mut encoder, &color_view) + .unwrap(); + + encoder.flush(&mut device); + gl_surface.swap_buffers(&gl_context).unwrap(); + device.cleanup(); + + if let Some(rate) = loop_helper.report_rate() { + window.set_title(&format!("{title} - {rate:.0} FPS")); + } - loop_helper.loop_start(); - } + loop_helper.loop_start(); + } + _ => (), + }, _ => (), } - }); + })?; + Ok(()) } diff --git a/gfx-glyph/examples/pre_positioned.rs b/gfx-glyph/examples/pre_positioned.rs index 4d2021b..50e08c8 100644 --- a/gfx-glyph/examples/pre_positioned.rs +++ b/gfx-glyph/examples/pre_positioned.rs @@ -11,14 +11,16 @@ use glutin_winit::GlWindow; use init::init_example; use std::error::Error; use winit::{ - event::{Event, KeyboardInput, VirtualKeyCode, WindowEvent}, + event::{Event, KeyEvent, WindowEvent}, event_loop::ControlFlow, + keyboard::{Key, NamedKey}, }; fn main() -> Result<(), Box> { init_example("pre_positioned"); - let event_loop = winit::event_loop::EventLoop::new(); + let event_loop = winit::event_loop::EventLoop::new()?; + event_loop.set_control_flow(ControlFlow::Poll); let title = "gfx_glyph example"; let window_builder = winit::window::WindowBuilder::new() .with_title(title) @@ -64,63 +66,63 @@ fn main() -> Result<(), Box> { }], ); - event_loop.run(move |event, _, control_flow| { - *control_flow = ControlFlow::Poll; - + event_loop.run(move |event, elwt| { match event { + Event::AboutToWait => window.request_redraw(), Event::WindowEvent { event, .. } => match event { - WindowEvent::KeyboardInput { - input: - KeyboardInput { - virtual_keycode: Some(VirtualKeyCode::Escape), + WindowEvent::CloseRequested + | WindowEvent::KeyboardInput { + event: + KeyEvent { + logical_key: Key::Named(NamedKey::Escape), .. }, .. - } - | WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit, - _ => (), - }, - Event::MainEventsCleared => { - // handle resizes - let w_size = window.inner_size(); - if view_size != w_size { - window.resize_surface(&gl_surface, &gl_context); - old_school_gfx_glutin_ext::resize_views( - w_size, - &mut color_view, - &mut depth_view, + } => elwt.exit(), + WindowEvent::RedrawRequested => { + // handle resizes + let w_size = window.inner_size(); + if view_size != w_size { + window.resize_surface(&gl_surface, &gl_context); + old_school_gfx_glutin_ext::resize_views( + w_size, + &mut color_view, + &mut depth_view, + ); + view_size = w_size; + } + + encoder.clear(&color_view, [0.02, 0.02, 0.02, 1.0]); + + glyph_brush.queue_pre_positioned( + glyphs.clone(), + vec![Extra { color, z: 0.0 }], + Rect { + min: point(0.0, 0.0), + max: point(width, height), + }, ); - view_size = w_size; - } - - encoder.clear(&color_view, [0.02, 0.02, 0.02, 1.0]); - glyph_brush.queue_pre_positioned( - glyphs.clone(), - vec![Extra { color, z: 0.0 }], - Rect { - min: point(0.0, 0.0), - max: point(width, height), - }, - ); + glyph_brush + .use_queue() + .draw(&mut encoder, &color_view) + .unwrap(); - glyph_brush - .use_queue() - .draw(&mut encoder, &color_view) - .unwrap(); + encoder.flush(&mut device); + gl_surface.swap_buffers(&gl_context).unwrap(); + device.cleanup(); - encoder.flush(&mut device); - gl_surface.swap_buffers(&gl_context).unwrap(); - device.cleanup(); + if let Some(rate) = loop_helper.report_rate() { + window.set_title(&format!("{title} - {rate:.0} FPS")); + } - if let Some(rate) = loop_helper.report_rate() { - window.set_title(&format!("{title} - {rate:.0} FPS")); + loop_helper.loop_sleep(); + loop_helper.loop_start(); } - - loop_helper.loop_sleep(); - loop_helper.loop_start(); - } + _ => (), + }, _ => (), } - }); + })?; + Ok(()) } diff --git a/gfx-glyph/examples/varied.rs b/gfx-glyph/examples/varied.rs index 071af75..e00a127 100644 --- a/gfx-glyph/examples/varied.rs +++ b/gfx-glyph/examples/varied.rs @@ -14,14 +14,16 @@ use glutin_winit::GlWindow; use init::init_example; use std::error::Error; use winit::{ - event::{Event, KeyboardInput, VirtualKeyCode, WindowEvent}, + event::{Event, KeyEvent, WindowEvent}, event_loop::ControlFlow, + keyboard::{Key, NamedKey}, }; fn main() -> Result<(), Box> { init_example("varied"); - let event_loop = winit::event_loop::EventLoop::new(); + let event_loop = winit::event_loop::EventLoop::new()?; + event_loop.set_control_flow(ControlFlow::Poll); let title = "gfx_glyph example - resize to see multi-text layout"; let window_builder = winit::window::WindowBuilder::new() .with_title(title) @@ -61,141 +63,141 @@ fn main() -> Result<(), Box> { let mut loop_helper = spin_sleep::LoopHelper::builder().build_with_target_rate(250.0); let mut view_size = window.inner_size(); - event_loop.run(move |event, _, control_flow| { - *control_flow = ControlFlow::Poll; - + event_loop.run(move |event, elwt| { match event { + Event::AboutToWait => window.request_redraw(), Event::WindowEvent { event, .. } => match event { - WindowEvent::KeyboardInput { - input: - KeyboardInput { - virtual_keycode: Some(VirtualKeyCode::Escape), + WindowEvent::CloseRequested + | WindowEvent::KeyboardInput { + event: + KeyEvent { + logical_key: Key::Named(NamedKey::Escape), .. }, .. - } - | WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit, - _ => (), - }, - Event::MainEventsCleared => { - // handle resizes - let w_size = window.inner_size(); - if view_size != w_size { - window.resize_surface(&gl_surface, &gl_context); - old_school_gfx_glutin_ext::resize_views( - w_size, - &mut color_view, - &mut depth_view, - ); - view_size = w_size; - } - - encoder.clear(&color_view, [0.02, 0.02, 0.02, 1.0]); - - let (width, height, ..) = color_view.get_dimensions(); - let (width, height) = (f32::from(width), f32::from(height)); - - glyph_brush.queue(Section { - screen_position: (0.0, height / 2.0), - bounds: (width * 0.49, height), - text: vec![ - Text { - text: "Lorem ipsum dolor sit amet, ferri simul omittantur eam eu, ", - scale: PxScale::from(45.0), - font_id: sans_font, - extra: Extra { - color: [0.9, 0.3, 0.3, 1.0], - z: 0.0, + } => elwt.exit(), + WindowEvent::RedrawRequested => { + // handle resizes + let w_size = window.inner_size(); + if view_size != w_size { + window.resize_surface(&gl_surface, &gl_context); + old_school_gfx_glutin_ext::resize_views( + w_size, + &mut color_view, + &mut depth_view, + ); + view_size = w_size; + } + + encoder.clear(&color_view, [0.02, 0.02, 0.02, 1.0]); + + let (width, height, ..) = color_view.get_dimensions(); + let (width, height) = (f32::from(width), f32::from(height)); + + glyph_brush.queue(Section { + screen_position: (0.0, height / 2.0), + bounds: (width * 0.49, height), + text: vec![ + Text { + text: "Lorem ipsum dolor sit amet, ferri simul omittantur eam eu, ", + scale: PxScale::from(45.0), + font_id: sans_font, + extra: Extra { + color: [0.9, 0.3, 0.3, 1.0], + z: 0.0, + }, }, - }, - Text { - text: "dolorem", - scale: PxScale::from(150.0), - font_id: serif_font, - extra: Extra { - color: [0.3, 0.9, 0.3, 1.0], - z: 0.0, + Text { + text: "dolorem", + scale: PxScale::from(150.0), + font_id: serif_font, + extra: Extra { + color: [0.3, 0.9, 0.3, 1.0], + z: 0.0, + }, }, - }, - Text { - text: " Iriure vocibus est te, natum delicata dignissim pri ea.", - scale: PxScale::from(25.0), - font_id: sans_font, - extra: Extra { - color: [0.3, 0.3, 0.9, 1.0], - z: 0.0, + Text { + text: " Iriure vocibus est te, natum delicata dignissim pri ea.", + scale: PxScale::from(25.0), + font_id: sans_font, + extra: Extra { + color: [0.3, 0.3, 0.9, 1.0], + z: 0.0, + }, }, - }, - ], - layout: Layout::default().v_align(VerticalAlign::Center), - }); - - glyph_brush.queue(Section { - screen_position: (width, height / 2.0), - bounds: (width * 0.49, height), - text: vec![ - Text { - text: "foo += bar;", - scale: PxScale::from(45.0), - font_id: mono_font, - extra: Extra { - color: [0.3, 0.3, 0.9, 1.0], - z: 0.0, + ], + layout: Layout::default().v_align(VerticalAlign::Center), + }); + + glyph_brush.queue(Section { + screen_position: (width, height / 2.0), + bounds: (width * 0.49, height), + text: vec![ + Text { + text: "foo += bar;", + scale: PxScale::from(45.0), + font_id: mono_font, + extra: Extra { + color: [0.3, 0.3, 0.9, 1.0], + z: 0.0, + }, }, - }, - Text { - text: " eruditi habemus qualisque eam an. No atqui apeirian phaedrum pri ex, hinc omnes sapientem. ", - scale: PxScale::from(30.0), - font_id: italic_font, - extra: Extra { - color: [0.9, 0.3, 0.3, 1.0], - z: 0.0, + Text { + text: " eruditi habemus qualisque eam an. No atqui apeirian phaedrum pri ex, hinc omnes sapientem. ", + scale: PxScale::from(30.0), + font_id: italic_font, + extra: Extra { + color: [0.9, 0.3, 0.3, 1.0], + z: 0.0, + }, }, - }, - Text { - text: "Eu facilisi maluisset eos.", - scale: PxScale::from(55.0), - font_id: sans_font, - extra: Extra { - color: [0.3, 0.9, 0.3, 1.0], - z: 0.0, + Text { + text: "Eu facilisi maluisset eos.", + scale: PxScale::from(55.0), + font_id: sans_font, + extra: Extra { + color: [0.3, 0.9, 0.3, 1.0], + z: 0.0, + }, }, - }, - Text { - text: " ius nullam impetus. ", - scale: PxScale { x: 25.0, y: 45.0 }, - font_id: serif_font, - extra: Extra { - color: [0.9, 0.9, 0.3, 1.0], - z: 0.0, + Text { + text: " ius nullam impetus. ", + scale: PxScale { x: 25.0, y: 45.0 }, + font_id: serif_font, + extra: Extra { + color: [0.9, 0.9, 0.3, 1.0], + z: 0.0, + }, }, - }, - Text { - text: "Ut quo elitr viderer constituam, pro omnesque forensibus at. Timeam scaevola mediocrem ut pri, te pro congue delicatissimi. Mei wisi nostro imperdiet ea, ridens salutatus per no, ut viris partem disputationi sit. Exerci eripuit referrentur vix at, sale mediocrem repudiare per te, modus admodum an eam. No vocent indoctum vis, ne quodsi patrioque vix. Vocent labores omittam et usu.", - scale: PxScale::from(22.0), - font_id: italic_font, - extra: Extra { - color: [0.8, 0.3, 0.5, 1.0], - z: 0.0, + Text { + text: "Ut quo elitr viderer constituam, pro omnesque forensibus at. Timeam scaevola mediocrem ut pri, te pro congue delicatissimi. Mei wisi nostro imperdiet ea, ridens salutatus per no, ut viris partem disputationi sit. Exerci eripuit referrentur vix at, sale mediocrem repudiare per te, modus admodum an eam. No vocent indoctum vis, ne quodsi patrioque vix. Vocent labores omittam et usu.", + scale: PxScale::from(22.0), + font_id: italic_font, + extra: Extra { + color: [0.8, 0.3, 0.5, 1.0], + z: 0.0, + }, }, - }, - ], - layout: Layout::default().h_align(HorizontalAlign::Right).v_align(VerticalAlign::Center), - }); + ], + layout: Layout::default().h_align(HorizontalAlign::Right).v_align(VerticalAlign::Center), + }); - glyph_brush.use_queue().draw(&mut encoder, &color_view).unwrap(); + glyph_brush.use_queue().draw(&mut encoder, &color_view).unwrap(); - encoder.flush(&mut device); - gl_surface.swap_buffers(&gl_context).unwrap(); - device.cleanup(); + encoder.flush(&mut device); + gl_surface.swap_buffers(&gl_context).unwrap(); + device.cleanup(); - if let Some(rate) = loop_helper.report_rate() { - window.set_title(&format!("{title} - {rate:.0} FPS")); + if let Some(rate) = loop_helper.report_rate() { + window.set_title(&format!("{title} - {rate:.0} FPS")); + } + loop_helper.loop_sleep(); + loop_helper.loop_start(); } - loop_helper.loop_sleep(); - loop_helper.loop_start(); - } + _ => (), + }, _ => (), } - }); + })?; + Ok(()) } diff --git a/glyph-brush/Cargo.toml b/glyph-brush/Cargo.toml index 730486f..642f1a7 100644 --- a/glyph-brush/Cargo.toml +++ b/glyph-brush/Cargo.toml @@ -26,12 +26,12 @@ approx = "0.5" criterion = "0.5" env_logger = { version = "0.10", default-features = false } gl = "0.14" -glutin = "0.30.3" -glutin-winit = "0.3" +glutin = "0.31" +glutin-winit = "0.4" once_cell = "1.3" raw-window-handle = "0.5" spin_sleep = "1" -winit = "0.28" +winit = "0.29" [[bench]] name = "glyph_brush" diff --git a/glyph-brush/examples/draw_cache_guts.rs b/glyph-brush/examples/draw_cache_guts.rs index 08e8713..d252b8a 100644 --- a/glyph-brush/examples/draw_cache_guts.rs +++ b/glyph-brush/examples/draw_cache_guts.rs @@ -5,7 +5,7 @@ use approx::relative_eq; use gl::types::*; use glutin::{ display::GetGlDisplay, - prelude::{GlConfig, GlDisplay, NotCurrentGlContextSurfaceAccessor}, + prelude::{GlConfig, GlDisplay, NotCurrentGlContext}, surface::GlSurface, }; use glutin_winit::GlWindow; @@ -14,8 +14,9 @@ use opengl::{GlGlyphTexture, GlTextPipe, Res, Vertex}; use raw_window_handle::HasRawWindowHandle; use std::{env, ffi::CString, mem}; use winit::{ - event::{ElementState, Event, KeyboardInput, MouseScrollDelta, VirtualKeyCode, WindowEvent}, + event::{ElementState, Event, KeyEvent, MouseScrollDelta, WindowEvent}, event_loop::ControlFlow, + keyboard::{Key, NamedKey}, }; /// `[left_top * 3, right_bottom * 2]` @@ -36,7 +37,8 @@ fn main() -> Res<()> { env::set_var("vblank_mode", "0"); } - let events = winit::event_loop::EventLoop::new(); + let events = winit::event_loop::EventLoop::new()?; + events.set_control_flow(ControlFlow::Poll); let (window, gl_config) = glutin_winit::DisplayBuilder::new() .with_window_builder(Some( @@ -119,33 +121,32 @@ fn main() -> Res<()> { let mut loop_helper = spin_sleep::LoopHelper::builder().build_with_target_rate(250.0); let mut fps = 0.0; let mut title = String::new(); - let mut mods = winit::event::ModifiersState::default(); - - events.run(move |event, _, control_flow| { - *control_flow = ControlFlow::Poll; + let mut mods = winit::event::Modifiers::default(); + events.run(move |event, elwt| { match event { + Event::AboutToWait => window.request_redraw(), Event::WindowEvent { event, .. } => match event { - WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit, + WindowEvent::CloseRequested => elwt.exit(), WindowEvent::KeyboardInput { - input: - KeyboardInput { + event: + KeyEvent { + logical_key, state: ElementState::Pressed, - virtual_keycode: Some(keypress), .. }, .. - } => match keypress { - VirtualKeyCode::Escape => *control_flow = ControlFlow::Exit, - VirtualKeyCode::Back if mods.ctrl() => section.text.clear(), - VirtualKeyCode::Back if !section.text.is_empty() => { + } => match logical_key { + Key::Named(NamedKey::Escape) => elwt.exit(), + Key::Named(NamedKey::Backspace) if mods.state().control_key() => section.text.clear(), + Key::Named(NamedKey::Backspace) if !section.text.is_empty() => { let mut end_text = section.text.remove(section.text.len() - 1); end_text.text.pop(); if !end_text.text.is_empty() { section.text.push(end_text); } } - VirtualKeyCode::R if mods.ctrl() && mods.shift() => { + Key::Character(r) if r == "R" && mods.state().control_key() && mods.state().shift_key() => { // reset draw cache to 16x16 and let it resize up to the minimum required eprintln!("Resetting draw cache"); texture = GlGlyphTexture::new((16, 16)); @@ -153,39 +154,38 @@ fn main() -> Res<()> { glyph_brush.resize_texture(16, 16); draw_cache_guts_pipe.update_geometry(dimensions, (16, 16)); } - VirtualKeyCode::R if mods.ctrl() => { + Key::Character(r) if r == "r" && mods.state().control_key() => { // reset draw cache eprintln!("Reordering draw cache - clear texture and reprocess current glyphs"); texture.clear(); let (tw, th) = glyph_brush.texture_dimensions(); glyph_brush.resize_texture(tw, th); } - _ => (), - }, - WindowEvent::ReceivedCharacter(c) => { - if c != '\u{7f}' && c != '\u{8}' { - if section.text.is_empty() { - section.text.push( - OwnedText::default() - .with_scale(font_size) - .with_color(text_color), - ); - } - if let Some(t) = section - .text - .last_mut() - .filter(|t| relative_eq!(t.scale.y, font_size)) - { - t.text.push(c); - } else { - section.text.push( - OwnedText::new(c.to_string()) - .with_scale(font_size) - .with_color(text_color), - ); + key => { + if let Some(str) = key.to_text() { + if section.text.is_empty() { + section.text.push( + OwnedText::default() + .with_scale(font_size) + .with_color(text_color), + ); + } + if let Some(t) = section + .text + .last_mut() + .filter(|t| relative_eq!(t.scale.y, font_size)) + { + t.text.push_str(str); + } else { + section.text.push( + OwnedText::new(str) + .with_scale(font_size) + .with_color(text_color), + ); + } } } - } + }, WindowEvent::ModifiersChanged(newmods) => mods = newmods, WindowEvent::MouseWheel { delta: MouseScrollDelta::LineDelta(_, y), @@ -200,103 +200,105 @@ fn main() -> Res<()> { }; font_size = (size.clamp(3.0, 2000.0) * 2.0).round() / 2.0; } - _ => (), - }, - Event::MainEventsCleared => { - // handle window size changes - let window_size = window.inner_size(); - if dimensions != window_size { - dimensions = window_size; - window.resize_surface(&gl_surface, &gl_ctx); - unsafe { - gl::Viewport(0, 0, dimensions.width as _, dimensions.height as _); - } + WindowEvent::RedrawRequested => { + + // handle window size changes + let window_size = window.inner_size(); + if dimensions != window_size { + dimensions = window_size; + window.resize_surface(&gl_surface, &gl_ctx); + unsafe { + gl::Viewport(0, 0, dimensions.width as _, dimensions.height as _); + } - section.bounds = (window_size.width as f32 * 0.5, window_size.height as _); - section.screen_position.1 = window_size.height as f32 * 0.5; + section.bounds = (window_size.width as f32 * 0.5, window_size.height as _); + section.screen_position.1 = window_size.height as f32 * 0.5; - text_pipe.update_geometry(dimensions); - draw_cache_guts_pipe.update_geometry(dimensions, glyph_brush.texture_dimensions()); - } + text_pipe.update_geometry(dimensions); + draw_cache_guts_pipe.update_geometry(dimensions, glyph_brush.texture_dimensions()); + } - glyph_brush.queue(§ion); - - let mut brush_action; - loop { - brush_action = glyph_brush.process_queued( - |rect, tex_data| unsafe { - // Update part of gpu texture with new glyph alpha values - gl::BindTexture(gl::TEXTURE_2D, texture.name); - gl::TexSubImage2D( - gl::TEXTURE_2D, - 0, - rect.min[0] as _, - rect.min[1] as _, - rect.width() as _, - rect.height() as _, - gl::RED, - gl::UNSIGNED_BYTE, - tex_data.as_ptr() as _, - ); - gl_assert_ok!(); - }, - opengl::to_vertex, - ); - - match brush_action { - Ok(_) => break, - Err(BrushError::TextureTooSmall { suggested, .. }) => { - let (new_width, new_height) = if (suggested.0 > max_image_dimension - || suggested.1 > max_image_dimension) - && (glyph_brush.texture_dimensions().0 < max_image_dimension - || glyph_brush.texture_dimensions().1 < max_image_dimension) - { - (max_image_dimension, max_image_dimension) - } else { - suggested - }; - - // Recreate texture as a larger size to fit more - texture = GlGlyphTexture::new((new_width, new_height)); - texture.clear(); - glyph_brush.resize_texture(new_width, new_height); - draw_cache_guts_pipe.update_geometry(dimensions, (new_width, new_height)); - eprintln!( - "Resizing texture -> {new_width}x{new_height} to fit glyphs"); + glyph_brush.queue(§ion); + + let mut brush_action; + loop { + brush_action = glyph_brush.process_queued( + |rect, tex_data| unsafe { + // Update part of gpu texture with new glyph alpha values + gl::BindTexture(gl::TEXTURE_2D, texture.name); + gl::TexSubImage2D( + gl::TEXTURE_2D, + 0, + rect.min[0] as _, + rect.min[1] as _, + rect.width() as _, + rect.height() as _, + gl::RED, + gl::UNSIGNED_BYTE, + tex_data.as_ptr() as _, + ); + gl_assert_ok!(); + }, + opengl::to_vertex, + ); + + match brush_action { + Ok(_) => break, + Err(BrushError::TextureTooSmall { suggested, .. }) => { + let (new_width, new_height) = if (suggested.0 > max_image_dimension + || suggested.1 > max_image_dimension) + && (glyph_brush.texture_dimensions().0 < max_image_dimension + || glyph_brush.texture_dimensions().1 < max_image_dimension) + { + (max_image_dimension, max_image_dimension) + } else { + suggested + }; + + // Recreate texture as a larger size to fit more + texture = GlGlyphTexture::new((new_width, new_height)); + texture.clear(); + glyph_brush.resize_texture(new_width, new_height); + draw_cache_guts_pipe.update_geometry(dimensions, (new_width, new_height)); + eprintln!( + "Resizing texture -> {new_width}x{new_height} to fit glyphs"); + } } } - } - match brush_action.unwrap() { - BrushAction::Draw(vertices) => text_pipe.upload_vertices(&vertices), - BrushAction::ReDraw => {} - } + match brush_action.unwrap() { + BrushAction::Draw(vertices) => text_pipe.upload_vertices(&vertices), + BrushAction::ReDraw => {} + } - unsafe { - gl::Clear(gl::COLOR_BUFFER_BIT); - } - text_pipe.draw(); - draw_cache_guts_pipe.draw(); + unsafe { + gl::Clear(gl::COLOR_BUFFER_BIT); + } + text_pipe.draw(); + draw_cache_guts_pipe.draw(); - gl_surface.swap_buffers(&gl_ctx).unwrap(); + gl_surface.swap_buffers(&gl_ctx).unwrap(); - if let Some(rate) = loop_helper.report_rate() { - fps = rate; - } + if let Some(rate) = loop_helper.report_rate() { + fps = rate; + } - let (tw, th) = glyph_brush.texture_dimensions(); - let new_title = - format!("draw cache example - typing size {font_size}, cache size {tw}x{th}, {fps:.0} FPS"); - if new_title != title { - title = new_title; - window.set_title(&title); - } + let (tw, th) = glyph_brush.texture_dimensions(); + let new_title = + format!("draw cache example - typing size {font_size}, cache size {tw}x{th}, {fps:.0} FPS"); + if new_title != title { + title = new_title; + window.set_title(&title); + } - loop_helper.loop_sleep(); - loop_helper.loop_start(); - } + loop_helper.loop_sleep(); + loop_helper.loop_start(); + } + _ => (), + }, _ => (), } - }); + })?; + Ok(()) } pub struct GlDrawCacheGutsPipe { diff --git a/glyph-brush/examples/opengl.rs b/glyph-brush/examples/opengl.rs index cceeffc..ce401c6 100644 --- a/glyph-brush/examples/opengl.rs +++ b/glyph-brush/examples/opengl.rs @@ -19,7 +19,7 @@ use gl::types::*; use glutin::{ display::GetGlDisplay, - prelude::{GlConfig, GlDisplay, NotCurrentGlContextSurfaceAccessor}, + prelude::{GlConfig, GlDisplay, NotCurrentGlContext}, surface::GlSurface, }; use glutin_winit::GlWindow; @@ -32,8 +32,9 @@ use std::{ mem, ptr, str, }; use winit::{ - event::{ElementState, Event, KeyboardInput, MouseScrollDelta, VirtualKeyCode, WindowEvent}, + event::{ElementState, Event, KeyEvent, MouseScrollDelta, WindowEvent}, event_loop::ControlFlow, + keyboard::{Key, NamedKey}, }; const TITLE: &str = "glyph_brush opengl example - scroll to size, type to modify"; @@ -58,7 +59,8 @@ fn main() -> Res<()> { env::set_var("vblank_mode", "0"); } - let events = winit::event_loop::EventLoop::new(); + let events = winit::event_loop::EventLoop::new()?; + events.set_control_flow(ControlFlow::Poll); let (window, gl_config) = glutin_winit::DisplayBuilder::new() .with_window_builder(Some( @@ -116,32 +118,30 @@ fn main() -> Res<()> { let mut vertex_count = 0; let mut vertex_max = vertex_count; - events.run(move |event, _, control_flow| { - *control_flow = ControlFlow::Poll; - + events.run(move |event, elwt| { match event { + Event::AboutToWait => window.request_redraw(), Event::WindowEvent { event, .. } => match event { - WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit, + WindowEvent::CloseRequested => elwt.exit(), WindowEvent::KeyboardInput { - input: - KeyboardInput { + event: + KeyEvent { + logical_key, state: ElementState::Pressed, - virtual_keycode: Some(keypress), .. }, .. - } => match keypress { - VirtualKeyCode::Escape => *control_flow = ControlFlow::Exit, - VirtualKeyCode::Back => { + } => match logical_key { + Key::Named(NamedKey::Escape) => elwt.exit(), + Key::Named(NamedKey::Backspace) => { text.pop(); } - _ => (), - }, - WindowEvent::ReceivedCharacter(c) => { - if c != '\u{7f}' && c != '\u{8}' { - text.push(c); + key => { + if let Some(str) = key.to_text() { + text.push_str(str); + } } - } + }, WindowEvent::MouseWheel { delta: MouseScrollDelta::LineDelta(_, y), .. @@ -161,125 +161,126 @@ fn main() -> Res<()> { let _ = io::stderr().flush(); } } - _ => (), - }, - Event::MainEventsCleared => { - // handle window size changes - let window_size = window.inner_size(); - if dimensions != window_size { - dimensions = window_size; - window.resize_surface(&gl_surface, &gl_ctx); - unsafe { - gl::Viewport(0, 0, dimensions.width as _, dimensions.height as _); + WindowEvent::RedrawRequested => { + // handle window size changes + let window_size = window.inner_size(); + if dimensions != window_size { + dimensions = window_size; + window.resize_surface(&gl_surface, &gl_ctx); + unsafe { + gl::Viewport(0, 0, dimensions.width as _, dimensions.height as _); + } + text_pipe.update_geometry(window_size); } - text_pipe.update_geometry(window_size); - } - let width = dimensions.width as f32; - let height = dimensions.height as _; - let scale = (font_size * window.scale_factor() as f32).round(); - let base_text = Text::new(&text).with_scale(scale); + let width = dimensions.width as f32; + let height = dimensions.height as _; + let scale = (font_size * window.scale_factor() as f32).round(); + let base_text = Text::new(&text).with_scale(scale); - // Queue up all sections of text to be drawn - glyph_brush.queue( - Section::default() - .add_text(base_text.with_color([0.9, 0.3, 0.3, 1.0])) - .with_bounds((width / 3.15, height)), - ); - - glyph_brush.queue( - Section::default() - .add_text(base_text.with_color([0.3, 0.9, 0.3, 1.0])) - .with_screen_position((width / 2.0, height / 2.0)) - .with_bounds((width / 3.15, height)) - .with_layout( - Layout::default() - .h_align(HorizontalAlign::Center) - .v_align(VerticalAlign::Center), - ), - ); + // Queue up all sections of text to be drawn + glyph_brush.queue( + Section::default() + .add_text(base_text.with_color([0.9, 0.3, 0.3, 1.0])) + .with_bounds((width / 3.15, height)), + ); - glyph_brush.queue( - Section::default() - .add_text(base_text.with_color([0.3, 0.3, 0.9, 1.0])) - .with_screen_position((width, height)) - .with_bounds((width / 3.15, height)) - .with_layout( - Layout::default() - .h_align(HorizontalAlign::Right) - .v_align(VerticalAlign::Bottom), - ), - ); + glyph_brush.queue( + Section::default() + .add_text(base_text.with_color([0.3, 0.9, 0.3, 1.0])) + .with_screen_position((width / 2.0, height / 2.0)) + .with_bounds((width / 3.15, height)) + .with_layout( + Layout::default() + .h_align(HorizontalAlign::Center) + .v_align(VerticalAlign::Center), + ), + ); - // Tell glyph_brush to process the queued text - let mut brush_action; - loop { - brush_action = glyph_brush.process_queued( - |rect, tex_data| unsafe { - // Update part of gpu texture with new glyph alpha values - gl::BindTexture(gl::TEXTURE_2D, texture.name); - gl::TexSubImage2D( - gl::TEXTURE_2D, - 0, - rect.min[0] as _, - rect.min[1] as _, - rect.width() as _, - rect.height() as _, - gl::RED, - gl::UNSIGNED_BYTE, - tex_data.as_ptr() as _, - ); - gl_assert_ok!(); - }, - to_vertex, + glyph_brush.queue( + Section::default() + .add_text(base_text.with_color([0.3, 0.3, 0.9, 1.0])) + .with_screen_position((width, height)) + .with_bounds((width / 3.15, height)) + .with_layout( + Layout::default() + .h_align(HorizontalAlign::Right) + .v_align(VerticalAlign::Bottom), + ), ); - // If the cache texture is too small to fit all the glyphs, resize and try again - match brush_action { - Ok(_) => break, - Err(BrushError::TextureTooSmall { suggested, .. }) => { - let (new_width, new_height) = if (suggested.0 > max_image_dimension - || suggested.1 > max_image_dimension) - && (glyph_brush.texture_dimensions().0 < max_image_dimension - || glyph_brush.texture_dimensions().1 < max_image_dimension) - { - (max_image_dimension, max_image_dimension) - } else { - suggested - }; - eprint!("\r \r"); - eprintln!("Resizing glyph texture -> {new_width}x{new_height}"); - - // Recreate texture as a larger size to fit more - texture = GlGlyphTexture::new((new_width, new_height)); - - glyph_brush.resize_texture(new_width, new_height); + // Tell glyph_brush to process the queued text + let mut brush_action; + loop { + brush_action = glyph_brush.process_queued( + |rect, tex_data| unsafe { + // Update part of gpu texture with new glyph alpha values + gl::BindTexture(gl::TEXTURE_2D, texture.name); + gl::TexSubImage2D( + gl::TEXTURE_2D, + 0, + rect.min[0] as _, + rect.min[1] as _, + rect.width() as _, + rect.height() as _, + gl::RED, + gl::UNSIGNED_BYTE, + tex_data.as_ptr() as _, + ); + gl_assert_ok!(); + }, + to_vertex, + ); + + // If the cache texture is too small to fit all the glyphs, resize and try again + match brush_action { + Ok(_) => break, + Err(BrushError::TextureTooSmall { suggested, .. }) => { + let (new_width, new_height) = if (suggested.0 > max_image_dimension + || suggested.1 > max_image_dimension) + && (glyph_brush.texture_dimensions().0 < max_image_dimension + || glyph_brush.texture_dimensions().1 < max_image_dimension) + { + (max_image_dimension, max_image_dimension) + } else { + suggested + }; + eprint!("\r \r"); + eprintln!("Resizing glyph texture -> {new_width}x{new_height}"); + + // Recreate texture as a larger size to fit more + texture = GlGlyphTexture::new((new_width, new_height)); + + glyph_brush.resize_texture(new_width, new_height); + } } } - } - // If the text has changed from what was last drawn, upload the new vertices to GPU - match brush_action.unwrap() { - BrushAction::Draw(vertices) => text_pipe.upload_vertices(&vertices), - BrushAction::ReDraw => {} - } + // If the text has changed from what was last drawn, upload the new vertices to GPU + match brush_action.unwrap() { + BrushAction::Draw(vertices) => text_pipe.upload_vertices(&vertices), + BrushAction::ReDraw => {} + } - // Draw the text to the screen - unsafe { - gl::Clear(gl::COLOR_BUFFER_BIT); - } - text_pipe.draw(); + // Draw the text to the screen + unsafe { + gl::Clear(gl::COLOR_BUFFER_BIT); + } + text_pipe.draw(); - gl_surface.swap_buffers(&gl_ctx).unwrap(); + gl_surface.swap_buffers(&gl_ctx).unwrap(); - if let Some(rate) = loop_helper.report_rate() { - window.set_title(&format!("{TITLE} {rate:.0} FPS")); + if let Some(rate) = loop_helper.report_rate() { + window.set_title(&format!("{TITLE} {rate:.0} FPS")); + } + loop_helper.loop_sleep(); + loop_helper.loop_start(); } - loop_helper.loop_sleep(); - loop_helper.loop_start(); - } + _ => (), + }, _ => (), } - }); + })?; + Ok(()) } pub fn gl_err_to_str(err: u32) -> &'static str { From 366afe60ea93e5d814c464c4f411f512423cdeac Mon Sep 17 00:00:00 2001 From: Alex Butler Date: Tue, 24 Oct 2023 23:08:21 +0100 Subject: [PATCH 2/2] old_school_gfx_glutin_ext 0.32 --- gfx-glyph/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gfx-glyph/Cargo.toml b/gfx-glyph/Cargo.toml index 237262d..688fec7 100644 --- a/gfx-glyph/Cargo.toml +++ b/gfx-glyph/Cargo.toml @@ -22,6 +22,6 @@ env_logger = { version = "0.10", default-features = false } gfx_device_gl = "0.16" glutin = "0.31" glutin-winit = "0.4" -old_school_gfx_glutin_ext = { path = "../../old-school-gfx-glutin-ext" } +old_school_gfx_glutin_ext = "0.32" spin_sleep = "1" winit = "0.29"