add blending to the window example

This commit is contained in:
Corwin 2022-08-06 19:04:48 +01:00
parent ff5a1fbbba
commit 921c26f7c2

View file

@ -1,9 +1,10 @@
#![no_std]
#![no_main]
use agb::display::blend::{BlendMode, Layer};
use agb::display::{example_logo, tiled::RegularBackgroundSize, window::WinIn};
use agb::display::{HEIGHT, WIDTH};
use agb::fixnum::{Num, Rect, Vector2D};
use agb::fixnum::{num, Num, Rect, Vector2D};
use agb::interrupt::VBlank;
type FNum = Num<i32, 8>;
@ -27,11 +28,27 @@ fn main(mut gba: agb::Gba) -> ! {
.set_position(&Rect::new((10, 10).into(), (64, 64).into()))
.enable();
window
.win_out()
.enable()
.set_background_enable(map.background(), true)
.set_blend_enable(true);
example_logo::display_logo(&mut map, &mut vram);
let mut blend = gba.display.blend.get();
blend
.set_background_enable(Layer::Top, map.background(), true)
.set_backdrop_enable(Layer::Bottom, true)
.set_blend_mode(BlendMode::Normal);
let mut pos: Vector2D<FNum> = (10, 10).into();
let mut velocity: Vector2D<FNum> = Vector2D::new(1.into(), 1.into());
let mut blend_amount: Num<i32, 8> = num!(0.5);
let mut blend_velocity: Num<i32, 8> = Num::new(1) / 128;
let vblank = VBlank::get();
loop {
@ -45,11 +62,21 @@ fn main(mut gba: agb::Gba) -> ! {
velocity.y *= -1;
}
blend_amount += blend_velocity;
if blend_amount > num!(0.75) || blend_amount < num!(0.25) {
blend_velocity *= -1;
}
blend_amount = blend_amount.clamp(0.into(), 1.into());
blend.set_blend_weight(Layer::Top, blend_amount.try_change_base().unwrap());
window
.win_in(WinIn::Win0)
.set_position(&Rect::new(pos.floor(), (64, 64).into()));
vblank.wait_for_vblank();
window.commit();
blend.commit();
}
}