Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
October 8, 2021 04:49 pm GMT

Hacktoberfest Week 1

This is my first time participating in hacktoberfest! Here's a small recap of my first PR :)

Repo

This year I have been getting into Rust so I used github search to find any issues in a rust repository with the hacktoberfest label. This lead me to Scion which is a 2d game library written in Rust.

Issue

The problem I had to solve was being able to pivot a rectangle about the center. The relevant issue can be found here

Solution

The first thing I did was the implement the new_with_offset function which creates a new rectangle with the passed offsets. At first I only had 1 offset parameter but that is wrong because a rectangle might have different width and height unlike a square

pub fn new_with_offset(        width: f32,        height: f32,        uvs: Option<[Coordinates; 4]>,        x_offset: f32,        y_offset: f32,    ) -> Self {        let a = Coordinates::new(0. - x_offset, 0. - y_offset);        let b = Coordinates::new(a.x(), a.y() + height);        let c = Coordinates::new(a.x() + width, a.y() + height);        let d = Coordinates::new(a.x() + width, a.y());        let uvs_ref = uvs.unwrap_or(default_uvs());        let contents = [            TexturedGlVertex::from((&a, &uvs_ref[0])),            TexturedGlVertex::from((&b, &uvs_ref[1])),            TexturedGlVertex::from((&c, &uvs_ref[2])),            TexturedGlVertex::from((&d, &uvs_ref[3])),        ];        Self { width, height, vertices: [a, b, c, d], uvs: Some(uvs_ref), contents, dirty: false }    }

then I called this function from a newly defined pivot function

pub fn pivot(self, pivot: Pivot) -> Self {        let (x_offset, y_offset) = match pivot {            Pivot::TopLeft => (0., 0.),            Pivot::Center => (self.width / 2., self.height / 2.),        };        Rectangle::new_with_offset(self.width, self.height, self.uvs, x_offset, y_offset)    }

I also called use it in the new function

pub fn new(width: f32, height: f32, uvs: Option<[Coordinates; 4]>) -> Self {        Rectangle::new_with_offset(width, height, uvs, 0., 0.)    }

The relevant PR can be found here

Thoughts

It was really fun contributing to Scion. I am not really into game development so it was an interesting change of pace to contribute to something I am not familiar with. The experience was fun overall and I am looking forward to contributing to more repos this month :)

Thanks for reading!


Original Link: https://dev.to/antoniobennett/hacktoberfest-week-1-4ebc

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To