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
This commit is contained in:
Steins7 2023-01-23 21:48:23 +01:00 committed by Steins7
parent 3a630fefaf
commit f6e0619be0
8 changed files with 77 additions and 42 deletions

View File

@ -73,11 +73,13 @@ impl Application<ExampleState> for ExampleApp {
let mut sub_sprite2 = canvas.create_texture_sprite(Size {w: 200, h: 200}); 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_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_rotation(0.0);
sub_sprite2.set_position(Position {x: 0, y: 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); 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); txt_sprite.set_scale(1.0);
canvas.clear(); canvas.clear();

View File

@ -16,6 +16,7 @@ pub const MATRIX_SIZE: usize = 48;//std::mem::size_of::<Matrix3<f32>>();
//--ModelMatrix struct------------------------------------------------------------------------------ //--ModelMatrix struct------------------------------------------------------------------------------
pub struct ModelMatrix { pub struct ModelMatrix {
position: Position, position: Position,
center: Position,
rotation: Deg<f32>, rotation: Deg<f32>,
scale: f32, scale: f32,
aspect_matrix: Matrix3<f32>, aspect_matrix: Matrix3<f32>,
@ -27,8 +28,8 @@ pub struct ModelMatrix {
impl ModelMatrix { impl ModelMatrix {
pub fn new(renderer: &WgpuRenderer, pub fn new(renderer: &WgpuRenderer,
pos: Position, position: Position,
rot: f32, rotation: f32,
scale: f32) scale: f32)
-> Self -> Self
{ {
@ -39,8 +40,9 @@ impl ModelMatrix {
* Matrix3::from_translation(-surface_size/2.0); * Matrix3::from_translation(-surface_size/2.0);
Self { Self {
position: pos, position,
rotation: Deg (rot), center: Position::origin(),
rotation: Deg(rotation),
scale, scale,
aspect_matrix, aspect_matrix,
@ -53,13 +55,8 @@ impl ModelMatrix {
Self::new(renderer, Position::origin(), 0.0, 1.0) Self::new(renderer, Position::origin(), 0.0, 1.0)
} }
pub fn set_position(&mut self, pos: Position) { pub fn set_position(&mut self, position: Position) {
self.position = pos; self.position = position;
self.is_synced = false;
}
pub fn set_rotation(&mut self, rot: f32) {
self.rotation = Deg (rot);
self.is_synced = false; self.is_synced = false;
} }
@ -68,17 +65,28 @@ impl ModelMatrix {
self.is_synced = false; 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<MATRIX_SIZE> { pub fn get_uniform(&mut self) -> &mut Uniform<MATRIX_SIZE> {
use cgmath::{Basis3, Rotation3}; use cgmath::{Basis3, Rotation3};
if self.is_synced == false { if self.is_synced == false {
let translation_mat = Matrix3::from_translation(self.position.into()); let position = Matrix3::from_translation(self.position.into());
let rotation_mat = Matrix3::from(Basis3::from_angle_z(self.rotation));
//same factor 2 as for the aspect ratio and the offset, no idea why its needed either //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(); let mat_bytes: [u8; 36] = bytemuck::bytes_of(&matrix).try_into().unwrap();

View File

@ -27,19 +27,22 @@ use crate::{
/// well as the unique function necessary for it to be rendered /// well as the unique function necessary for it to be rendered
pub trait Sprite { pub trait Sprite {
/// Set the position of the [Sprite] on the screen /// Sets the position of the [`Sprite`] on the screen
fn set_position(&mut self, pos: Position); fn set_position(&mut self, position: Position);
/// Set the rotation of the [Sprite] on the screen /// Sets the center of the [`Sprite`] for rotations as well as translations
fn set_rotation(&mut self, rot: f32); 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); 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); 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); fn render(&mut self, renderer: &mut WgpuRenderer);
} }

View File

@ -35,11 +35,15 @@ impl ShapeSprite {
impl Sprite for ShapeSprite { impl Sprite for ShapeSprite {
fn set_position(&mut self, _pos: Position) { fn set_position(&mut self, _position: Position) {
todo!(); todo!();
} }
fn set_rotation(&mut self, _rot: f32) { fn set_center(&mut self, _center: Position) {
todo!();
}
fn set_rotation(&mut self, _rotation: f32) {
todo!(); todo!();
} }

View File

@ -124,8 +124,8 @@ impl TextSprite {
self.texture_sprite.set_pixel( self.texture_sprite.set_pixel(
Position { Position {
x: (glyph_x + x) as u32, x: (glyph_x + x) as i32,
y: (glyph_y + y) as u32, y: (glyph_y + y) as i32,
}, },
self.text_color.with_alpha(bitmap[x + y * glyph.width]) self.text_color.with_alpha(bitmap[x + y * glyph.width])
); );
@ -145,12 +145,16 @@ impl TextSprite {
impl Sprite for TextSprite { impl Sprite for TextSprite {
fn set_position(&mut self, pos: Position) { fn set_position(&mut self, position: Position) {
self.texture_sprite.set_position(pos); self.texture_sprite.set_position(position);
} }
fn set_rotation(&mut self, rot: f32) { fn set_center(&mut self, center: Position) {
self.texture_sprite.set_rotation(rot); 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) { fn set_alpha(&mut self, alpha: u8) {

View File

@ -142,12 +142,16 @@ impl TextureSprite {
impl Sprite for TextureSprite { impl Sprite for TextureSprite {
fn set_position(&mut self, pos: Position) { fn set_position(&mut self, position: Position) {
self.matrix.set_position(pos); self.matrix.set_position(position);
} }
fn set_rotation(&mut self, rot: f32) { fn set_center(&mut self, center: Position) {
self.matrix.set_rotation(rot); self.matrix.set_center(center);
}
fn set_rotation(&mut self, rotation: f32) {
self.matrix.set_rotation(rotation);
} }
fn set_alpha(&mut self, _alpha: u8) { fn set_alpha(&mut self, _alpha: u8) {

View File

@ -30,7 +30,7 @@ impl TextureHandle {
//TODO check pos ? //TODO check pos ?
let mut texture = self.texture.borrow_mut(); 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.buffer[(pos.x + pos.y*width) as usize] = pix;
texture.is_synced = false; texture.is_synced = false;
} }
@ -51,9 +51,9 @@ impl TextureHandle {
{ {
//TODO check offset and pos ? //TODO check offset and pos ?
let mut texture = self.texture.borrow_mut(); let mut texture = self.texture.borrow_mut();
let width = texture.size.w; let width = texture.size.w as i32;
for x in offset.x..(offset.x + size.w) { for x in offset.x..(offset.x + size.w as i32) {
for y in offset.y..(offset.y + size.h) { for y in offset.y..(offset.y + size.h as i32) {
func(&mut texture.buffer[(x + y*width) as usize]); func(&mut texture.buffer[(x + y*width) as usize]);
} }
} }
@ -76,4 +76,3 @@ impl TextureHandle {
} }
} }

View File

@ -8,8 +8,8 @@ use cgmath::{Vector3, Vector2};
//--Position struct--------------------------------------------------------------------------------- //--Position struct---------------------------------------------------------------------------------
#[derive(Copy, Clone, Debug)] #[derive(Copy, Clone, Debug)]
pub struct Position { pub struct Position {
pub x: u32, pub x: i32,
pub y: u32, pub y: i32,
} }
impl Position { impl Position {
@ -47,6 +47,17 @@ impl std::ops::Add for Position {
} }
} }
impl std::ops::Mul<f32> 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------------------------------------------------------------------------------------- //--Size struct-------------------------------------------------------------------------------------
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
pub struct Size { pub struct Size {