| 1 |
/* |
| 2 |
* Copyright (C) 2004 Ivo Danihelka ([email protected]) |
| 3 |
* |
| 4 |
* This program is free software; you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU General Public License as published by |
| 6 |
* the Free Software Foundation; either version 2 of the License, or |
| 7 |
* (at your option) any later version. |
| 8 |
*/ |
| 9 |
#include "SurfaceTool.h" |
| 10 |
|
| 11 |
#include "SDLException.h" |
| 12 |
|
| 13 |
//----------------------------------------------------------------- |
| 14 |
/** |
| 15 |
* Return new empty surface with the same format. |
| 16 |
* @param surface surface to ask for pixel format |
| 17 |
* @param width width or 0 for the same width |
| 18 |
* @param height height or 0 for the same height |
| 19 |
* @throws SDLException when function fails |
| 20 |
*/ |
| 21 |
SDL_Surface * |
| 22 |
SurfaceTool::createEmpty(SDL_Surface *surface, int width, int height) |
| 23 |
{ |
| 24 |
if (!width) { |
| 25 |
width = surface->w; |
| 26 |
} |
| 27 |
if (!height) { |
| 28 |
height = surface->h; |
| 29 |
} |
| 30 |
|
| 31 |
SDL_Surface *result = SDL_CreateRGBSurface(surface->flags, width, height, |
| 32 |
surface->format->BitsPerPixel, |
| 33 |
surface->format->Rmask, |
| 34 |
surface->format->Gmask, |
| 35 |
surface->format->Bmask, |
| 36 |
surface->format->Amask); |
| 37 |
if (NULL == result) { |
| 38 |
throw SDLException(ExInfo("CreateRGBSurface")); |
| 39 |
} |
| 40 |
return result; |
| 41 |
} |
| 42 |
//----------------------------------------------------------------- |
| 43 |
/** |
| 44 |
* Return new surface with transparent background. |
| 45 |
*/ |
| 46 |
SDL_Surface * |
| 47 |
SurfaceTool::createTransparent(int w, int h, const SDL_Color &transparent) |
| 48 |
{ |
| 49 |
SDL_Surface *surface = SDL_CreateRGBSurface(SDL_SWSURFACE|SDL_SRCCOLORKEY, |
| 50 |
w, h, 32, |
| 51 |
0, 0, 0, 0); |
| 52 |
if (NULL == surface) { |
| 53 |
throw SDLException(ExInfo("CreateRGBSurface")); |
| 54 |
} |
| 55 |
|
| 56 |
Uint32 transparentKey = SDL_MapRGB(surface->format, |
| 57 |
transparent.r, transparent.g, transparent.b); |
| 58 |
SDL_SetColorKey(surface, SDL_SRCCOLORKEY|SDL_RLEACCEL, transparentKey); |
| 59 |
|
| 60 |
SurfaceTool::alphaFill(surface, NULL, transparent); |
| 61 |
return surface; |
| 62 |
} |
| 63 |
//----------------------------------------------------------------- |
| 64 |
/** |
| 65 |
* Return new cloned surface. |
| 66 |
* @return new surface, free it after use |
| 67 |
* @throws SDLException when function fails |
| 68 |
*/ |
| 69 |
SDL_Surface * |
| 70 |
SurfaceTool::createClone(SDL_Surface *surface) |
| 71 |
{ |
| 72 |
SDL_Surface *clone = SDL_ConvertSurface(surface, |
| 73 |
surface->format, surface->flags); |
| 74 |
if (NULL == clone) { |
| 75 |
throw SDLException(ExInfo("ConvertSurface")); |
| 76 |
} |
| 77 |
return clone; |
| 78 |
} |
| 79 |
//----------------------------------------------------------------- |
| 80 |
/** |
| 81 |
* Fill surface with given color. Alpha value in color is supported. |
| 82 |
* The final blit rectangle is saved in dstrect. |
| 83 |
* |
| 84 |
* @param surface surface to fill |
| 85 |
* @param dstrect dstrect or NULL (the whole surface will be filled with color). |
| 86 |
* @param color {red, green, blue, alpha} |
| 87 |
*/ |
| 88 |
void |
| 89 |
SurfaceTool::alphaFill(SDL_Surface *surface, SDL_Rect *dstrect, |
| 90 |
const SDL_Color &color) |
| 91 |
{ |
| 92 |
int w = surface->w; |
| 93 |
int h = surface->h; |
| 94 |
if (dstrect) { |
| 95 |
w = dstrect->w; |
| 96 |
h = dstrect->h; |
| 97 |
} |
| 98 |
SDL_Surface *canvas = createEmpty(surface, w, h); |
| 99 |
Uint32 pixel = SDL_MapRGB(canvas->format, color.r, color.g, color.b); |
| 100 |
SDL_FillRect(canvas, NULL, pixel); |
| 101 |
SDL_SetAlpha(canvas, SDL_SRCALPHA|SDL_RLEACCEL, color.unused); |
| 102 |
|
| 103 |
SDL_BlitSurface(canvas, NULL, surface, dstrect); |
| 104 |
SDL_FreeSurface(canvas); |
| 105 |
} |
| 106 |
|