glyphon/examples/hello-world.rs

147 lines
5 KiB
Rust
Raw Normal View History

2022-11-28 02:26:00 -05:00
use cosmic_text::{Attrs, Buffer, Color, Family, FontSystem, Metrics, SwashCache};
2022-10-27 23:45:02 -04:00
use glyphon::{Resolution, TextAtlas, TextRenderer};
2022-05-09 08:49:10 -04:00
use wgpu::{
2022-10-18 11:32:31 -04:00
Backends, CommandEncoderDescriptor, CompositeAlphaMode, DeviceDescriptor, Features, Instance,
Limits, LoadOp, Operations, PresentMode, RenderPassColorAttachment, RenderPassDescriptor,
2022-10-25 22:51:14 -04:00
RequestAdapterOptions, SurfaceConfiguration, TextureFormat, TextureUsages,
TextureViewDescriptor,
2022-05-09 08:49:10 -04:00
};
use winit::{
2022-10-27 23:14:15 -04:00
dpi::LogicalSize,
2022-05-09 08:49:10 -04:00
event::{Event, WindowEvent},
event_loop::{ControlFlow, EventLoop},
2022-10-27 23:14:15 -04:00
window::WindowBuilder,
2022-05-09 08:49:10 -04:00
};
fn main() {
pollster::block_on(run());
}
2022-10-24 23:41:57 -04:00
static mut FONT_SYSTEM: Option<FontSystem> = None;
2022-05-09 08:49:10 -04:00
async fn run() {
2022-10-27 23:14:15 -04:00
// Set up window
let (width, height) = (800, 600);
let event_loop = EventLoop::new();
let window = WindowBuilder::new()
.with_inner_size(LogicalSize::new(width as f64, height as f64))
.with_title("glyphon hello world")
.build(&event_loop)
.unwrap();
let size = window.inner_size();
let scale_factor = window.scale_factor();
// Set up surface
2022-05-09 21:10:00 -04:00
let instance = Instance::new(Backends::all());
2022-05-09 08:49:10 -04:00
let adapter = instance
2022-05-09 21:10:00 -04:00
.request_adapter(&RequestAdapterOptions::default())
2022-05-09 08:49:10 -04:00
.await
.unwrap();
let (device, queue) = adapter
.request_device(
2022-05-09 21:10:00 -04:00
&DeviceDescriptor {
2022-05-09 08:49:10 -04:00
label: None,
2022-05-09 21:10:00 -04:00
features: Features::empty(),
limits: Limits::downlevel_defaults(),
2022-05-09 08:49:10 -04:00
},
None,
)
.await
.unwrap();
let surface = unsafe { instance.create_surface(&window) };
2022-10-25 22:51:14 -04:00
// TODO: handle srgb
let swapchain_format = TextureFormat::Bgra8Unorm;
2022-05-09 21:10:00 -04:00
let mut config = SurfaceConfiguration {
usage: TextureUsages::RENDER_ATTACHMENT,
2022-05-09 08:49:10 -04:00
format: swapchain_format,
width: size.width,
height: size.height,
2022-11-28 00:53:10 -05:00
present_mode: PresentMode::Fifo,
2022-10-18 11:32:31 -04:00
alpha_mode: CompositeAlphaMode::Opaque,
2022-05-09 08:49:10 -04:00
};
surface.configure(&device, &config);
2022-10-27 23:14:15 -04:00
// Set up text renderer
2022-10-27 23:44:07 -04:00
let mut text_renderer = TextRenderer::new(&device, &queue);
2022-10-25 21:08:12 -04:00
unsafe {
FONT_SYSTEM = Some(FontSystem::new());
}
2022-10-27 23:44:07 -04:00
let mut cache = SwashCache::new(unsafe { FONT_SYSTEM.as_ref().unwrap() });
2022-06-02 23:08:59 -04:00
let mut atlas = TextAtlas::new(&device, &queue, swapchain_format);
2022-11-03 19:35:21 -04:00
let mut buffer = Buffer::new(
2022-10-25 21:08:12 -04:00
unsafe { FONT_SYSTEM.as_ref().unwrap() },
2022-11-28 02:26:00 -05:00
Metrics::new(14, 20),
2022-10-25 21:08:12 -04:00
);
2022-10-27 23:14:15 -04:00
buffer.set_size((width as f64 * scale_factor) as i32, (height as f64) as i32);
2022-11-28 02:26:00 -05:00
buffer.set_text(
include_str!("./emoji.txt"),
Attrs::new().monospaced(true).family(Family::Monospace),
);
2022-11-03 19:35:21 -04:00
buffer.shape_until_scroll();
2022-05-09 08:49:10 -04:00
event_loop.run(move |event, _, control_flow| {
let _ = (&instance, &adapter);
*control_flow = ControlFlow::Poll;
match event {
Event::WindowEvent {
event: WindowEvent::Resized(size),
..
} => {
config.width = size.width;
config.height = size.height;
surface.configure(&device, &config);
window.request_redraw();
}
Event::RedrawRequested(_) => {
text_renderer
.prepare(
&device,
&queue,
2022-06-02 23:08:59 -04:00
&mut atlas,
2022-05-09 08:49:10 -04:00
Resolution {
width: config.width,
height: config.height,
},
2022-10-24 23:41:57 -04:00
&mut buffer,
Color::rgb(255, 255, 255),
2022-10-27 23:44:07 -04:00
&mut cache,
2022-05-09 08:49:10 -04:00
)
.unwrap();
let frame = surface.get_current_texture().unwrap();
let view = frame.texture.create_view(&TextureViewDescriptor::default());
let mut encoder =
device.create_command_encoder(&CommandEncoderDescriptor { label: None });
{
let mut pass = encoder.begin_render_pass(&RenderPassDescriptor {
label: None,
2022-07-13 08:04:49 -04:00
color_attachments: &[Some(RenderPassColorAttachment {
2022-05-09 08:49:10 -04:00
view: &view,
resolve_target: None,
ops: Operations {
2022-05-09 21:10:00 -04:00
load: LoadOp::Clear(wgpu::Color::BLACK),
2022-05-09 08:49:10 -04:00
store: true,
},
2022-07-13 08:04:49 -04:00
})],
2022-05-09 08:49:10 -04:00
depth_stencil_attachment: None,
});
2022-06-02 23:08:59 -04:00
text_renderer.render(&atlas, &mut pass).unwrap();
2022-05-09 08:49:10 -04:00
}
queue.submit(Some(encoder.finish()));
frame.present();
}
Event::WindowEvent {
event: WindowEvent::CloseRequested,
..
} => *control_flow = ControlFlow::Exit,
Event::MainEventsCleared => {
window.request_redraw();
}
_ => {}
}
});
}