agb 0.23.1

Library for Game Boy Advance Development
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
#![warn(missing_docs)]
use core::{alloc::Layout, mem};

use alloc::rc::Rc;

use crate::{
    display::{
        GraphicsFrame, Priority,
        tile_data::TileData,
        tiled::{screenblock::Screenblock, tiles::Tiles},
    },
    fixnum::Vector2D,
};

use super::{
    BackgroundControlRegister, DynamicTile16, DynamicTile256, RegularBackgroundCommitData,
    RegularBackgroundData, RegularBackgroundId, SCREENBLOCK_SIZE, TRANSPARENT_TILE_INDEX, Tile,
    TileEffect, TileFormat, TileSet, TileSetting, VRAM_MANAGER,
};

use bilge::prelude::*;

/// The backgrounds in the GameBoy Advance are made of 8x8 tiles. Each different background option lets
/// you decide how big the background should be before it wraps. Ideally, you should use the smallest background
/// size you can while minimising the number of times you have to redraw tiles.
///
/// If you want more space than can be provided here, or want to keep more video ram free, then you should use
/// the [`InfiniteScrolledMap`](super::InfiniteScrolledMap) which will dynamically load tile data for any size
/// as you scroll around.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[repr(u16)]
pub enum RegularBackgroundSize {
    /// 32x32 tiles (256x256 pixels)
    Background32x32 = 0,
    /// 64x32 tiles (512x256 pixels)
    Background64x32 = 1,
    /// 32x64 tiles (256x512 pixels)
    Background32x64 = 2,
    /// 64x64 tiles (512x512 pixels)
    Background64x64 = 3,
}

impl RegularBackgroundSize {
    const fn width(self) -> usize {
        match self {
            RegularBackgroundSize::Background32x32 => 32,
            RegularBackgroundSize::Background64x32 => 64,
            RegularBackgroundSize::Background32x64 => 32,
            RegularBackgroundSize::Background64x64 => 64,
        }
    }

    const fn height(self) -> usize {
        match self {
            RegularBackgroundSize::Background32x32 => 32,
            RegularBackgroundSize::Background64x32 => 32,
            RegularBackgroundSize::Background32x64 => 64,
            RegularBackgroundSize::Background64x64 => 64,
        }
    }

    const fn size_in_bytes(self) -> usize {
        self.num_tiles() * mem::size_of::<Tile>()
    }

    pub(crate) fn layout(self) -> Layout {
        Layout::from_size_align(self.size_in_bytes(), SCREENBLOCK_SIZE)
            .expect("failed to create layout, should never happen")
    }

    pub(crate) const fn num_tiles(self) -> usize {
        self.width() * self.height()
    }

    const fn gba_offset(self, pos: Vector2D<i32>) -> usize {
        let x_mod = (pos.x & (self.width() as i32 - 1)) as u32;
        let y_mod = (pos.y & (self.height() as i32 - 1)) as u32;

        let screenblock = (x_mod / 32) + (y_mod / 32) * (self.width() as u32 / 32);

        let pos = screenblock * 32 * 32 + (x_mod % 32 + 32 * (y_mod % 32));

        pos as usize
    }
}

/// Represents a collections of background tiles.
///
/// Note that while this is in scope, space in
/// the GBA's VRAM will be allocated and unavailable for other backgrounds. You should use the
/// smallest [`RegularBackgroundSize`] you can while still being able to render the scene you want.
///
/// You can show up to 4 regular backgrounds at once (or 2 regular backgrounds and 1 [affine background](super::AffineBackground)).
///
/// To display a regular background to the screen, you need to call its [`show()`](RegularBackground::show())
/// method on a given [`GraphicsFrame`](crate::display::GraphicsFrame).
///
/// ## Example
///
/// ```rust,no_run
/// # #![no_main]
/// # #![no_std]
/// #
/// # use agb::Gba;
/// # fn foo(gba: &mut Gba) {
/// use agb::display::{
///     Priority,
///     tiled::{RegularBackground, RegularBackgroundSize, TileFormat, VRAM_MANAGER},
/// };
///
/// let mut gfx = gba.graphics.get();
///
/// let bg = RegularBackground::new(
///     Priority::P0,
///     RegularBackgroundSize::Background32x32,
///     TileFormat::FourBpp
/// );
///
/// // load the background with some tiles
///
/// loop {
///     let mut frame = gfx.frame();
///     bg.show(&mut frame);
///     frame.commit();
/// }
/// # }
/// ```
pub struct RegularBackground {
    priority: Priority,

    tiles: Tiles<Tile>,
    screenblock: Rc<Screenblock<RegularBackgroundSize>>,

    scroll: Vector2D<i32>,
}

