Tiled background rendering... This is the basic foundation of a frame. The basic idea is that you fill the screen with tiles so that an appropriate view of the game world is displayed. You'll need a few things to accomplish this task:
Tile graphics - You'll
need some tiles to draw with. It is typically a
good idea to make these square, with dimensions being a
power of two (for speed purposes). I chose
32x32-sized tiles for my game. The easiest data
structure to hold tiles in is probably some sort of array
of bitmaps. Using Allegro, I loaded a datafile of
tiles and referenced them like this: datafile[tilenum].dat
A game world, or "map"
- This is usually a two-dimensional array of
references to tiles. The easiest way to do this is
to have the 2D array hold the subscript numbers of the
tiles. I called my 2D tile array "map".
Referencing the Tile Map
Here is an example of how I originally referenced this
tile array: map[y][x]
So, doing that
would give me the tile number at coordinates (x,y) on the
map. Then, knowing that tile number, you can access
your array of tile bitmaps to find the right tile bitmap
for a map location, like this: datafile[map[y][x]].dat
Viewport coordinates -
You need some way to represent where the
viewport will be, looking into the game world. I
chose to have map coordinates for the upper-left of the
tiled background. These would be the map (world)
coordinates of the first (upper-left) tile drawn. I
call these bgx and bgy.
Now, if you've played Bapple2 (and I hope you have for
understanding's sake), you know that the background
scrolls on a pixel-by-pixel basis rather than one full
tile at a time. This is accomplished by having another
pair of coordinates, which are actually pixel offsets
from the tile coordinates. I call these bgxoff
and bgyoff. Confused? Well, let's say
that bgx is 0 and bgy is 0. What gets displayed? The
upper left corner of the map. Now, let's say that we have
bgxoff be 16 and bgyoff be 16. This will shift the
viewport down and to the right by 16 pixels.
Yeah, ok, whatever, you're saying.. What do I put into
bgx, bgy, bgxoff, and bgyoff? You typically want the
camera (viewport) to follow the player, right? So since
the player has tile coordinates and pixel offsets, I just
load my background coordinates based on those. Here is
some simplified code:
bgx = player.x - (SCREEN_W / 64);
bgy = player.y - (SCREEN_H / 60);
bgxoff = player.xoff + 8;
bgyoff = player.yoff + 16;
It could be better, but that is basically putting the
player in the center of the viewport by moving the
upper-left corner of the tiled background to the
upper-left corner of the screen. This is all based on
tile coordinates though, so the player's world location
is the only available reference point.
[more later]