Handle initial window scale factor

This commit is contained in:
grovesNL 2022-10-28 00:44:15 -02:30 committed by Josh Groves
parent cb68955045
commit 981a7d1682

View file

@ -7,9 +7,10 @@ use wgpu::{
TextureViewDescriptor, TextureViewDescriptor,
}; };
use winit::{ use winit::{
dpi::LogicalSize,
event::{Event, WindowEvent}, event::{Event, WindowEvent},
event_loop::{ControlFlow, EventLoop}, event_loop::{ControlFlow, EventLoop},
window::Window, window::WindowBuilder,
}; };
fn main() { fn main() {
@ -33,6 +34,18 @@ impl HasColor for GlyphUserData {
static mut FONT_SYSTEM: Option<FontSystem> = None; static mut FONT_SYSTEM: Option<FontSystem> = None;
async fn run() { async fn run() {
// 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
let instance = Instance::new(Backends::all()); let instance = Instance::new(Backends::all());
let adapter = instance let adapter = instance
.request_adapter(&RequestAdapterOptions::default()) .request_adapter(&RequestAdapterOptions::default())
@ -49,11 +62,7 @@ async fn run() {
) )
.await .await
.unwrap(); .unwrap();
let event_loop = EventLoop::new();
let window = Window::new(&event_loop).unwrap();
let surface = unsafe { instance.create_surface(&window) }; let surface = unsafe { instance.create_surface(&window) };
let size = window.inner_size();
// TODO: handle srgb // TODO: handle srgb
let swapchain_format = TextureFormat::Bgra8Unorm; let swapchain_format = TextureFormat::Bgra8Unorm;
let mut config = SurfaceConfiguration { let mut config = SurfaceConfiguration {
@ -66,6 +75,7 @@ async fn run() {
}; };
surface.configure(&device, &config); surface.configure(&device, &config);
// Set up text renderer
unsafe { unsafe {
FONT_SYSTEM = Some(FontSystem::new()); FONT_SYSTEM = Some(FontSystem::new());
} }
@ -77,7 +87,7 @@ async fn run() {
Attrs::new(), Attrs::new(),
TextMetrics::new(32, 44), TextMetrics::new(32, 44),
); );
buffer.set_size(800, 600); buffer.set_size((width as f64 * scale_factor) as i32, (height as f64) as i32);
buffer.set_text(include_str!("./ligature.txt")); buffer.set_text(include_str!("./ligature.txt"));
buffer.shape_until_cursor(); buffer.shape_until_cursor();