impl RegularBackground {
    /// Create a new RegularBackground with given `priority`, `size` and `colours`.
    ///
    /// This allocates some space in VRAM to store the actual tile data, but doesn't show anything until you call
    /// the [`show()`](RegularBackground::show()) function on a [`GraphicsFrame`](crate::display::GraphicsFrame).
    ///
    /// You can have more `RegularBackgroundTile` instances then there are backgrounds, but you can only show
    /// 4 at once in a given frame (or 2 and a single [affine background](super::AffineBackground)).
    ///
    /// For [`Priority`], a higher priority is rendered first, so is behind lower priorities. Therefore, `P0`
    /// will be rendered at the _front_ and `P3` at the _back_. For equal priorities, backgrounds are rendered
    /// _behind_ objects.
    #[must_use]
    pub fn new(priority: Priority, size: RegularBackgroundSize, colours: TileFormat) -> Self {
        Self {
            priority,

            tiles: Tiles::new(size.num_tiles(), colours),

            scroll: Vector2D::default(),

            screenblock: Rc::new(Screenblock::new(size)),
        }
    }

    /// Sets the scroll position of the background.
    ///
    /// This determines the pixel coordinate of the _screen_
    /// in the background. So increasing the `x` coordinate of the scroll position moves the screen to the right,
    /// effectively rendering the background more to the left.
    ///
    /// To get the current scroll position, you can call [`scroll_pos()`](RegularBackground::scroll_pos()).
    ///
    /// Returns self so you can chain with other `set_` calls.
    pub fn set_scroll_pos(&mut self, scroll: impl Into<Vector2D<i32>>) -> &mut Self {
        self.scroll = scroll.into();
        self
    }

    /// Gets the current scroll position of the background.
    ///
    /// This determines the pixel coordinate of the _screen_
    /// in the background. So increasing the `x` coordinate of the scroll position moves the screen to the right,
    /// effectively rendering the background more to the left.
    ///
    /// To set the current scroll position, you can call [`set_scroll_pos()`](RegularBackground::set_scroll_pos()).
    #[must_use]
    pub fn scroll_pos(&self) -> Vector2D<i32> {
        self.scroll
    }

    /// Sets a tile at the given position to the given [`TileSet`] / [`TileSetting`] combination.
    ///
    /// The number of colours which you set when creating the background (in the [`TileFormat`] argument)
    /// must match the number of colours in the tileset you are creating.
    ///
    /// This will resulting in copying the tile data to video RAM. However, setting the same tile across multiple locations
    /// in the background will reference that same tile only once to reduce video RAM usage.
    ///
    /// Returns self so you can chain with other `set_` calls.
    pub fn set_tile(
        &mut self,
        pos: impl Into<Vector2D<i32>>,
        tileset: &TileSet,
        tile_setting: TileSetting,
    ) -> &mut Self {
        assert_eq!(
            tileset.format(),
            self.tiles.colours(),
            "Cannot set a {:?} colour tile on a {:?} colour background",
            tileset.format(),
            self.tiles.colours()
        );

        let pos = self.screenblock.size().gba_offset(pos.into());
        self.set_tile_at_pos(pos, tileset, tile_setting);

        self
    }

    /// Sets a tile at the given position to the given [`DynamicTile16`] / [`TileEffect`] combination.
    ///
    /// This only works on a [16 colour background](TileFormat::FourBpp).
    ///
    /// Returns self so you can chain with other `set_` calls.
    pub fn set_tile_dynamic16(
        &mut self,
        pos: impl Into<Vector2D<i32>>,
        tile: &DynamicTile16,
        effect: TileEffect,
    ) -> &mut Self {
        assert_eq!(
            self.tiles.colours(),
            TileFormat::FourBpp,
            "Cannot set a 16-colour dynamic tile on a {:?} colour background",
            self.tiles.colours()
        );

        let pos = self.screenblock.size().gba_offset(pos.into());
        self.set_tile_at_pos(
            pos,
            &tile.tile_set(),
            TileSetting::new(tile.tile_id(), effect),
        );

        self
    }

    /// Sets a tile at the given position to the given [`DynamicTile256`] / [`TileEffect`] combination.
    ///
    /// This only works on a [256 colour background](TileFormat::EightBpp).
    ///
    /// Returns self so you can chain with other `set_` calls.
    pub fn set_tile_dynamic256(
        &mut self,
        pos: impl Into<Vector2D<i32>>,
        tile: &DynamicTile256,
        effect: TileEffect,
    ) -> &mut Self {
        assert_eq!(
            self.tiles.colours(),
            TileFormat::EightBpp,
            "Cannot set a 256-colour dynamic tile on a {:?} colour background",
            self.tiles.colours()
        );

        let pos = self.screenblock.size().gba_offset(pos.into());
        self.set_tile_at_pos(
            pos,
            &tile.tile_set(),
            TileSetting::new(tile.tile_id(), effect),
        );

        self
    }

