- Main Page
- Classes
- Files
- Directories
- Related Pages

# painter.h

    00001 /*--License:
    00002         Kyra Sprite Engine
    00003         Copyright Lee Thomason (Grinning Lizard Software) 2001-2005
    00004         www.grinninglizard.com/kyra
    00005         www.sourceforge.net/projects/kyra
    00006
    00007         Kyra is provided under the LGPL.
    00008
    00009         I kindly request you display a splash screen (provided in the HTML documentation)
    00010         to promote Kyra and acknowledge the software and everyone who has contributed to it,
    00011         but it is not required by the license.
    00012
    00013 --- LGPL License --
    00014
    00015     This library is free software; you can redistribute it and/or
    00016     modify it under the terms of the GNU Lesser General Public
    00017     License as published by the Free Software Foundation; either
    00018     version 2.1 of the License, or (at your option) any later version.
    00019
    00020     This library is distributed in the hope that it will be useful,
    00021     but WITHOUT ANY WARRANTY; without even the implied warranty of
    00022     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00023     Lesser General Public License for more details.
    00024
    00025     You should have received a copy of the GNU Lesser General Public
    00026     License along with this library; if not, write to the Free Software
    00027     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    00028
    00029         The full text of the license can be found in lgpl.txt
    00030 */
    00031
    00032 #ifndef SURFACE_INCLUDED
    00033 #define SURFACE_INCLUDED
    00034
    00035 #ifdef _MSC_VER
    00036 #pragma warning( disable : 4530 )
    00037 #pragma warning( disable : 4786 )
    00038 #endif
    00039
    00040 #include <string>
    00041
    00042 #include "SDL.h"
    00043 #include "../../grinliz/gltypes.h"
    00044 #include "color.h"
    00045 #include "../engine/krmath.h"
    00046 #include "../engine/kyrabuild.h"
    00047
    00048 struct KrPaintInfo;
    00049 class KrTexture;
    00050
    00051
    00057 class KrPainter
    00058 {
    00059   public:
    00060         KrPainter( SDL_Surface* _surface );
    00061         KrPainter( KrPaintInfo* _info );
    00062
    00063         ~KrPainter();
    00064
    00069         void SetPixel( int x, int y, const KrRGBA& color );
    00070
    00072         void SetPixel( void* target, U8 red, U8 green, U8 blue, U8 alpha );
    00073
    00075         void DrawBox(   int x, int y, int w, int h,
    00076                                         U8 red, U8 green, U8 blue );
    00077
    00079         void DrawBox(   int x, int y, int w, int h,
    00080                                         const KrRGBA* colors, int nColors );
    00081
    00083         void DrawFilledBox(     int x, int y, int w, int h,
    00084                                                 U8 red, U8 green, U8 blue );
    00085
    00087         void DrawVLine( int x, int y, int h,
    00088                                         U8 red, U8 green, U8 blue );
    00089
    00091         void DrawVLine( int x, int y, int h,
    00092                                         const KrRGBA* colors, int nColors );
    00093
    00094
    00096         void DrawHLine( int x, int y, int h,
    00097                                         U8 red, U8 green, U8 blue );
    00098
    00100         void DrawHLine( int x, int y, int h,
    00101                                         const KrRGBA* colors, int nColors );
    00102
    00103         /*  Given the starting location, and a maximum width,
    00104                 returns the number of consectutive transparent pixels.
    00105         */
    00106         int CalcTransparentRun( int xmin, int xmax, int y );
    00107
    00108         /*  Given the starting location, and a maximum width,
    00109                 returns the number of consectutive transparent pixels.
    00110         */
    00111         int CalcTransparentColumn( int ymin, int ymax, int x );
    00112
    00113         /*  Given the starting location, and a maximum width,
    00114                 returns the number of consectutive pixels which are
    00115                 not transparent.
    00116         */
    00117         int CalcNotTransparentRun( int xmin, int xmax, int y );
    00118
    00119         /*  Given the starting location, and a maximum width,
    00120                 returns the number of consectutive pixels which are
    00121                 not transparent.
    00122         */
    00123         int CalcOpaqueRun( int xmin, int xmax, int y );
    00124
    00125         /*  Given the starting location, and a maximum width,
    00126                 returns the number of consectutive pixels which are
    00127                 not transparent.
    00128         */
    00129         int CalcTranslucentRun( int xmin, int xmax, int y );
    00130
    00131         /*      Finds a pixel of a given color.
    00132                 x, y:   where to start looking
    00133                 dx, dy: the step to travel. (1,1) would be a diagonal to the upper left
    00134                 color:  the pixel color to look for
    00135                 useAlpha: if true, will use the alpha channel as part of the comparison
    00136                 invert: flip the logic. Find the first pixel that is NOT == color
    00137
    00138                 returns: -1 if not found, or the number of steps taken if the pixel was found.
    00139         */
    00140         int FindPixel( int x, int y, int dx, int dy, KrRGBA color, bool useAlpha, bool invert = false );
    00141
    00146         void BreakPixel( int x, int y, U8* r, U8* g, U8* b, U8* a );
    00147
    00152         void BreakPixel( int x, int y, KrRGBA* rgba )   { BreakPixel( x, y, &rgba->c.red, &rgba->c.green, &rgba->c.blue, &rgba->c.alpha ); }
    00153
    00154   private:
    00155         SDL_Surface* surface;
    00156 };
    00157
    00158
    00159 typedef void (*KrPaintFunc)(    KrPaintInfo* info,
    00160                                                                 void* target,
    00161                                                                 KrRGBA* source,
    00162                                                                 int nPixel,
    00163                                                                 const KrColorTransform cform );
    00164
    00165 typedef void (*KrPaintFuncRotated)( KrPaintInfo* info,
    00166                                                                     void*               target,
    00167                                                                         KrRGBA* source,
    00168                                                                         int sPitch,
    00169                                                                         int nPixel );
    00170
    00171
    00172 struct KrPaintInfo
    00173 {
    00174         // Initialize to paint to a surface:
    00175         KrPaintInfo( SDL_Surface* screen );
    00176
    00177         // Initialize to paint to a block of KrRGBA:
    00178         KrPaintInfo( KrRGBA* memory, int width, int height );
    00179
    00180         ~KrPaintInfo()  { if ( needToFreeSurface ) SDL_FreeSurface( surface ); }
    00181
    00182         // Based on the cform and the source alpha,
    00183         // get the correct Bltter. 'sourceAlpha' should be true
    00184         // if there can be alpha anywhere in the source.
    00185         KrPaintFunc GetBlitter( bool sourceAlpha,
    00186                                                         const KrColorTransform cform );
    00187
    00188         // Like get blitter, for openGl. Sets up the texture and color of the poly.
    00189         void SetOpenGLTextureMode(      bool sourceAlpha,
    00190                                                                 const KrColorTransform cform,
    00191                                                                 bool isScaled,
    00192                                                                 KrTexture* texture );
    00193
    00194         void GetBlitterName( KrPaintFunc func, std::string* name );
    00195
    00196         bool OpenGL()   { return openGL; }
    00197
    00198         int width;
    00199         int height;
    00200         int pitch;
    00201         int bytesPerPixel;
    00202         void* pixels;
    00203         bool openGL;
    00204
    00205         // Copies of the SDL stuff to avoid dereferencing in the blitters
    00206         // 32 and 16 bit modes.
    00207         U8  redShift;
    00208         U8  greenShift;
    00209         U8  blueShift;
    00210         U8  alphaShift;
    00211
    00212         U32     redMask;
    00213         U32     greenMask;
    00214         U32     blueMask;
    00215         U32 alphaMask;
    00216
    00217         U8  redLoss;
    00218         U8      greenLoss;
    00219         U8  blueLoss;
    00220         U8      alphaLoss;
    00221
    00222         // 24 bit mode.
    00223         U8      redByte;
    00224         U8      greenByte;
    00225         U8      blueByte;
    00226
    00227         KrPaintFunc Paint_Simple_NoAlpha;
    00228         KrPaintFunc Paint_Color_NoAlpha;
    00229         KrPaintFunc Paint_Alpha_NoAlpha;
    00230         KrPaintFunc Paint_Full_NoAlpha;
    00231
    00232         KrPaintFunc Paint_Simple_Alpha;
    00233         KrPaintFunc Paint_Color_Alpha;
    00234         KrPaintFunc Paint_Alpha_Alpha;
    00235         KrPaintFunc Paint_Full_Alpha;
    00236
    00237         KrPaintFuncRotated PaintRotated_Simple_NoAlpha;
    00238
    00239         SDL_Surface*    surface;
    00240
    00241   private:
    00242         bool needToFreeSurface;
    00243         void InitCopies();
    00244 };
    00245
    00246 // Format:
    00247 // KrPaint #Bits Target Source
    00248 //
    00249 // where Target can be:
    00250 //              Simple: no color transform
    00251 //              Color:  color transform, but alpha==255
    00252 //              Alpha:  no RGB transform, but has alpha
    00253 //              Full:   RGB and alpha transform
    00254 //
    00255 // where Source image can be:
    00256 //              NoAlpha: all alpha values in source == 255
    00257 //              Alpha: some alpha values are not 255
    00258 //
    00259
    00260 void KrPaint32_Simple_NoAlpha( KrPaintInfo* info, void* target, KrRGBA* source, int nPixel, const KrColorTransform cform );
    00261 void KrPaint32B_Simple_NoAlpha( KrPaintInfo* info, void* target, KrRGBA* source, int nPixel, const KrColorTransform cform );
    00262
    00263 void KrPaint32_Color_NoAlpha(  KrPaintInfo* info, void* target, KrRGBA* source, int nPixel, const KrColorTransform cform );
    00264 void KrPaint32_Alpha_NoAlpha( KrPaintInfo* info, void* target, KrRGBA* source, int nPixel, const KrColorTransform cform );
    00265 void KrPaint32_Full_NoAlpha( KrPaintInfo* info, void* target, KrRGBA* source, int nPixel, const KrColorTransform cform );
    00266 void KrPaint32_Simple_Alpha(   KrPaintInfo* info, void* target, KrRGBA* source, int nPixel, const KrColorTransform cform );
    00267 void KrPaint32_Color_Alpha( KrPaintInfo* info, void* target, KrRGBA* source, int nPixel, const KrColorTransform cform );
    00268 void KrPaint32_Alpha_Alpha( KrPaintInfo* info, void* target, KrRGBA* source, int nPixel, const KrColorTransform cform );
    00269 void KrPaint32_Full_Alpha( KrPaintInfo* info, void* target, KrRGBA* source, int nPixel, const KrColorTransform cform );
    00270
    00271 void KrPaintRGBA_Simple_NoAlpha( KrPaintInfo* info, void* target, KrRGBA* source, int nPixel, const KrColorTransform cform );
    00272 void KrPaintRGBA_Color_NoAlpha(  KrPaintInfo* info, void* target, KrRGBA* source, int nPixel, const KrColorTransform cform );
    00273 void KrPaintRGBA_Alpha_NoAlpha( KrPaintInfo* info, void* target, KrRGBA* source, int nPixel, const KrColorTransform cform );
    00274 void KrPaintRGBA_Full_NoAlpha( KrPaintInfo* info, void* target, KrRGBA* source, int nPixel, const KrColorTransform cform );
    00275 void KrPaintRGBA_Simple_Alpha(   KrPaintInfo* info, void* target, KrRGBA* source, int nPixel, const KrColorTransform cform );
    00276 void KrPaintRGBA_Color_Alpha( KrPaintInfo* info, void* target, KrRGBA* source, int nPixel, const KrColorTransform cform );
    00277 void KrPaintRGBA_Alpha_Alpha( KrPaintInfo* info, void* target, KrRGBA* source, int nPixel, const KrColorTransform cform );
    00278 void KrPaintRGBA_Full_Alpha( KrPaintInfo* info, void* target, KrRGBA* source, int nPixel, const KrColorTransform cform );
    00279
    00280 void KrPaint24_Simple_NoAlpha( KrPaintInfo* info, void* target, KrRGBA* source, int nPixel, const KrColorTransform cform );
    00281 void KrPaint24_Color_NoAlpha(  KrPaintInfo* info, void* target, KrRGBA* source, int nPixel, const KrColorTransform cform );
    00282 void KrPaint24_Alpha_NoAlpha( KrPaintInfo* info, void* target, KrRGBA* source, int nPixel, const KrColorTransform cform );
    00283 void KrPaint24_Full_NoAlpha( KrPaintInfo* info, void* target, KrRGBA* source, int nPixel, const KrColorTransform cform );
    00284 void KrPaint24_Simple_Alpha(   KrPaintInfo* info, void* target, KrRGBA* source, int nPixel, const KrColorTransform cform );
    00285 void KrPaint24_Color_Alpha( KrPaintInfo* info, void* target, KrRGBA* source, int nPixel, const KrColorTransform cform );
    00286 void KrPaint24_Alpha_Alpha( KrPaintInfo* info, void* target, KrRGBA* source, int nPixel, const KrColorTransform cform );
    00287 void KrPaint24_Full_Alpha( KrPaintInfo* info, void* target, KrRGBA* source, int nPixel, const KrColorTransform cform );
    00288
    00289 void KrPaint16_Simple_NoAlpha( KrPaintInfo* info, void* target, KrRGBA* source, int nPixel, const KrColorTransform cform );
    00290 void KrPaint16_Color_NoAlpha(  KrPaintInfo* info, void* target, KrRGBA* source, int nPixel, const KrColorTransform cform );
    00291 void KrPaint16_Alpha_NoAlpha( KrPaintInfo* info, void* target, KrRGBA* source, int nPixel, const KrColorTransform cform );
    00292 void KrPaint16_Full_NoAlpha( KrPaintInfo* info, void* target, KrRGBA* source, int nPixel, const KrColorTransform cform );
    00293 void KrPaint16_Simple_Alpha(   KrPaintInfo* info, void* target, KrRGBA* source, int nPixel, const KrColorTransform cform );
    00294 void KrPaint16_Color_Alpha( KrPaintInfo* info, void* target, KrRGBA* source, int nPixel, const KrColorTransform cform );
    00295 void KrPaint16_Alpha_Alpha( KrPaintInfo* info, void* target, KrRGBA* source, int nPixel, const KrColorTransform cform );
    00296 void KrPaint16_Full_Alpha( KrPaintInfo* info, void* target, KrRGBA* source, int nPixel, const KrColorTransform cform );
    00297
    00298 void KrPaintRGBARotated_Simple_NoAlpha( KrPaintInfo* info,
    00299                                                                                 void*           target,
    00300                                                                                 KrRGBA* source,
    00301                                                                                 int sPitch,
    00302                                                                                 int nPixel );
    00303 void KrPaint32Rotated_Simple_NoAlpha( KrPaintInfo* info,
    00304                                                                           void*         target,
    00305                                                                           KrRGBA*       source,
    00306                                                                           int sPitch,
    00307                                                                           int nPixel );
    00308 void KrPaint24Rotated_Simple_NoAlpha( KrPaintInfo* info,
    00309                                                                           void*         target,
    00310                                                                           KrRGBA*       source,
    00311                                                                           int sPitch,
    00312                                                                           int nPixel );
    00313 void KrPaint16Rotated_Simple_NoAlpha( KrPaintInfo* info,
    00314                                                                           void*         target,
    00315                                                                           KrRGBA*       source,
    00316                                                                           int sPitch,
    00317                                                                           int nPixel );
    00318
    00319 #endif
    00320