glyphon/examples/hello-world.rs

182 lines
6 KiB
Rust
Raw Normal View History

use fontdue::layout::{HorizontalAlign, VerticalAlign};
2022-05-09 10:22:38 -04:00
use glyphon::{
fontdue::{
layout::{CoordinateSystem, Layout, LayoutSettings, TextStyle},
Font, FontSettings,
},
Color, HasColor, Resolution, TextAtlas, TextOverflow, 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-05-09 21:10:00 -04:00
RequestAdapterOptions, SurfaceConfiguration, TextureUsages, TextureViewDescriptor,
2022-05-09 08:49:10 -04:00
};
use winit::{
event::{Event, WindowEvent},
event_loop::{ControlFlow, EventLoop},
window::Window,
};
fn main() {
pollster::block_on(run());
}
2022-05-09 21:10:00 -04:00
#[derive(Clone, Copy)]
struct GlyphUserData;
2022-05-09 21:10:00 -04:00
impl HasColor for GlyphUserData {
fn color(&self) -> Color {
Color {
r: 255,
g: 255,
b: 0,
a: 255,
}
2022-05-09 21:10:00 -04:00
}
}
2022-05-09 08:49:10 -04:00
async fn run() {
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 event_loop = EventLoop::new();
let window = Window::new(&event_loop).unwrap();
let surface = unsafe { instance.create_surface(&window) };
let size = window.inner_size();
2022-07-13 08:04:49 -04:00
let swapchain_format = surface.get_supported_formats(&adapter)[0];
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-05-09 21:10:00 -04:00
present_mode: PresentMode::Mailbox,
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-06-02 23:08:59 -04:00
let mut atlas = TextAtlas::new(&device, &queue, swapchain_format);
let mut text_renderer = TextRenderer::new(&device, &queue);
2022-05-09 08:49:10 -04:00
let font = include_bytes!("./Inter-Bold.ttf") as &[u8];
let font = Font::from_bytes(font, FontSettings::default()).unwrap();
let fonts = vec![font];
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(_) => {
let mut layout1 = Layout::new(CoordinateSystem::PositiveYDown);
2022-05-10 07:18:28 -04:00
layout1.reset(&LayoutSettings {
2022-05-09 08:49:10 -04:00
x: 0.0,
y: 0.0,
..LayoutSettings::default()
});
layout1.append(
2022-05-09 08:49:10 -04:00
fonts.as_slice(),
2022-05-09 21:10:00 -04:00
&TextStyle::with_user_data(
"Hello world!\nI'm on a new line!",
50.0,
0,
GlyphUserData,
2022-05-09 21:10:00 -04:00
),
2022-05-09 08:49:10 -04:00
);
let mut layout2 = Layout::new(CoordinateSystem::PositiveYDown);
layout2.reset(&LayoutSettings {
x: 0.0,
y: 200.0,
max_width: Some(200.0),
max_height: Some(190.0),
horizontal_align: HorizontalAlign::Center,
vertical_align: VerticalAlign::Middle,
..LayoutSettings::default()
});
layout2.append(
fonts.as_slice(),
&TextStyle::with_user_data(
"abcdefghijklmnopqrstuvwxyz\nThis should be partially clipped!\nabcdefghijklmnopqrstuvwxyz",
25.0,
0,
GlyphUserData,
),
);
2022-05-09 08:49:10 -04:00
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,
},
&fonts,
&[(layout1, TextOverflow::Hide), (layout2, TextOverflow::Hide)],
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();
}
_ => {}
}
});
}