    /// Fills the screen with the data given in `tile_data`.
    ///
    /// This is useful mainly e.g. title screens or other full screen backgrounds.
    ///
    /// This method assumes that `tile_data` was loaded via [`include_background_gfx!`](crate::include_background_gfx) and
    /// that it is at least the size of the Game Boy Advance's screen resolution of 240x160 pixels (or 20x30 tiles).
    ///
    /// ## Example
    ///
    /// ```rust
    /// # #![no_main]
    /// # #![no_std]
    /// use agb::{
    ///     display::{
    ///         Priority,
    ///         tiled::{RegularBackground, RegularBackgroundSize, TileFormat, VRAM_MANAGER},
    ///     },
    ///     include_background_gfx,
    /// };
    ///
    /// include_background_gfx!(mod logo, logo => deduplicate "examples/gfx/test_logo.aseprite");
    ///
    /// # #[agb::doctest]
    /// # fn test(gba: agb::Gba) {
    /// VRAM_MANAGER.set_background_palettes(logo::PALETTES);
    /// let mut bg = RegularBackground::new(Priority::P0, RegularBackgroundSize::Background32x32, TileFormat::FourBpp);
    ///
    /// bg.fill_with(&logo::logo);
    /// # }
    /// ```
    ///
    /// Returns self so you can chain with other `set_` calls.
    pub fn fill_with(&mut self, tile_data: &TileData) -> &mut Self {
        assert!(
            tile_data.width >= 30,
            "Don't have a full screen's width of tile data, got: {}",
            tile_data.width
        );
        assert!(
            tile_data.height >= 20,
            "Don't have a full screen's height worth of tile data, got: {}",
            tile_data.height
        );
        assert_eq!(
            tile_data.tiles.format(),
            self.tiles.colours(),
            "Cannot set a {:?} colour tile on a {:?} colour background",
            tile_data.tiles.format(),
            self.tiles.colours()
        );

        for y in 0..20 {
            for x in 0..30 {
                let tile_id = y * tile_data.width + x;
                let tile_pos = y * 32 + x;
                self.set_tile_at_pos(tile_pos, &tile_data.tiles, tile_data.tile_settings[tile_id]);
            }
        }

        self
    }

    fn set_tile_at_pos(&mut self, pos: usize, tileset: &TileSet, tile_setting: TileSetting) {
        let old_tile = self.tiles.get(pos);

        let tile_index = tile_setting.tile_id();

        let new_tile = if tile_index != TRANSPARENT_TILE_INDEX {
            let new_tile_idx = VRAM_MANAGER.add_tile(tileset, tile_index, false);
            Tile::new(new_tile_idx, tile_setting)
        } else {
            Tile::default()
        };

        if old_tile != Tile::default() {
            VRAM_MANAGER.remove_tile(old_tile.tile_index(self.tiles.colours()));
        }

        if old_tile == new_tile {
            // no need to mark as dirty if nothing changes
            return;
        }

        self.tiles.set_tile(pos, new_tile);
    }

    /// Show this background on a given frame.
    ///
    /// The background itself won't be visible until you call [`commit()`](GraphicsFrame::commit()) on the provided [`GraphicsFrame`].
    ///
    /// After this call, you can safely drop the background and the provided [`GraphicsFrame`] will maintain the
    /// references needed until the frame is drawn on screen.
    ///
    /// Note that after this call, any modifications made to the background will _not_ show this frame. Effectively
    /// calling `show()` takes a snapshot of the current state of the background, so you can even modify
    /// the background and `show()` it again and both will show in the frame.
    ///
    /// The returned [`RegularBackgroundId`] can be passed to a [`Blend`](crate::display::Blend) or [`Window`](crate::display::Window),
    /// or used for [dma effects](crate::dma).
    ///
    /// # Panics
    ///
    /// If you try to show more than 4 regular backgrounds, or more than 2 backgrounds and a single affine background,
    /// or if there are already 2 affine backgrounds.
    pub fn show(&self, frame: &mut GraphicsFrame<'_>) -> RegularBackgroundId {
        let commit_data = if self.tiles.is_dirty(self.screenblock.ptr()) {
            Some(RegularBackgroundCommitData {
                tiles: self.tiles.clone(),
                screenblock: Rc::clone(&self.screenblock),
            })
        } else {
            None
        };

        frame.bg_frame.set_next_regular(RegularBackgroundData {
            bg_ctrl: self.bg_ctrl_value(),
            scroll_offset: Vector2D::new(self.scroll.x as u16, self.scroll.y as u16),
            commit_data,
        })
    }

    /// Get the size of this background.
    #[must_use]
    pub fn size(&self) -> RegularBackgroundSize {
        self.screenblock.size()
    }

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

    /// Sets the [`Priority`] of this background.
    ///
    /// This won't take effect until the next call to [`show()`](RegularBackground::show()).
    ///
    /// Returns self so you can chain with other `set_` calls.
    pub fn set_priority(&mut self, priority: Priority) -> &mut Self {
        self.priority = priority;
        self
    }

    fn bg_ctrl_value(&self) -> BackgroundControlRegister {
        let mut background_control_register = BackgroundControlRegister::default();

        background_control_register.set_tile_format(self.tiles.colours().into());
        background_control_register.set_priority(self.priority.into());
        background_control_register
            .set_screen_base_block(u5::new(self.screenblock.screen_base_block() as u8));
        background_control_register.set_screen_size(self.size().into());

        background_control_register
    }
}

#[cfg(test)]
mod test;