#include "Entity.h"
Entity::Entity()
{
surface = NULL;
setVisible(true);
setPos(0, 0);
setTile(0,0);
setAlpha(255);
}
Entity::~Entity()
{
if (surface != NULL)
SDL_FreeSurface(surface);
}
bool Entity::vLoad(TiXmlElement *pXMLData)
{
string sImage = pXMLData->FirstChild("image")->FirstChild()->Value();
bool bLoadOK = vLoadImageFromFile(sImage);
return bLoadOK;
}
TiXmlElement Entity::vGetSaveData()
{
TiXmlElement imageElm("image");
TiXmlText imageTxt(image.c_str());
imageElm.InsertEndChild(imageTxt);
return imageElm;
}
void Entity::vThink(const int &elapsedTime)
{
}
void Entity::vRender(SDL_Surface *pDestSurface)
{
if (surface == NULL || isVisible == false || alpha == 0)
return;
SDL_Rect rect;
rect.x = posX;
rect.y = posY;
rect.w = surface->w;
rect.h = surface->h;
if (alpha != 255)
SDL_SetAlpha(surface, SDL_SRCALPHA, alpha);
SDL_BlitSurface(surface, &surface->clip_rect, pDestSurface, &rect);
}
bool Entity::vLoadImageFromFile(const string &sFile)
{
if (surface != NULL)
SDL_FreeSurface(surface);
SDL_Surface *tempSurface;
tempSurface = SDL_LoadBMP(sFile.c_str());
image = sFile;
if (tempSurface == NULL)
{
cout << "Cannot open file: " << sFile << " : " << SDL_GetError() << endl;
return false;
}
else
{
if (SDL_SetColorKey(tempSurface, SDL_SRCCOLORKEY | SDL_RLEACCEL, SDL_MapRGB(tempSurface->format, 255, 0, 255)) == -1)
cout << "Image cannot color keyed: " << sFile << " : " << SDL_GetError() << endl;
}
surface = tempSurface;
return true;
}
SDL_Rect Entity::getRect()
{
SDL_Rect temp = {getPosX(), getPosY(), getWidth(), getHeight() };
return temp;
}