Struct pixel_engine_draw::vector2::Vec2d
source · pub struct Vec2d<T> {
pub x: T,
pub y: T,
}
Expand description
Basicly a glorified tuple
Fields§
§x: T
x
y: T
y
Implementations§
source§impl<T> Vec2d<T>where
T: Add<Output = T> + Sub<Output = T> + Mul<Output = T> + Div<Output = T> + Neg<Output = T> + Into<f32> + From<f32> + Copy,
impl<T> Vec2d<T>where
T: Add<Output = T> + Sub<Output = T> + Mul<Output = T> + Div<Output = T> + Neg<Output = T> + Into<f32> + From<f32> + Copy,
source§impl Vec2d<i32>
impl Vec2d<i32>
sourcepub fn cast_u32(self) -> Vec2d<u32>
pub fn cast_u32(self) -> Vec2d<u32>
Examples found in repository?
src/graphics.rs (line 547)
534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698
fn draw<P: Into<Vi2d>>(&mut self, pos: P, col: Color) {
let pixel_mode = self.get_pixel_mode();
let blend_factor = self.get_blend_factor();
let pos @ Vi2d { x, y } = pos.into();
if x >= self.sprite.size().x.try_into().unwrap()
|| y >= self.sprite.size().y.try_into().unwrap()
|| x < 0
|| y < 0
{
return;
}
match pixel_mode {
PixelMode::Normal => unsafe {
self.sprite.set_pixel_unchecked(pos.cast_u32(), col);
},
PixelMode::Mask => {
if col.a == 255 {
unsafe {
self.sprite.set_pixel_unchecked(pos.cast_u32(), col);
}
}
}
PixelMode::Alpha => {
let current_color: Color =
unsafe { self.sprite.get_pixel_unchecked(pos.cast_u32()) };
let alpha: f32 = (f32::from(col.a) / 255.0f32) * blend_factor;
let inverse_alpha: f32 = 1.0 - alpha;
let red: f32 =
alpha * f32::from(col.r) + inverse_alpha * f32::from(current_color.r);
let green: f32 =
alpha * f32::from(col.g) + inverse_alpha * f32::from(current_color.g);
let blue: f32 =
alpha * f32::from(col.b) + inverse_alpha * f32::from(current_color.b);
unsafe {
self.sprite
.set_pixel_unchecked(pos.cast_u32(), [red, green, blue].into());
}
}
}
}
fn get_textsheet(&self) -> &'static Sprite {
create_text()
}
fn clear(&mut self, col: Color) {
let size = self.sprite.size();
for y in 0..size.y {
for x in 0..size.x {
unsafe {
self.sprite.set_pixel_unchecked(Vu2d { x, y }, col);
}
}
}
}
fn get_pixel_mode(&self) -> PixelMode {
self.draw_data.pixel_mode
}
fn get_blend_factor(&self) -> f32 {
self.draw_data.blend_factor
}
fn set_pixel_mode(&mut self, mode: PixelMode) {
self.draw_data.pixel_mode = mode;
}
fn set_blend_factor(&mut self, f: f32) {
self.draw_data.blend_factor = f;
}
}
pub trait DrawSpriteTrait {
fn get_pixel(&self, pos: Vi2d) -> Option<Color>;
fn set_pixel(&mut self, pos: Vi2d, col: Color);
fn size(&self) -> Vu2d;
/// Get the pixel at the given location, but bypassing any bounds check
///
/// # Safety
/// You must ensure that the pos in bounds
unsafe fn get_pixel_unchecked(&self, pos: Vu2d) -> Color;
/// Set the pixel at the given location, but bypassing any bounds check
///
/// # Safety
/// You must ensure that the pos in bounds
unsafe fn set_pixel_unchecked(&mut self, pos: Vu2d, col: Color);
}
impl DrawSpriteTrait for Sprite {
fn get_pixel(&self, pos: Vi2d) -> Option<Color> {
if pos.x < 0 || pos.y < 0 || pos.x as u32 >= self.size().x || pos.y as u32 >= self.size().y
{
return None;
}
let (raw, lock) = self.get_read_lock();
let mut col = Color::BLANK;
let pos = pos.cast_u32();
col.r = raw[(pos.y * self.width() + pos.x) as usize * 4];
col.g = raw[(pos.y * self.width() + pos.x) as usize * 4 + 1];
col.b = raw[(pos.y * self.width() + pos.x) as usize * 4 + 2];
col.a = raw[(pos.y * self.width() + pos.x) as usize * 4 + 3];
drop(lock);
Some(col)
}
fn set_pixel(&mut self, pos: Vi2d, col: Color) {
if pos.x < 0 || pos.y < 0 {
return;
};
self.set_pixel(pos.x as u32, pos.y as u32, col);
}
fn size(&self) -> Vu2d {
*self.size()
}
/// # Safety
/// its up to the caller to make sure that the given position is in bounds
unsafe fn get_pixel_unchecked(&self, pos: Vu2d) -> Color {
let (raw, _lock) = self.get_read_lock();
let mut col = Color::BLANK;
col.r = *raw.get_unchecked((pos.y * self.width() + pos.x) as usize * 4);
col.g = *raw.get_unchecked((pos.y * self.width() + pos.x) as usize * 4 + 1);
col.b = *raw.get_unchecked((pos.y * self.width() + pos.x) as usize * 4 + 2);
col.a = *raw.get_unchecked((pos.y * self.width() + pos.x) as usize * 4 + 3);
col
}
/// # Safety
/// its up to the caller to make sure that the given position is in bounds
unsafe fn set_pixel_unchecked(&mut self, pos: Vu2d, col: Color) {
let Vu2d { x, y } = pos;
let width = self.width();
// SAFETY: Its up to the caller to make sure that the bounds are correct to not write oob
*self
.raw
.get_mut()
.get_unchecked_mut((y * width + x) as usize * 4) = col.r;
*self
.raw
.get_mut()
.get_unchecked_mut((y * width + x) as usize * 4 + 1) = col.g;
*self
.raw
.get_mut()
.get_unchecked_mut((y * width + x) as usize * 4 + 2) = col.b;
*self
.raw
.get_mut()
.get_unchecked_mut((y * width + x) as usize * 4 + 3) = col.a;
}
}
impl<'spr> DrawSpriteTrait for SpriteMutRef<'spr> {
fn get_pixel(&self, pos: Vi2d) -> Option<Color> {
if pos.x < 0 || pos.y < 0 {
return None;
};
self.get_pixel(pos.cast_u32())
}
fn set_pixel(&mut self, pos: Vi2d, col: Color) {
if pos.x < 0 || pos.y < 0 {
return;
};
self.set_pixel(pos.cast_u32(), col);
}
Trait Implementations§
source§impl<T> AddAssign<Vec2d<T>> for Vec2d<T>where
T: AddAssign<T> + Copy,
impl<T> AddAssign<Vec2d<T>> for Vec2d<T>where
T: AddAssign<T> + Copy,
source§fn add_assign(&mut self, rhs: Self)
fn add_assign(&mut self, rhs: Self)
Performs the
+=
operation. Read moresource§impl<T> DivAssign<T> for Vec2d<T>where
T: DivAssign<T> + Copy,
impl<T> DivAssign<T> for Vec2d<T>where
T: DivAssign<T> + Copy,
source§fn div_assign(&mut self, rhs: T)
fn div_assign(&mut self, rhs: T)
Performs the
/=
operation. Read moresource§impl<T> MulAssign<T> for Vec2d<T>where
T: MulAssign<T> + Copy,
impl<T> MulAssign<T> for Vec2d<T>where
T: MulAssign<T> + Copy,
source§fn mul_assign(&mut self, rhs: T)
fn mul_assign(&mut self, rhs: T)
Performs the
*=
operation. Read moresource§impl<T: PartialEq> PartialEq<Vec2d<T>> for Vec2d<T>
impl<T: PartialEq> PartialEq<Vec2d<T>> for Vec2d<T>
source§impl<T> SubAssign<Vec2d<T>> for Vec2d<T>where
T: SubAssign<T> + Copy,
impl<T> SubAssign<Vec2d<T>> for Vec2d<T>where
T: SubAssign<T> + Copy,
source§fn sub_assign(&mut self, rhs: Self)
fn sub_assign(&mut self, rhs: Self)
Performs the
-=
operation. Read more