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
This commit is contained in:
parent
1e9fb49a49
commit
7a46eff82c
@ -27,7 +27,7 @@ pub fn run_canvas<S: 'static + Sized, A: 'static + Application<S>>(title: &'stat
|
|||||||
let event_loop = EventLoop::new();
|
let event_loop = EventLoop::new();
|
||||||
let window = WindowBuilder::new()
|
let window = WindowBuilder::new()
|
||||||
.with_inner_size(size)
|
.with_inner_size(size)
|
||||||
//.with_resizable(false)
|
.with_resizable(false)
|
||||||
.with_title(title)
|
.with_title(title)
|
||||||
.with_visible(false) //keep window invisible until we are ready to write to it
|
.with_visible(false) //keep window invisible until we are ready to write to it
|
||||||
.build(&event_loop)
|
.build(&event_loop)
|
||||||
@ -63,7 +63,8 @@ pub fn run_canvas<S: 'static + Sized, A: 'static + Application<S>>(title: &'stat
|
|||||||
},
|
},
|
||||||
WindowEvent::ScaleFactorChanged {new_inner_size, ..} => {
|
WindowEvent::ScaleFactorChanged {new_inner_size, ..} => {
|
||||||
// new_inner_size is &&mut so we have to dereference it twice
|
// 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());
|
||||||
},
|
},
|
||||||
_ => (),
|
_ => (),
|
||||||
},
|
},
|
||||||
|
|||||||
@ -48,6 +48,7 @@ struct ExampleState {
|
|||||||
pub last_instant: Instant,
|
pub last_instant: Instant,
|
||||||
pub last_offset: u32,
|
pub last_offset: u32,
|
||||||
pub last_pos: Position,
|
pub last_pos: Position,
|
||||||
|
pub last_rot: f32,
|
||||||
}
|
}
|
||||||
|
|
||||||
struct ExampleApp {}
|
struct ExampleApp {}
|
||||||
@ -80,6 +81,7 @@ impl Application<ExampleState> for ExampleApp {
|
|||||||
last_instant,
|
last_instant,
|
||||||
last_offset: 0,
|
last_offset: 0,
|
||||||
last_pos: Position::origin(),
|
last_pos: Position::origin(),
|
||||||
|
last_rot: 0.0,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -100,9 +102,12 @@ impl Application<ExampleState> for ExampleApp {
|
|||||||
|
|
||||||
state.last_offset += 1;
|
state.last_offset += 1;
|
||||||
state.last_pos.x += 1;
|
state.last_pos.x += 1;
|
||||||
|
state.last_rot += 1.0;
|
||||||
state.sub_sprite.set_texture(state.texture.clone(),
|
state.sub_sprite.set_texture(state.texture.clone(),
|
||||||
Some(Position {x: state.last_offset, y: 0}));
|
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
|
// inputs
|
||||||
//if canvas.key_pressed(Key::A) {
|
//if canvas.key_pressed(Key::A) {
|
||||||
|
|||||||
@ -317,11 +317,13 @@ impl WgpuRenderer {
|
|||||||
render_pass.set_index_buffer(gpu_mesh.index_buffer.slice(..),
|
render_pass.set_index_buffer(gpu_mesh.index_buffer.slice(..),
|
||||||
wgpu::IndexFormat::Uint16);
|
wgpu::IndexFormat::Uint16);
|
||||||
render_pass.set_bind_group(0, &texture.bind_group, &[]);
|
render_pass.set_bind_group(0, &texture.bind_group, &[]);
|
||||||
debug!("mat: {:#?}", matrix.get_matrix());
|
|
||||||
render_pass.set_push_constants(
|
render_pass.set_push_constants(
|
||||||
wgpu::ShaderStages::VERTEX,
|
wgpu::ShaderStages::VERTEX,
|
||||||
0,
|
0,
|
||||||
bytemuck::bytes_of(&(self.aspect_matrix * matrix.get_matrix()))
|
bytemuck::bytes_of(&(
|
||||||
|
self.aspect_matrix
|
||||||
|
* matrix.compute_matrix(self.surface_size)
|
||||||
|
))
|
||||||
);
|
);
|
||||||
render_pass.draw_indexed(0..gpu_mesh.index_number, 0, 0..1);
|
render_pass.draw_indexed(0..gpu_mesh.index_number, 0, 0..1);
|
||||||
|
|
||||||
|
|||||||
@ -33,7 +33,7 @@ pub trait Sprite {
|
|||||||
|
|
||||||
//--ModelMatrix struct------------------------------------------------------------------------------
|
//--ModelMatrix struct------------------------------------------------------------------------------
|
||||||
pub struct ModelMatrix {
|
pub struct ModelMatrix {
|
||||||
position: Vector2<f32>,
|
position: Position,
|
||||||
rotation: Deg<f32>,
|
rotation: Deg<f32>,
|
||||||
scale: f32,
|
scale: f32,
|
||||||
|
|
||||||
@ -43,7 +43,7 @@ pub struct ModelMatrix {
|
|||||||
|
|
||||||
impl ModelMatrix {
|
impl ModelMatrix {
|
||||||
|
|
||||||
pub fn new(pos: Vector2<f32>, rot: f32, scale: f32) -> Self {
|
pub fn new(pos: Position, rot: f32, scale: f32) -> Self {
|
||||||
use cgmath::SquareMatrix;
|
use cgmath::SquareMatrix;
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
@ -57,10 +57,10 @@ impl ModelMatrix {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn default() -> Self {
|
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<f32>) {
|
pub fn set_position(&mut self, pos: Position) {
|
||||||
self.position = pos;
|
self.position = pos;
|
||||||
self.is_synced = false;
|
self.is_synced = false;
|
||||||
}
|
}
|
||||||
@ -75,17 +75,20 @@ impl ModelMatrix {
|
|||||||
self.is_synced = false;
|
self.is_synced = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_matrix(&mut self) -> Matrix3<f32> {
|
pub fn compute_matrix(&mut self, size: Size) -> Matrix3<f32> {
|
||||||
use cgmath::{Basis3, Rotation3, SquareMatrix};
|
use cgmath::{Basis3, Rotation3, SquareMatrix};
|
||||||
|
|
||||||
if self.is_synced == false {
|
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);
|
let scale_mat = Matrix3::from_scale(self.scale);
|
||||||
debug!("scale_mat : {:#?}", scale_mat);
|
let translation_mat = Matrix3::from_translation(pos_vec);
|
||||||
let pos_mat = Matrix3::from_translation(self.position);
|
|
||||||
debug!("pos_mat : {:#?}", pos_mat);
|
self.matrix = translation_mat * rotation_mat * scale_mat;
|
||||||
self.matrix = pos_mat * rot_mat * scale_mat;
|
|
||||||
self.is_synced = true;
|
self.is_synced = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -132,19 +135,19 @@ impl TextureSprite {
|
|||||||
let mesh = Mesh {
|
let mesh = Mesh {
|
||||||
vertices: [
|
vertices: [
|
||||||
TextureVertex {
|
TextureVertex {
|
||||||
position: [0.0, 0.0],
|
position: [-0.5, -0.5],
|
||||||
tex_coords: [x_offset , y_offset + y_size],
|
tex_coords: [x_offset , y_offset + y_size],
|
||||||
},
|
},
|
||||||
TextureVertex {
|
TextureVertex {
|
||||||
position: [1.0, 0.0],
|
position: [ 0.5, -0.5],
|
||||||
tex_coords: [x_offset + x_size, y_offset + y_size],
|
tex_coords: [x_offset + x_size, y_offset + y_size],
|
||||||
},
|
},
|
||||||
TextureVertex {
|
TextureVertex {
|
||||||
position: [1.0, 1.0],
|
position: [ 0.5, 0.5],
|
||||||
tex_coords: [x_offset + x_size, y_offset ],
|
tex_coords: [x_offset + x_size, y_offset ],
|
||||||
},
|
},
|
||||||
TextureVertex {
|
TextureVertex {
|
||||||
position: [0.0, 1.0],
|
position: [-0.5, 0.5],
|
||||||
tex_coords: [x_offset , y_offset ],
|
tex_coords: [x_offset , y_offset ],
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
@ -171,8 +174,7 @@ impl TextureSprite {
|
|||||||
impl Sprite for TextureSprite {
|
impl Sprite for TextureSprite {
|
||||||
|
|
||||||
fn set_position(&mut self, pos: Position) {
|
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(pos);
|
||||||
self.matrix.set_position(normalized_pos);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_rotation(&mut self, rot: f32) {
|
fn set_rotation(&mut self, rot: f32) {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user