pub struct WinitIo { /* private fields */ }
Expand description
A Thyme Input/Output adapter for winit
.
This adapter handles events from winit
and sends them to the Thyme Context
.
WindowEvents should be passed to this handler, assuming Context.wants_mouse
returns true for the given frame.
Example
fn main_loop(event_loop: winit::EventLoop<()>, thyme: thyme::Context) {
event_loop.run(move |event, _, control_flow| match event {
Event::MainEventsCleared => {
// Renderer specific code here
let mut ui = context.create_frame();
// create UI here
// draw the frame and finish up rendering here
}
Event::WindowEvent { event: WindowEvent::CloseRequested, .. } => *control_flow = ControlFlow::Exit,
event => {
io.handle_event(&mut context, &event);
}
})
}
Implementations§
source§impl WinitIo
impl WinitIo
sourcepub fn new<T>(
event_loop: &EventLoop<T>,
logical_display_size: Point
) -> Result<WinitIo, WinitError>
pub fn new<T>( event_loop: &EventLoop<T>, logical_display_size: Point ) -> Result<WinitIo, WinitError>
Creates a new adapter from the given EventLoop
, with the specified initial display size,
in logical pixels. This may change over time.
Examples found in repository?
examples/demo_glium.rs (line 29)
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
fn main() -> Result<(), Box<dyn std::error::Error>> {
use glium::glutin::{window::WindowBuilder};
use glium::{Display, Surface};
// initialize our very basic logger so error messages go to stdout
thyme::log::init(log::Level::Warn).unwrap();
let window_size = [1280.0, 720.0];
let events_loop = EventLoop::new();
// create glium display
let builder = WindowBuilder::new()
.with_title("Thyme Demo")
.with_inner_size(glium::glutin::dpi::LogicalSize::new(window_size[0], window_size[1]));
let context = glium::glutin::ContextBuilder::new();
let display = Display::new(builder, context, &events_loop)?;
// create thyme backend
let mut renderer = thyme::GliumRenderer::new(&display)?;
let mut io = thyme::WinitIo::new(&events_loop, window_size.into())?;
let mut context_builder = thyme::ContextBuilder::with_defaults();
demo::register_assets(&mut context_builder);
let mut context = context_builder.build(&mut renderer, &mut io)?;
let mut party = demo::Party::default();
let mut last_frame = std::time::Instant::now();
let frame_time = std::time::Duration::from_millis(16);
// run main loop
events_loop.run(move |event, _, control_flow| match event {
Event::MainEventsCleared => {
if std::time::Instant::now() > last_frame + frame_time {
display.gl_window().window().request_redraw();
}
*control_flow = ControlFlow::WaitUntil(last_frame + frame_time);
},
Event::RedrawRequested(_) => {
last_frame = std::time::Instant::now();
party.check_context_changes(&mut context, &mut renderer);
let mut target = display.draw();
target.clear_color(0.21404, 0.21404, 0.21404, 1.0); // manual sRGB conversion for 0.5
bench::run("thyme", || {
display.gl_window().window().set_cursor_visible(!party.theme_has_mouse_cursor());
let mut ui = context.create_frame();
bench::run("frame", || {
demo::build_ui(&mut ui, &mut party);
});
bench::run("draw", || {
renderer.draw_frame(&mut target, ui).unwrap();
});
});
target.finish().unwrap();
},
Event::WindowEvent { event: WindowEvent::CloseRequested, .. } => *control_flow = ControlFlow::Exit,
event => {
io.handle_event(&mut context, &event);
}
})
}
More examples
examples/demo_gl.rs (line 50)
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
fn main() -> Result<(), Box<dyn std::error::Error>> {
// initialize our very basic logger so error messages go to stdout
thyme::log::init(log::Level::Warn).unwrap();
// create glium display
let event_loop = glutin::event_loop::EventLoop::new();
let window_builder = glutin::window::WindowBuilder::new()
.with_title("Hello world!")
.with_inner_size(glutin::dpi::LogicalSize::new(1280.0, 720.0));
let windowed_context = glutin::ContextBuilder::new()
.with_gl(glutin::GlRequest::Specific(
glutin::Api::OpenGl,
(OPENGL_MAJOR_VERSION, OPENGL_MINOR_VERSION),
))
.build_windowed(window_builder, &event_loop)?;
let windowed_context = unsafe {
windowed_context
.make_current().map_err(|(_context, e)| e)?
};
{
let gl_context = windowed_context.context();
gl::load_with(|ptr| gl_context.get_proc_address(ptr) as *const _)
}
// create thyme backend
let mut renderer = thyme::GLRenderer::new();
let mut context_builder = thyme::ContextBuilder::with_defaults();
demo::register_assets(&mut context_builder);
let window_size = [1280.0, 720.0];
let mut io = thyme::WinitIo::new(&event_loop, window_size.into())?;
let mut context = context_builder.build(&mut renderer, &mut io)?;
let mut party = demo::Party::default();
let mut last_frame = std::time::Instant::now();
let frame_time = std::time::Duration::from_millis(16);
// run main loop
event_loop.run(move |event, _, control_flow| match event {
Event::MainEventsCleared => {
if std::time::Instant::now() > last_frame + frame_time {
windowed_context.window().request_redraw();
}
*control_flow = ControlFlow::WaitUntil(last_frame + frame_time);
}
Event::RedrawRequested(_) => {
last_frame = std::time::Instant::now();
party.check_context_changes(&mut context, &mut renderer);
renderer.clear_color(0.5, 0.5, 0.5, 1.0);
bench::run("thyme", || {
windowed_context.window().set_cursor_visible(!party.theme_has_mouse_cursor());
let mut ui = context.create_frame();
bench::run("frame", || {
demo::build_ui(&mut ui, &mut party);
});
bench::run("draw", || {
renderer.draw_frame(ui);
});
});
windowed_context.swap_buffers().unwrap();
}
Event::WindowEvent {
event: WindowEvent::CloseRequested,
..
} => *control_flow = ControlFlow::Exit,
event => {
io.handle_event(&mut context, &event);
}
})
}
examples/demo_wgpu.rs (line 40)
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
fn main() -> Result<(), Box<dyn std::error::Error>> {
use winit::{ window::WindowBuilder };
// initialize our very basic logger so error messages go to stdout
thyme::log::init(log::Level::Warn).unwrap();
let window_size = [1280.0, 720.0];
let events_loop = EventLoop::new();
// create winit window
let window = WindowBuilder::new()
.with_title("Thyme WGPU Demo")
.with_inner_size(winit::dpi::LogicalSize::new(window_size[0], window_size[1]))
.build(&events_loop)?;
// setup WGPU
let instance_desc = wgpu::InstanceDescriptor {
backends: wgpu::Backends::PRIMARY,
dx12_shader_compiler: wgpu::Dx12Compiler::Fxc,
};
let instance = wgpu::Instance::new(instance_desc);
let surface = unsafe { instance.create_surface(&window).map_err(thyme::Error::WgpuSurface)? };
let (_adapter, device, queue) = futures::executor::block_on(setup_wgpu(&instance, &surface));
let surface_config = get_surface_config(window_size[0] as u32, window_size[1] as u32);
surface.configure(&device, &surface_config);
// create thyme backend
let mut renderer = thyme::WgpuRenderer::new(Arc::clone(&device), Arc::clone(&queue));
let mut io = thyme::WinitIo::new(&events_loop, window_size.into())?;
let mut context_builder = thyme::ContextBuilder::with_defaults();
demo::register_assets(&mut context_builder);
let mut context = context_builder.build(&mut renderer, &mut io)?;
let mut party = demo::Party::default();
let mut last_frame = std::time::Instant::now();
let frame_time = std::time::Duration::from_millis(16);
// run main loop
events_loop.run(move |event, _, control_flow| match event {
Event::MainEventsCleared => {
if std::time::Instant::now() > last_frame + frame_time {
window.request_redraw();
}
*control_flow = ControlFlow::WaitUntil(last_frame + frame_time);
},
Event::RedrawRequested(_) => {
last_frame = std::time::Instant::now();
party.check_context_changes(&mut context, &mut renderer);
let frame = surface.get_current_texture().unwrap();
let view = frame.texture.create_view(&wgpu::TextureViewDescriptor::default());
let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None });
bench::run("thyme", || {
window.set_cursor_visible(!party.theme_has_mouse_cursor());
let mut ui = context.create_frame();
bench::run("frame", || {
demo::build_ui(&mut ui, &mut party);
});
bench::run("draw", || {
{
let mut render_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: None,
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
view: &view,
resolve_target: None,
ops: wgpu::Operations {
load: wgpu::LoadOp::Clear(wgpu::Color { r: 0.5, g: 0.5, b: 0.5, a: 1.0 }),
store: true,
},
})],
depth_stencil_attachment: None,
});
renderer.draw_frame(ui, &mut render_pass);
}
queue.submit(Some(encoder.finish()));
frame.present();
});
});
},
Event::WindowEvent { event: WindowEvent::CloseRequested, .. } => *control_flow = ControlFlow::Exit,
event => {
// recreate swap chain on resize, but also still pass the event to thyme
if let Event::WindowEvent { event: WindowEvent::Resized(_), ..} = event {
let size: (u32, u32) = window.inner_size().into();
let surface_config = get_surface_config(size.0, size.1);
surface.configure(&device, &surface_config);
}
io.handle_event(&mut context, &event);
}
})
}
sourcepub fn handle_event<T>(&mut self, context: &mut Context, event: &Event<'_, T>)
pub fn handle_event<T>(&mut self, context: &mut Context, event: &Event<'_, T>)
Handles a winit Event
and passes it to the Thyme Context
.
Examples found in repository?
examples/demo_glium.rs (line 75)
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
fn main() -> Result<(), Box<dyn std::error::Error>> {
use glium::glutin::{window::WindowBuilder};
use glium::{Display, Surface};
// initialize our very basic logger so error messages go to stdout
thyme::log::init(log::Level::Warn).unwrap();
let window_size = [1280.0, 720.0];
let events_loop = EventLoop::new();
// create glium display
let builder = WindowBuilder::new()
.with_title("Thyme Demo")
.with_inner_size(glium::glutin::dpi::LogicalSize::new(window_size[0], window_size[1]));
let context = glium::glutin::ContextBuilder::new();
let display = Display::new(builder, context, &events_loop)?;
// create thyme backend
let mut renderer = thyme::GliumRenderer::new(&display)?;
let mut io = thyme::WinitIo::new(&events_loop, window_size.into())?;
let mut context_builder = thyme::ContextBuilder::with_defaults();
demo::register_assets(&mut context_builder);
let mut context = context_builder.build(&mut renderer, &mut io)?;
let mut party = demo::Party::default();
let mut last_frame = std::time::Instant::now();
let frame_time = std::time::Duration::from_millis(16);
// run main loop
events_loop.run(move |event, _, control_flow| match event {
Event::MainEventsCleared => {
if std::time::Instant::now() > last_frame + frame_time {
display.gl_window().window().request_redraw();
}
*control_flow = ControlFlow::WaitUntil(last_frame + frame_time);
},
Event::RedrawRequested(_) => {
last_frame = std::time::Instant::now();
party.check_context_changes(&mut context, &mut renderer);
let mut target = display.draw();
target.clear_color(0.21404, 0.21404, 0.21404, 1.0); // manual sRGB conversion for 0.5
bench::run("thyme", || {
display.gl_window().window().set_cursor_visible(!party.theme_has_mouse_cursor());
let mut ui = context.create_frame();
bench::run("frame", || {
demo::build_ui(&mut ui, &mut party);
});
bench::run("draw", || {
renderer.draw_frame(&mut target, ui).unwrap();
});
});
target.finish().unwrap();
},
Event::WindowEvent { event: WindowEvent::CloseRequested, .. } => *control_flow = ControlFlow::Exit,
event => {
io.handle_event(&mut context, &event);
}
})
}
More examples
examples/demo_gl.rs (line 93)
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
fn main() -> Result<(), Box<dyn std::error::Error>> {
// initialize our very basic logger so error messages go to stdout
thyme::log::init(log::Level::Warn).unwrap();
// create glium display
let event_loop = glutin::event_loop::EventLoop::new();
let window_builder = glutin::window::WindowBuilder::new()
.with_title("Hello world!")
.with_inner_size(glutin::dpi::LogicalSize::new(1280.0, 720.0));
let windowed_context = glutin::ContextBuilder::new()
.with_gl(glutin::GlRequest::Specific(
glutin::Api::OpenGl,
(OPENGL_MAJOR_VERSION, OPENGL_MINOR_VERSION),
))
.build_windowed(window_builder, &event_loop)?;
let windowed_context = unsafe {
windowed_context
.make_current().map_err(|(_context, e)| e)?
};
{
let gl_context = windowed_context.context();
gl::load_with(|ptr| gl_context.get_proc_address(ptr) as *const _)
}
// create thyme backend
let mut renderer = thyme::GLRenderer::new();
let mut context_builder = thyme::ContextBuilder::with_defaults();
demo::register_assets(&mut context_builder);
let window_size = [1280.0, 720.0];
let mut io = thyme::WinitIo::new(&event_loop, window_size.into())?;
let mut context = context_builder.build(&mut renderer, &mut io)?;
let mut party = demo::Party::default();
let mut last_frame = std::time::Instant::now();
let frame_time = std::time::Duration::from_millis(16);
// run main loop
event_loop.run(move |event, _, control_flow| match event {
Event::MainEventsCleared => {
if std::time::Instant::now() > last_frame + frame_time {
windowed_context.window().request_redraw();
}
*control_flow = ControlFlow::WaitUntil(last_frame + frame_time);
}
Event::RedrawRequested(_) => {
last_frame = std::time::Instant::now();
party.check_context_changes(&mut context, &mut renderer);
renderer.clear_color(0.5, 0.5, 0.5, 1.0);
bench::run("thyme", || {
windowed_context.window().set_cursor_visible(!party.theme_has_mouse_cursor());
let mut ui = context.create_frame();
bench::run("frame", || {
demo::build_ui(&mut ui, &mut party);
});
bench::run("draw", || {
renderer.draw_frame(ui);
});
});
windowed_context.swap_buffers().unwrap();
}
Event::WindowEvent {
event: WindowEvent::CloseRequested,
..
} => *control_flow = ControlFlow::Exit,
event => {
io.handle_event(&mut context, &event);
}
})
}
examples/demo_wgpu.rs (line 111)
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
fn main() -> Result<(), Box<dyn std::error::Error>> {
use winit::{ window::WindowBuilder };
// initialize our very basic logger so error messages go to stdout
thyme::log::init(log::Level::Warn).unwrap();
let window_size = [1280.0, 720.0];
let events_loop = EventLoop::new();
// create winit window
let window = WindowBuilder::new()
.with_title("Thyme WGPU Demo")
.with_inner_size(winit::dpi::LogicalSize::new(window_size[0], window_size[1]))
.build(&events_loop)?;
// setup WGPU
let instance_desc = wgpu::InstanceDescriptor {
backends: wgpu::Backends::PRIMARY,
dx12_shader_compiler: wgpu::Dx12Compiler::Fxc,
};
let instance = wgpu::Instance::new(instance_desc);
let surface = unsafe { instance.create_surface(&window).map_err(thyme::Error::WgpuSurface)? };
let (_adapter, device, queue) = futures::executor::block_on(setup_wgpu(&instance, &surface));
let surface_config = get_surface_config(window_size[0] as u32, window_size[1] as u32);
surface.configure(&device, &surface_config);
// create thyme backend
let mut renderer = thyme::WgpuRenderer::new(Arc::clone(&device), Arc::clone(&queue));
let mut io = thyme::WinitIo::new(&events_loop, window_size.into())?;
let mut context_builder = thyme::ContextBuilder::with_defaults();
demo::register_assets(&mut context_builder);
let mut context = context_builder.build(&mut renderer, &mut io)?;
let mut party = demo::Party::default();
let mut last_frame = std::time::Instant::now();
let frame_time = std::time::Duration::from_millis(16);
// run main loop
events_loop.run(move |event, _, control_flow| match event {
Event::MainEventsCleared => {
if std::time::Instant::now() > last_frame + frame_time {
window.request_redraw();
}
*control_flow = ControlFlow::WaitUntil(last_frame + frame_time);
},
Event::RedrawRequested(_) => {
last_frame = std::time::Instant::now();
party.check_context_changes(&mut context, &mut renderer);
let frame = surface.get_current_texture().unwrap();
let view = frame.texture.create_view(&wgpu::TextureViewDescriptor::default());
let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None });
bench::run("thyme", || {
window.set_cursor_visible(!party.theme_has_mouse_cursor());
let mut ui = context.create_frame();
bench::run("frame", || {
demo::build_ui(&mut ui, &mut party);
});
bench::run("draw", || {
{
let mut render_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: None,
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
view: &view,
resolve_target: None,
ops: wgpu::Operations {
load: wgpu::LoadOp::Clear(wgpu::Color { r: 0.5, g: 0.5, b: 0.5, a: 1.0 }),
store: true,
},
})],
depth_stencil_attachment: None,
});
renderer.draw_frame(ui, &mut render_pass);
}
queue.submit(Some(encoder.finish()));
frame.present();
});
});
},
Event::WindowEvent { event: WindowEvent::CloseRequested, .. } => *control_flow = ControlFlow::Exit,
event => {
// recreate swap chain on resize, but also still pass the event to thyme
if let Event::WindowEvent { event: WindowEvent::Resized(_), ..} = event {
let size: (u32, u32) = window.inner_size().into();
let surface_config = get_surface_config(size.0, size.1);
surface.configure(&device, &surface_config);
}
io.handle_event(&mut context, &event);
}
})
}
Trait Implementations§
Auto Trait Implementations§
impl RefUnwindSafe for WinitIo
impl Send for WinitIo
impl Sync for WinitIo
impl Unpin for WinitIo
impl UnwindSafe for WinitIo
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more