Skip to content

Commit

Permalink
Add priority set / get and set_visible / is_visible to maps (#563)
Browse files Browse the repository at this point in the history
Adds `.priority()`, `.set_priority()` and `.is_visible()` and replace
`show` and `hide` with `.set_visible()` in `RegularMap`, `AffineMap` and
`InfiniteScrolledMap`.

- [x] Changelog updated / no changelog update needed
  • Loading branch information
gwilymk authored Feb 16, 2024
2 parents 77dba9f + 34b5b5f commit e610a1c
Show file tree
Hide file tree
Showing 23 changed files with 127 additions and 91 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added
- Added `.priority()`, `.set_priority()` and `.is_visible()` to `RegularMap`, `AffineMap` and `InfiniteScrolledMap`.
- Replaced `.show()` and `.hide()` with `.set_visible()`in `RegularMap`, `AffineMap` and `InfiniteScrolledMap`.

## [0.18.1] - 2024/02/06

### Added
Expand Down
2 changes: 1 addition & 1 deletion agb/examples/affine_background.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fn main(mut gba: agb::Gba) -> ! {
}

bg.commit(&mut vram);
bg.show();
bg.set_visible(true);

let mut rotation = num!(0.);
let rotation_increase: Num<i32, 16> = num!(0.01);
Expand Down
2 changes: 1 addition & 1 deletion agb/examples/animated_background.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ fn main(mut gba: agb::Gba) -> ! {
}

bg.commit(&mut vram);
bg.show();
bg.set_visible(true);

let mut i = 0;
loop {
Expand Down
2 changes: 1 addition & 1 deletion agb/examples/chicken.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ fn main(mut gba: agb::Gba) -> ! {
);
}

background.show();
background.set_visible(true);
background.commit(&mut vram);

let object = gba.display.object.get_managed();
Expand Down
2 changes: 1 addition & 1 deletion agb/examples/dynamic_tiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ fn main(mut gba: agb::Gba) -> ! {
}

bg.commit(&mut vram);
bg.show();
bg.set_visible(true);

loop {
vblank.wait_for_vblank();
Expand Down
2 changes: 1 addition & 1 deletion agb/examples/mixer_32768.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ fn main(mut gba: Gba) -> ! {
writer.commit();

bg.commit(&mut vram);
bg.show();
bg.set_visible(true);

let timer_controller = gba.timers.timers();
let mut timer = timer_controller.timer2;
Expand Down
2 changes: 1 addition & 1 deletion agb/examples/stereo_sound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ fn main(mut gba: Gba) -> ! {
writer.commit();

bg.commit(&mut vram);
bg.show();
bg.set_visible(true);

let timer_controller = gba.timers.timers();
let mut timer = timer_controller.timer2;
Expand Down
2 changes: 1 addition & 1 deletion agb/examples/text_render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ fn main(mut gba: agb::Gba) -> ! {
writer.commit();

bg.commit(&mut vram);
bg.show();
bg.set_visible(true);

let mut frame = 0;

Expand Down
2 changes: 1 addition & 1 deletion agb/src/display/example_logo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub fn display_logo(map: &mut RegularMap, vram: &mut VRamManager) {
map.fill_with(vram, &agb_logo::test_logo);

map.commit(vram);
map.show();
map.set_visible(true);
}

#[cfg(test)]
Expand Down
4 changes: 2 additions & 2 deletions agb/src/display/font.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,14 +314,14 @@ mod tests {
writeln!(&mut writer, "World!").unwrap();
writer.commit();
bg.commit(&mut vram);
bg.show();
bg.set_visible(true);

// Test writing with same renderer after showing background
let mut writer = renderer.writer(1, 2, &mut bg, &mut vram);
writeln!(&mut writer, "This is a font rendering example").unwrap();
writer.commit();
bg.commit(&mut vram);
bg.show();
bg.set_visible(true);

crate::test_runner::assert_image_output("examples/font/font-test-output.png");
renderer.clear(&mut vram);
Expand Down
34 changes: 26 additions & 8 deletions agb/src/display/tiled/infinite_scrolled_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use super::{
};

use crate::{
display,
display::{self, Priority},
fixnum::{Rect, Vector2D},
};

Expand Down Expand Up @@ -69,7 +69,7 @@ use crate::{
///
/// backdrop.set_pos(&mut vram, (3, 5).into());
/// backdrop.commit(&mut vram);
/// backdrop.show();
/// backdrop.set_visible(true);
/// # }
/// ```
pub struct InfiniteScrolledMap<'a> {
Expand Down Expand Up @@ -389,14 +389,32 @@ impl<'a> InfiniteScrolledMap<'a> {
PartialUpdateStatus::Done
}

/// Makes the map visible
pub fn show(&mut self) {
self.map.show();
/// Sets wether the map is visible
/// Use [is_visible](Self::is_visible) to get the value
pub fn set_visible(&mut self, visible: bool) {
self.map.set_visible(visible);
}

/// Hides the map
pub fn hide(&mut self) {
self.map.hide();
/// Checks whether the map is not marked as hidden
/// Use [set_visible](Self::set_visible) to set the value
#[must_use]
pub fn is_visible(&self) -> bool {
self.map.is_visible()
}

/// Sets the map priority
/// This require to call [commit](Self::commit) in order to apply the value
/// Use [priority](Self::priority) to get the value
pub fn set_priority(&mut self, priority: Priority) {
self.map.set_priority(priority);
}

/// Returns the latest map priority set
/// This will only be the currently applied priority if you called [commit](Self::commit) before calling this function
/// Use [set_priority](Self::set_priority) to set the value
#[must_use]
pub fn priority(&self) -> Priority {
self.map.priority()
}

/// Copies data to vram. Needs to be called during vblank if possible
Expand Down
48 changes: 40 additions & 8 deletions agb/src/display/tiled/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ trait TiledMapPrivate: TiledMapTypes {
/// it is 'sealed' so you cannot implement this yourself.
pub trait TiledMap: TiledMapTypes {
fn clear(&mut self, vram: &mut VRamManager);
fn show(&mut self);
fn hide(&mut self);
fn set_visible(&mut self, visible: bool);
fn is_visible(&self) -> bool;
fn commit(&mut self, vram: &mut VRamManager);
fn size(&self) -> Self::Size;
}
Expand All @@ -70,16 +70,22 @@ where
}
}

fn show(&mut self) {
/// Sets wether the map is visible
/// Use [is_visible](TiledMap::is_visible) to get the value
fn set_visible(&mut self, visible: bool) {
let mode = DISPLAY_CONTROL.get();
let new_mode = mode | (1 << (self.background_id() + 0x08)) as u16;
let new_mode = if visible {
mode | (1 << (self.background_id() + 0x08)) as u16
} else {
mode & !(1 << (self.background_id() + 0x08)) as u16
};
DISPLAY_CONTROL.set(new_mode);
}

fn hide(&mut self) {
let mode = DISPLAY_CONTROL.get();
let new_mode = mode & !(1 << (self.background_id() + 0x08)) as u16;
DISPLAY_CONTROL.set(new_mode);
/// Checks whether the map is not marked as hidden
/// Use [set_visible](TiledMap::set_visible) to set the value
fn is_visible(&self) -> bool {
DISPLAY_CONTROL.get() & (1 << (self.background_id() + 0x08)) > 0
}

fn commit(&mut self, vram: &mut VRamManager) {
Expand Down Expand Up @@ -266,6 +272,21 @@ impl RegularMap {
*self.tiles_dirty() = true;
}

/// Returns the latest map priority set
/// This will only be the currently applied priority if you called [commit](TiledMap::commit) before calling this function
/// Use [set_priority](Self::set_priority) to set the value
#[must_use]
pub fn priority(&self) -> Priority {
self.priority
}

/// Sets the map priority
/// This require to call [commit](TiledMap::commit) in order to apply the value
/// Use [priority](Self::priority) to get the value
pub fn set_priority(&mut self, priority: Priority) {
self.priority = priority;
}

#[must_use]
pub fn scroll_pos(&self) -> Vector2D<i16> {
self.scroll
Expand Down Expand Up @@ -386,6 +407,17 @@ impl AffineMap {
self.transform = transformation.into();
}

// Gets the map priority
#[must_use]
pub fn priority(&self) -> Priority {
self.priority
}

/// Sets the map priority
pub fn set_priority(&mut self, priority: Priority) {
self.priority = priority;
}

fn bg_affine_matrix(&self) -> MemoryMapped<AffineMatrixBackground> {
unsafe { MemoryMapped::new(0x0400_0000 + 0x10 * self.background_id()) }
}
Expand Down
2 changes: 1 addition & 1 deletion agb/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
/// }
/// }
/// bg.commit(&mut vram);
/// bg.show();
/// bg.set_visible(true);
/// # }
/// ```
///
Expand Down
4 changes: 2 additions & 2 deletions examples/combo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ fn get_game(gba: &mut agb::Gba) -> Game {

bg.set_pos(&mut vram, (0, 0).into());
bg.commit(&mut vram);
bg.show();
bg.set_visible(true);

let mut position: Vector2D<Num<i32, 8>> = (0, 0).into();
let mut game_idx = 0;
Expand Down Expand Up @@ -109,7 +109,7 @@ fn get_game(gba: &mut agb::Gba) -> Game {
}
};

bg.hide();
bg.set_visible(false);
bg.clear(&mut vram);
bg.commit(&mut vram);

Expand Down
15 changes: 5 additions & 10 deletions examples/hyperspace-roll/src/background.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,12 @@ pub fn show_title_screen(background: &mut RegularMap, vram: &mut VRamManager, sf
background.set_scroll_pos((0i16, 0).into());
vram.set_background_palettes(backgrounds::PALETTES);

background.hide();
background.set_visible(false);

background.fill_with(vram, &backgrounds::title);
background.commit(vram);
sfx.frame();
background.show();
background.set_visible(true);
}

pub struct StarBackground<'a> {
Expand Down Expand Up @@ -141,13 +141,8 @@ impl<'a> StarBackground<'a> {
self.background2.commit(vram);
}

pub fn hide(&mut self) {
self.background1.hide();
self.background2.hide();
}

pub fn show(&mut self) {
self.background1.show();
self.background2.show();
pub fn set_visible(&mut self, visible: bool) {
self.background1.set_visible(visible);
self.background2.set_visible(visible);
}
}
6 changes: 3 additions & 3 deletions examples/hyperspace-roll/src/battle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -592,19 +592,19 @@ pub(crate) fn battle_screen(
agb.sfx.frame();
agb.vblank.wait_for_vblank();
help_background.commit(&mut agb.vram);
help_background.show();
help_background.set_visible(true);

if current_battle_state.enemy.health == 0 {
agb.sfx.ship_explode();
help_background.hide();
help_background.set_visible(false);
crate::background::load_help_text(&mut agb.vram, help_background, 3, (0, 0));
crate::background::load_help_text(&mut agb.vram, help_background, 3, (0, 1));
return BattleResult::Win;
}

if current_battle_state.player.health == 0 {
agb.sfx.ship_explode();
help_background.hide();
help_background.set_visible(false);
crate::background::load_help_text(&mut agb.vram, help_background, 3, (0, 0));
crate::background::load_help_text(&mut agb.vram, help_background, 3, (0, 1));
return BattleResult::Loss;
Expand Down
12 changes: 6 additions & 6 deletions examples/hyperspace-roll/src/customise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,9 +292,9 @@ pub(crate) fn customise_screen(
&mut agb.vram,
);
}
descriptions_map.show();
descriptions_map.set_visible(true);
} else {
descriptions_map.hide();
descriptions_map.set_visible(false);
}

let (x, y) = upgrade_position(cursor.upgrade);
Expand All @@ -307,7 +307,7 @@ pub(crate) fn customise_screen(
} else if input.is_just_pressed(Button::A)
&& player_dice.dice[cursor.dice].faces[cursor.face] != upgrades[cursor.upgrade]
{
descriptions_map.hide();
descriptions_map.set_visible(false);

modified.push(Cursor {
dice: cursor.dice,
Expand Down Expand Up @@ -347,12 +347,12 @@ pub(crate) fn customise_screen(
agb.obj.commit();
descriptions_map.commit(&mut agb.vram);
help_background.commit(&mut agb.vram);
help_background.show();
help_background.set_visible(true);
agb.star_background.commit(&mut agb.vram);
}

descriptions_map.hide();
help_background.hide();
descriptions_map.set_visible(false);
help_background.set_visible(false);
crate::background::load_help_text(&mut agb.vram, help_background, 3, (0, 0));
crate::background::load_help_text(&mut agb.vram, help_background, 3, (0, 1));
descriptions_map.clear(&mut agb.vram);
Expand Down
6 changes: 3 additions & 3 deletions examples/hyperspace-roll/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ pub fn main(mut gba: agb::Gba) -> ! {
let mut score_display = NumberDisplay::new((216, 9).into());
score_display.set_value(Some(save::load_high_score()), &agb.obj);
agb.obj.commit();
agb.star_background.hide();
agb.star_background.set_visible(false);

let mut input = agb::input::ButtonController::new();
loop {
Expand All @@ -187,13 +187,13 @@ pub fn main(mut gba: agb::Gba) -> ! {

agb.obj.commit();

help_background.hide();
help_background.set_visible(false);
help_background.clear(&mut agb.vram);
help_background.commit(&mut agb.vram);
agb.sfx.frame();

background::load_palettes(&mut agb.vram);
agb.star_background.show();
agb.star_background.set_visible(true);

loop {
dice = customise::customise_screen(
Expand Down
Loading

0 comments on commit e610a1c

Please sign in to comment.