Quote:
// Header file
#ifndef CSURFACEOBJECT_H
#define CSURFACEOBJECT_H
#include <string>
struct SDL_Surface;
namespace Core
{
/// OO class that wraps around SDL_Surface and functions from SDL
class CSurfaceObject
{
private:
/// Pointer to the SDL_Surface
SDL_Surface * m_Surface;
public:
/// Standard constructor. NULLs the Surface
CSurfaceObject(void);
/// Contructor loading an image for the surface
CSurfaceObject(const std::string &Filename);
/// Destructor. Frees the surface if not NULL
~CSurfaceObject(void);
/// Returns pointer, the Surface
/**
\return A pointer to m_Surface
*/
SDL_Surface * GetSurfacePtr(void) const;
/// Load a new image. Frees the surface if not NULL
/**
\param Filename a const string of the file path to the image
\return True if loading the image succeded, false if failed
*/
bool LoadImage(const std::string &Filename);
/// Scale the surface to a defined width and height
/**
\param Width Unsigned integer of the new width in pixels
\param Height Unsigned integer of the new height in pixels
\return True if loading the scaling succeded, false if failed
*/
bool ScaleImage(unsigned int Width, unsigned int Height);
/// Scale the surface by a scaling factor
/**
\param Width Float of the Scaling factor
\return True if loading the scaling succeded, false if failed
*/
bool ScaleImage(float ScalingFactor);
/// Frees the Surface
void FreeSurface(void);
};
}
#endif // CSURFACEOBJECT_H
Quote:
// 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";
}
}