Table of Contents

Class Background

Namespace
GB
Assembly
GBSharp.Framework.dll

The background layer: a 32x32 grid of tiles, of which 20x18 is on screen.

public static class Background
Inheritance
Background
Inherited Members

Remarks

The background is what a Game Boy game is mostly made of. Tile data is loaded once into VRAM, and the map then names tiles by index, so a screen of artwork costs one byte per cell rather than one byte per pixel.

Tile data is shared with the window layer. Loading through LoadTiles(byte, byte, byte[]) or LoadBackground(byte, byte, byte[]) makes the same tiles available to both.

Fields

MapHeight

Height of the tile map, in tiles. Only 18 rows are on screen.

public const byte MapHeight = 32

Field Value

byte

MapWidth

Width of the tile map, in tiles. Only 20 columns are on screen.

public const byte MapWidth = 32

Field Value

byte

Properties

ScrollX

The horizontal scroll register.

public static byte ScrollX { get; set; }

Property Value

byte

ScrollY

The vertical scroll register.

public static byte ScrollY { get; set; }

Property Value

byte

Methods

DrawRegion(TileMap, byte, byte, byte, byte, byte, byte)

Copies a window of a larger map into the hardware map.

[Native("gbs_background_draw_region")]
public static void DrawRegion(TileMap map, byte destinationX, byte destinationY, byte width, byte height, byte sourceX, byte sourceY)

Parameters

map TileMap
destinationX byte
destinationY byte
width byte
height byte
sourceX byte
sourceY byte

Remarks

The hardware map is 32x32 cells. A map larger than that does not fit, and this is how the part you want gets there: sourceX and sourceY pick the top-left corner in the asset, and the rest names where it lands and how much of it to copy.

This is the primitive a scrolling world is built from, not a camera. GB# does not keep a camera for you: that would be per-frame state you cannot see the cost of. Track the position yourself and call this when it moves.

// The screen is 20x18 tiles. Draw the part of the world at (camX, camY).
Background.DrawRegion(World, 0, 0, 20, 18, camX, camY);

GetTile(byte, byte)

Reads one map cell.

[Native("get_bkg_tile_xy")]
public static byte GetTile(byte x, byte y)

Parameters

x byte
y byte

Returns

byte

Load(TileMap)

Loads a converted image: its tiles, its map, and on Game Boy Color its attributes and palettes.

[Native("gbs_background_load")]
public static void Load(TileMap map)

Parameters

map TileMap

Remarks

One call in place of the four the pieces would otherwise need, with the sizes filled in by the compiler from the image itself. The colour parts are skipped at runtime on an original Game Boy.

LoadAttributes(byte, byte, byte, byte, byte[])

Writes a rectangle of Game Boy Color attributes into the map.

[Native("gbs_bkg_load_attributes")]
public static void LoadAttributes(byte x, byte y, byte width, byte height, byte[] attributes)

Parameters

x byte
y byte
width byte
height byte
attributes byte[]

Remarks

One byte per cell: the low three bits pick a palette, bit 5 flips the tile horizontally and bit 6 vertically. Does nothing on an original Game Boy, so guard with IsColorHardware if the game runs on both.

LoadMap(byte, byte, byte, byte, byte[])

Writes a rectangle of tile indices into the map.

[Native("gbs_bkg_load_map")]
public static void LoadMap(byte x, byte y, byte width, byte height, byte[] map)

Parameters

x byte
y byte
width byte
height byte
map byte[]

LoadTiles(byte, byte, byte[])

Copies tile data into VRAM.

[Native("gbs_bkg_load_tiles")]
public static void LoadTiles(byte firstTile, byte count, byte[] data)

Parameters

firstTile byte
count byte
data byte[]

Remarks

Each tile is 16 bytes. At most 255 tiles can be loaded in one call, and the background and window share a 256-tile region.

Move(byte, byte)

Scrolls to an absolute position.

[Native("move_bkg")]
public static void Move(byte x, byte y)

Parameters

x byte
y byte

Scroll(sbyte, sbyte)

Scrolls by a relative amount, wrapping at the edges of the map.

[Native("scroll_bkg")]
public static void Scroll(sbyte dx, sbyte dy)

Parameters

dx sbyte
dy sbyte

SetTile(byte, byte, byte)

Sets one map cell.

[Native("gbs_bkg_set_tile")]
public static void SetTile(byte x, byte y, byte tile)

Parameters

x byte
y byte
tile byte