• Advertisement
Sign in to follow this  

Creating a Tile-Based Map

This topic is 2513 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Is it possible to have DirectX read a matrix and depending on the numbers, place a sprite in a certain location?

For example:

map = {

1 = Grass
2 = Stone Road

I know its possible with Allegro, but I want to know if you can do the same thing in DirectX or do I have to do it in a different format?

Share this post

Link to post
Share on other sites
Yeah, nice and simple... You'd need to loop through each of them values and for each one draw a tile corresponding to what number is in that array and each iteration of the loop you just change the a value of the X and Y values. If you can do it in Allegro you can do it in DirectX, these are just API's which you use in your game code to do certain things, like drawing for this case.

Share this post

Link to post
Share on other sites
I'm going to stick out my nose and post some code here, I hope I won't regret it as I might have to try to explain it :-)

What the.......I have just released my first DirectX game and most game graphics (tiles) as public domain so I can post
some code as well :-) This is how I draw the tile map in my game, but there are some stuff declared outside this
function so you'll have to think a bit anyway....See it as some kind of pseudo code......Post if you need additional info.

From the beginning my intention was to post the source code as well, but it simply grew too big, and if I would start
over today with the experience I got after this game I'd change some stuff in the code for sure......Enough, lets jump
to the exiting part....

// The function below writes the tile map (if one exists) to
// the back buffer. There might be situations where no tile map
// is present, for example if only an animation is played.
// We only check for top and left tile clipping where we set
// negative display coordinates so they are partly or totally
// clipped by the viewport we set up. The right and bottom tiles
// are clipped automagically by our viewport.

void Gfx_PresentMap (void)
int iScreenX=0; //Screen X offset for writing tile map
int iScreenY=0; //Screen Y offset for writing tile map
UINT uiTileNumber; //Tile number to write to screen
int iTileDivX, iTileDivY; //X and Y divisions for tile map number calc
int iTileModX, iTileModY; //X and Y modulo for rect calc within tile
int iScreenDivX, iScreenDivY; //Tile to write within screen position
int iTotalX, iTotalY; //Calculate total tile offset
BOOL bFirstX=TRUE; //TRUE if writing first tile row in X position
BOOL bFirstY=TRUE; //TRUE if writing first tile row in Y position

// Return if no tile map exist for this level
if (g_LevelInfo.uiTileSizeX==0 && g_LevelInfo.uiTileSizeY==0) return;

iTileDivX=World.uiMinX/TILESIZE; //Calculate X offset within tile map
iTileModX=World.uiMinX%TILESIZE; //Calculate X modulo offset within tile map
iTileDivY=World.uiMinY/TILESIZE; //Calculate Y offset within tile map
iTileModY=World.uiMinY%TILESIZE; //Calculate Y modulo offset within tile map

while (iScreenX<HSIZE) //Write all tiles in X position
iScreenDivX=iScreenX/TILESIZE; //Calculate X offset within screen offset
iTotalX=iTileDivX+iScreenDivX; //Calculate total X offset within tile map
iScreenY=0; //Reset Y screen offset
bFirstY=TRUE; //We are writing first tile row in Y position

while (iScreenY<VSIZE) //Write all tiles in Y position
iScreenDivY=iScreenY/TILESIZE; //Calculate Y offset within screen offset
iTotalY=iTileDivY+iScreenDivY; //Calculate total Y offset within tile map

uiTileNumber=(UINT)iTotalY+(g_LevelInfo.uiTileSizeY*(UINT)iTotalX); //Index within tile map

// Correct tile number if we have clipped the first tile in
// X or Y position.

if (iTileModX && !bFirstX) uiTileNumber+=g_LevelInfo.uiTileSizeY;
if (iTileModY && !bFirstY) uiTileNumber++;

// Calculate out of screen top tile clipping
if (iTileModY && bFirstY) iScreenY=0-iTileModY;

// Calculate out of screen left tile clipping
if (iTileModX && bFirstX) iScreenX=0-iTileModX;

// Write the tile on screen, skip space tile, which is hard-coded by
// design at tile map value == 0.

if (g_LevelInfo.byTileMap[uiTileNumber]!=0)
Gfx_WriteTile (iScreenX,iScreenY,g_LevelInfo.byTileMap[uiTileNumber],NULL);

iScreenY+=TILESIZE; //Increase Y offset
bFirstY=FALSE; //We are not writing clipped Y tile anymore

iScreenX+=TILESIZE; //Increase X offset
bFirstX=FALSE; //We are not writing clipped X tile anymore

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement