agb/examples/hyperspace-roll/src/background.rs

148 lines
3.9 KiB
Rust
Raw Normal View History

2022-07-25 06:33:51 +10:00
use agb::{
display::tiled::{RegularMap, TileFormat, TileSet, TileSetting, VRamManager},
2022-07-25 06:33:51 +10:00
include_gfx, rng,
};
use crate::sfx::Sfx;
include_gfx!("gfx/stars.toml");
2022-07-25 06:33:51 +10:00
include_gfx!("gfx/help.toml");
2022-08-12 07:22:44 +10:00
pub fn load_palettes(vram: &mut VRamManager) {
vram.set_background_palettes(&[
stars::stars.palettes[0].clone(),
crate::customise::DESCRIPTIONS_1_PALETTE.clone(),
crate::customise::DESCRIPTIONS_2_PALETTE.clone(),
help::help.palettes[0].clone(),
]);
2022-07-25 06:33:51 +10:00
}
pub(crate) fn load_help_text(
vram: &mut VRamManager,
background: &mut RegularMap,
help_text_line: u16,
at_tile: (u16, u16),
) {
let help_tileset = TileSet::new(help::help.tiles, agb::display::tiled::TileFormat::FourBpp);
2022-07-25 06:33:51 +10:00
for x in 0..16 {
background.set_tile(
vram,
(x + at_tile.0, at_tile.1).into(),
&help_tileset,
TileSetting::new(help_text_line * 16 + x, false, false, 3),
2022-07-25 06:33:51 +10:00
)
}
}
// Expects a 64x32 map
fn create_background_map(map: &mut RegularMap, vram: &mut VRamManager, stars_tileset: &TileSet) {
for x in 0..64u16 {
for y in 0..32u16 {
let blank = rng::gen().rem_euclid(32) < 30;
let tile_id = if blank {
(1 << 10) - 1
} else {
rng::gen().rem_euclid(64) as u16
};
let tile_setting = TileSetting::new(tile_id, false, false, 0);
2022-07-25 06:33:51 +10:00
map.set_tile(vram, (x, y).into(), stars_tileset, tile_setting);
}
}
map.set_scroll_pos((0u16, rng::gen().rem_euclid(8) as u16).into());
}
pub fn show_title_screen(background: &mut RegularMap, vram: &mut VRamManager, sfx: &mut Sfx) {
background.set_scroll_pos((0_u16, 0_u16).into());
vram.set_background_palettes(stars::title.palettes);
let tile_set = TileSet::new(stars::title.tiles, agb::display::tiled::TileFormat::FourBpp);
2022-07-25 06:33:51 +10:00
background.hide();
for x in 0..30u16 {
for y in 0..20u16 {
let tile_id = y * 30 + x;
background.set_tile(
vram,
(x, y).into(),
&tile_set,
TileSetting::new(
tile_id,
false,
false,
stars::title.palette_assignments[tile_id as usize],
2022-07-25 06:33:51 +10:00
),
);
}
sfx.frame();
}
background.commit(vram);
sfx.frame();
background.show();
}
pub struct StarBackground<'a> {
background1: &'a mut RegularMap,
background2: &'a mut RegularMap,
background1_timer: u32,
background2_timer: u32,
}
impl<'a> StarBackground<'a> {
pub fn new(
background1: &'a mut RegularMap,
background2: &'a mut RegularMap,
vram: &'_ mut VRamManager,
) -> Self {
let stars_tileset = TileSet::new(stars::stars.tiles, TileFormat::FourBpp);
2022-07-25 06:33:51 +10:00
create_background_map(background1, vram, &stars_tileset);
create_background_map(background2, vram, &stars_tileset);
Self {
background1,
background2,
background1_timer: 0,
background2_timer: 0,
}
}
pub fn update(&mut self) {
if self.background1_timer == 0 {
self.background1
.set_scroll_pos(self.background1.scroll_pos() + (1u16, 0).into());
self.background1_timer = 2;
}
if self.background2_timer == 0 {
self.background2
.set_scroll_pos(self.background2.scroll_pos() + (1u16, 0).into());
self.background2_timer = 3;
}
self.background1_timer -= 1;
self.background2_timer -= 1;
}
pub fn commit(&mut self, vram: &mut VRamManager) {
self.background1.commit(vram);
self.background2.commit(vram);
}
pub fn hide(&mut self) {
self.background1.hide();
self.background2.hide();
}
pub fn show(&mut self) {
self.background1.show();
self.background2.show();
}
}