window resize well and render is not stretched

This commit is contained in:
Arkitu 2024-09-07 20:01:12 +02:00
parent 53f521fc62
commit d5701cfae4
4 changed files with 33 additions and 53 deletions

View File

@ -36,7 +36,7 @@ impl Default for Uniforms {
pub struct Graphics<'a> {
// window: &'a Window,
surface_config: SurfaceConfiguration,
pub surface_config: SurfaceConfiguration,
surface: Surface<'a>,
device: Device,
render_pipeline: RenderPipeline,
@ -47,10 +47,8 @@ pub struct Graphics<'a> {
uniforms_bind_group: BindGroup
}
impl<'a> Graphics<'a> {
pub async fn init(/*window: &'a Window, state: &'a mut State*/app: &App<'a>) -> Self {
pub async fn init(app: &App<'a>) -> Self {
let mut size = app.window.inner_size();
size.width = size.width.max(1);
size.height = size.height.max(1);
let instance = wgpu::Instance::new(wgpu::InstanceDescriptor {
backends: wgpu::Backends::PRIMARY,
@ -169,7 +167,7 @@ impl<'a> Graphics<'a> {
});
let surface_config = surface
.get_default_config(&adapter, size.width, size.height)
.get_default_config(&adapter, 1, 1)
.unwrap();
surface.configure(&device, &surface_config);
@ -193,8 +191,8 @@ impl<'a> Graphics<'a> {
..
} => {
// Reconfigure the surface with the new size
self.surface_config.width = new_size.width.max(1);
self.surface_config.height = new_size.height.max(1);
self.surface_config.width = new_size.width;
self.surface_config.height = new_size.height;
self.surface.configure(&self.device, &self.surface_config);
// On macos the window needs to be redrawn manually after resizing
window.request_redraw();
@ -207,40 +205,6 @@ impl<'a> Graphics<'a> {
_ => {}
}
}
// pub fn run(&mut self, event_loop: EventLoop<()>) {
// event_loop.run(move |event, target| {
// // Have the closure take ownership of the resources.
// // `event_loop.run` never returns, therefore we must do this to ensure
// // the resources are properly cleaned up.
// let _ = &self;
// match event {
// Event::WindowEvent {
// event: WindowEvent::Resized(new_size),
// ..
// } => {
// // Reconfigure the surface with the new size
// self.surface_config.width = new_size.width.max(1);
// self.surface_config.height = new_size.height.max(1);
// self.surface.configure(&self.device, &self.surface_config);
// // On macos the window needs to be redrawn manually after resizing
// self.window.request_redraw();
// },
// Event::WindowEvent {
// event: WindowEvent::CloseRequested,
// ..
// } => target.exit(),
// Event::AboutToWait => {
// // RedrawRequested will only trigger once unless we manually
// // request it.
// self.window.request_redraw();
// },
// _ => {}
// }
// })
// .unwrap();
// }
pub fn render(&self, state: &State) {
let frame = self.surface
.get_current_texture()

View File

@ -3,7 +3,7 @@ mod state;
use state::State;
use graphics::Graphics;
use winit::{event::{Event, WindowEvent}, event_loop::EventLoop, window::Window};
use winit::{dpi::PhysicalSize, event::{Event, WindowEvent}, event_loop::EventLoop, window::Window};
struct App<'a> {
// event_loop: EventLoop<()>,
@ -33,7 +33,7 @@ impl<'a> App<'a> {
} => {
if let Some(g) = &mut self.graphics {
self.state.update_if_needed();
self.state.render();
self.state.render(self.window.inner_size());
g.update(&self.state);
g.render(&self.state);
}

View File

@ -3,7 +3,7 @@ use log::trace;
use map::{CellKind, Map};
use rand::prelude::*;
use voronoice::Point;
use winit::{event::{DeviceEvent, Event, KeyEvent, MouseButton, MouseScrollDelta, WindowEvent}, keyboard::{KeyCode, PhysicalKey}, window::Window};
use winit::{dpi::PhysicalSize, event::{DeviceEvent, Event, KeyEvent, MouseButton, MouseScrollDelta, WindowEvent}, keyboard::{KeyCode, PhysicalKey}, window::Window};
use crate::graphics::{Uniforms, Vertex};
@ -11,12 +11,14 @@ mod entity;
mod map;
mod ui;
use entity::{Entity, EntityKind, ExternOp};
use ui::UI;
pub struct State {
pub vertices: Vec<Vertex>,
pub indices: Vec<u32>,
pub uniforms: Uniforms,
map: Map,
ui: UI,
start: Instant,
last_frame: Instant,
t: usize, // Time in frames
@ -37,7 +39,7 @@ impl State {
last_frame: Instant::now(),
t: 0,
map: Map::new(0, 0),
// entities: Vec::new(),
ui: UI::new(),
selected_tile: 0,
mouse_pressed: false,
framerate: 1.,
@ -45,7 +47,8 @@ impl State {
next_eid: 0,
cells_entities: HashMap::new()
};
s.render();
// Create vertices / indices to estimate vertex / index buffer size
s.render(PhysicalSize::new(1, 1));
s
}
pub fn event(&mut self, event: &Event<()>, window: &Window) {
@ -72,10 +75,15 @@ impl State {
},
Event::WindowEvent { event: WindowEvent::CursorMoved { position, .. }, ..} => {
let w_size = window.inner_size();
dbg!(position);
let pos = Point {
x: ((2.*position.x/(w_size.width as f64))-1.)*(self.uniforms.camera[2] as f64) + self.uniforms.camera[0] as f64,
y: (1.-(2.*position.y/(w_size.height as f64)))*(self.uniforms.camera[2] as f64) + self.uniforms.camera[1] as f64
x: (((position.x / w_size.width as f64)*2.)-1.)*(w_size.width as f64/w_size.height as f64).min(1.)*(self.uniforms.camera[2] as f64) + self.uniforms.camera[0] as f64,
y: -(((position.y / w_size.height as f64)*2.)-1.)*(w_size.height as f64/w_size.width as f64).min(1.)*(self.uniforms.camera[2] as f64) + self.uniforms.camera[1] as f64
};
// let pos = Point {
// x: ((2.*position.x/(w_size.width.max(w_size.height) as f64))-1.)*(self.uniforms.camera[2] as f64) + self.uniforms.camera[0] as f64,
// y: (1.-(2.*position.y/(w_size.height.max(w_size.width) as f64)))*(self.uniforms.camera[2] as f64) + self.uniforms.camera[1] as f64
// };
let c = self.map.voronoi.cell(self.selected_tile);
if self.mouse_pressed {
for i in c.iter_path(pos.clone()) {
@ -126,7 +134,10 @@ impl State {
_ => {}
}
}
pub fn render(&mut self) {
pub fn render(&mut self, screen_size: PhysicalSize<u32>) {
let screen_size = (screen_size.width as f32, screen_size.height as f32);
let y_ratio = (screen_size.0 / screen_size.1).max(1.);
let x_ratio = (screen_size.1 / screen_size.0).max(1.);
trace!("render");
self.vertices = Vec::new();
self.indices = Vec::new();
@ -141,7 +152,7 @@ impl State {
let vs = c.iter_vertices().collect::<Vec<_>>();
let i = self.vertices.len() as u32;
for v in vs.iter() {
self.vertices.push(Vertex { pos: [v.x as f32, v.y as f32], color });
self.vertices.push(Vertex { pos: [v.x as f32 * x_ratio, v.y as f32 * y_ratio], color });
}
for v in 1..(vs.len()-1) as u32 {
self.indices.push(i);
@ -164,7 +175,7 @@ impl State {
let vs = c.iter_vertices().collect::<Vec<_>>();
let i = self.vertices.len() as u32;
for v in vs.iter() {
self.vertices.push(Vertex { pos: [v.x as f32, v.y as f32], color });
self.vertices.push(Vertex { pos: [v.x as f32 * x_ratio, v.y as f32 * y_ratio], color });
}
for v in 1..(vs.len()-1) as u32 {
self.indices.push(i);

View File

@ -1,8 +1,13 @@
struct UI {
use crate::graphics::Vertex;
pub struct UI {
}
impl UI {
fn new() -> Self {
pub fn new() -> Self {
Self {}
}
pub fn render(&self, vertices: &mut Vec<Vertex>, indices: &mut Vec<u32>) {
}
}