Main Page | Class Hierarchy | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

SurfaceTool.cpp

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2004 Ivo Danihelka (ivo@danihelka.net)
00003  *
00004  * This program is free software; you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License as published by
00006  * the Free Software Foundation; either version 2 of the License, or
00007  * (at your option) any later version.
00008  */
00009 #include "SurfaceTool.h"
00010 
00011 #include "SDLException.h"
00012 
00013 //-----------------------------------------------------------------
00014 /**
00015  * Return new empty surface with the same format.
00016  * @param surface surface to ask for pixel format
00017  * @param width width or 0 for the same width
00018  * @param height height or 0 for the same height
00019  * @throws SDLException when function fails
00020  */
00021     SDL_Surface *
00022 SurfaceTool::createEmpty(SDL_Surface *surface, int width, int height)
00023 {
00024     if (!width) {
00025         width = surface->w;
00026     }
00027     if (!height) {
00028         height = surface->h;
00029     }
00030 
00031     SDL_Surface *result = SDL_CreateRGBSurface(surface->flags, width, height,
00032             surface->format->BitsPerPixel,
00033             surface->format->Rmask,
00034             surface->format->Gmask,
00035             surface->format->Bmask,
00036             surface->format->Amask);
00037     if (NULL == result) {
00038         throw SDLException(ExInfo("ConvertSurface"));
00039     }
00040     return result;
00041 }
00042 //-----------------------------------------------------------------
00043 /**
00044  * Return new cloned surface.
00045  * @return new surface, free it after use
00046  * @throws SDLException when function fails
00047  */
00048     SDL_Surface *
00049 SurfaceTool::createClone(SDL_Surface *surface)
00050 {
00051     SDL_Surface *clone = SDL_ConvertSurface(surface,
00052             surface->format, surface->flags);
00053     if (NULL == clone) {
00054         throw SDLException(ExInfo("ConvertSurface"));
00055     }
00056     return clone;
00057 }
00058 //-----------------------------------------------------------------
00059 /**
00060  * Fill surface with given color. Alpha value in color is supported.
00061  * The final blit rectangle is saved in dstrect.
00062  *
00063  * @param surface surface to fill
00064  * @param dstrect dstrect or NULL (the whole surface will be filled with color).
00065  * @param color {red, green, blue, alpha}
00066  */
00067     void
00068 SurfaceTool::alphaFill(SDL_Surface *surface, SDL_Rect *dstrect,
00069         const SDL_Color &color)
00070 {
00071     int w = surface->w;
00072     int h = surface->h;
00073     if (dstrect) {
00074         w = dstrect->w;
00075         h = dstrect->h;
00076     }
00077     SDL_Surface *canvas = createEmpty(surface, w, h);
00078     Uint32 pixel = SDL_MapRGB(canvas->format, color.r, color.g, color.b);
00079     SDL_FillRect(canvas, NULL, pixel);
00080     SDL_SetAlpha(canvas, SDL_SRCALPHA|SDL_RLEACCEL, color.unused);
00081 
00082     SDL_BlitSurface(canvas, NULL, surface, dstrect);
00083     SDL_FreeSurface(canvas);
00084 }
00085 

Generated on Wed Jun 1 09:54:31 2005 for Fish Fillets - Next Generation by  doxygen 1.4.2