SGADE Tutorial – Boulder Bombers Advance

by Mark T. Price
Chief Scientist, Sudden Presence

 
 
 
 Previous Chapter Index Next Chapter 

Chapter 3 – Setting Up a Tile Display

In this chapter, we will replace the “Hello World” program we wrote in Chapter 2 with a program that builds a tile-mode display of the canyon from our game. We will start by taking a tile data bitmap and use it to produce a set of source data to be #included in the project. We will then use this data to setup a tile mode display.

What you need for this chapter
1. The project created in Chapter 2
2. The bitmap containing the canyon tiles

Since my artist is currently out of town, we’ll be using left-over “Programmer-Art” from the PC version of Boulder Bombers for the canyon tile bitmap.

The first step to putting the tile data on the screen is to convert the bitmap into code that can be included in the project.

Put the bbtiles.bmp file in the data subdirectory of the BBAdvance project.

From a command prompt in the BBAdvance/data directory, run the SoConverter.exe tool from the SGADE distribution with the bbtiles.bmp file as an argument. A display like the following will appear:

  C:\dev\gba\Projects\BBAdvance\data>SoConverter.exe bbtiles.bmp
  Unable to deduce converter type from command line or inputfile type
		
  Please select a converter:
  1: SoTileSet
  2: SoImage
  3: SoPalette
  4: SoSpriteAnimation
  5: SoSoundEffect - Not implemented yet
  6: SoVideo - Not implemented yet
  7: SoSong - Not implemented yet

  Enter a number and press enter:

Because bitmaps are used to make SoTileSets, SoImages and SoPalettes, you must now specify what type of data to create. We want a tile set, so press '1' and hit enter. SoConverter then converts the data and gives another prompt:

  Please wait while converting....
		
  Done

  Press almost any key to continue...

Just hit return and the converter will exit.

Note that the converter has created a new file called “bbtilesTileSet.dat” in the data subdirectory*. Take a moment to examine the contents of this file. You will see that it contains two variable declarations. The first of these, bbtilesTileSetData, is a byte array that contains the actual image data for the tiles. The second, bbtilesTileSet, is a structure that contains the tile meta-data which includes the color depth, number of tiles and a pointer to the tile image data.

Next, run SoConverter.exe again, this time selecting option ‘3’ to produce a palette. The converter will produce a file called “bbtilesPalette.dat”. Examining this file reveals a single variable declaration for “bbtilesPalette” which is a word array containing the color data for the tiles.

Now that we have our data squared away, it’s time to add code to use it. Open the bbadvance.c file in your editor and make the following changes:

Incorporate the data you just generated by inserting the following #include statements after the #include “Socrates.h” statement:

  // =======================================================================
  // Constant data
  // =======================================================================

  #include "bbtilesPalette.dat"
  #include "bbtilesTileSet.dat"

Insert the following variable declaration after the __gba_multiboot declaration. This variable contains the tile index (map) data for a sample canyon. Please feel free to cut-and-paste this section to your source – typing this in was no fun at all.

  const u16 canyonData[] = {
      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
      0,0,4,4,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,5,5,0,0,
      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
      1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
      1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
      10,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,9,
      6,10,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,8,7,
      6,7,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,6,7,
      6,6,10,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,8,6,7,
      6,6,7,3,3,3,3,3,3,8,10,3,3,3,3,3,3,3,3,8,10,3,3,3,3,3,3,6,6,7,
      6,6,6,10,3,3,3,3,8,6,6,6,10,3,3,3,3,8,6,6,6,10,3,3,3,3,8,6,6,7,
      6,6,6,6,10,3,3,8,6,6,6,6,6,10,3,3,8,6,6,6,6,6,10,3,3,8,6,6,6,7
  };

Remove SoPaletteSetGreyScale call and all of the SoMode4Renderer calls from AgbMain and in their place insert a call to a new subroutine 'setupPlayfield'.

  setupPlayfield();

