gba/examples-bak/light_cycle.rs

62 lines
1.8 KiB
Rust
Raw Normal View History

#![no_std]
2018-12-16 14:35:57 +11:00
#![feature(start)]
2018-12-24 09:10:14 +11:00
#![forbid(unsafe_code)]
use gba::{
io::{
2018-12-26 17:19:16 +11:00
display::{spin_until_vblank, spin_until_vdraw, DisplayControlSetting, DisplayMode, DISPCNT},
2018-12-24 09:10:14 +11:00
keypad::read_key_input,
},
2018-12-27 17:13:10 +11:00
vram::bitmap::Mode3,
2018-12-24 09:10:14 +11:00
Color,
};
#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
loop {}
}
#[start]
fn main(_argc: isize, _argv: *const *const u8) -> isize {
2018-12-27 14:19:53 +11:00
const SETTING: DisplayControlSetting = DisplayControlSetting::new().with_mode(DisplayMode::Mode3).with_bg2(true);
2018-12-24 09:10:14 +11:00
DISPCNT.write(SETTING);
2018-12-24 09:10:14 +11:00
let mut px = Mode3::SCREEN_WIDTH / 2;
let mut py = Mode3::SCREEN_HEIGHT / 2;
let mut color = Color::from_rgb(31, 0, 0);
loop {
// read the input for this frame
2018-12-24 09:10:14 +11:00
let this_frame_keys = read_key_input();
// adjust game state and wait for vblank
2018-12-24 09:10:14 +11:00
px = px.wrapping_add(2 * this_frame_keys.column_direction() as usize);
py = py.wrapping_add(2 * this_frame_keys.row_direction() as usize);
spin_until_vblank();
// draw the new game and wait until the next frame starts.
2018-12-24 09:10:14 +11:00
const BLACK: Color = Color::from_rgb(0, 0, 0);
if px >= Mode3::SCREEN_WIDTH || py >= Mode3::SCREEN_HEIGHT {
// out of bounds, reset the screen and position.
Mode3::clear_to(BLACK);
color = color.rotate_left(5);
px = Mode3::SCREEN_WIDTH / 2;
py = Mode3::SCREEN_HEIGHT / 2;
} else {
let color_here = Mode3::read_pixel(px, py);
if color_here != Some(BLACK) {
// crashed into our own line, reset the screen
2018-12-25 09:43:36 +11:00
Mode3::dma_clear_to(BLACK);
color = color.rotate_left(5);
} else {
2018-12-24 09:10:14 +11:00
// draw the new part of the line
Mode3::write_pixel(px, py, color);
Mode3::write_pixel(px, py + 1, color);
Mode3::write_pixel(px + 1, py, color);
Mode3::write_pixel(px + 1, py + 1, color);
}
}
2018-12-24 09:10:14 +11:00
spin_until_vdraw();
}
}