feat(renderer): added scale_factor to the trait to allow checking for scale factors in the event loop

This commit is contained in:
lisk77 2025-10-26 02:32:50 +02:00
parent dd89d71565
commit 87f0233066
3 changed files with 32 additions and 7 deletions

View file

@ -381,6 +381,9 @@ impl App {
WindowEvent::Resized(physical_size) => { WindowEvent::Resized(physical_size) => {
renderer.resize(*physical_size); renderer.resize(*physical_size);
} }
WindowEvent::ScaleFactorChanged { scale_factor, .. } => {
renderer.set_scale_factor(*scale_factor);
}
WindowEvent::RedrawRequested => { WindowEvent::RedrawRequested => {
window.request_redraw(); window.request_redraw();
match renderer.render() { match renderer.render() {

View file

@ -1,12 +1,15 @@
use comet_colors::Color;
use std::sync::Arc; use std::sync::Arc;
use winit::dpi::PhysicalSize; use winit::dpi::PhysicalSize;
use winit::window::Window; use winit::window::Window;
use comet_colors::Color;
pub trait Renderer: Sized + Send + Sync { pub trait Renderer: Sized + Send + Sync {
fn new(window: Arc<Window>, clear_color: Option<impl Color>) -> Self; fn new(window: Arc<Window>, clear_color: Option<impl Color>) -> Self;
fn size(&self) -> PhysicalSize<u32>; fn size(&self) -> PhysicalSize<u32>;
fn resize(&mut self, new_size: winit::dpi::PhysicalSize<u32>); fn resize(&mut self, new_size: winit::dpi::PhysicalSize<u32>);
fn scale_factor(&self) -> f64;
fn set_scale_factor(&mut self, scale_factor: f64);
fn update(&mut self) -> f32; fn update(&mut self) -> f32;
fn render(&mut self) -> Result<(), wgpu::SurfaceError>; fn render(&mut self) -> Result<(), wgpu::SurfaceError>;
} }

View file

@ -26,6 +26,7 @@ pub struct Renderer2D<'a> {
queue: wgpu::Queue, queue: wgpu::Queue,
config: wgpu::SurfaceConfiguration, config: wgpu::SurfaceConfiguration,
size: PhysicalSize<u32>, size: PhysicalSize<u32>,
scale_factor: f64,
universal_render_pipeline: wgpu::RenderPipeline, universal_render_pipeline: wgpu::RenderPipeline,
texture_bind_group_layout: wgpu::BindGroupLayout, texture_bind_group_layout: wgpu::BindGroupLayout,
texture_sampler: wgpu::Sampler, texture_sampler: wgpu::Sampler,
@ -43,6 +44,7 @@ pub struct Renderer2D<'a> {
impl<'a> Renderer2D<'a> { impl<'a> Renderer2D<'a> {
pub fn new(window: Arc<Window>, clear_color: Option<impl Color>) -> Renderer2D<'a> { pub fn new(window: Arc<Window>, clear_color: Option<impl Color>) -> Renderer2D<'a> {
let size = window.inner_size(); //PhysicalSize::<u32>::new(1920, 1080); let size = window.inner_size(); //PhysicalSize::<u32>::new(1920, 1080);
let scale_factor = window.scale_factor();
let instance = wgpu::Instance::new(wgpu::InstanceDescriptor { let instance = wgpu::Instance::new(wgpu::InstanceDescriptor {
backends: wgpu::Backends::PRIMARY, backends: wgpu::Backends::PRIMARY,
@ -258,6 +260,7 @@ impl<'a> Renderer2D<'a> {
queue, queue,
config, config,
size, size,
scale_factor,
universal_render_pipeline, universal_render_pipeline,
texture_bind_group_layout, texture_bind_group_layout,
texture_sampler, texture_sampler,
@ -294,6 +297,14 @@ impl<'a> Renderer2D<'a> {
} }
} }
pub fn scale_factor(&self) -> f64 {
self.scale_factor
}
pub fn set_scale_factor(&mut self, scale_factor: f64) {
self.scale_factor = scale_factor
}
pub fn add_draw_call(&mut self, draw_call: String, texture: Texture) { pub fn add_draw_call(&mut self, draw_call: String, texture: Texture) {
let draw_info = DrawInfo::new( let draw_info = DrawInfo::new(
draw_call, draw_call,
@ -919,6 +930,14 @@ impl<'a> Renderer for Renderer2D<'a> {
self.resize(new_size) self.resize(new_size)
} }
fn scale_factor(&self) -> f64 {
self.scale_factor()
}
fn set_scale_factor(&mut self, scale_factor: f64) {
self.set_scale_factor(scale_factor);
}
fn update(&mut self) -> f32 { fn update(&mut self) -> f32 {
self.update() self.update()
} }