git.y1.nz

gbdk-2020

GameBoy Development Kit
download: https://git.y1.nz/archives/gbdk.tar.gz
README | Files | Log | Refs | LICENSE

commit 5e1f97a8d737c239bc5bda6d304238b68089b43f
parent eb29f54bfc593b9ef950e3823c0204ec640f1e5d
Author: Toxa <56631470+untoxa@users.noreply.github.com>
Date:   Tue, 17 Jan 2023 23:11:12 +0400

Merge pull request #467 from michel-iwaniec/gbc_set_attributes_function

GBC: Add cross-platform attribute setting functions
Diffstat:
Mgbdk-lib/examples/cross-platform/logo/src/main.c26++------------------------
Mgbdk-lib/include/gb/gb.h107+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mgbdk-lib/include/sms/sms.h14++++++++++++++
Mgbdk-support/png2asset/png2asset.cpp13+++++++++----
4 files changed, 132 insertions(+), 28 deletions(-)

diff --git a/gbdk-lib/examples/cross-platform/logo/src/main.c b/gbdk-lib/examples/cross-platform/logo/src/main.c @@ -15,38 +15,16 @@ void main() { set_bkg_palette(0, GBDK_2020_logo_PALETTE_COUNT, GBDK_2020_logo_palettes); #endif set_native_tile_data(0, GBDK_2020_logo_TILE_COUNT, GBDK_2020_logo_tiles); -#if defined(SYSTEM_CGB) - if (_cpu == CGB_TYPE) { - VBK_REG = VBK_ATTRIBUTES; - set_tile_map((DEVICE_SCREEN_WIDTH - (GBDK_2020_logo_WIDTH >> 3)) >> 1, - (DEVICE_SCREEN_HEIGHT - (GBDK_2020_logo_HEIGHT >> 3)) >> 1, - GBDK_2020_logo_WIDTH >> 3, - GBDK_2020_logo_HEIGHT >> 3, - GBDK_2020_logo_map_attributes); - VBK_REG = VBK_TILES; - } -#elif defined(SYSTEM_NES) - // Make sure tile coordinates are rounded to 2, to match attribute table - set_bkg_attributes(((DEVICE_SCREEN_WIDTH - (GBDK_2020_logo_WIDTH >> 3)) >> 1) & 0xFE, - ((DEVICE_SCREEN_HEIGHT - (GBDK_2020_logo_HEIGHT >> 3)) >> 1) & 0xFE, + set_bkg_attributes((DEVICE_SCREEN_WIDTH - (GBDK_2020_logo_WIDTH >> 3)) >> 1, + (DEVICE_SCREEN_HEIGHT - (GBDK_2020_logo_HEIGHT >> 3)) >> 1, GBDK_2020_logo_WIDTH >> 3, GBDK_2020_logo_HEIGHT >> 3, GBDK_2020_logo_map_attributes); -#endif -#if defined(SYSTEM_NES) - // Make sure tile coordinates are rounded to 2, to match attribute table - set_tile_map(((DEVICE_SCREEN_WIDTH - (GBDK_2020_logo_WIDTH >> 3)) >> 1) & 0xFE, - ((DEVICE_SCREEN_HEIGHT - (GBDK_2020_logo_HEIGHT >> 3)) >> 1) & 0xFE, - GBDK_2020_logo_WIDTH >> 3, - GBDK_2020_logo_HEIGHT >> 3, - GBDK_2020_logo_map); -#else set_tile_map((DEVICE_SCREEN_WIDTH - (GBDK_2020_logo_WIDTH >> 3)) >> 1, (DEVICE_SCREEN_HEIGHT - (GBDK_2020_logo_HEIGHT >> 3)) >> 1, GBDK_2020_logo_WIDTH >> 3, GBDK_2020_logo_HEIGHT >> 3, GBDK_2020_logo_map); -#endif SHOW_BKG; DISPLAY_ON; } diff --git a/gbdk-lib/include/gb/gb.h b/gbdk-lib/include/gb/gb.h @@ -1082,6 +1082,60 @@ inline void set_bkg_based_tiles(uint8_t x, uint8_t y, uint8_t w, uint8_t h, cons } +/** Sets a rectangular region of Background Tile Map Attributes. + + @param x X Start position in Background Map tile coordinates. Range 0 - 31 + @param y Y Start position in Background Map tile coordinates. Range 0 - 31 + @param w Width of area to set in tiles. Range 1 - 32 + @param h Height of area to set in tiles. Range 1 - 32 + @param tiles Pointer to source tile map data + + Entries are copied from map at __tiles__ to the Background Tile Map starting at + __x__, __y__ writing across for __w__ tiles and down for __h__ tiles. + + Use @ref set_bkg_submap_attributes() instead when: + \li Source map is wider than 32 tiles. + \li Writing a width that does not match the source map width __and__ more + than one row high at a time. + + One byte per source tile map entry. + + Writes that exceed coordinate 31 on the x or y axis will wrap around to + the Left and Top edges. + + GBC Tile Attributes are defined as: + \li Bit 7 - Priority flag. When this is set, it puts the tile above the sprites + with colour 0 being transparent. + \n 0: Below sprites + \n 1: Above sprites + \n Note: @ref SHOW_BKG needs to be set for these priorities to take place. + \li Bit 6 - Vertical flip. Dictates which way up the tile is drawn vertically. + \n 0: Normal + \n 1: Flipped Vertically + \li Bit 5 - Horizontal flip. Dictates which way up the tile is drawn horizontally. + \n 0: Normal + \n 1: Flipped Horizontally + \li Bit 4 - Not used + \li Bit 3 - Character Bank specification. Dictates from which bank of + Background Tile Patterns the tile is taken. + \n 0: Bank 0 + \n 1: Bank 1 + \li Bit 2 - See bit 0. + \li Bit 1 - See bit 0. + \li Bit 0 - Bits 0-2 indicate which of the 7 BKG colour palettes the tile is + assigned. + + @see SHOW_BKG + @see set_bkg_data, set_bkg_submap_attributes, set_win_tiles, set_tiles +*/ +inline void set_bkg_attributes(uint8_t x, uint8_t y, uint8_t w, uint8_t h, const uint8_t *tiles) +{ + VBK_REG = VBK_ATTRIBUTES; + set_bkg_tiles(x, y, w, h, tiles); + VBK_REG = VBK_TILES; +} + + /** Sets a rectangular area of the Background Tile Map using a sub-region from a source tile map. Useful for scrolling implementations of maps larger than 32 x 32 tiles. @@ -1159,6 +1213,59 @@ inline void set_bkg_based_submap(uint8_t x, uint8_t y, uint8_t w, uint8_t h, con } +/** Sets a rectangular area of the Background Tile Map Attributes using a sub-region + from a source tile attribute map. Useful for scrolling implementations of maps + larger than 32 x 32 tiles. + + @param x X Start position in both the Source Tile Map and hardware Background Map tile coordinates. Range 0 - 255 + @param y Y Start position in both the Source Tile Map and hardware Background Map tile coordinates. Range 0 - 255 + @param w Width of area to set in tiles. Range 1 - 255 + @param h Height of area to set in tiles. Range 1 - 255 + @param map Pointer to source tile map data + @param map_w Width of source tile map in tiles. Range 1 - 255 + + Entries are copied from __map__ to the Background Tile Map starting at + __x__, __y__ writing across for __w__ tiles and down for __h__ tiles, + using __map_w__ as the rowstride for the source tile map. + + The __x__ and __y__ parameters are in Source Tile Map tile + coordinates. The location tiles will be written to on the + hardware Background Map is derived from those, but only uses + the lower 5 bits of each axis, for range of 0-31 (they are + bit-masked: `x & 0x1F` and `y & 0x1F`). As a result the two + coordinate systems are aligned together. + + In order to transfer tile map data in a way where the + coordinate systems are not aligned, an offset from the + Source Tile Map pointer can be passed in: + `(map_ptr + x + (y * map_width))`. + + For example, if you want the tile id at `1,2` from the source map to + show up at `0,0` on the hardware Background Map (instead of at `1,2`) + then modify the pointer address that is passed in: + `map_ptr + 1 + (2 * map_width)` + + Use this instead of @ref set_bkg_tiles when the source map is wider than + 32 tiles or when writing a width that does not match the source map width. + + One byte per source tile map entry. + + Writes that exceed coordinate 31 on the x or y axis will wrap around to + the Left and Top edges. + + See @ref set_bkg_tiles for setting CGB attribute maps with @ref VBK_REG. + + @see SHOW_BKG + @see set_bkg_data, set_bkg_attributes, set_win_submap, set_tiles +*/ +inline void set_bkg_submap_attributes(uint8_t x, uint8_t y, uint8_t w, uint8_t h, const uint8_t *map, uint8_t map_w) +{ + VBK_REG = VBK_ATTRIBUTES; + set_bkg_submap(x, y, w, h, map, map_w); + VBK_REG = VBK_TILES; +} + + /** Copies a rectangular region of Background Tile Map entries into a buffer. @param x X Start position in Background Map tile coordinates. Range 0 - 31 diff --git a/gbdk-lib/include/sms/sms.h b/gbdk-lib/include/sms/sms.h @@ -592,6 +592,13 @@ inline void set_win_based_tiles(uint8_t x, uint8_t y, uint8_t w, uint8_t h, cons _map_tile_offset = 0; } +inline void set_bkg_attributes(uint8_t x, uint8_t y, uint8_t w, uint8_t h, const uint8_t *tiles) +{ + VBK_REG = VBK_ATTRIBUTES; + set_bkg_tiles(x, y, w, h, tiles); + VBK_REG = VBK_TILES; +} + void set_tile_submap(uint8_t x, uint8_t y, uint8_t w, uint8_t h, uint8_t map_w, const uint8_t *map) Z88DK_CALLEE PRESERVES_REGS(iyh, iyl); void set_tile_submap_compat(uint8_t x, uint8_t y, uint8_t w, uint8_t h, uint8_t map_w, const uint8_t *map) Z88DK_CALLEE PRESERVES_REGS(iyh, iyl); inline void set_bkg_submap(uint8_t x, uint8_t y, uint8_t w, uint8_t h, const uint8_t *map, uint8_t map_w) { @@ -613,6 +620,13 @@ inline void set_win_based_submap(uint8_t x, uint8_t y, uint8_t w, uint8_t h, con _submap_tile_offset = 0; } +inline void set_bkg_submap_attributes(uint8_t x, uint8_t y, uint8_t w, uint8_t h, const uint8_t *map, uint8_t map_w) +{ + VBK_REG = VBK_ATTRIBUTES; + set_bkg_submap(x, y, w, h, map, map_w); + VBK_REG = VBK_TILES; +} + void fill_rect(uint8_t x, uint8_t y, uint8_t w, uint8_t h, const uint16_t tile) Z88DK_CALLEE PRESERVES_REGS(iyh, iyl); void fill_rect_compat(uint8_t x, uint8_t y, uint8_t w, uint8_t h, const uint16_t tile) Z88DK_CALLEE PRESERVES_REGS(iyh, iyl); #define fill_bkg_rect fill_rect_compat diff --git a/gbdk-support/png2asset/png2asset.cpp b/gbdk-support/png2asset/png2asset.cpp @@ -1300,12 +1300,17 @@ bool export_h_file(void) { { fprintf(file, "extern const unsigned char %s_map[%d];\n", data_name.c_str(), (unsigned int)map.size()); - if(use_map_attributes) { - if(map_attributes.size()) { + if(use_map_attributes && map_attributes.size()) { fprintf(file, "extern const unsigned char %s_map_attributes[%d];\n", data_name.c_str(), (unsigned int)map_attributes.size()); - } } - else if ((includeTileData) && (use_structs)) { + else + { + // Some platforms (like SMS/GG) encode attributes as part of map + // For compatibility, add a define that makes _map_attributes equal _map, + // so that set_bkg_attributes can work the same on these platforms + fprintf(file, "#define %s_map_attributes %s_map\n", data_name.c_str(), data_name.c_str()); + } + if (!use_map_attributes && (includeTileData) && (use_structs)) { fprintf(file, "extern const unsigned char* %s_tile_pals[%d];\n", data_name.c_str(), (unsigned int)tiles.size()); } }

This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.