GB101:Tiles and Background Maps

From NYC Resistor Wiki
Jump to navigation Jump to search
GB101 Class Notes
Sections
  1. Home
  2. Installing Gameboy Development Tools
  3. Introduction to the Gameboy Advance
  4. Hello Background
  5. Working with Palettes
  6. Tiles and Background Maps
  7. Sprites
  8. Scrolling Backgrounds
  9. Collision Detection
  10. Fixed point math
  11. Affine Sprites
  12. Sound
  13. Where do I go from here?

Introduction[edit]

Tiled backgrounds

Most GBA games don't actually use any of the bitmap modes. Unless you're going to port Doom or do some vector games, the bitmap modes are pretty useless. GBA games get their responsiveness by using hardware-based tile modes. In these modes we can load up some tiled graphics into one part of video memory, and the tile map into another and let the GBA draw the rest. Think of this as the same as palette mode 4, except instead of each pixel as an index to a color, each tile is an index to an 8x8 bitmap.


Mode 0[edit]

Mode 0 is the tiled mode we'll be using. It's a little stranger than Modes 1 and 2 because instead of an 8-bit index representing a single tile we'll be using a 16-bit index that represents a tile and some extra information. This extra information allows us to, among other things, rotate tiles so they can be re-used. The real reason we'll be using Mode 0 is because there is a good tool for making maps..

TileMax[edit]

TileMax is a GBA tile editor written in Java. With TileMax we can load a bitmap, generate tiles, and export some C files. There isn't any documentation, nor much information about it at all. I suspect that TileMax was intended as a professional product, but was abandoned at some point and the source was released. This aside, it's a pretty straightforward tool.

Loading bitmaps[edit]

The easiest way to load bitmaps is to turn them into code and then use memcpy to shove them into video memory.

Implementation[edit]

#include <gba_video.h>
#include <gba_interrupt.h>
#include <gba_systemcalls.h>
#include <string.h>
#include "tiledata.h"

int main(void) {
	// Set up our pointers vram
	u16* vramMap = (u16*)SCREEN_BASE_BLOCK(31);
	u16* vramTiles = (u16*)CHAR_BASE_BLOCK(0);
	u16* vramPalette = (u16*)BG_PALETTE;
	
	// Load palette, tile and map data
	memcpy(vramPalette, (u16*)PAL_Zelda, 256*2);
	memcpy(vramTiles, (u8*)TS_Zelda, 256*256);
	memcpy(vramMap, (u16*)MAP_Zelda, 32*20*2);
	
	// Mode 0 with BG 0, and sprites enabled.
	// Mode 0 uses a 16-bit 32x20? map, which isn't documented anywhere
	// Modes 1 and 2 use a 8-bit map.  The extra bits in Mode 0 allow for
	// flipping tiles.
	SetMode(MODE_0 | BG0_ENABLE | OBJ_1D_MAP | OBJ_ENABLE);
	
	// Set BG0 to use 256 color tiles, tile map starts on page 0, tile gfx on
	// page 31.
	REG_BG0CNT = BG_256_COLOR | SCREEN_BASE(31) | CHAR_BASE(0);
	
	while(1) {}
}