2022-05-09 10:22:38 -04:00
|
|
|
use glyphon::{
|
|
|
|
fontdue::{
|
|
|
|
layout::{CoordinateSystem, Layout, LayoutSettings, TextStyle},
|
|
|
|
Font, FontSettings,
|
|
|
|
},
|
2022-05-09 21:23:09 -04:00
|
|
|
Color, HasColor, Resolution, TextRenderer,
|
2022-05-09 08:49:10 -04:00
|
|
|
};
|
|
|
|
use wgpu::{
|
2022-05-09 21:10:00 -04:00
|
|
|
Backends, CommandEncoderDescriptor, DeviceDescriptor, Features, Instance, Limits, LoadOp,
|
|
|
|
Operations, PresentMode, RenderPassColorAttachment, RenderPassDescriptor,
|
|
|
|
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)]
|
2022-05-09 21:23:09 -04:00
|
|
|
struct GlyphUserData;
|
2022-05-09 21:10:00 -04:00
|
|
|
|
2022-05-09 21:23:09 -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();
|
|
|
|
let swapchain_format = surface.get_preferred_format(&adapter).unwrap();
|
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-05-09 08:49:10 -04:00
|
|
|
};
|
|
|
|
surface.configure(&device, &config);
|
|
|
|
|
2022-05-09 10:59:23 -04:00
|
|
|
let mut text_renderer = TextRenderer::new(&device, &queue, swapchain_format);
|
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(_) => {
|
2022-05-10 07:18:28 -04:00
|
|
|
let mut layout = Layout::new(CoordinateSystem::PositiveYDown);
|
|
|
|
|
2022-05-09 08:49:10 -04:00
|
|
|
layout.reset(&LayoutSettings {
|
|
|
|
x: 0.0,
|
|
|
|
y: 0.0,
|
|
|
|
..LayoutSettings::default()
|
|
|
|
});
|
|
|
|
|
|
|
|
layout.append(
|
|
|
|
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,
|
2022-05-09 21:23:09 -04:00
|
|
|
GlyphUserData,
|
2022-05-09 21:10:00 -04:00
|
|
|
),
|
2022-05-09 08:49:10 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
text_renderer
|
|
|
|
.prepare(
|
|
|
|
&device,
|
|
|
|
&queue,
|
|
|
|
Resolution {
|
|
|
|
width: config.width,
|
|
|
|
height: config.height,
|
|
|
|
},
|
|
|
|
&fonts,
|
2022-05-10 07:18:28 -04:00
|
|
|
&[layout],
|
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,
|
|
|
|
color_attachments: &[RenderPassColorAttachment {
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
}],
|
|
|
|
depth_stencil_attachment: None,
|
|
|
|
});
|
|
|
|
|
|
|
|
text_renderer.render(&mut pass).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
queue.submit(Some(encoder.finish()));
|
|
|
|
frame.present();
|
|
|
|
}
|
|
|
|
Event::WindowEvent {
|
|
|
|
event: WindowEvent::CloseRequested,
|
|
|
|
..
|
|
|
|
} => *control_flow = ControlFlow::Exit,
|
|
|
|
Event::MainEventsCleared => {
|
|
|
|
window.request_redraw();
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|