gba/examples/hello_world.rs

80 lines
2.1 KiB
Rust
Raw Normal View History

2019-02-13 19:47:12 +11:00
#![no_std]
#![no_main]
2019-02-13 19:47:12 +11:00
use gba::prelude::*;
2019-02-13 19:47:12 +11:00
#[panic_handler]
#[allow(unused)]
2019-02-13 19:47:12 +11:00
fn panic(info: &core::panic::PanicInfo) -> ! {
// This kills the emulation with a message if we're running inside an
// emulator we support (mGBA or NO$GBA), or just crashes the game if we
// aren't.
//fatal!("{}", info);
loop {
DISPCNT.read();
}
2019-02-13 19:47:12 +11:00
}
2019-02-15 15:18:21 +11:00
/// Performs a busy loop until VBlank starts.
///
/// This is very inefficient, and please keep following the lessons until we
/// cover how interrupts work!
pub fn spin_until_vblank() {
while VCOUNT.read() < 160 {}
2019-02-15 15:18:21 +11:00
}
/// Performs a busy loop until VDraw starts.
///
/// This is very inefficient, and please keep following the lessons until we
/// cover how interrupts work!
pub fn spin_until_vdraw() {
while VCOUNT.read() >= 160 {}
2019-02-15 15:18:21 +11:00
}
#[no_mangle]
pub fn main() -> ! {
const SETTING: DisplayControl = DisplayControl::new().with_display_mode(3).with_display_bg2(true);
2019-02-13 19:47:12 +11:00
DISPCNT.write(SETTING);
let mut px = mode3::WIDTH / 2;
let mut py = mode3::HEIGHT / 2;
let mut color = Color::from_rgb(31, 3, 1);
2019-02-13 19:47:12 +11:00
loop {
2019-02-15 13:16:09 +11:00
// read our keys for this frame
let keys: Keys = KEYINPUT.read().into();
2019-02-15 13:16:09 +11:00
2019-02-15 13:39:34 +11:00
// adjust game state and wait for vblank
px = px.wrapping_add((2 * keys.x_signum()) as usize);
py = py.wrapping_add((2 * keys.y_signum()) as usize);
if keys.l() {
2019-02-15 13:39:34 +11:00
color = Color(color.0.rotate_left(5));
}
if keys.r() {
2019-02-15 13:39:34 +11:00
color = Color(color.0.rotate_right(5));
}
// now we wait
spin_until_vblank();
// draw the new game and wait until the next frame starts.
if (px + 1) >= mode3::WIDTH || (py + 1) >= mode3::HEIGHT {
2019-02-15 13:39:34 +11:00
// out of bounds, reset the screen and position.
mode3::dma3_clear_to(Color::from_rgb(0, 0, 0));
px = mode3::WIDTH / 2;
py = mode3::HEIGHT / 2;
color = Color(color.0.rotate_left(7));
2019-02-15 13:39:34 +11:00
} else {
// draw the new part of the line
mode3::bitmap_xy(px, py).write(color);
mode3::bitmap_xy(px, py + 1).write(color);
mode3::bitmap_xy(px + 1, py).write(color);
mode3::bitmap_xy(px + 1, py + 1).write(color);
2019-02-15 13:39:34 +11:00
}
// now we wait again
spin_until_vdraw();
2019-02-13 19:47:12 +11:00
}
}