pub struct Vec2d<T> {
    pub x: T,
    pub y: T,
}
Expand description

Basicly a glorified tuple

Fields§

§x: T

x

§y: T

y

Implementations§

Cast the Vec2d to an other Vec2d with a differant inner type

Return the magnitude (hypotenus) of the given Vec2d as f64

Examples found in repository?
src/vector2.rs (line 61)
60
61
62
63
64
65
66
    pub fn norm(&self) -> Self {
        let r: T = self.mag_f64().recip().into();
        Vec2d {
            x: self.x * r,
            y: self.y * r,
        }
    }

Return the magnitude (hypotenus) of the given Vec2d as T

Return the magnitude (hypotenus) of the given Vec2d as T without doing the square root

Examples found in repository?
src/vector2.rs (line 44)
43
44
45
46
47
48
49
50
51
    pub fn mag_f64(&self) -> f32 {
        let mag2: f32 = self.mag2().into();
        mag2.sqrt()
    }
    /// Return the magnitude (hypotenus) of the given Vec2d as T
    pub fn mag(&self) -> T {
        let mag2: f32 = self.mag2().into();
        mag2.sqrt().into()
    }

Return a normalized version of the Vec2d

Return the normal of the given Vec2d

Perform the dot product on the Vec2ds

Perform the cross product on the Vec2ds

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§

The resulting type after applying the + operator.
Performs the + operation. Read more
Performs the += operation. Read more
Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
The resulting type after applying the / operator.
Performs the / operation. Read more
The resulting type after applying the / operator.
Performs the / operation. Read more
Performs the /= operation. Read more
Converts to this type from the input type.
Converts to this type from the input type.
Feeds this value into the given Hasher. Read more
Feeds a slice of this type into the given Hasher. Read more
The resulting type after applying the * operator.
Performs the * operation. Read more
The resulting type after applying the * operator.
Performs the * operation. Read more
Performs the *= operation. Read more
This method tests for self and other values to be equal, and is used by ==.
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
The resulting type after applying the - operator.
Performs the - operation. Read more
Performs the -= operation. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The alignment of pointer.
The type for initializers.
Initializes a with the given initializer. Read more
Dereferences the given pointer. Read more
Mutably dereferences the given pointer. Read more
Drops the object pointed to by the given pointer. Read more
The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.