Sign in to follow this  
kingpinzs

Help with tile engine and loading images

Recommended Posts

kingpinzs    117
well you see I got this tile engine I made it is a ISO engine and I thought I was all cool and stuff. I loaded the tiles form one bitmap and laoded the map from a text file and all was good. The tiles were all 64 x 64 but the base tiles were 64 x 32 so on life went. now I need to use all kindes of differnt tile sizes. How can I make it so my engine can use any size of tile? And or all kinds of differnt bmp to load the tiles?

Share this post


Link to post
Share on other sites
Programmer16    2321
The way that I do this is by using an array of tiles:

class Tile
{
public:
UINT m_nColumn, m_nRow; // In the tileset
UINT m_nWidth, m_nHeight;
RECT m_BoundsRect;
};

class Tileset
{
Tile* m_pTiles;
UINT m_nColCount, m_nRowCount;
// texture information here (i.e. filename)
public:

void Create(UINT nColCount, UINT nRowCount/*,TextureParams*/)
{
m_nColCount = nColCount;
m_nRowCount = nRowCount;
m_pTiles = new Tile[m_nColCount * m_nRowCount];
}

void Release()
{
delete [] m_pTiles;
}

Tile* GetTiles()
{
return m_pTiles;
}
};

class TileMap
{
Tileset m_Tileset;
UINT* m_pTiles; // Indices into our tileset
public:
void ReadTileset(const char* pFileName)
{
// Open the map file
std::ifstream fin(pFileName);

// Read in the column and row count for the tileset
int iColCount = 0, iRowCount = 0;
fin>>iColCount>>iRowCount;
m_Tileset.Create(iColCount, iRowCount);
Tile* pTiles = m_Tileset.GetTiles();

// Iterate through and fill the tileset structure
for(unsigned int nCurRow = 0; nCurRow < iRowCount; ++nCurRow)
{
for(unsigned int nCurCol = 0; nCurCol < iColCount; ++nCurCol)
{
int iSector = (nCurRow * iColCount) + nCurCol;
pTiles[iSector].m_nColumn = nCurCol;
pTiles[iSector].m_nRow = nCurRow;
fin>>pTiles[iSector].m_nWidth>>pTiles[iSector].m_nHeight;
fin>>pTiles[iSector].m_BoundsRect.left>>pTiles[iSector].m_BoundsRect.top>>pTiles[iSector].m_BoundsRect.right>>pTiles[iSector].m_BoundsRect.bottom;
}
}
fin.close();
}

void ReadMap(const char* pFileName)
{
std::ifstream fin(pFileName);

// Read in the coumn and row count for the map
int iColCount = 0, iRowCount = 0;
fin>>iColCount>>iRowCount;

// Create our index array
m_pTiles = new UINT[iColCount * iRowCount];

// Iterate through and fill our tile indices
for(unsigned int nCurRow = 0; nCurRow < iRowCount; ++nCurRow)
{
for(unsigned int nCurCol = 0; nCurCol < iColCount; ++nCurCol)
{
int iSector = (nCurRow * iColCount) + nCurCol;
fin>>m_pTiles[iSector];
}
}
fin.close();
}

void Release()
{
delete m_pTiles;
m_Tileset.Release();
}
};


The tileset and map files

/*Our tileset (remove the oomments to use)
3 1 // 3 columns 1 row
64 64 0 32 64 64 // 64 wide by 64 tall; our rect is the bottom half (64x32); floor tile
32 64 0 0 32 64 // 32 wide by 64 tall; our rect is the entire tile (32x64); wall tile
64 96 0 0 64 128 // 64 wide by 96 tall; our rect is the entire tile (64x96); tree tile
*/


/*And our map (remove the comments to use)
// 0 is the floor tile
// 1 is the wall tile
// 2 is the tree tile
// 10 columns 10 rows
10 10
0 0 0 0 0 0 0 0 0 0
0 1 1 1 1 1 0 0 0 0
0 1 0 0 0 1 1 1 0 0
0 1 0 0 0 0 0 1 0 0
0 1 0 0 0 0 0 1 1 0
0 1 0 0 2 0 0 0 1 0
0 1 0 0 0 0 0 0 1 0
0 1 0 0 0 0 0 0 1 0
0 1 1 1 1 1 1 1 1 0
0 0 0 0 0 0 0 0 0 0
*/




Of course, this map won't really work with an iso engine (since one wall tile can't be used for all 4 directions.)

Hope that helps!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this