Move the palettes to a single constant rather than being repeated

This commit is contained in:
Gwilym Kuiper 2022-10-08 23:02:54 +01:00
parent a5c86e8ce8
commit 1e759dbeca
11 changed files with 46 additions and 48 deletions

View file

@ -20,7 +20,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Many of the places that originally disabled IRQs now use the `sync` module, reducing the chance of missed interrupts. - Many of the places that originally disabled IRQs now use the `sync` module, reducing the chance of missed interrupts.
- HashMap iterators now implement `size_hint` which should result in slightly better generation of code using those iterators. - HashMap iterators now implement `size_hint` which should result in slightly better generation of code using those iterators.
- Transparency of backgrounds is now set once in the toml file rather than once for every image. - Transparency of backgrounds is now set once in the toml file rather than once for every image.
- Palette generation now takes into account every single background a toml definition rather than one at a time. - Palette generation now takes into account every single background a toml definition rather than one at a time, you can now find it in the PALETTES constant rather than in every individual image.
### Fixed ### Fixed
- Fixed the fast magnitude function in agb_fixnum. This is also used in fast_normalise. Previously only worked for positive (x, y). - Fixed the fast magnitude function in agb_fixnum. This is also used in fast_normalise. Previously only worked for positive (x, y).

View file

@ -127,10 +127,15 @@ pub fn include_gfx(input: TokenStream) -> TokenStream {
)); ));
} }
let palette_code =
rust_generator::generate_palette_code(&optimisation_results, &config.crate_prefix());
let module = quote! { let module = quote! {
mod #module_name { mod #module_name {
const _: &[u8] = include_bytes!(#include_path); const _: &[u8] = include_bytes!(#include_path);
#palette_code
#(#image_code)* #(#image_code)*
} }
}; };

View file

@ -7,19 +7,13 @@ use quote::{format_ident, quote};
use std::iter; use std::iter;
pub(crate) fn generate_code( pub(crate) fn generate_palette_code(
output_variable_name: &str,
results: &Palette16OptimisationResults, results: &Palette16OptimisationResults,
image: &Image, crate_prefix: &str,
image_filename: &str,
tile_size: TileSize,
crate_prefix: String,
assignment_offset: Option<usize>,
) -> TokenStream { ) -> TokenStream {
let crate_prefix = format_ident!("{}", crate_prefix); let crate_prefix = format_ident!("{}", crate_prefix);
let output_variable_name = format_ident!("{}", output_variable_name);
let palette_data = results.optimised_palettes.iter().map(|palette| { let palettes = results.optimised_palettes.iter().map(|palette| {
let colours = palette let colours = palette
.clone() .clone()
.into_iter() .into_iter()
@ -35,6 +29,23 @@ pub(crate) fn generate_code(
} }
}); });
quote! {
pub const PALETTES: &[#crate_prefix::display::palette16::Palette16] = &[#(#palettes),*];
}
}
pub(crate) fn generate_code(
output_variable_name: &str,
results: &Palette16OptimisationResults,
image: &Image,
image_filename: &str,
tile_size: TileSize,
crate_prefix: String,
assignment_offset: Option<usize>,
) -> TokenStream {
let crate_prefix = format_ident!("{}", crate_prefix);
let output_variable_name = format_ident!("{}", output_variable_name);
let (tile_data, assignments) = if let Some(assignment_offset) = assignment_offset { let (tile_data, assignments) = if let Some(assignment_offset) = assignment_offset {
let mut tile_data = Vec::new(); let mut tile_data = Vec::new();
@ -68,17 +79,13 @@ pub(crate) fn generate_code(
pub const #output_variable_name: #crate_prefix::display::tile_data::TileData = { pub const #output_variable_name: #crate_prefix::display::tile_data::TileData = {
const _: &[u8] = include_bytes!(#image_filename); const _: &[u8] = include_bytes!(#image_filename);
const PALETTE_DATA: &[#crate_prefix::display::palette16::Palette16] = &[
#(#palette_data),*
];
const TILE_DATA: &[u8] = #data; const TILE_DATA: &[u8] = #data;
const PALETTE_ASSIGNMENT: &[u8] = &[ const PALETTE_ASSIGNMENT: &[u8] = &[
#(#assignments),* #(#assignments),*
]; ];
#crate_prefix::display::tile_data::TileData::new(PALETTE_DATA, TILE_DATA, PALETTE_ASSIGNMENT) #crate_prefix::display::tile_data::TileData::new(TILE_DATA, PALETTE_ASSIGNMENT)
}; };
} }
} }

View file

@ -19,7 +19,7 @@ fn main(mut gba: agb::Gba) -> ! {
let tileset = TileSet::new(affine_tiles::water_tiles.tiles, TileFormat::EightBpp); let tileset = TileSet::new(affine_tiles::water_tiles.tiles, TileFormat::EightBpp);
vram.set_background_palettes(affine_tiles::water_tiles.palettes); vram.set_background_palettes(affine_tiles::PALETTES);
let mut bg = gfx.background(Priority::P0, AffineBackgroundSize::Background32x32); let mut bg = gfx.background(Priority::P0, AffineBackgroundSize::Background32x32);

View file

@ -18,7 +18,7 @@ fn main(mut gba: agb::Gba) -> ! {
let tileset = TileSet::new(water_tiles::water_tiles.tiles, TileFormat::FourBpp); let tileset = TileSet::new(water_tiles::water_tiles.tiles, TileFormat::FourBpp);
vram.set_background_palettes(water_tiles::water_tiles.palettes); vram.set_background_palettes(water_tiles::PALETTES);
let mut bg = gfx.background(Priority::P0, RegularBackgroundSize::Background32x32); let mut bg = gfx.background(Priority::P0, RegularBackgroundSize::Background32x32);

View file

@ -3,7 +3,7 @@ use super::tiled::{RegularMap, TileFormat, TileSet, TileSetting, TiledMap, VRamM
crate::include_gfx!("gfx/agb_logo.toml"); crate::include_gfx!("gfx/agb_logo.toml");
pub fn display_logo(map: &mut RegularMap, vram: &mut VRamManager) { pub fn display_logo(map: &mut RegularMap, vram: &mut VRamManager) {
vram.set_background_palettes(agb_logo::test_logo.palettes); vram.set_background_palettes(agb_logo::PALETTES);
let background_tilemap = TileSet::new(agb_logo::test_logo.tiles, TileFormat::FourBpp); let background_tilemap = TileSet::new(agb_logo::test_logo.tiles, TileFormat::FourBpp);

View file

@ -1,20 +1,13 @@
use crate::display::palette16::Palette16; #[non_exhaustive]
pub struct TileData { pub struct TileData {
pub palettes: &'static [Palette16],
pub tiles: &'static [u8], pub tiles: &'static [u8],
pub palette_assignments: &'static [u8], pub palette_assignments: &'static [u8],
} }
impl TileData { impl TileData {
#[must_use] #[must_use]
pub const fn new( pub const fn new(tiles: &'static [u8], palette_assignments: &'static [u8]) -> Self {
palettes: &'static [Palette16],
tiles: &'static [u8],
palette_assignments: &'static [u8],
) -> Self {
TileData { TileData {
palettes,
tiles, tiles,
palette_assignments, palette_assignments,
} }

View file

@ -8,7 +8,7 @@ use crate::sfx::Sfx;
include_gfx!("gfx/backgrounds.toml"); include_gfx!("gfx/backgrounds.toml");
pub fn load_palettes(vram: &mut VRamManager) { pub fn load_palettes(vram: &mut VRamManager) {
vram.set_background_palettes(backgrounds::stars.palettes); vram.set_background_palettes(backgrounds::PALETTES);
} }
pub(crate) fn load_help_text( pub(crate) fn load_help_text(
@ -101,7 +101,7 @@ fn create_background_map(map: &mut RegularMap, vram: &mut VRamManager, stars_til
pub fn show_title_screen(background: &mut RegularMap, vram: &mut VRamManager, sfx: &mut Sfx) { pub fn show_title_screen(background: &mut RegularMap, vram: &mut VRamManager, sfx: &mut Sfx) {
background.set_scroll_pos((0i16, 0).into()); background.set_scroll_pos((0i16, 0).into());
vram.set_background_palettes(backgrounds::title.palettes); vram.set_background_palettes(backgrounds::PALETTES);
let tile_set = TileSet::new( let tile_set = TileSet::new(
backgrounds::title.tiles, backgrounds::title.tiles,
agb::display::tiled::TileFormat::FourBpp, agb::display::tiled::TileFormat::FourBpp,

View file

@ -785,7 +785,7 @@ fn agb_main(mut gba: agb::Gba) -> ! {
pub fn main(mut agb: agb::Gba) -> ! { pub fn main(mut agb: agb::Gba) -> ! {
let (tiled, mut vram) = agb.display.video.tiled0(); let (tiled, mut vram) = agb.display.video.tiled0();
vram.set_background_palettes(tile_sheet::background.palettes); vram.set_background_palettes(tile_sheet::PALETTES);
let mut splash_screen = tiled.background(Priority::P0, RegularBackgroundSize::Background32x32); let mut splash_screen = tiled.background(Priority::P0, RegularBackgroundSize::Background32x32);
let mut world_display = tiled.background(Priority::P0, RegularBackgroundSize::Background32x32); let mut world_display = tiled.background(Priority::P0, RegularBackgroundSize::Background32x32);
@ -817,7 +817,7 @@ pub fn main(mut agb: agb::Gba) -> ! {
world_display.commit(&mut vram); world_display.commit(&mut vram);
world_display.show(); world_display.show();
vram.set_background_palettes(tile_sheet::background.palettes); vram.set_background_palettes(tile_sheet::PALETTES);
let object = agb.display.object.get(); let object = agb.display.object.get();
let mut mixer = agb.mixer.mixer(); let mut mixer = agb.mixer.mixer();

View file

@ -19,20 +19,13 @@ pub fn show_splash_screen(
vram: &mut VRamManager, vram: &mut VRamManager,
) { ) {
map.set_scroll_pos((0i16, 0i16).into()); map.set_scroll_pos((0i16, 0i16).into());
let (tileset, palette) = match which { let tileset = match which {
SplashScreen::Start => { SplashScreen::Start => TileSet::new(splash_screens::splash.tiles, TileFormat::FourBpp),
let tileset = TileSet::new(splash_screens::splash.tiles, TileFormat::FourBpp);
(tileset, splash_screens::splash.palettes) SplashScreen::End => TileSet::new(
} splash_screens::thanks_for_playing.tiles,
SplashScreen::End => { TileFormat::FourBpp,
let tileset = TileSet::new( ),
splash_screens::thanks_for_playing.tiles,
TileFormat::FourBpp,
);
(tileset, splash_screens::thanks_for_playing.palettes)
}
}; };
let vblank = agb::interrupt::VBlank::get(); let vblank = agb::interrupt::VBlank::get();
@ -77,7 +70,7 @@ pub fn show_splash_screen(
} }
map.commit(vram); map.commit(vram);
vram.set_background_palettes(palette); vram.set_background_palettes(splash_screens::PALETTES);
map.show(); map.show();
loop { loop {

View file

@ -2146,7 +2146,7 @@ impl<'a> Game<'a> {
} }
fn update_sunrise(vram: &mut VRamManager, time: u16) { fn update_sunrise(vram: &mut VRamManager, time: u16) {
let mut modified_palette = background::background.palettes[0].clone(); let mut modified_palette = background::PALETTES[0].clone();
let a = modified_palette.colour(0); let a = modified_palette.colour(0);
let b = modified_palette.colour(1); let b = modified_palette.colour(1);
@ -2160,7 +2160,7 @@ impl<'a> Game<'a> {
} }
fn update_fade_out(vram: &mut VRamManager, time: u16) { fn update_fade_out(vram: &mut VRamManager, time: u16) {
let mut modified_palette = background::background.palettes[0].clone(); let mut modified_palette = background::PALETTES[0].clone();
let c = modified_palette.colour(2); let c = modified_palette.colour(2);
@ -2216,7 +2216,7 @@ fn game_with_level(gba: &mut agb::Gba) {
loop { loop {
let (background, mut vram) = gba.display.video.tiled0(); let (background, mut vram) = gba.display.video.tiled0();
vram.set_background_palettes(background::background.palettes); vram.set_background_palettes(background::PALETTES);
let tileset = TileSet::new(background::background.tiles, TileFormat::FourBpp); let tileset = TileSet::new(background::background.tiles, TileFormat::FourBpp);