mirror of
https://github.com/italicsjenga/agb.git
synced 2024-12-24 00:31:34 +11:00
Add super simple animated tiles
This commit is contained in:
parent
c623d8b708
commit
d2dceeb656
48
agb/examples/animated_background.rs
Normal file
48
agb/examples/animated_background.rs
Normal file
|
@ -0,0 +1,48 @@
|
||||||
|
#![no_std]
|
||||||
|
#![no_main]
|
||||||
|
|
||||||
|
use agb::{
|
||||||
|
display::{
|
||||||
|
tiled::{TileFormat, TileSet, TileSetting},
|
||||||
|
Priority,
|
||||||
|
},
|
||||||
|
include_gfx,
|
||||||
|
};
|
||||||
|
|
||||||
|
include_gfx!("examples/water_tiles.toml");
|
||||||
|
|
||||||
|
#[agb::entry]
|
||||||
|
fn main(mut gba: agb::Gba) -> ! {
|
||||||
|
let (gfx, mut vram) = gba.display.video.tiled0();
|
||||||
|
let vblank = agb::interrupt::VBlank::get();
|
||||||
|
|
||||||
|
let tileset = TileSet::new(water_tiles::water_tiles.tiles, TileFormat::FourBpp);
|
||||||
|
let tileset_ref = vram.add_tileset(tileset);
|
||||||
|
|
||||||
|
vram.set_background_palettes(water_tiles::water_tiles.palettes);
|
||||||
|
|
||||||
|
let mut bg = gfx.background(Priority::P0);
|
||||||
|
|
||||||
|
for y in 0..20u16 {
|
||||||
|
for x in 0..30u16 {
|
||||||
|
bg.set_tile(
|
||||||
|
&mut vram,
|
||||||
|
(x, y).into(),
|
||||||
|
tileset_ref,
|
||||||
|
TileSetting::new(0, false, false, 0),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bg.commit();
|
||||||
|
bg.show();
|
||||||
|
|
||||||
|
let mut i = 0;
|
||||||
|
loop {
|
||||||
|
i = (i + 1) % 8;
|
||||||
|
|
||||||
|
vram.replace_tile(tileset_ref, 0, tileset_ref, i);
|
||||||
|
|
||||||
|
vblank.wait_for_vblank();
|
||||||
|
}
|
||||||
|
}
|
BIN
agb/examples/water_tiles.png
Normal file
BIN
agb/examples/water_tiles.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 209 B |
5
agb/examples/water_tiles.toml
Normal file
5
agb/examples/water_tiles.toml
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
version = "1.0"
|
||||||
|
|
||||||
|
[image.water_tiles]
|
||||||
|
filename = "water_tiles.png"
|
||||||
|
tile_size = "8x8"
|
|
@ -177,6 +177,45 @@ impl<'a> VRamManager<'a> {
|
||||||
TileReference(NonNull::new(ptr as *mut _).unwrap())
|
TileReference(NonNull::new(ptr as *mut _).unwrap())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn replace_tile(
|
||||||
|
&mut self,
|
||||||
|
source_tile_set_ref: TileSetReference,
|
||||||
|
source_tile: u16,
|
||||||
|
target_tile_set_ref: TileSetReference,
|
||||||
|
target_tile: u16,
|
||||||
|
) {
|
||||||
|
if let Some(&reference) = self
|
||||||
|
.tile_set_to_vram
|
||||||
|
.get(&(source_tile_set_ref.id, source_tile))
|
||||||
|
{
|
||||||
|
let tile_slice = if let ArenaStorageItem::Data(data, generation) =
|
||||||
|
&self.tilesets[target_tile_set_ref.id as usize]
|
||||||
|
{
|
||||||
|
debug_assert_eq!(
|
||||||
|
*generation, target_tile_set_ref.generation,
|
||||||
|
"Stale tile data requested"
|
||||||
|
);
|
||||||
|
|
||||||
|
let tile_offset = (target_tile as usize) * data.format.tile_size();
|
||||||
|
&data.tiles[tile_offset..(tile_offset + data.format.tile_size())]
|
||||||
|
} else {
|
||||||
|
panic!("Target tile set ref must point to an existing tile set reference")
|
||||||
|
};
|
||||||
|
|
||||||
|
let tile_size_in_half_words = tile_slice.len() / 2;
|
||||||
|
|
||||||
|
let target_location = reference.0.as_ptr() as *mut _;
|
||||||
|
|
||||||
|
unsafe {
|
||||||
|
dma_copy16(
|
||||||
|
tile_slice.as_ptr() as *const u16,
|
||||||
|
target_location,
|
||||||
|
tile_size_in_half_words,
|
||||||
|
)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub(crate) fn add_tile(&mut self, tile_set_ref: TileSetReference, tile: u16) -> TileIndex {
|
pub(crate) fn add_tile(&mut self, tile_set_ref: TileSetReference, tile: u16) -> TileIndex {
|
||||||
let reference = self.tile_set_to_vram.get(&(tile_set_ref.id, tile));
|
let reference = self.tile_set_to_vram.get(&(tile_set_ref.id, tile));
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue