iter_tiles_of_area took a surprising amount of time to write in a way that avoids duplicate lookups.

This commit is contained in:
Lokathor 2022-12-19 18:24:21 -07:00
parent 7a036ed05e
commit 2c6dff76aa

View file

@ -16,30 +16,46 @@ fn panic_handler(info: &core::panic::PanicInfo) -> ! {
#[no_mangle]
extern "C" fn main() -> ! {
// game simulation
let mut player_x = Wrapping(13);
let mut player_y = Wrapping(37);
let mut world = [[0_u8; 32]; 32];
world[0][0] = b'B';
world[1][0] = b'G';
world[2][0] = b'0';
// hardware configuration
DISPSTAT.write(DisplayStatus::new().with_irq_vblank(true));
IE.write(IrqBits::VBLANK);
IME.write(true);
TIMER0_CONTROL.write(TimerControl::new().with_enabled(true));
Cga8x8Thick.bitunpack_4bpp(CHARBLOCK0_4BPP.as_region(), 0);
Cga8x8Thick.bitunpack_4bpp(OBJ_TILES.as_region(), 0);
BG_PALETTE.index(1).write(Color::MAGENTA);
OBJ_PALETTE.index(1).write(Color::CYAN);
let no_display = ObjAttr0::new().with_style(ObjDisplayStyle::NotDisplayed);
OBJ_ATTR0.iter().for_each(|va| va.write(no_display));
Cga8x8Thick.bitunpack_4bpp(CHARBLOCK0_4BPP.as_region(), 0);
Cga8x8Thick.bitunpack_4bpp(OBJ_TILES.as_region(), 0);
let mut x = Wrapping(13);
let mut y = Wrapping(37);
BG0CNT.write(BackgroundControl::new().with_screenblock(8));
let screenblock_addr = TextScreenblockAddress::new(8);
for row in 0..32 {
for col in 0..32 {
let te = TextEntry::new().with_tile(world[row][col] as u16);
screenblock_addr.row_col(row, col).write(te);
}
}
let mut obj = ObjAttr::new();
obj.set_x(x.0);
obj.set_y(y.0);
obj.set_x(player_x.0);
obj.set_y(player_y.0);
obj.set_tile_id(1);
OBJ_ATTR_ALL.index(0).write(obj);
DISPCNT.write(DisplayControl::new().with_show_obj(true));
let no_display = ObjAttr0::new().with_style(ObjDisplayStyle::NotDisplayed);
OBJ_ATTR0.iter().skip(1).for_each(|va| va.write(no_display));
DISPCNT.write(DisplayControl::new().with_show_obj(true).with_show_bg0(true));
loop {
// wait for vblank
@ -51,18 +67,34 @@ extern "C" fn main() -> ! {
// get input and prepare next frame
let keys = KEYINPUT.read();
if keys.up() {
y -= 1;
player_y -= 1;
}
if keys.down() {
y += 1;
player_y += 1;
}
if keys.left() {
x -= 1;
player_x -= 1;
}
if keys.right() {
x += 1;
player_x += 1;
}
obj.set_x(x.0);
obj.set_y(y.0);
obj.set_x(player_x.0);
obj.set_y(player_y.0);
}
}
const fn allows_movement(u: u8) -> bool {
u == 0 || u == b' ' || u == u8::MAX
}
fn iter_tiles_of_area(
(x, y): (u16, u16), (width, height): (u16, u16),
) -> impl Iterator<Item = (u16, u16)> {
let y_range_incl = (y / 8)..=((y + height - 1) / 8);
let x_range_incl = (x / 8)..=((x + width - 1) / 8);
y_range_incl
.map(move |y_index| {
x_range_incl.clone().map(move |x_index| (x_index, y_index))
})
.flatten()
}