Cpp:Building A Simple Tile Engine:Part 2

From GPWiki

Part 2: Creating the base entity


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: Entity.h

#ifndef ENTITY_H
#define ENTITY_H
 
#include "Interfaces.h"
 
/** Base entity class */
class CEntity: public IBaseEntity
{
private:
	int		m_iPosX;
	int		m_iPosY;
	SDL_Surface*	m_pSurface;
	bool		m_bVisible;
	int		m_iAlpha;
	string		m_sImage;
	int		m_iTileX;
	int		m_iTileY;
 
	void SetPosX	( int iPosX )			{ m_iPosX = iPosX; };
	void SetPosY	( int iPosY )			{ m_iPosY = iPosY; };
	void SetPos	( int iPosX, int iPosY )	{ SetPosX( iPosX ); SetPosY( iPosY ); }
 
protected:
	virtual bool VLoadImageFromFile( const string& sFile);
 
	int GetPosX	()			{ return m_iPosX; }
	int GetPosY	()			{ return m_iPosY; }
 
	void SetAlpha	( int iAlpha )		{ m_iAlpha = iAlpha; }
	void SetVisible	( bool bVisible )	{ m_bVisible = bVisible; }
 
	SDL_Rect GetRect();
 
public:
	CEntity();
	virtual ~CEntity();
 
	virtual void		VThink		( const int& iElapsedTime );
	virtual void		VRender		( SDL_Surface* pDestSurface );
 
	virtual bool		VLoad		( TiXmlElement* pXMLData );
	virtual TiXmlElement	VGetSaveData();
 
	void SetTileX		( int iTileX )			{ m_iTileX = iTileX; SetPosX( iTileX * 128 ); };
	void SetTileY		( int iTileY )			{ m_iTileY = iTileY; SetPosY( iTileY * 128 ); };
	void SetTile		( int iTileX, int iTileY )	{ SetTileX( iTileX ); SetTileY( iTileY ); }
 
	int	GetTileX	()				{ return m_iTileX; }
	int	GetTileY	()				{ return m_iTileY; }
	
	int 	GetHeight	()				{ return m_pSurface->h; }
	int 	GetWidth	()				{ return m_pSurface->w; }
	int 	GetBottom	()				{ return GetPosY() + GetHeight(); }
	int 	GetRight	() 				{ return GetPosX() + GetWidth(); }
 
	int	GetAlpha	() 				{ return m_iAlpha; }
	bool 	GetVisible	() 				{ return m_bVisible; }
};
 
#endif // ENTITY_H 

File: Entity.cpp

#include "Entity.h"
 
CEntity::CEntity()
{
	m_pSurface	= 0;
	SetVisible( true );
	SetPos( 0, 0 );
	SetTile( 0, 0 );
	SetAlpha( 255 );
}
 
 
CEntity::~CEntity()
{
	if ( m_pSurface != 0 )
		SDL_FreeSurface( m_pSurface );
}
 
bool CEntity::VLoad( TiXmlElement* pXMLData )
{
	string sImage = pXMLData->FirstChild( "image" )->FirstChild()->Value();
	bool bLoadOK = VLoadImageFromFile( sImage );
 
	return bLoadOK;
}
 
TiXmlElement CEntity::VGetSaveData()
{
	TiXmlElement ImageElm( "image" );
	TiXmlText ImageTxt( m_sImage.c_str() );
	ImageElm.InsertEndChild( ImageTxt );
	
	return ImageElm;
}
 
void CEntity::VThink( const int& iElapsedTime )
{
 
}
 
void CEntity::VRender( SDL_Surface *pDestSurface ) {
    if ( ( m_pSurface == 0 ) || ( m_bVisible == false) || ( m_iAlpha == 0 ) ) // If the surface is invalid or it's 100% transparent.
		return;
 
	SDL_Rect SDestRect;
	SDestRect.x = m_iPosX;  
	SDestRect.y = m_iPosY;
	SDestRect.w = m_pSurface->w;
	SDestRect.h = m_pSurface->h;
 
	if ( m_iAlpha != 255 ) 
		SDL_SetAlpha( m_pSurface, SDL_SRCALPHA, m_iAlpha );
 
	SDL_BlitSurface( m_pSurface, &m_pSurface->clip_rect, pDestSurface, &SDestRect );
}
 
bool CEntity::VLoadImageFromFile( const string& sFile) {
	if ( m_pSurface != 0 )
		SDL_FreeSurface( m_pSurface );
 
	SDL_Surface *pTempSurface;
	pTempSurface = SDL_LoadBMP( sFile.c_str() );
 
	m_sImage = sFile;
 
	if ( pTempSurface == 0 )
	{
		char czError[256];
		sprintf( czError, "Image '%s' could not be opened. Reason: %s", sFile.c_str(), SDL_GetError() );
		fprintf( stderr, "\nERROR: %s", czError );
		return false;
	} 
	else 
	{
		if( SDL_SetColorKey( pTempSurface, SDL_SRCCOLORKEY | SDL_RLEACCEL, SDL_MapRGB( pTempSurface->format, 255, 0, 255) ) == -1 )
		{
			char czWarning[256];
			sprintf( czWarning, "Image '%s' can't be color keyed. Reason: %s", sFile.c_str(), SDL_GetError() );
			fprintf( stderr, "\nWARNING: %s", czWarning );
		}
	}
 
    m_pSurface	= pTempSurface;
 
	return true;
}
 
SDL_Rect CEntity::GetRect()
{
	SDL_Rect stTemp = { GetPosX(), GetPosY(), GetWidth(), GetHeight() };
	return stTemp;
}

Proceed to Part 3: Creating tiles


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