Skip to content

adi_screen tutorial 2: Redraw

Jeron Aldaron Lau edited this page May 17, 2017 · 1 revision

Tutorial #2: Redraw

Now that we've successfully created a window, we need to do something interesting inside the window.

Change The Background Color

Right now, our main.rs looks like this:

extern crate adi_screen;

use adi_screen::{ Window, Input };

fn main() {
	let mut window = Window::create("project_name",
		include_bytes!("res/logo.ppm"), &[]);

	loop {
		match window.update() {
			Input::Back => break,
			_ => {},
		}
	}
}

Right before the loop, add the following line:

window.background(1.0, 0.0, 0.0);

Go back to your terminal, and use the following command ( that you should have memorized ):

cargo run

You should see a window that has a red background. The background() function is implemented by Window, and changes the color of the window's background. The color is in RGB format. Here's some example colors that you should know:

  • BLACK: 0.0, 0.0, 0.0
  • GRAY: 0.5, 0.5, 0.5
  • WHITE: 1.0, 1.0, 1.0
  • RED: 1.0, 0.0, 0.0
  • GREEN: 0.0, 1.0, 0.0
  • BLUE: 0.0, 0.0, 1.0
  • YELLOW: 1.0, 1.0, 0.0
  • BLUE-GREEN: 0.0, 1.0, 1.0
  • PURPLE: 1.0, 0.0, 1.0
  • ORANGE: 1.0, 0.25, 0.0
  • BROWN: 0.1, 0.1, 0.0

This might seem a bit weird to painters, because paint does subtractive color mixing, and computer screens do additive color mixing. But, it's a good thing to get used to.

Animations

Next, we're going to animate the background. To do so, we need to create another function. Add the following function below use adi_screen::{ Window, Input }; and above fn main() {

fn redraw(window: &mut Window) {
	let disp2 = window.pulse_full_linear(2.0);

	window.background(disp2, disp2, disp2);
}

Now I'll explain what this does.

fn redraw(window: &mut Window) {

This declares a function called redraw. Anything inside the parenthesis is a parameter. Parameters are separated by commas. window: &mut Window is a parameter called window. window's type is a mutable reference to a Window. The name and type are separated by a colon : ( not to be confused with a semi-colon ).

	let disp2 = window.pulse_full_linear(2.0);

As explained in tutorial 1, the let keyword declares a variable. There is no mut keyword so this variable is a constant. pulse_full_linear() is a function implemented by Window. It has one parameter, which is the number of seconds. So, in the code segment above, it will take 2.0 seconds to complete the animation before it starts again. There are actually 4 types of animations:

  • pulse_full_linear(): Return value moves from 0.0 to 1.0 to 0.0 and then repeats.

  • pulse_full_smooth(): Return value moves from 0.0 to 1.0 to 0.0 and then repeats. It moves slower near 0.0 and 1.0.

  • pulse_half_linear(): Return value moves from 0.0 to 1.0 and then repeats.

  • pulse_half_smooth(): Return value moves from 0.0 to 1.0 and then repeats. It moves slower near 0.0 and 1.0.

      window.background(disp2, disp2, disp2);
    

This sets the background to be a shade of gray. White is when the animation hits 1.0 and black is when the animation hits 0.0.

One last change we need to make. Find the part of the code that looks like this:

		match window.update() {
			Input::Back => break,
			_ => {},
		}

Add the following line after Input::Back => break, and before _ => {}:

			Input::Redraw => redraw(&mut window),

If you got lost, here's the full code:

extern crate adi_screen;

use adi_screen::{ Window, Input };

fn redraw(window: &mut Window) {
	let disp2 = window.pulse_full_linear(2.0);

	window.background(disp2, disp2, disp2);
}

fn main() {
	let mut window = Window::create("project_name",
		include_bytes!("res/logo.ppm"), &[]);

	loop {
		match window.update() {
			Input::Back => break,
			Input::Redraw => redraw(&mut window),
			_ => {},
		}
	}
}

Go back to your terminal, and type cargo run, and see the magic!