Tuesday, July 13, 2021

Trying Piston Game Engine in Rust

Piston is a game engine written in Rust. It is a modular game engine with a minimal core abstraction and designed for optimal modularity, making it optional to even use the core modules in many cases.

This example was running in Windows OS. Just make sure you have installed Rust and Cargo.

You can download the source in here

 

Setting up the project

1.  Create new project with cargo

    cargo new --bin getting-started
    cd getting-started

 

2. Setting dependencies in Cargo.toml

    [package]
    name = "spinning-square"
    version = "0.1.0"
    authors = [
        "TyOverby <ty@pre-alpha.com>",
        "Nikita Pekin <contact@nikitapek.in>"
    ]

    [[bin]]
    name = "spinning-square"

    [dependencies]
    piston = "0.53.0"
    piston2d-graphics = "0.40.0"
    pistoncore-glutin_window = "0.69.0"
    piston2d-opengl_graphics = "0.78.0"

 

Copy the code in main.rs

 Open src/main.rs and copy the code below

    extern crate glutin_window;
    extern crate graphics;
    extern crate opengl_graphics;
    extern crate piston;

    use glutin_window::GlutinWindow as Window;
    use opengl_graphics::{GlGraphics, OpenGL};
    use piston::event_loop::{EventSettings, Events};
    use piston::input::{RenderArgs, RenderEvent, UpdateArgs, UpdateEvent};
    use piston::window::WindowSettings;

    pub struct App {
        gl: GlGraphics, // OpenGL drawing backend.
        rotation: f64,  // Rotation for the square.
    }

    impl App {
        fn render(&mut self, args: &RenderArgs) {
            use graphics::*;

            const GREEN: [f32; 4] = [0.0, 1.0, 0.0, 1.0];
            const RED: [f32; 4] = [1.0, 0.0, 0.0, 1.0];

            let square = rectangle::square(0.0, 0.0, 50.0);
            let rotation = self.rotation;
            let (x, y) = (args.window_size[0] / 2.0, args.window_size[1] / 2.0);

            self.gl.draw(args.viewport(), |c, gl| {
                // Clear the screen.
                clear(GREEN, gl);

                let transform = c
                    .transform
                    .trans(x, y)
                    .rot_rad(rotation)
                    .trans(-25.0, -25.0);

                // Draw a box rotating around the middle of the screen.
                rectangle(RED, square, transform, gl);
            });
        }

        fn update(&mut self, args: &UpdateArgs) {
            // Rotate 2 radians per second.
            self.rotation += 2.0 * args.dt;
        }
    }

    fn main() {
        // Change this to OpenGL::V2_1 if not working.
        let opengl = OpenGL::V3_2;

        // Create an Glutin window.
        let mut window: Window = WindowSettings::new("spinning-square", [200, 200])
            .graphics_api(opengl)
            .exit_on_esc(true)
            .build()
            .unwrap();

        // Create a new game and run it.
        let mut app = App {
            gl: GlGraphics::new(opengl),
            rotation: 0.0,
        };

        let mut events = Events::new(EventSettings::new());
        while let Some(e) = events.next(&mut window) {
            if let Some(args) = e.render_args() {
                app.render(&args);
            }

            if let Some(args) = e.update_args() {
                app.update(&args);
            }
        }
    }


Compiling and Running

 Compiling with "cargo build" and it will download all dependencies


Finally, running the code with "cargo run"


You should have a rotating square as shown as picture above





Monday, July 12, 2021

Printing Pyramid Patterns in Rust

 This article is aimed at giving a Rust implementation in printing of pyramid patterns.


First Pattern

Output:

*
**
***
****
*****


fn pyramid(n:i32) {
    for row in 1..=n {
        for _col in 1..=row {
            print!("*");
        }
        println!("");
    }
}

fn main() {
    pyramid(5);
}


Second Pattern

  Output:

    *
   **
  ***
 ****
*****



fn pyramid(n:i32) {
    for row in (1..=n).rev() {
        for col in 1..=n {
            if col >= row {
                print!("*");
            } else {
                print!(" ");
            }            
        }
        println!("");
    }
}

fn main() {
    pyramid(5);
}


Third Pattern

Output:

*****
****
***
**
*


fn pyramid(n:i32) {
    for row in (1..=n).rev() {
        for _col in 1..=row {
            print!("*");
        }
        println!("");
    }
}

fn main() {
    pyramid(5);
}


Fourth Pattern

Output:

*****
 ****
  ***
   **
    *


fn pyramid(n:i32) {

    let mut k = 2 * n - 2;

    for row in (1..=n).rev() {
        for _col in 1..=n-row {
            print!(" ");
        }

        = k - 2;

        for _col in 1..=row {
            print!("*");
        }

        println!("");
    }
}

fn main() {
    pyramid(5);
}



Fifth Pattern

Output:

   *
  ***
 *****
*******



fn pyramid(n:i32) {
    let mut i:i32 = 0;
    let mut j:i32 = 0;
    let mut k:i32 = 0;
    while i < n {
       
        // for spacing
        while k <= (n - i - 2) {
            print!(" ");
            = k + 1;
        }
        0;
       
        // For Patter printing
        while j < (2 * i - 1) {
            print!("*");
            = j + 1;
        }
        0;
        = i + 1;
        println!(" ");
    }
}

fn main() {
    pyramid(5);
}