From d28325f2ff7fd22cd0d6fc7c75808ee20d3af1cc Mon Sep 17 00:00:00 2001 From: Steins7 Date: Mon, 23 Jan 2023 21:48:23 +0100 Subject: [PATCH] Add sprite center configuration For now, the center is juste defined by coordinates in a haphazard way, this will need a proper implementation at some point --- src/main.rs | 4 +++- src/renderer/matrix.rs | 38 ++++++++++++++++++++++-------------- src/sprite.rs | 17 +++++++++------- src/sprite/shape_sprite.rs | 8 ++++++-- src/sprite/text_sprite.rs | 16 +++++++++------ src/sprite/texture_sprite.rs | 12 ++++++++---- src/texture.rs | 9 ++++----- src/utils.rs | 15 ++++++++++++-- 8 files changed, 77 insertions(+), 42 deletions(-) diff --git a/src/main.rs b/src/main.rs index 81bb219..d125ac7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -73,11 +73,13 @@ impl Application for ExampleApp { let mut sub_sprite2 = canvas.create_texture_sprite(Size {w: 200, h: 200}); sub_sprite2.set_texture(texture.clone(), Some(Position {x: 100, y: 0}), 1.0); + sub_sprite2.set_center(Position {x:-100, y:100}); sub_sprite2.set_rotation(0.0); sub_sprite2.set_position(Position {x: 0, y: 0}); let mut txt_sprite = canvas.create_text_sprite("00", Size {w: 100, h: 100}, 22.0); - txt_sprite.set_position(Position {x:50, y: 720-50}); + txt_sprite.set_center(Position {x:-50, y:50}); + txt_sprite.set_position(Position {x:0, y:720}); txt_sprite.set_scale(1.0); canvas.clear(); diff --git a/src/renderer/matrix.rs b/src/renderer/matrix.rs index fad06c9..30ec820 100644 --- a/src/renderer/matrix.rs +++ b/src/renderer/matrix.rs @@ -16,6 +16,7 @@ pub const MATRIX_SIZE: usize = 48;//std::mem::size_of::>(); //--ModelMatrix struct------------------------------------------------------------------------------ pub struct ModelMatrix { position: Position, + center: Position, rotation: Deg, scale: f32, aspect_matrix: Matrix3, @@ -27,8 +28,8 @@ pub struct ModelMatrix { impl ModelMatrix { pub fn new(renderer: &WgpuRenderer, - pos: Position, - rot: f32, + position: Position, + rotation: f32, scale: f32) -> Self { @@ -39,8 +40,9 @@ impl ModelMatrix { * Matrix3::from_translation(-surface_size/2.0); Self { - position: pos, - rotation: Deg (rot), + position, + center: Position::origin(), + rotation: Deg(rotation), scale, aspect_matrix, @@ -53,13 +55,8 @@ impl ModelMatrix { Self::new(renderer, Position::origin(), 0.0, 1.0) } - pub fn set_position(&mut self, pos: Position) { - self.position = pos; - self.is_synced = false; - } - - pub fn set_rotation(&mut self, rot: f32) { - self.rotation = Deg (rot); + pub fn set_position(&mut self, position: Position) { + self.position = position; self.is_synced = false; } @@ -68,17 +65,28 @@ impl ModelMatrix { self.is_synced = false; } + pub fn set_rotation(&mut self, rotation: f32) { + self.rotation = Deg(rotation); + self.is_synced = false; + } + + pub fn set_center(&mut self, center: Position) { + self.center = center; + self.is_synced = false; + } + pub fn get_uniform(&mut self) -> &mut Uniform { use cgmath::{Basis3, Rotation3}; if self.is_synced == false { - let translation_mat = Matrix3::from_translation(self.position.into()); - let rotation_mat = Matrix3::from(Basis3::from_angle_z(self.rotation)); + let position = Matrix3::from_translation(self.position.into()); //same factor 2 as for the aspect ratio and the offset, no idea why its needed either - let scale_mat = Matrix3::from_scale(self.scale/2.0); + let scale = Matrix3::from_scale(self.scale/2.0); + let rotation = Matrix3::from(Basis3::from_angle_z(self.rotation)); + let center = Matrix3::from_translation((self.center*-2.0).into()); - let matrix = self.aspect_matrix * translation_mat * scale_mat * rotation_mat; + let matrix = self.aspect_matrix * position * scale * rotation * center; let mat_bytes: [u8; 36] = bytemuck::bytes_of(&matrix).try_into().unwrap(); diff --git a/src/sprite.rs b/src/sprite.rs index f8c0cf2..47af0b3 100644 --- a/src/sprite.rs +++ b/src/sprite.rs @@ -27,19 +27,22 @@ use crate::{ /// well as the unique function necessary for it to be rendered pub trait Sprite { - /// Set the position of the [Sprite] on the screen - fn set_position(&mut self, pos: Position); + /// Sets the position of the [`Sprite`] on the screen + fn set_position(&mut self, position: Position); - /// Set the rotation of the [Sprite] on the screen - fn set_rotation(&mut self, rot: f32); + /// Sets the center of the [`Sprite`] for rotations as well as translations + fn set_center(&mut self, center: Position); - /// Set the alpha of the [Sprite] on the screen + /// Sets the rotation of the [`Sprite`] on the screen + fn set_rotation(&mut self, rotation: f32); + + /// Sets the alpha of the [`Sprite`] on the screen fn set_alpha(&mut self, alpha: u8); - /// Set the scale of the [Sprite] on the screen + /// Sets the scale of the [`Sprite`] on the screen fn set_scale(&mut self, scale: f32); - /// Renders the [Sprite] using the given rendering context + /// Renders the [`Sprite`] using the given rendering context fn render(&mut self, renderer: &mut WgpuRenderer); } diff --git a/src/sprite/shape_sprite.rs b/src/sprite/shape_sprite.rs index a05ba0c..cfcb1e4 100644 --- a/src/sprite/shape_sprite.rs +++ b/src/sprite/shape_sprite.rs @@ -35,11 +35,15 @@ impl ShapeSprite { impl Sprite for ShapeSprite { - fn set_position(&mut self, _pos: Position) { + fn set_position(&mut self, _position: Position) { todo!(); } - fn set_rotation(&mut self, _rot: f32) { + fn set_center(&mut self, _center: Position) { + todo!(); + } + + fn set_rotation(&mut self, _rotation: f32) { todo!(); } diff --git a/src/sprite/text_sprite.rs b/src/sprite/text_sprite.rs index c4af1f1..5a3f62d 100644 --- a/src/sprite/text_sprite.rs +++ b/src/sprite/text_sprite.rs @@ -124,8 +124,8 @@ impl TextSprite { self.texture_sprite.set_pixel( Position { - x: (glyph_x + x) as u32, - y: (glyph_y + y) as u32, + x: (glyph_x + x) as i32, + y: (glyph_y + y) as i32, }, self.text_color.with_alpha(bitmap[x + y * glyph.width]) ); @@ -145,12 +145,16 @@ impl TextSprite { impl Sprite for TextSprite { - fn set_position(&mut self, pos: Position) { - self.texture_sprite.set_position(pos); + fn set_position(&mut self, position: Position) { + self.texture_sprite.set_position(position); } - fn set_rotation(&mut self, rot: f32) { - self.texture_sprite.set_rotation(rot); + fn set_center(&mut self, center: Position) { + self.texture_sprite.set_center(center); + } + + fn set_rotation(&mut self, rotation: f32) { + self.texture_sprite.set_rotation(rotation); } fn set_alpha(&mut self, alpha: u8) { diff --git a/src/sprite/texture_sprite.rs b/src/sprite/texture_sprite.rs index 0f433b7..6aaefc6 100644 --- a/src/sprite/texture_sprite.rs +++ b/src/sprite/texture_sprite.rs @@ -142,12 +142,16 @@ impl TextureSprite { impl Sprite for TextureSprite { - fn set_position(&mut self, pos: Position) { - self.matrix.set_position(pos); + fn set_position(&mut self, position: Position) { + self.matrix.set_position(position); } - fn set_rotation(&mut self, rot: f32) { - self.matrix.set_rotation(rot); + fn set_center(&mut self, center: Position) { + self.matrix.set_center(center); + } + + fn set_rotation(&mut self, rotation: f32) { + self.matrix.set_rotation(rotation); } fn set_alpha(&mut self, _alpha: u8) { diff --git a/src/texture.rs b/src/texture.rs index 1572b47..8921dc1 100644 --- a/src/texture.rs +++ b/src/texture.rs @@ -30,7 +30,7 @@ impl TextureHandle { //TODO check pos ? let mut texture = self.texture.borrow_mut(); - let width = texture.size.w; + let width = texture.size.w as i32; texture.buffer[(pos.x + pos.y*width) as usize] = pix; texture.is_synced = false; } @@ -51,9 +51,9 @@ impl TextureHandle { { //TODO check offset and pos ? let mut texture = self.texture.borrow_mut(); - let width = texture.size.w; - for x in offset.x..(offset.x + size.w) { - for y in offset.y..(offset.y + size.h) { + let width = texture.size.w as i32; + for x in offset.x..(offset.x + size.w as i32) { + for y in offset.y..(offset.y + size.h as i32) { func(&mut texture.buffer[(x + y*width) as usize]); } } @@ -76,4 +76,3 @@ impl TextureHandle { } } - diff --git a/src/utils.rs b/src/utils.rs index 14a8393..62e5cf6 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -8,8 +8,8 @@ use cgmath::{Vector3, Vector2}; //--Position struct--------------------------------------------------------------------------------- #[derive(Copy, Clone, Debug)] pub struct Position { - pub x: u32, - pub y: u32, + pub x: i32, + pub y: i32, } impl Position { @@ -47,6 +47,17 @@ impl std::ops::Add for Position { } } +impl std::ops::Mul for Position { + type Output = Self; + + fn mul(self, rhs: f32) -> Self::Output { + Position { + x: ((self.x as f32) * rhs) as i32, + y: ((self.y as f32) * rhs) as i32, + } + } +} + //--Size struct------------------------------------------------------------------------------------- #[derive(Copy, Clone)] pub struct Size {