// Source file
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include "../Logger/CLogger.h"
#include "SDL_rotozoom.h"
#include "CSurfaceObject.h"
using namespace Core;
CSurfaceObject::CSurfaceObject(void) : m_Surface(0)
{
ErrLog << "<< Creating a new Surface Object >>\n";
ErrLog << "<< Created a new Surface Object >>\n";
}
CSurfaceObject::CSurfaceObject(const std::string &Filename) : m_Surface(0)
{
ErrLog << "<< Creating a new Surface Object >>\n";
this->LoadImage(Filename);
ErrLog << "<< Created a new Surface Object >>\n";
}
CSurfaceObject::~CSurfaceObject(void)
{
ErrLog << "<< Destroying a Surface Object >>\n";
this->FreeSurface();
ErrLog << "<< Destroyed a Surface Object >>\n";
}
SDL_Surface * CSurfaceObject::GetSurfacePtr(void) const
{
return m_Surface;
}
bool CSurfaceObject::LoadImage(const std::string &Filename)
{
this->FreeSurface();
ErrLog << " Loading image: " << Filename << "\n";
m_Surface = IMG_Load(Filename.c_str() );
if(!m_Surface)
{
ErrLog << " ERROR: Image loading failed\n";
return false;
}
ErrLog << " Image loading succedeed\n";
// Set the Transparency by Colour key and convert the surface to the same format as the screen
SDL_SetColorKey(m_Surface, SDL_SRCCOLORKEY | SDL_RLEACCEL, SDL_MapRGB(m_Surface->format, 255, 0, 255) );
SDL_Surface * OldSurface = m_Surface;
m_Surface = SDL_DisplayFormat(m_Surface);
SDL_FreeSurface(OldSurface);
if(!m_Surface)
{
ErrLog << " ERROR: Conversion of Surface failed\n";
return false;
}
return true;
}
bool CSurfaceObject::ScaleImage(unsigned int Width, unsigned int Height)
{
ErrLog << " Scaling the Surface to Width: " << Width << " Height: " << Height << "\n";
if(!m_Surface)
{
ErrLog << " ERROR: Surface is NULL\n";
return false;
}
SDL_Surface * OldSurface = m_Surface;
m_Surface = zoomSurface
(
m_Surface,
static_cast<float>(Width) / static_cast<float>(m_Surface->w),
static_cast<float>(Height) / static_cast<float>(m_Surface->h),
0
);
if(!m_Surface)
{
ErrLog << " ERROR: Scaling falied. Reverting to orignal surface\n";
m_Surface = OldSurface;
return false;
}
// Set the Transparency by Colour key and convert the surface to the same format as the screen
SDL_SetColorKey(m_Surface, SDL_SRCCOLORKEY | SDL_RLEACCEL, SDL_MapRGB(m_Surface->format, 255, 0, 255) );
OldSurface = m_Surface;
m_Surface = SDL_DisplayFormat(m_Surface);
SDL_FreeSurface(OldSurface);
if(!m_Surface)
{
ErrLog << " ERROR: Conversion of Surface failed\n";
return false;
}
return true;
}
bool CSurfaceObject::ScaleImage(float ScaleFactor)
{
ErrLog << " Scaling the Surface by a factor of: " << ScaleFactor << "\n";
if(!m_Surface)
{
ErrLog << " ERROR: Surface is NULL\n";
return false;
}
SDL_Surface * OldSurface = m_Surface;
m_Surface = zoomSurface(m_Surface, ScaleFactor, ScaleFactor, 0);
if(!m_Surface)
{
ErrLog << " ERROR: Scaling falied. Reverting to orignal surface\n";
m_Surface = OldSurface;
return false;
}
// Set the Transparency by Colour key and convert the surface to the same format as the screen
SDL_SetColorKey(m_Surface, SDL_SRCCOLORKEY | SDL_RLEACCEL, SDL_MapRGB(m_Surface->format, 255, 0, 255) );
OldSurface = m_Surface;
m_Surface = SDL_DisplayFormat(m_Surface);
SDL_FreeSurface(OldSurface);
if(!m_Surface)
{
ErrLog << " ERROR: Conversion of Surface failed\n";
return false;
}
return true;
}
void CSurfaceObject::FreeSurface(void)
{
if(m_Surface != 0)
{
ErrLog << " Freeing the current Surface\n";
SDL_FreeSurface(m_Surface);
m_Surface = 0;
ErrLog << " Freed Surface\n";
}
else
{
ErrLog << " Current Surface is NULL\n";
}
}
Bookmarks