pub struct TextEntry(/* private fields */);
Expand description
An entry within a tile mode tilemap.
tile
is the index of the tile, offset from thecharblock
that the background is using. This is a 10-bit value, so indexes are in the range0..=1023
. You cannot index past the end of background VRAM into object VRAM (it just won’t draw properly), but you can index past the end of one charblock into the next charblock.hflip
If you want the tile horizontally flipped.vflip
If you want the tile vertically flipped.palbank
sets the palbank for this tile. If the background is in 8bpp mode this has no effect.
Implementations§
source§impl TextEntry
impl TextEntry
sourcepub const fn new() -> Self
pub const fn new() -> Self
Examples found in repository?
examples/hello.rs (line 63)
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
extern "C" fn main() -> ! {
RUST_IRQ_HANDLER.write(Some(irq_handler));
DISPSTAT.write(DisplayStatus::new().with_irq_vblank(true));
IE.write(IrqBits::VBLANK);
IME.write(true);
if let Ok(mut logger) = MgbaBufferedLogger::try_new(MgbaMessageLevel::Debug) {
writeln!(logger, "hello!").ok();
let fx_u: Fixed<u32, 8> =
Fixed::<u32, 8>::wrapping_from(7) + Fixed::<u32, 8>::from_bits(12);
writeln!(logger, "fixed unsigned: {fx_u:?}").ok();
let fx_i1: Fixed<i32, 8> =
Fixed::<i32, 8>::wrapping_from(8) + Fixed::<i32, 8>::from_bits(15);
writeln!(logger, "fixed signed positive: {fx_i1:?}").ok();
let fx_i2: Fixed<i32, 8> = Fixed::<i32, 8>::wrapping_from(0)
- Fixed::<i32, 8>::wrapping_from(3)
- Fixed::<i32, 8>::from_bits(17);
writeln!(logger, "fixed signed negative: {fx_i2:?}").ok();
}
{
// get our tile data into memory.
Cga8x8Thick.bitunpack_4bpp(CHARBLOCK0_4BPP.as_region(), 0);
}
{
// set up the tilemap
let tsb = TEXT_SCREENBLOCKS.get_frame(31).unwrap();
for y in 0..16 {
let row = tsb.get_row(y).unwrap();
for (x, addr) in row.iter().enumerate().take(16) {
let te = TextEntry::new().with_tile((y * 16 + x) as u16);
addr.write(te);
}
}
}
{
// Set BG0 to use the tilemap we just made, and set it to be shown.
BG0CNT.write(BackgroundControl::new().with_screenblock(31));
DISPCNT.write(DisplayControl::new().with_show_bg0(true));
}
let mut x_off = 0_u32;
let mut y_off = 0_u32;
let mut backdrop_color = Color(0);
loop {
VBlankIntrWait();
// show current frame
BACKDROP_COLOR.write(backdrop_color);
BG0HOFS.write(x_off as u16);
BG0VOFS.write(y_off as u16);
// prep next frame
let k = FRAME_KEYS.read();
backdrop_color = Color(k.to_u16());
if k.up() {
y_off = y_off.wrapping_add(1);
}
if k.down() {
y_off = y_off.wrapping_sub(1);
}
if k.left() {
x_off = x_off.wrapping_add(1);
}
if k.right() {
x_off = x_off.wrapping_sub(1);
}
}
}
More examples
examples/game.rs (line 101)
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
extern "C" fn main() -> ! {
// game simulation setup
let mut creatures = [Position::default(); 5];
creatures[0].x = 11;
creatures[0].y = 14;
//
creatures[1].x = 44;
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;
// indexing with `[y][x]`
let mut world = [[0_u8; 32]; 32];
for i in 0..32 {
world[0][i] = Cga8x8Thick::BOX_HORIZONTAL;
world[19][i] = Cga8x8Thick::BOX_HORIZONTAL;
world[i][0] = Cga8x8Thick::BOX_VERTICAL;
world[i][29] = Cga8x8Thick::BOX_VERTICAL;
}
world[0][0] = Cga8x8Thick::BOX_UPPER_LEFT;
world[0][29] = Cga8x8Thick::BOX_UPPER_RIGHT;
world[19][0] = Cga8x8Thick::BOX_LOWER_LEFT;
world[19][29] = Cga8x8Thick::BOX_LOWER_RIGHT;
world[1][3] = b'B';
world[2][3] = b'G';
world[3][3] = b'0';
// interrupt configuration
DISPSTAT.write(DisplayStatus::new().with_irq_vblank(true));
IE.write(IrqBits::VBLANK);
IME.write(true);
// bg
BG_PALETTE.index(1).write(Color::MAGENTA);
// 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 = TEXT_SCREENBLOCKS.get_frame(8).unwrap();
for y in 0..32 {
let row = screenblock.get_row(y).unwrap();
for (x, addr) in row.iter().enumerate() {
let te = TextEntry::new().with_tile(world[y][x] as u16);
addr.write(te);
}
}
let no_display = ObjAttr0::new().with_style(ObjDisplayStyle::NotDisplayed);
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 {
// wait for vblank
VBlankIntrWait();
// update graphics MMIO
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);
}
// 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();
// 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.
let (player, enemies) = match &mut creatures {
[player, enemies @ ..] => (player, enemies),
};
if keys.up() {
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 };
let terrain_clear = new_r
.iter_tiles()
.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 {
*player = new_p;
}
}
if keys.down() {
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 };
let terrain_clear = new_r
.iter_tiles()
.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 {
*player = new_p;
}
}
if keys.left() {
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 };
let terrain_clear = new_r
.iter_tiles()
.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 {
*player = new_p;
}
}
if keys.right() {
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 };
let terrain_clear = new_r
.iter_tiles()
.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 {
*player = new_p;
}
}
}
}
examples/game_vblank_draw.rs (line 127)
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
extern "C" fn main() -> ! {
// game simulation setup
let mut creatures = [Position::default(); 5];
creatures[0].x = 11;
creatures[0].y = 14;
//
creatures[1].x = 44;
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;
// indexing with `[y][x]`
let mut world = [[0_u8; 32]; 32];
for i in 0..32 {
world[0][i] = Cga8x8Thick::BOX_HORIZONTAL;
world[19][i] = Cga8x8Thick::BOX_HORIZONTAL;
world[i][0] = Cga8x8Thick::BOX_VERTICAL;
world[i][29] = Cga8x8Thick::BOX_VERTICAL;
}
world[0][0] = Cga8x8Thick::BOX_UPPER_LEFT;
world[0][29] = Cga8x8Thick::BOX_UPPER_RIGHT;
world[19][0] = Cga8x8Thick::BOX_LOWER_LEFT;
world[19][29] = Cga8x8Thick::BOX_LOWER_RIGHT;
world[1][3] = b'B';
world[2][3] = b'G';
world[3][3] = b'0';
// interrupt configuration
RUST_IRQ_HANDLER.write(Some(irq_handler));
DISPSTAT.write(DisplayStatus::new().with_irq_vblank(true));
IE.write(IrqBits::VBLANK);
IME.write(true);
// bg
BG_PALETTE.index(1).write(Color::MAGENTA);
// 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 = TEXT_SCREENBLOCKS.get_frame(8).unwrap();
for y in 0..32 {
let row = screenblock.get_row(y).unwrap();
for (x, addr) in row.iter().enumerate() {
let te = TextEntry::new().with_tile(world[y][x] as u16);
addr.write(te);
}
}
let no_display = ObjAttr0::new().with_style(ObjDisplayStyle::NotDisplayed);
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 {
// copy the current data into memory that the interrupt handler can see, so
// that the handler can update all the graphics in proper timing with the
// start of vblank.
for (c, c_pos) in creatures.iter().zip(CREATURE_POSITIONS.iter()) {
c_pos.0.write(c.x);
c_pos.1.write(c.y);
}
// wait for vblank, graphics updates during the handler
VBlankIntrWait();
// 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();
// 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.
let (player, enemies) = match &mut creatures {
[player, enemies @ ..] => (player, enemies),
};
if keys.up() {
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 };
let terrain_clear = new_r
.iter_tiles()
.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 {
*player = new_p;
}
}
if keys.down() {
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 };
let terrain_clear = new_r
.iter_tiles()
.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 {
*player = new_p;
}
}
if keys.left() {
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 };
let terrain_clear = new_r
.iter_tiles()
.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 {
*player = new_p;
}
}
if keys.right() {
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 };
let terrain_clear = new_r
.iter_tiles()
.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 {
*player = new_p;
}
}
}
}
pub const fn tile(self) -> u16
sourcepub const fn with_tile(self, val: u16) -> Self
pub const fn with_tile(self, val: u16) -> Self
Examples found in repository?
examples/hello.rs (line 63)
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
extern "C" fn main() -> ! {
RUST_IRQ_HANDLER.write(Some(irq_handler));
DISPSTAT.write(DisplayStatus::new().with_irq_vblank(true));
IE.write(IrqBits::VBLANK);
IME.write(true);
if let Ok(mut logger) = MgbaBufferedLogger::try_new(MgbaMessageLevel::Debug) {
writeln!(logger, "hello!").ok();
let fx_u: Fixed<u32, 8> =
Fixed::<u32, 8>::wrapping_from(7) + Fixed::<u32, 8>::from_bits(12);
writeln!(logger, "fixed unsigned: {fx_u:?}").ok();
let fx_i1: Fixed<i32, 8> =
Fixed::<i32, 8>::wrapping_from(8) + Fixed::<i32, 8>::from_bits(15);
writeln!(logger, "fixed signed positive: {fx_i1:?}").ok();
let fx_i2: Fixed<i32, 8> = Fixed::<i32, 8>::wrapping_from(0)
- Fixed::<i32, 8>::wrapping_from(3)
- Fixed::<i32, 8>::from_bits(17);
writeln!(logger, "fixed signed negative: {fx_i2:?}").ok();
}
{
// get our tile data into memory.
Cga8x8Thick.bitunpack_4bpp(CHARBLOCK0_4BPP.as_region(), 0);
}
{
// set up the tilemap
let tsb = TEXT_SCREENBLOCKS.get_frame(31).unwrap();
for y in 0..16 {
let row = tsb.get_row(y).unwrap();
for (x, addr) in row.iter().enumerate().take(16) {
let te = TextEntry::new().with_tile((y * 16 + x) as u16);
addr.write(te);
}
}
}
{
// Set BG0 to use the tilemap we just made, and set it to be shown.
BG0CNT.write(BackgroundControl::new().with_screenblock(31));
DISPCNT.write(DisplayControl::new().with_show_bg0(true));
}
let mut x_off = 0_u32;
let mut y_off = 0_u32;
let mut backdrop_color = Color(0);
loop {
VBlankIntrWait();
// show current frame
BACKDROP_COLOR.write(backdrop_color);
BG0HOFS.write(x_off as u16);
BG0VOFS.write(y_off as u16);
// prep next frame
let k = FRAME_KEYS.read();
backdrop_color = Color(k.to_u16());
if k.up() {
y_off = y_off.wrapping_add(1);
}
if k.down() {
y_off = y_off.wrapping_sub(1);
}
if k.left() {
x_off = x_off.wrapping_add(1);
}
if k.right() {
x_off = x_off.wrapping_sub(1);
}
}
}
More examples
examples/game.rs (line 101)
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
extern "C" fn main() -> ! {
// game simulation setup
let mut creatures = [Position::default(); 5];
creatures[0].x = 11;
creatures[0].y = 14;
//
creatures[1].x = 44;
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;
// indexing with `[y][x]`
let mut world = [[0_u8; 32]; 32];
for i in 0..32 {
world[0][i] = Cga8x8Thick::BOX_HORIZONTAL;
world[19][i] = Cga8x8Thick::BOX_HORIZONTAL;
world[i][0] = Cga8x8Thick::BOX_VERTICAL;
world[i][29] = Cga8x8Thick::BOX_VERTICAL;
}
world[0][0] = Cga8x8Thick::BOX_UPPER_LEFT;
world[0][29] = Cga8x8Thick::BOX_UPPER_RIGHT;
world[19][0] = Cga8x8Thick::BOX_LOWER_LEFT;
world[19][29] = Cga8x8Thick::BOX_LOWER_RIGHT;
world[1][3] = b'B';
world[2][3] = b'G';
world[3][3] = b'0';
// interrupt configuration
DISPSTAT.write(DisplayStatus::new().with_irq_vblank(true));
IE.write(IrqBits::VBLANK);
IME.write(true);
// bg
BG_PALETTE.index(1).write(Color::MAGENTA);
// 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 = TEXT_SCREENBLOCKS.get_frame(8).unwrap();
for y in 0..32 {
let row = screenblock.get_row(y).unwrap();
for (x, addr) in row.iter().enumerate() {
let te = TextEntry::new().with_tile(world[y][x] as u16);
addr.write(te);
}
}
let no_display = ObjAttr0::new().with_style(ObjDisplayStyle::NotDisplayed);
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 {
// wait for vblank
VBlankIntrWait();
// update graphics MMIO
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);
}
// 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();
// 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.
let (player, enemies) = match &mut creatures {
[player, enemies @ ..] => (player, enemies),
};
if keys.up() {
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 };
let terrain_clear = new_r
.iter_tiles()
.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 {
*player = new_p;
}
}
if keys.down() {
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 };
let terrain_clear = new_r
.iter_tiles()
.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 {
*player = new_p;
}
}
if keys.left() {
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 };
let terrain_clear = new_r
.iter_tiles()
.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 {
*player = new_p;
}
}
if keys.right() {
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 };
let terrain_clear = new_r
.iter_tiles()
.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 {
*player = new_p;
}
}
}
}
examples/game_vblank_draw.rs (line 127)
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
extern "C" fn main() -> ! {
// game simulation setup
let mut creatures = [Position::default(); 5];
creatures[0].x = 11;
creatures[0].y = 14;
//
creatures[1].x = 44;
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;
// indexing with `[y][x]`
let mut world = [[0_u8; 32]; 32];
for i in 0..32 {
world[0][i] = Cga8x8Thick::BOX_HORIZONTAL;
world[19][i] = Cga8x8Thick::BOX_HORIZONTAL;
world[i][0] = Cga8x8Thick::BOX_VERTICAL;
world[i][29] = Cga8x8Thick::BOX_VERTICAL;
}
world[0][0] = Cga8x8Thick::BOX_UPPER_LEFT;
world[0][29] = Cga8x8Thick::BOX_UPPER_RIGHT;
world[19][0] = Cga8x8Thick::BOX_LOWER_LEFT;
world[19][29] = Cga8x8Thick::BOX_LOWER_RIGHT;
world[1][3] = b'B';
world[2][3] = b'G';
world[3][3] = b'0';
// interrupt configuration
RUST_IRQ_HANDLER.write(Some(irq_handler));
DISPSTAT.write(DisplayStatus::new().with_irq_vblank(true));
IE.write(IrqBits::VBLANK);
IME.write(true);
// bg
BG_PALETTE.index(1).write(Color::MAGENTA);
// 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 = TEXT_SCREENBLOCKS.get_frame(8).unwrap();
for y in 0..32 {
let row = screenblock.get_row(y).unwrap();
for (x, addr) in row.iter().enumerate() {
let te = TextEntry::new().with_tile(world[y][x] as u16);
addr.write(te);
}
}
let no_display = ObjAttr0::new().with_style(ObjDisplayStyle::NotDisplayed);
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 {
// copy the current data into memory that the interrupt handler can see, so
// that the handler can update all the graphics in proper timing with the
// start of vblank.
for (c, c_pos) in creatures.iter().zip(CREATURE_POSITIONS.iter()) {
c_pos.0.write(c.x);
c_pos.1.write(c.y);
}
// wait for vblank, graphics updates during the handler
VBlankIntrWait();
// 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();
// 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.
let (player, enemies) = match &mut creatures {
[player, enemies @ ..] => (player, enemies),
};
if keys.up() {
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 };
let terrain_clear = new_r
.iter_tiles()
.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 {
*player = new_p;
}
}
if keys.down() {
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 };
let terrain_clear = new_r
.iter_tiles()
.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 {
*player = new_p;
}
}
if keys.left() {
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 };
let terrain_clear = new_r
.iter_tiles()
.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 {
*player = new_p;
}
}
if keys.right() {
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 };
let terrain_clear = new_r
.iter_tiles()
.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 {
*player = new_p;
}
}
}
}
pub const fn hflip(self) -> bool
pub const fn with_hflip(self, b: bool) -> Self
pub const fn vflip(self) -> bool
pub const fn with_vflip(self, b: bool) -> Self
pub const fn palbank(self) -> u16
pub const fn with_palbank(self, val: u16) -> Self
Trait Implementations§
source§impl Ord for TextEntry
impl Ord for TextEntry
source§impl PartialOrd for TextEntry
impl PartialOrd for TextEntry
impl Copy for TextEntry
impl Eq for TextEntry
impl StructuralPartialEq for TextEntry
Auto Trait Implementations§
impl Freeze for TextEntry
impl RefUnwindSafe for TextEntry
impl Send for TextEntry
impl Sync for TextEntry
impl Unpin for TextEntry
impl UnwindSafe for TextEntry
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
🔬This is a nightly-only experimental API. (
clone_to_uninit
)