gba/examples/game.rs

206 lines
5.8 KiB
Rust
Raw Normal View History

#![no_std]
#![no_main]
use gba::prelude::*;
#[panic_handler]
fn panic_handler(info: &core::panic::PanicInfo) -> ! {
2023-01-01 18:34:14 +11:00
#[cfg(debug_assertions)]
if let Ok(mut logger) = MgbaBufferedLogger::try_new(MgbaMessageLevel::Fatal) {
2023-01-01 18:34:14 +11:00
use core::fmt::Write;
writeln!(logger, "{info}").ok();
}
loop {}
}
2022-12-23 15:10:52 +11:00
#[derive(Debug, Clone, Copy, Default)]
struct Position {
x: u16,
y: u16,
}
#[derive(Debug, Clone, Copy, Default)]
struct Rect {
x: u16,
y: u16,
w: u16,
h: u16,
}
impl Rect {
2023-01-02 12:04:10 +11:00
fn intersect(self, other: Self) -> bool {
2022-12-23 15:10:52 +11:00
self.x < other.x + other.w
&& self.x + self.w > other.x
&& self.y < other.y + other.h
&& self.h + self.y > other.y
}
fn iter_tiles(self) -> impl Iterator<Item = (u16, u16)> {
let y_range_incl = (self.y / 8)..=((self.y + self.h - 1) / 8);
let x_range_incl = (self.x / 8)..=((self.x + self.w - 1) / 8);
y_range_incl
.map(move |y_index| {
x_range_incl.clone().map(move |x_index| (x_index, y_index))
})
.flatten()
}
2022-12-23 15:10:52 +11:00
}
#[no_mangle]
extern "C" fn main() -> ! {
2022-12-20 12:50:25 +11:00
// game simulation setup
2022-12-23 15:10:52 +11:00
let mut creatures = [Position::default(); 5];
2022-12-23 15:14:03 +11:00
creatures[0].x = 11;
creatures[0].y = 14;
2022-12-23 15:10:52 +11:00
//
2023-01-02 12:04:10 +11:00
creatures[1].x = 44;
2022-12-23 15:10:52 +11:00
creatures[1].y = 38;
creatures[2].x = 100;
creatures[2].y = 23;
creatures[3].x = 14;
creatures[3].y = 101;
creatures[4].x = 72;
creatures[4].y = 59;
let mut world = [[0_u8; 32]; 32];
2022-12-20 12:50:25 +11:00
for i in 0..32 {
world[0][i] = b'z';
2023-01-02 12:04:10 +11:00
world[19][i] = b'z';
2022-12-20 12:50:25 +11:00
world[i][0] = b'z';
2023-01-02 12:04:10 +11:00
world[i][29] = b'z';
2022-12-20 12:50:25 +11:00
}
2022-12-23 15:10:52 +11:00
world[1][3] = b'B';
world[2][3] = b'G';
world[3][3] = 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));
2023-01-02 12:04:10 +11:00
// bg
BG_PALETTE.index(1).write(Color::MAGENTA);
2023-01-02 12:04:10 +11:00
// obj
let colors =
[Color::CYAN, Color::GREEN, Color::RED, Color::BLUE, Color::YELLOW];
for (pal, color) in colors.iter().enumerate() {
obj_palbank(pal).index(1).write(*color);
}
Cga8x8Thick.bitunpack_4bpp(CHARBLOCK0_4BPP.as_region(), 0);
Cga8x8Thick.bitunpack_4bpp(OBJ_TILES.as_region(), 0);
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);
}
}
2022-12-10 09:30:01 +11:00
let no_display = ObjAttr0::new().with_style(ObjDisplayStyle::NotDisplayed);
2022-12-23 15:10:52 +11:00
OBJ_ATTR0.iter().skip(creatures.len()).for_each(|va| va.write(no_display));
DISPCNT.write(DisplayControl::new().with_show_obj(true).with_show_bg0(true));
let mut l_was_pressed = false;
let mut r_was_pressed = false;
loop {
2022-12-10 09:30:01 +11:00
// wait for vblank
VBlankIntrWait();
2022-12-10 09:30:01 +11:00
2022-12-20 12:50:25 +11:00
// update graphics MMIO
2022-12-23 15:10:52 +11:00
for (i, (creature_pos, attr_addr)) in
creatures.iter().zip(OBJ_ATTR_ALL.iter()).enumerate()
{
let mut obj = ObjAttr::new();
obj.set_x(creature_pos.x);
obj.set_y(creature_pos.y);
obj.set_tile_id(1);
obj.set_palbank(i as u16);
attr_addr.write(obj);
}
2022-12-10 09:30:01 +11:00
2022-12-20 12:50:25 +11:00
// handle input
let keys = KEYINPUT.read();
if keys.l() && !l_was_pressed {
creatures.rotate_left(1);
}
if keys.r() && !r_was_pressed {
creatures.rotate_right(1);
}
l_was_pressed = keys.l();
r_was_pressed = keys.r();
2022-12-20 12:50:25 +11:00
// the way we handle movement here is per-direction. If you're against a
// wall and you press a diagonal then one axis will progress while the other
// will be halted by the wall. This makes the player slide along the wall
// when bumping into walls.
2022-12-23 15:10:52 +11:00
let (player, enemies) = match &mut creatures {
[player, enemies @ ..] => (player, enemies),
};
2022-12-10 09:30:01 +11:00
if keys.up() {
2022-12-23 15:10:52 +11:00
let new_p = Position { x: player.x, y: player.y - 1 };
let new_r = Rect { x: new_p.x, y: new_p.y, w: 8, h: 8 };
2023-01-01 18:20:11 +11:00
let terrain_clear = new_r
.iter_tiles()
2023-01-01 18:20:11 +11:00
.all(|(tx, ty)| allows_movement(world[ty as usize][tx as usize]));
let enemy_clear = enemies.iter().all(|enemy| {
let enemy_r = Rect { x: enemy.x, y: enemy.y, w: 8, h: 8 };
!new_r.intersect(enemy_r)
});
if terrain_clear && enemy_clear {
2022-12-23 15:10:52 +11:00
*player = new_p;
2022-12-20 12:50:25 +11:00
}
2022-12-10 09:30:01 +11:00
}
if keys.down() {
2022-12-23 15:10:52 +11:00
let new_p = Position { x: player.x, y: player.y + 1 };
let new_r = Rect { x: new_p.x, y: new_p.y, w: 8, h: 8 };
2023-01-01 18:20:11 +11:00
let terrain_clear = new_r
.iter_tiles()
2023-01-01 18:20:11 +11:00
.all(|(tx, ty)| allows_movement(world[ty as usize][tx as usize]));
let enemy_clear = enemies.iter().all(|enemy| {
let enemy_r = Rect { x: enemy.x, y: enemy.y, w: 8, h: 8 };
!new_r.intersect(enemy_r)
});
if terrain_clear && enemy_clear {
2022-12-23 15:10:52 +11:00
*player = new_p;
2022-12-20 12:50:25 +11:00
}
2022-12-10 09:30:01 +11:00
}
if keys.left() {
2022-12-23 15:10:52 +11:00
let new_p = Position { x: player.x - 1, y: player.y };
let new_r = Rect { x: new_p.x, y: new_p.y, w: 8, h: 8 };
2023-01-01 18:20:11 +11:00
let terrain_clear = new_r
.iter_tiles()
2023-01-01 18:20:11 +11:00
.all(|(tx, ty)| allows_movement(world[ty as usize][tx as usize]));
let enemy_clear = enemies.iter().all(|enemy| {
let enemy_r = Rect { x: enemy.x, y: enemy.y, w: 8, h: 8 };
!new_r.intersect(enemy_r)
});
if terrain_clear && enemy_clear {
2022-12-23 15:10:52 +11:00
*player = new_p;
2022-12-20 12:50:25 +11:00
}
2022-12-10 09:30:01 +11:00
}
if keys.right() {
2022-12-23 15:10:52 +11:00
let new_p = Position { x: player.x + 1, y: player.y };
let new_r = Rect { x: new_p.x, y: new_p.y, w: 8, h: 8 };
2023-01-01 18:20:11 +11:00
let terrain_clear = new_r
.iter_tiles()
2023-01-01 18:20:11 +11:00
.all(|(tx, ty)| allows_movement(world[ty as usize][tx as usize]));
let enemy_clear = enemies.iter().all(|enemy| {
let enemy_r = Rect { x: enemy.x, y: enemy.y, w: 8, h: 8 };
!new_r.intersect(enemy_r)
});
if terrain_clear && enemy_clear {
2022-12-23 15:10:52 +11:00
*player = new_p;
2022-12-20 12:50:25 +11:00
}
}
}
}
const fn allows_movement(u: u8) -> bool {
u == 0 || u == b' ' || u == u8::MAX
}