Cpp:Building A Simple Tile Engine:Part 4

From GPWiki

Files:GUITutorial_warn.gif The Game Programming Wiki has moved! Files:GUITutorial_warn.gif

The wiki is now hosted by GameDev.NET at wiki.gamedev.net. All gpwiki.org content has been moved to the new server.

However, the GPWiki forums are still active! Come say hello.

Part 4: Creating the tile manager


I haven't got the time to finish this article right now, so until I have here's a stub box for you to look at:

This page is far from complete, go ahead and write some more!
You can extend this page by editing it.

File: TileManager.h

#ifndef TILEMANAGER_H
#define TILEMANAGER_H
 
#include <vector>
using std::vector;
 
#include "Interfaces.h"
#include "Tile.h"
 
/** Tile mananger */
class CTileManager: public IBaseEntity
{
protected:
	vector< vector<CTile*> > m_vvpTiles;
 
	void AddTile( CTile* pTile );
 
public:
	CTileManager();
	virtual ~CTileManager();
 
	virtual void			VThink		( const int& iElapsedTime );
	virtual void			VRender		( SDL_Surface* pDestSurface	);
 
	virtual bool			VLoad		( TiXmlElement* pXMLData );
	virtual TiXmlElement	VGetSaveData();
 
	CTile* GetTile( const int& iX, const int& iY );
};
 
#endif // TILEMANAGER_H 

File: TileManager.cpp

#include "TileManager.h"
#include "Tile.h"
 
CTileManager::CTileManager()
{
	unsigned int maxXSize = 8;
	unsigned int maxYSize = 8;
 
	m_vvpTiles.resize( maxXSize );
	
	for( unsigned int x = 0; x < maxXSize; ++x )
	{
		m_vvpTiles[x].resize(maxYSize);
	}
}
 
CTileManager::~CTileManager()
{
	while( m_vvpTiles.size() > 0 ) 
	{
		while ( m_vvpTiles.back().size() > 0 )
		{
			if ( m_vvpTiles.back().back() != 0 ) {
				delete m_vvpTiles.back().back();
			}
			m_vvpTiles.back().pop_back();
		}
		m_vvpTiles.pop_back();
	}
}
 
void CTileManager::AddTile( CTile* pTile )
{
	if ( (unsigned int)pTile->GetTileX() < m_vvpTiles.size() && (unsigned int)pTile->GetTileY() < m_vvpTiles[pTile->GetTileX()].size() )
		m_vvpTiles[ pTile->GetTileX() ][ pTile->GetTileY() ] = pTile;
}
 
void CTileManager::VRender( SDL_Surface* pDestSurface )
{
	for ( unsigned int y = 0; y < m_vvpTiles.size(); ++y )
	{
		for ( unsigned int x = 0; x < m_vvpTiles[ y ].size(); ++x ) {
			if ( m_vvpTiles[x][y] != 0 )
				m_vvpTiles[x][y]->VRender( pDestSurface );
		}
	}
}
 
void CTileManager::VThink( const int& iElapsedTime )
{
	
}
 
 
bool CTileManager::VLoad( TiXmlElement* pXMLData )
{
	TiXmlNode* pXMLTile = 0;
	CTile* pTile;
	bool bLoadOK;
	while( pXMLTile = pXMLData->IterateChildren( pXMLTile ) ) {
		pTile = new CTile;
		bLoadOK = pTile->VLoad( pXMLTile->ToElement() );
		if ( bLoadOK == false ) {
			return false;
		}
		AddTile( pTile );
	}
	
	return true;
}
 
TiXmlElement CTileManager::VGetSaveData()
{
	TiXmlElement pXMLTiles( "tiles" );
 
	for ( unsigned int y = 0; y < m_vvpTiles.size(); ++y )
	{
		for ( unsigned int x = 0; x < m_vvpTiles[ y ].size(); ++x ) {
			if ( m_vvpTiles[x][y] != 0 )
				pXMLTiles.InsertEndChild( m_vvpTiles[x][y]->VGetSaveData() );
		}
	}
 
    return pXMLTiles;
}
 
CTile* CTileManager::GetTile( const int& iX, const int& iY )
{
	if ( (iX < 0 || (unsigned int)iX > m_vvpTiles.size()) && (iY < 0 || (unsigned int)iY > m_vvpTiles[0].size()) )
        return 0;
 
	return m_vvpTiles[iX][iY];
}

Proceed to Part 5: Creating units


Table of Content - Part 1 - Part 2 - Part 3 - Part 4 - Part 5 - Part 6 - Part 7 - Part 8 - Part X