Scrolling Tile Maps

Top  Previous  Next

A map contains the images and definitions for a scrollable tile map, sometimes referred to as 2D maps. A map consists of tile images and a definition file. The definition file contains the locations where each tile should appear in the map. Using maps allows creation of complex levels and backgrounds using a small set of individual images placed in a grid pattern. This saves on video card memory and CPU overhead as copying a set of small images is much faster than copying a very large one to a buffer.

Creating a map is a two step process. First an image file containing all of the tiles is loaded, second the map must be defined either by loading a data file containing the x, y positions of each tile or by manually assigning tile numbers to the map. A screen must be created before using any of the map functions.

Creating the map, loading the tiles

Create the map by using the NEWMAP function. NEWMAP returns a pointer the the newly created map and operates in the same manner as LOADSPRITE and supports the same image formats. The pointer returned will be used with all of the map functions and commands. Before your screen is closed you must free the map by using the FREEMAP statement. Each tile in the image file must have the same dimensions.
 

myMap = NEWMAP(GETSTARTPATH+"mapdata.bmp",64,64)

Each tile in the image file is assigned a number counting from zero. So if you image file has 4 tiles they will be assigned tile numbers 0,1,2,3. After the tile images are loaded the map is considered to be empty until you give the map a size and set tiles into the grid.

 

Loading an saving definition files

A definition file contains the size of the map and the tile indexes for every position within the map grid. Load a previously created definition file with the LOADMAPDATA function.
 

IF LOADMAPDATA(myMap, GETSTARTPATH + "level1.dat", TRUE) = FALSE
    CLOSESCREEN:MESSAGEBOX( 0, "Failed to load level data","Error"):END
ENDIF

LOADMAPDATA will return FALSE if the file could not be opened. The optional scroll wrap parameter specifies whether the map will display as wrapped around when scrolling or just stop when the edges of the defined map are met.

Saving a map definition file is done with the SAVEMAPDATA function. The widely used extension for a tile position data file is .dat but you can use any extension you wish.
 

SAVEMAPDATA myMap, "level1.dat"

 

Creating an empty map and setting tiles

To define the map dimensions instead of loading a data file use the CREATEMAPDATA function.
 

CREATEMAPDATA myMap, 10, 10, -1, TRUE

Would define a map with dimensions of 10x10, filled with empty tile positions, and allowing scroll wrapping. Once the map dimensions are established you place tiles in the map by using the SETMAPDATA function. SETMAPDATA takes the x and y coordinate of the map and assigns it a tile number from the image file. It is this tile that will be displayed at the position specified when the map is rendered to the buffer.
 

SETMAPDATA myMap, 0, 1, 0

Sets tile number 0 at coordinates 0, 1.  It is important to remember that when talking about map coordinates that they refer to the tile grid and not pixels. You can retrieve the currently set tile of a position using the GETMAPDATA function.

Displaying the map

The map is rendered to the buffer using the DRAWMAP statement. The default drawing mode for the tiles is @BLOCKCOPY which is a straight copy from image to buffer. You can also use transparency, or masking, when drawing by setting the drawing mode with the MAPDRAWMODE statement to @TRANS. Change the mask color with MAPMASKCOLOR.
 

MAPDRAWMODE map,@TRANS

MAPMASKCOLOR map,RGB(43,83,199)

DRAWMAP map

A map is usually drawn as the first graphical element in your program. Using transparency you can create multiple layers of maps on the screen.

 

Moving and scrolling the map

A map can be moved to an absolute pixel position in the map with the MOVEMAP command. The x an y coordinates are specified in pixels and must be in the total pixel width and height of the map. Normally MOVEMAP is only used to initially set the displayed position of the map.
 

MOVEMAP myMap, 0, 0

Scrolling the map is the whole purpose of a scrolling tile map. Use the SCROLLMAP statement to move the map in any direction specified by an offset. SCROLLMAP accepts a directional parameter which can be one of @SCROLLUP, @SCROLLDOWN, @SCROLLLEFT, or  @SCROLLRIGHT.  The distance is specified in pixels.
 

IF KEYDOWN(DIK_UP)
    ScrollMap( myMap,@SCROLLUP,2 )
ENDIF
IF KEYDOWN(DIK_DOWN)
    ScrollMap( myMap,@SCROLLDOWN,2 )
ENDIF
IF KEYDOWN(DIK_LEFT)
    ScrollMap( myMap,@SCROLLLEFT,2 )
ENDIF
IF KEYDOWN(DIK_RIGHT)
    ScrollMap( myMap,@SCROLLRIGHT,2 )
ENDIF

 

Changing a maps viewport

The view port is a rectangular area that the map is rendered into. This is set by default to the same size as the screen dimensions. You can restrict the area the map renders into by defining the viewport rectangle. When using a viewport, the map automatically shifts the map rendering to match the upper left corner of the viewport as position 0,0.
 

DEF vp as WINRECT
vp.top = 64
vp.bottom = 480-64
vp.left = 64
vp.right = 640-64
SETMAPVIEWPORT myMap, vp

 

Map information functions

tilenum = GETMAPDATA(map,x ,y)

Returns the tile number located at map position x, y.

width = GETMAPWIDTH(map)

Returns the width of the map in tiles.

height = GETMAPHEIGHT(map)

Returns the height of the map in tiles.

pwidth = GETMAPPIXELWIDTH(map)

Returns the total width of the map in pixels.

pheight = GETMAPPIXELHEIGHT(map)

Returns the total height of the map in pixels.

size = GETMAPCOUNT(map)

Returns the size of the map in tiles.