Skip to content

Latest commit

 

History

History
60 lines (44 loc) · 2.46 KB

File metadata and controls

60 lines (44 loc) · 2.46 KB

This repository implements the examples from Gabriel Gambetta's Computer Graphics from Scratch book, in Rust. I am not affiliated with Gabriel or his book in any way.

The book uses pseudocode for its examples, but assumes a hypothetical PutPixel function is available that displays a pixel of a given color at given screen coordinates. This repository implements this functionality as a Rust library, and takes care of basic window creation and event handling. You may find it useful if you wish to implement the examples in the book without first having to create something similar yourself. The repository's examples directory contains my implementations of the examples from the book, but this is probably of less interest to you as you likely want to create your own.

Here's an example that creates a window with a fixed canvas 600 pixels wide by 600 pixels high, draws a tiny red "H" in the center and displays the results until the user presses escape or closes the window.

use rust_computer_graphics_from_scratch::canvas::{Canvas, Rgb};

const WIDTH: usize = 600;
const HEIGHT: usize = 600;

fn main() {
    let mut my_canvas = Canvas::new("A tiny red 'H'", WIDTH, HEIGHT);

    let red = Rgb::from_ints(255, 0, 0);

    my_canvas.put_pixel(-1, 1, &red);
    my_canvas.put_pixel(-1, 0, &red);
    my_canvas.put_pixel(-1, -1, &red);
    my_canvas.put_pixel(0, 0, &red);
    my_canvas.put_pixel(1, 1, &red);
    my_canvas.put_pixel(1, 0, &red);
    my_canvas.put_pixel(1, -1, &red);

    my_canvas.display_until_exit();
}

The center of the canvas is the origin, i.e., where x = 0, y = 0. x ranges from -width/2 at the furthest left to width/2 - 1 at the furthest right. y ranges from -height/2 at the bottom to height/2 - 1 at the top. color is passed as an Rgb object formed from separate red, green and blue values.

License

The source code is licensed under the Unlicense license. See LICENSE or https://unlicense.org.

The crate-texture.jpg image is taken from Gabriel Gambetta's GitHub repository and he has released it under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International license (BY-NC-SA). See LICENSE or https://creativecommons.org/licenses/by/4.0/

Contribution

You agree that any contribution you submit for inclusion will be licensed under the Unlicense license.