From 7a46eff82c81a3dfa750515de76b9679dcc5776c Mon Sep 17 00:00:00 2001 From: Steins7 Date: Sat, 20 Aug 2022 17:04:15 +0200 Subject: [PATCH] Moved surface size handling to renderer Canvas is no longer resizeable by the interface as that would shift all sprites on the screen. The user can still do it through the API --- src/lib.rs | 5 +++-- src/main.rs | 7 ++++++- src/renderer.rs | 8 +++++--- src/sprite.rs | 36 +++++++++++++++++++----------------- 4 files changed, 33 insertions(+), 23 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 749bec6..9332b46 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -27,7 +27,7 @@ pub fn run_canvas>(title: &'stat let event_loop = EventLoop::new(); let window = WindowBuilder::new() .with_inner_size(size) - //.with_resizable(false) + .with_resizable(false) .with_title(title) .with_visible(false) //keep window invisible until we are ready to write to it .build(&event_loop) @@ -63,7 +63,8 @@ pub fn run_canvas>(title: &'stat }, WindowEvent::ScaleFactorChanged {new_inner_size, ..} => { // new_inner_size is &&mut so we have to dereference it twice - canvas.set_size((*new_inner_size).into()); + // canvas does not support resize, do nothing + //canvas.set_size((*new_inner_size).into()); }, _ => (), }, diff --git a/src/main.rs b/src/main.rs index 7d52cc8..4692394 100644 --- a/src/main.rs +++ b/src/main.rs @@ -48,6 +48,7 @@ struct ExampleState { pub last_instant: Instant, pub last_offset: u32, pub last_pos: Position, + pub last_rot: f32, } struct ExampleApp {} @@ -80,6 +81,7 @@ impl Application for ExampleApp { last_instant, last_offset: 0, last_pos: Position::origin(), + last_rot: 0.0, }) } @@ -100,9 +102,12 @@ impl Application for ExampleApp { state.last_offset += 1; state.last_pos.x += 1; + state.last_rot += 1.0; state.sub_sprite.set_texture(state.texture.clone(), Some(Position {x: state.last_offset, y: 0})); - state.sub_sprite.set_position(state.last_pos); + //state.sub_sprite.set_position(state.last_pos); + state.sub_sprite.set_rotation(state.last_rot); + state.sub_sprite.set_scale(state.last_rot/1000.0); // inputs //if canvas.key_pressed(Key::A) { diff --git a/src/renderer.rs b/src/renderer.rs index 4e7ceb9..601eb9f 100644 --- a/src/renderer.rs +++ b/src/renderer.rs @@ -317,11 +317,13 @@ impl WgpuRenderer { render_pass.set_index_buffer(gpu_mesh.index_buffer.slice(..), wgpu::IndexFormat::Uint16); render_pass.set_bind_group(0, &texture.bind_group, &[]); - debug!("mat: {:#?}", matrix.get_matrix()); render_pass.set_push_constants( wgpu::ShaderStages::VERTEX, - 0, - bytemuck::bytes_of(&(self.aspect_matrix * matrix.get_matrix())) + 0, + bytemuck::bytes_of(&( + self.aspect_matrix + * matrix.compute_matrix(self.surface_size) + )) ); render_pass.draw_indexed(0..gpu_mesh.index_number, 0, 0..1); diff --git a/src/sprite.rs b/src/sprite.rs index d652630..358fc5d 100644 --- a/src/sprite.rs +++ b/src/sprite.rs @@ -33,7 +33,7 @@ pub trait Sprite { //--ModelMatrix struct------------------------------------------------------------------------------ pub struct ModelMatrix { - position: Vector2, + position: Position, rotation: Deg, scale: f32, @@ -43,7 +43,7 @@ pub struct ModelMatrix { impl ModelMatrix { - pub fn new(pos: Vector2, rot: f32, scale: f32) -> Self { + pub fn new(pos: Position, rot: f32, scale: f32) -> Self { use cgmath::SquareMatrix; Self { @@ -57,10 +57,10 @@ impl ModelMatrix { } pub fn default() -> Self { - Self::new(Vector2 {x: 0.1, y: 0.1}, 0.0, 1.0) + Self::new(Position::origin(), 0.0, 1.0) } - pub fn set_position(&mut self, pos: Vector2) { + pub fn set_position(&mut self, pos: Position) { self.position = pos; self.is_synced = false; } @@ -75,17 +75,20 @@ impl ModelMatrix { self.is_synced = false; } - pub fn get_matrix(&mut self) -> Matrix3 { + pub fn compute_matrix(&mut self, size: Size) -> Matrix3 { use cgmath::{Basis3, Rotation3, SquareMatrix}; if self.is_synced == false { - let rot_mat = Matrix3::from(Basis3::from_angle_z(self.rotation)); - debug!("rot_mat : {:#?}", rot_mat); + + let pos_vec = Vector2 { + x: self.position.x as f32 / size.w as f32, + y: self.position.y as f32 / size.h as f32, + }; + let rotation_mat = Matrix3::from(Basis3::from_angle_z(self.rotation)); let scale_mat = Matrix3::from_scale(self.scale); - debug!("scale_mat : {:#?}", scale_mat); - let pos_mat = Matrix3::from_translation(self.position); - debug!("pos_mat : {:#?}", pos_mat); - self.matrix = pos_mat * rot_mat * scale_mat; + let translation_mat = Matrix3::from_translation(pos_vec); + + self.matrix = translation_mat * rotation_mat * scale_mat; self.is_synced = true; } @@ -132,19 +135,19 @@ impl TextureSprite { let mesh = Mesh { vertices: [ TextureVertex { - position: [0.0, 0.0], + position: [-0.5, -0.5], tex_coords: [x_offset , y_offset + y_size], }, TextureVertex { - position: [1.0, 0.0], + position: [ 0.5, -0.5], tex_coords: [x_offset + x_size, y_offset + y_size], }, TextureVertex { - position: [1.0, 1.0], + position: [ 0.5, 0.5], tex_coords: [x_offset + x_size, y_offset ], }, TextureVertex { - position: [0.0, 1.0], + position: [-0.5, 0.5], tex_coords: [x_offset , y_offset ], }, ], @@ -171,8 +174,7 @@ impl TextureSprite { impl Sprite for TextureSprite { fn set_position(&mut self, pos: Position) { - let normalized_pos = Vector2 { x: pos.x as f32 / 720.0, y: pos.y as f32 / 1280.0 }; - self.matrix.set_position(normalized_pos); + self.matrix.set_position(pos); } fn set_rotation(&mut self, rot: f32) {