gba/examples/hello_world.rs

84 lines
2.3 KiB
Rust
Raw Normal View History

2019-02-13 19:47:12 +11:00
#![no_std]
#![feature(start)]
#![forbid(unsafe_code)]
use gba::{
fatal,
2019-02-15 13:39:34 +11:00
io::{
display::{DisplayControlSetting, DisplayMode, DISPCNT, VBLANK_SCANLINE, VCOUNT},
keypad::read_key_input,
},
2019-02-15 13:16:09 +11:00
vram::bitmap::Mode3,
Color,
2019-02-13 19:47:12 +11:00
};
#[panic_handler]
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.
2019-02-13 19:47:12 +11:00
fatal!("{}", info);
}
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() < VBLANK_SCANLINE {}
}
/// 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() >= VBLANK_SCANLINE {}
}
2019-02-13 19:47:12 +11:00
#[start]
fn main(_argc: isize, _argv: *const *const u8) -> isize {
const SETTING: DisplayControlSetting =
2019-02-15 13:16:09 +11:00
DisplayControlSetting::new().with_mode(DisplayMode::Mode3).with_bg2(true);
2019-02-13 19:47:12 +11:00
DISPCNT.write(SETTING);
2019-02-15 13:16:09 +11:00
let mut px = Mode3::WIDTH / 2;
let mut py = Mode3::HEIGHT / 2;
let mut color = Color::from_rgb(31, 0, 0);
2019-02-13 19:47:12 +11:00
loop {
2019-02-15 13:16:09 +11:00
// read our keys for this frame
let this_frame_keys = read_key_input();
2019-02-15 13:39:34 +11:00
// adjust game state and wait for vblank
px = px.wrapping_add((2 * this_frame_keys.x_tribool() as i32) as usize);
py = py.wrapping_add((2 * this_frame_keys.y_tribool() as i32) as usize);
2019-02-15 13:39:34 +11:00
if this_frame_keys.l() {
color = Color(color.0.rotate_left(5));
}
if this_frame_keys.r() {
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 >= Mode3::WIDTH || py >= Mode3::HEIGHT {
// out of bounds, reset the screen and position.
2019-02-15 15:18:21 +11:00
Mode3::dma_clear_to(Color::from_rgb(0, 0, 0));
2019-02-15 13:39:34 +11:00
px = Mode3::WIDTH / 2;
py = Mode3::HEIGHT / 2;
} else {
// draw the new part of the line
Mode3::write(px, py, color);
Mode3::write(px, py + 1, color);
Mode3::write(px + 1, py, color);
Mode3::write(px + 1, py + 1, color);
}
// now we wait again
spin_until_vdraw();
2019-02-13 19:47:12 +11:00
}
}