Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/CSurface.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/CSurface.cpp')
-rwxr-xr-xsrc/CSurface.cpp310
1 files changed, 310 insertions, 0 deletions
diff --git a/src/CSurface.cpp b/src/CSurface.cpp
new file mode 100755
index 0000000..645f089
--- /dev/null
+++ b/src/CSurface.cpp
@@ -0,0 +1,310 @@
+#include "CSurface.h"
+
+CSurface::CSurface(void)
+{
+ mImage = NULL;
+}
+
+CSurface::CSurface(int aWidth, int aHeight)
+{
+ // create_blank_surface: Generates a new surface based on the
+// current video settings.
+
+ // Acquire the settings in the surface.
+ mImage = SDL_GetVideoSurface();
+
+ // Create the new surface using the Right-To_Left principle.
+ mImage = SDL_CreateRGBSurface (mImage->flags, aWidth, aHeight,
+ mImage->format->BitsPerPixel,
+ mImage->format->Rmask,
+ mImage->format->Gmask,
+ mImage->format->Bmask,
+ mImage->format->Amask);
+
+ SDL_SetColorKey(mImage, SDL_SRCCOLORKEY, SDL_MapRGB(mImage->format, 255, 0, 255));
+}
+
+CSurface::CSurface(const char *aFile)
+{
+ mImage = CSurface::loadImage(aFile);
+}
+
+CSurface::CSurface(SDL_Surface *aSDLSurface)
+{
+ mImage = aSDLSurface;
+}
+
+CSurface::CSurface(SDL_Surface *aSDLSurface, int aFlipFlags)
+{
+ mImage = flipSurface(aSDLSurface, aFlipFlags);
+}
+
+CSurface::~CSurface(void)
+{
+ destroy();
+}
+
+void CSurface::destroy(void)
+{
+ if (mImage != NULL)
+ {
+ SDL_FreeSurface(mImage);
+ mImage = NULL;
+ }
+}
+
+//--------------------------------------------------------------------------------------------------
+// getImage()
+// Returns the image.
+//
+// Arguments:
+// None.
+//
+// Returns: Pointer to the image.
+//--------------------------------------------------------------------------------------------------
+SDL_Surface *CSurface::getImage(void)
+{
+ return mImage;
+}
+
+//--------------------------------------------------------------------------------------------------
+// draw()
+// Blits the entire image on (x,y) coordinates.
+//
+// Arguments:
+// aImgDst: Surface in which we draw the image.
+// aX, aY: Coordinates where to draw.
+//
+// Returns: Nothing.
+//--------------------------------------------------------------------------------------------------
+void CSurface::draw(SDL_Surface *aImgDst, int aX, int aY)
+{
+ CSurface::drawImage(aImgDst, mImage, aX, aY);
+}
+
+//--------------------------------------------------------------------------------------------------
+// loadImage()
+// It loads an image and returns a pointer to the image converted to the screen format.
+//
+// Arguments:
+// aFile: Name of the image file to load.
+//
+// Returns: Pointer to the created surface. NULL if there is an error.
+//--------------------------------------------------------------------------------------------------
+SDL_Surface *CSurface::loadImage(const char *aFile)
+{
+ SDL_Surface *loadedImg = NULL;
+ SDL_Surface *optimizedImg = NULL;
+
+ //loadedImg = SDL_LoadBMP(file);
+
+ // Load the image using SDL_image.
+ return loadedImg = IMG_Load(aFile);
+
+ if (loadedImg != NULL)
+ {
+ optimizedImg = SDL_DisplayFormat(loadedImg);
+ // If the conversion fails or runs out of memory...
+ if (optimizedImg == NULL)
+ {
+ fprintf(stderr, "SDL_DisplayFormat() failed!\n");
+ fprintf(stderr, "Error: %s\n", SDL_GetError());
+ }
+ //else
+ //{
+ // SDL_SetColorKey(optimizedImg, SDL_SRCCOLORKEY, SDL_MapRGB(optimizedImg->format, 0, 0xFF, 0xFF));
+ //}
+
+ SDL_FreeSurface(loadedImg);
+ }
+ else
+ {
+ fprintf(stderr, "loadImage() failed!\n");
+ fprintf(stderr, "Error: %s\n", SDL_GetError());
+ }
+
+ return optimizedImg;
+}
+
+//--------------------------------------------------------------------------------------------------
+// drawImage()
+// Blits the entire image on (x,y) coordinates.
+//
+// Arguments:
+// aImgDst: Surface in which we draw the image.
+// aImgSrc: Image to be drawn.
+// aX, aY: Coordinates where to draw.
+//
+// Returns: Nothing.
+//--------------------------------------------------------------------------------------------------
+void CSurface::drawImage(SDL_Surface *aImgDst, SDL_Surface *aImgSrc, int aX, int aY)
+{
+ SDL_Rect dest;
+ dest.x = aX;
+ dest.y = aY;
+ SDL_BlitSurface(aImgSrc, NULL, aImgDst, &dest);
+}
+
+//--------------------------------------------------------------------------------------------------
+// drawImage()
+// Blits the rectangle (xSrc,ySrc,wSrc,hSrc) of the source image on (x,y) coordinates of the
+// destination image.
+//
+// Arguments:
+// aImgDst: Surface in which we draw the image.
+// aImgSrc: Source image where the rectangle to be drawn resides in.
+// xDest, yDest: Coordinates where to draw in the destination image.
+// xSrc, ySrc: Coordinates of the rectangle to be drawn (in the source image).
+// wSrc, hSrc: Width and height of the rectangle to be drawn (in the source image).
+//
+// Returns: Nothing.
+//--------------------------------------------------------------------------------------------------
+void CSurface::drawImage(SDL_Surface *aImgDst, SDL_Surface *aImgSrc, int aXDest, int aYDest, int aXSrc, int aYSrc, int aWSrc, int aHSrc)
+{
+ SDL_Rect srcRect;
+ srcRect.x = aXSrc;
+ srcRect.y = aYSrc;
+ srcRect.w = aWSrc;
+ srcRect.h = aHSrc;
+
+ SDL_Rect srcDest;
+ srcDest.x = aXDest;
+ srcDest.y = aYDest;
+
+ SDL_BlitSurface(aImgSrc, &srcRect, aImgDst, &srcDest);
+}
+
+//--------------------------------------------------------------------------------------------------
+// drawImage()
+// Blits the entire image on (x,y) coordinates.
+//
+// Arguments:
+// aImgDst: Surface in which we draw the image.
+// aImgSrc: Image to be drawn.
+// aX, aY: Coordinates where to draw.
+//
+// Returns: Nothing.
+//--------------------------------------------------------------------------------------------------
+void CSurface::drawImage(CSurface *aImgDst, CSurface *aImgSrc, int aX, int aY)
+{
+ CSurface::drawImage(aImgDst->getImage(), aImgSrc->getImage(), aX, aY);
+}
+
+//--------------------------------------------------------------------------------------------------
+// drawImage()
+// Blits the rectangle (xSrc,ySrc,wSrc,hSrc) of the source image on (x,y) coordinates of the
+// destination image.
+//
+// Arguments:
+// aImgDst: Surface in which we draw the image.
+// aImgSrc: Source image where the rectangle to be drawn resides in.
+// xDest, yDest: Coordinates where to draw in the destination image.
+// xSrc, ySrc: Coordinates of the rectangle to be drawn (in the source image).
+// wSrc, hSrc: Width and height of the rectangle to be drawn (in the source image).
+//
+// Returns: Nothing.
+//--------------------------------------------------------------------------------------------------
+void CSurface::drawImage(CSurface *aImgDst, CSurface *aImgSrc, int aXDest, int aYDest, int aXSrc, int aYSrc, int aWSrc, int aHSrc)
+{
+ CSurface::drawImage(aImgDst->getImage(), aImgSrc->getImage(), aXDest, aYDest, aXSrc, aYSrc, aWSrc, aHSrc);
+}
+
+void CSurface::drawRect(SDL_Surface *aImgDst, int aX, int aY, int aW, int aH, int aColor)
+{
+ SDL_Rect dest;
+ dest.x = aX;
+ dest.y = aY;
+ dest.w = aW;
+ dest.h = aH;
+ SDL_FillRect(aImgDst, &dest, aColor);
+}
+
+void CSurface::drawRect(CSurface *aImgDst, int aX, int aY, int aW, int aH, int aColor)
+{
+ CSurface::drawRect(aImgDst->getImage(), aX, aY, aW, aH, aColor);
+}
+
+
+Uint32 CSurface::get_pixel32(SDL_Surface *surface, int x, int y)
+{
+ //Convert the pixels to 32 bit
+ Uint32 *pixels = (Uint32 *)surface->pixels;
+
+ //Get the requested pixel
+ return pixels[ ( y * surface->w ) + x ];
+}
+
+void CSurface::put_pixel32( SDL_Surface *surface, int x, int y, Uint32 pixel )
+{
+ //Convert the pixels to 32 bit
+ Uint32 *pixels = (Uint32 *)surface->pixels;
+
+ //Set the pixel
+ pixels[ ( y * surface->w ) + x ] = pixel;
+}
+
+SDL_Surface *CSurface::flipSurface(SDL_Surface *surface, int flags)
+{
+ //Pointer to the soon to be flipped surface
+ SDL_Surface *flipped = NULL;
+
+ //If the image is color keyed
+ if( surface->flags & SDL_SRCCOLORKEY )
+ {
+ flipped = SDL_CreateRGBSurface( SDL_SWSURFACE, surface->w, surface->h, surface->format->BitsPerPixel, surface->format->Rmask, surface->format->Gmask, surface->format->Bmask, 0 );
+ }
+ //Otherwise
+ else
+ {
+ flipped = SDL_CreateRGBSurface( SDL_SWSURFACE, surface->w, surface->h, surface->format->BitsPerPixel, surface->format->Rmask, surface->format->Gmask, surface->format->Bmask, surface->format->Amask );
+ }
+
+ //If the surface must be locked
+ if( SDL_MUSTLOCK( surface ) )
+ {
+ //Lock the surface
+ SDL_LockSurface( surface );
+ }
+
+ //Go through columns
+ for( int x = 0, rx = flipped->w - 1; x < flipped->w; x++, rx-- )
+ {
+ //Go through rows
+ for( int y = 0, ry = flipped->h - 1; y < flipped->h; y++, ry-- )
+ {
+ //Get pixel
+ Uint32 pixel = get_pixel32( surface, x, y );
+
+ //Copy pixel
+ if( ( flags & FLIP_VERTICAL ) && ( flags & FLIP_HORIZONTAL ) )
+ {
+ put_pixel32( flipped, rx, ry, pixel );
+ }
+ else if( flags & FLIP_HORIZONTAL )
+ {
+ put_pixel32( flipped, rx, y, pixel );
+ }
+ else if( flags & FLIP_VERTICAL )
+ {
+ put_pixel32( flipped, x, ry, pixel );
+ }
+ }
+ }
+
+ //Unlock surface
+ if( SDL_MUSTLOCK( surface ) )
+ {
+ SDL_UnlockSurface( surface );
+ }
+
+ //Copy color key
+ if( surface->flags & SDL_SRCCOLORKEY )
+ {
+ SDL_SetColorKey( flipped, SDL_RLEACCEL | SDL_SRCCOLORKEY, surface->format->colorkey );
+ }
+
+ //Return flipped surface
+ return flipped;
+}
+
+