Insert the following implementation of the setupPlayfield subroutine after the canyonData declaration:

  // ***********************************************************************
  // ***  INITIALIZATION
  // ***********************************************************************
  
  // Setup Playfield (BKGs)
  void setupPlayfield(void)
  {
      // set palette
      SoPaletteSetPalette(SO_SCREEN_PALETTE, bbtilesPalette, true);

      // set mode 0 (tile mode)
      SoDisplaySetMode(0);
		
      // load tile data
      SoTileSetBkgLoad(&bbtilesTileSet, 0, 0);

      // allocate a background layer
      SoBkgSetup(0, SO_BKG_PRIORITY_0 | SO_BKG_CHARBASE(0)
        | SO_BKG_MOSAIC_DISABLE | SO_BKG_CLRMODE_256 | SO_BKG_SCRBASE(0x1f)
        | SO_BKG_TEXTSIZE_256x256);
		
      // load the tile indexes into the background
      SoBkgWriteBlock(0, 0, 0, 30, 20, 30, (u16*)&canyonData[0]);

      // turn on the background display
      SoBkgEnable(0, true);
  }

Since understanding these calls is important for getting tile mode working, we’ll examine them in greater detail.

The SoPaletteSetPalette call initializes the GBA's playfield palette to contain the colors from our bitmap.

The SoDisplaySetMode call puts the GBA into mode 0, or text tile mode. In this mode each 8x8 block of pixels on the screen is represented by a single 16-bit index value. The GBA uses this index to look up the actual pixel data in its BG memory and displays the result on the screen.

Setting the GBA in mode 0 is only the start though. Next we have to load the actual pixel data for the tiles in BG memory for the GBA to read. This is done in the SoTileSetBkgLoad function. In this instance, we load all of the tiles in the specified TileSet in bank 0 at index position 0 of BG memory.

Next, we use SoBkgSetup to configure a background layer to hold our index data. There are a lot of flags in this call, and it’s important to get them all right. Here’s a breakdown of the flag’s were using and what they mean:

SO_BKG_PRIORITY_0
specifies background’s front-to-back display order. Priority 0 is the highest priority. You can change this order after the background is configured by calling SoBkgSetPriority.
SO_BKG_CHARBASE(0)
specifies the character bank in BG memory that this background will use. It should be the same as the bank used in SoTileSetBkgLoad if you want to use 0-based indexes.
SO_BKG_MOSAIC_DISABLE
just specifies that we won’t be doing any mosaic effects on the background. You can change this after the background is configured by calling SoBkgSetMosaicEnable
SO_BKG_CLRMODE_256
specifies that we’re using 256 color data for our tiles. If you used 16 color data, you should use SO_BKG_CLRMODE_16 here instead.
SO_BKG_SCRBASE(0x1f)
specifies the location in BG memory where the array containing the background tile indices (AKA the background map) will be stored. 0x1f is the maximum value for this and leaves the most contiguous room for tile data.
SO_BKG_TEXTSIZE_256x256
specifies the size of the background map. Note that specifying a size larger than 256x256 will require more space in BG memory and thus the SO_BKG_SCRBASE would have to be adjusted.

Next, the SoBkgWriteBlock call is used to copy data into the background map. In this case, we are copying data to background 0, at position (0,0) with size (30,20). Each line in the source is 30 columns wide, and the source is canyonData.

Finally, the SoBkgEnable call is used to show the tile background layer on the screen.

When you’re all finished, save the file.

Compile your updated project by running ‘make’ from a command shell in the BBAdvance directory. Once it’s done, try it out in your GBA emulator. You will get a screen like this one.

click here to download the solution to this chapter

Footnotes

* The SoConverter.exe always defaults to creating 256 color data. To force it to create 16 color data, you must specify an option on the command-line. In this case, had we wanted to create a 16 color tileset for bbtiles.bmp, we would have used this command (and ignored the bogus "Invalid value for the -converter argument" error message :-):

  % SoConverter.exe -file bbtiles.bmp -converter SoTileSet -tileSet.m_ColorDepth 16

 Previous Chapter Index Next Chapter