sdlgraphics.cpp

00001 /*      _______   __   __   __   ______   __   __   _______   __   __
00002  *     / _____/\ / /\ / /\ / /\ / ____/\ / /\ / /\ / ___  /\ /  |\/ /\
00003  *    / /\____\// / // / // / // /\___\// /_// / // /\_/ / // , |/ / /
00004  *   / / /__   / / // / // / // / /    / ___  / // ___  / // /| ' / /
00005  *  / /_// /\ / /_// / // / // /_/_   / / // / // /\_/ / // / |  / /
00006  * /______/ //______/ //_/ //_____/\ /_/ //_/ //_/ //_/ //_/ /|_/ /
00007  * \______\/ \______\/ \_\/ \_____\/ \_\/ \_\/ \_\/ \_\/ \_\/ \_\/
00008  *
00009  * Copyright (c) 2004, 2005, 2006, 2007 Olof Naessén and Per Larsson
00010  *
00011  *                                                         Js_./
00012  * Per Larsson a.k.a finalman                          _RqZ{a<^_aa
00013  * Olof Naessén a.k.a jansem/yakslem                _asww7!uY`>  )\a//
00014  *                                                 _Qhm`] _f "'c  1!5m
00015  * Visit: http://guichan.darkbits.org             )Qk<P ` _: :+' .'  "{[
00016  *                                               .)j(] .d_/ '-(  P .   S
00017  * License: (BSD)                                <Td/Z <fP"5(\"??"\a.  .L
00018  * Redistribution and use in source and          _dV>ws?a-?'      ._/L  #'
00019  * binary forms, with or without                 )4d[#7r, .   '     )d`)[
00020  * modification, are permitted provided         _Q-5'5W..j/?'   -?!\)cam'
00021  * that the following conditions are met:       j<<WP+k/);.        _W=j f
00022  * 1. Redistributions of source code must       .$%w\/]Q  . ."'  .  mj$
00023  *    retain the above copyright notice,        ]E.pYY(Q]>.   a     J@\
00024  *    this list of conditions and the           j(]1u<sE"L,. .   ./^ ]{a
00025  *    following disclaimer.                     4'_uomm\.  )L);-4     (3=
00026  * 2. Redistributions in binary form must        )_]X{Z('a_"a7'<a"a,  ]"[
00027  *    reproduce the above copyright notice,       #}<]m7`Za??4,P-"'7. ).m
00028  *    this list of conditions and the            ]d2e)Q(<Q(  ?94   b-  LQ/
00029  *    following disclaimer in the                <B!</]C)d_, '(<' .f. =C+m
00030  *    documentation and/or other materials      .Z!=J ]e []('-4f _ ) -.)m]'
00031  *    provided with the distribution.          .w[5]' _[ /.)_-"+?   _/ <W"
00032  * 3. Neither the name of Guichan nor the      :$we` _! + _/ .        j?
00033  *    names of its contributors may be used     =3)= _f  (_yQmWW$#(    "
00034  *    to endorse or promote products derived     -   W,  sQQQQmZQ#Wwa]..
00035  *    from this software without specific        (js, \[QQW$QWW#?!V"".
00036  *    prior written permission.                    ]y:.<\..          .
00037  *                                                 -]n w/ '         [.
00038  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT       )/ )/           !
00039  * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY         <  (; sac    ,    '
00040  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING,               ]^ .-  %
00041  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF            c <   r
00042  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR            aga<  <La
00043  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE          5%  )P'-3L
00044  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR        _bQf` y`..)a
00045  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,          ,J?4P'.P"_(\?d'.,
00046  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES               _Pa,)!f/<[]/  ?"
00047  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT      _2-..:. .r+_,.. .
00048  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,     ?a.<%"'  " -'.a_ _,
00049  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION)                     ^
00050  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
00051  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00052  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00053  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
00054  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00055  */
00056 
00057 /*
00058  * For comments regarding functions please see the header file.
00059  */
00060 
00061 #include "guichan/sdl/sdlgraphics.hpp"
00062 
00063 #include "guichan/exception.hpp"
00064 #include "guichan/font.hpp"
00065 #include "guichan/image.hpp"
00066 #include "guichan/sdl/sdlimage.hpp"
00067 #include "guichan/sdl/sdlpixel.hpp"
00068 
00069 // For some reason an old version of MSVC did not like std::abs,
00070 // so we added this macro.
00071 #ifndef ABS
00072 #define ABS(x) ((x)<0?-(x):(x))
00073 #endif
00074 
00075 namespace gcn
00076 {
00077 
00078     SDLGraphics::SDLGraphics()
00079     {
00080         mAlpha = false;
00081     }
00082 
00083     void SDLGraphics::_beginDraw()
00084     {
00085         Rectangle area;
00086         area.x = 0;
00087         area.y = 0;
00088         area.width = mTarget->w;
00089         area.height = mTarget->h;
00090         pushClipArea(area);
00091     }
00092 
00093     void SDLGraphics::_endDraw()
00094     {
00095         popClipArea();
00096     }
00097 
00098     void SDLGraphics::setTarget(SDL_Surface* target)
00099     {
00100         mTarget = target;
00101     }
00102 
00103     bool SDLGraphics::pushClipArea(Rectangle area)
00104     {
00105         SDL_Rect rect;
00106         bool result = Graphics::pushClipArea(area);
00107 
00108         const ClipRectangle& carea = mClipStack.top();
00109         rect.x = carea.x;
00110         rect.y = carea.y;
00111         rect.w = carea.width;
00112         rect.h = carea.height;
00113 
00114         SDL_SetClipRect(mTarget, &rect);
00115 
00116         return result;
00117     }
00118 
00119     void SDLGraphics::popClipArea()
00120     {
00121         Graphics::popClipArea();
00122 
00123         if (mClipStack.empty())
00124         {
00125             return;
00126         }
00127 
00128         const ClipRectangle& carea = mClipStack.top();
00129         SDL_Rect rect;
00130         rect.x = carea.x;
00131         rect.y = carea.y;
00132         rect.w = carea.width;
00133         rect.h = carea.height;
00134 
00135         SDL_SetClipRect(mTarget, &rect);
00136     }
00137 
00138     SDL_Surface* SDLGraphics::getTarget() const
00139     {
00140         return mTarget;
00141     }
00142 
00143     void SDLGraphics::drawImage(const Image* image,
00144                                 int srcX,
00145                                 int srcY,
00146                                 int dstX,
00147                                 int dstY,
00148                                 int width,
00149                                 int height)
00150     {
00151         if (mClipStack.empty())
00152         {
00153             throw GCN_EXCEPTION("Clip stack is empty, perhaps you called a draw funtion outside of _beginDraw() and _endDraw()?");
00154         }
00155 
00156         const ClipRectangle& top = mClipStack.top();
00157 
00158         SDL_Rect src;
00159         SDL_Rect dst;
00160         src.x = srcX;
00161         src.y = srcY;
00162         src.w = width;
00163         src.h = height;
00164         dst.x = dstX + top.xOffset;
00165         dst.y = dstY + top.yOffset;
00166 
00167         const SDLImage* srcImage = dynamic_cast<const SDLImage*>(image);
00168 
00169         if (srcImage == NULL)
00170         {
00171             throw GCN_EXCEPTION("Trying to draw an image of unknown format, must be an SDLImage.");
00172         }
00173 
00174         SDL_BlitSurface(srcImage->getSurface(), &src, mTarget, &dst);
00175     }
00176 
00177     void SDLGraphics::fillRectangle(const Rectangle& rectangle)
00178     {
00179         if (mClipStack.empty())
00180         {
00181             throw GCN_EXCEPTION("Clip stack is empty, perhaps you called a draw funtion outside of _beginDraw() and _endDraw()?");
00182         }
00183 
00184         const ClipRectangle& top = mClipStack.top();
00185         
00186         Rectangle area = rectangle;
00187         area.x += top.xOffset;
00188         area.y += top.yOffset;
00189 
00190         if(!area.intersect(top))
00191         {
00192             return;
00193         }
00194 
00195         if (mAlpha)
00196         {
00197             int x1 = area.x > top.x ? area.x : top.x;
00198             int y1 = area.y > top.y ? area.y : top.y;
00199             int x2 = area.x + area.width < top.x + top.width ? area.x + area.width : top.x + top.width;
00200             int y2 = area.y + area.height < top.y + top.height ? area.y + area.height : top.y + top.height;
00201             int x, y;
00202 
00203             SDL_LockSurface(mTarget);
00204             for (y = y1; y < y2; y++)
00205             {
00206                 for (x = x1; x < x2; x++)
00207                 {
00208                     SDLputPixelAlpha(mTarget, x, y, mColor);
00209                 }
00210             }
00211             SDL_UnlockSurface(mTarget);
00212 
00213         }
00214         else
00215         {
00216             SDL_Rect rect;
00217             rect.x = area.x;
00218             rect.y = area.y;
00219             rect.w = area.width;
00220             rect.h = area.height;
00221 
00222             Uint32 color = SDL_MapRGBA(mTarget->format,
00223                                        mColor.r,
00224                                        mColor.g,
00225                                        mColor.b,
00226                                        mColor.a);
00227             SDL_FillRect(mTarget, &rect, color);
00228         }
00229     }
00230 
00231     void SDLGraphics::drawPoint(int x, int y)
00232     {
00233         if (mClipStack.empty())
00234         {
00235             throw GCN_EXCEPTION("Clip stack is empty, perhaps you called a draw funtion outside of _beginDraw() and _endDraw()?");
00236         }
00237 
00238         const ClipRectangle& top = mClipStack.top();
00239         
00240         x += top.xOffset;
00241         y += top.yOffset;
00242 
00243         if(!top.isPointInRect(x,y))
00244             return;
00245 
00246         if (mAlpha)
00247         {
00248             SDLputPixelAlpha(mTarget, x, y, mColor);
00249         }
00250         else
00251         {
00252             SDLputPixel(mTarget, x, y, mColor);
00253         }
00254     }
00255 
00256     void SDLGraphics::drawHLine(int x1, int y, int x2)
00257     {
00258         if (mClipStack.empty())
00259         {
00260             throw GCN_EXCEPTION("Clip stack is empty, perhaps you called a draw funtion outside of _beginDraw() and _endDraw()?");
00261         }
00262 
00263         const ClipRectangle& top = mClipStack.top();
00264 
00265         x1 += top.xOffset;
00266         y += top.yOffset;
00267         x2 += top.xOffset;
00268 
00269         if (y < top.y || y >= top.y + top.height)
00270         {
00271             return;
00272         }
00273 
00274         if (x1 > x2)
00275         {
00276             x1 ^= x2;
00277             x2 ^= x1;
00278             x1 ^= x2;
00279         }
00280 
00281         if (top.x > x1)
00282         {
00283             if (top.x > x2)
00284             {
00285                 return;
00286             }
00287 
00288             x1 = top.x;
00289         }
00290 
00291         if (top.x + top.width <= x2)
00292         {
00293             if (top.x + top.width <= x1)
00294             {
00295                 return;
00296             }
00297             
00298             x2 = top.x + top.width -1;
00299         }
00300 
00301         int bpp = mTarget->format->BytesPerPixel;
00302 
00303         SDL_LockSurface(mTarget);
00304 
00305         Uint8 *p = (Uint8 *)mTarget->pixels + y * mTarget->pitch + x1 * bpp;
00306 
00307         Uint32 pixel = SDL_MapRGB(mTarget->format,
00308                                   mColor.r,
00309                                   mColor.g,
00310                                   mColor.b);
00311         Uint16* q;
00312         switch(bpp)
00313         {
00314             case 1:
00315                 for (;x1 <= x2; ++x1)
00316                 {
00317                     *(p++) = pixel;
00318                 }
00319                 break;
00320                 
00321             case 2:
00322             {
00323                 Uint16* q = (Uint16*)p;
00324                 for (;x1 <= x2; ++x1)
00325                 {
00326                     *(q++) = pixel;
00327                 }
00328                 break;
00329             }
00330             case 3:
00331                 if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
00332                 {
00333                     for (;x1 <= x2; ++x1)
00334                     {
00335                         p[0] = (pixel >> 16) & 0xff;
00336                         p[1] = (pixel >> 8) & 0xff;
00337                         p[2] = pixel & 0xff;
00338                         p += 3;
00339                     }
00340                 }
00341                 else
00342                 {
00343                     for (;x1 <= x2; ++x1)
00344                     {
00345                         p[0] = pixel & 0xff;
00346                         p[1] = (pixel >> 8) & 0xff;
00347                         p[2] = (pixel >> 16) & 0xff;
00348                         p += 3;
00349                     }
00350                 }
00351                 break;
00352 
00353             case 4:  
00354             {          
00355                 Uint32* q = (Uint32*)p;
00356                 for (;x1 <= x2; ++x1)
00357                 {
00358                     if (mAlpha)
00359                     {
00360                         *q = SDLAlpha32(pixel,*q,mColor.a);
00361                         q++;
00362                     }
00363                     else
00364                     {
00365                         *(q++) = pixel;
00366                     }
00367                 }
00368                 break;
00369             }
00370                 
00371         } // end switch
00372 
00373         SDL_UnlockSurface(mTarget);
00374     }
00375 
00376     void SDLGraphics::drawVLine(int x, int y1, int y2)
00377     {
00378         if (mClipStack.empty())
00379         {
00380             throw GCN_EXCEPTION("Clip stack is empty, perhaps you called a draw funtion outside of _beginDraw() and _endDraw()?");
00381         }
00382 
00383         const ClipRectangle& top = mClipStack.top();
00384 
00385         x += top.xOffset;
00386         y1 += top.yOffset;
00387         y2 += top.yOffset;
00388 
00389         if (x < top.x || x >= top.x + top.width)
00390         {
00391             return;
00392         }
00393         
00394         if (y1 > y2)
00395         {
00396             y1 ^= y2;
00397             y2 ^= y1;
00398             y1 ^= y2;
00399         }
00400 
00401         if (top.y > y1)
00402         {
00403             if (top.y > y2)
00404             {
00405                 return;
00406             }
00407 
00408             y1 = top.y;
00409         }
00410 
00411         if (top.y + top.height <= y2)
00412         {
00413             if (top.y + top.height <= y1)
00414             {
00415                 return;
00416             }
00417 
00418             y2 = top.y + top.height - 1;
00419         }
00420 
00421         int bpp = mTarget->format->BytesPerPixel;
00422 
00423         SDL_LockSurface(mTarget);
00424 
00425         Uint8 *p = (Uint8 *)mTarget->pixels + y1 * mTarget->pitch + x * bpp;
00426 
00427         Uint32 pixel = SDL_MapRGB(mTarget->format, mColor.r, mColor.g, mColor.b);
00428 
00429         switch(bpp)
00430         {            
00431           case 1:
00432               for (;y1 <= y2; ++y1)
00433               {
00434                   *p = pixel;
00435                   p += mTarget->pitch;
00436               }
00437               break;
00438 
00439           case 2:
00440               for (;y1 <= y2; ++y1)
00441               {
00442                   *(Uint16*)p = pixel;
00443                   p += mTarget->pitch;
00444               }
00445               break;
00446 
00447           case 3:
00448               if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
00449               {
00450                   for (;y1 <= y2; ++y1)
00451                   {
00452                       p[0] = (pixel >> 16) & 0xff;
00453                       p[1] = (pixel >> 8) & 0xff;
00454                       p[2] = pixel & 0xff;
00455                       p += mTarget->pitch;
00456                   }
00457               }
00458               else
00459               {
00460                   for (;y1 <= y2; ++y1)
00461                   {
00462                       p[0] = pixel & 0xff;
00463                       p[1] = (pixel >> 8) & 0xff;
00464                       p[2] = (pixel >> 16) & 0xff;
00465                       p += mTarget->pitch;
00466                   }
00467               }
00468               break;
00469 
00470           case 4:
00471               for (;y1 <= y2; ++y1)
00472               {
00473                   if (mAlpha)
00474                   {
00475                       *(Uint32*)p = SDLAlpha32(pixel,*(Uint32*)p,mColor.a);
00476                   }
00477                   else
00478                   {
00479                       *(Uint32*)p = pixel;
00480                   }
00481                   p += mTarget->pitch;
00482               }
00483               break;
00484               
00485         } // end switch
00486         
00487         SDL_UnlockSurface(mTarget);
00488     }
00489 
00490     void SDLGraphics::drawRectangle(const Rectangle& rectangle)
00491     {
00492         int x1 = rectangle.x;
00493         int x2 = rectangle.x + rectangle.width - 1;
00494         int y1 = rectangle.y;
00495         int y2 = rectangle.y + rectangle.height - 1;
00496 
00497         drawHLine(x1, y1, x2);
00498         drawHLine(x1, y2, x2);
00499 
00500         drawVLine(x1, y1, y2);
00501         drawVLine(x2, y1, y2);
00502     }
00503 
00504     void SDLGraphics::drawLine(int x1, int y1, int x2, int y2)
00505     {
00506         if (x1 == x2)
00507         {
00508             drawVLine(x1, y1, y2);
00509             return;
00510         }
00511         if (y1 == y2)
00512         {
00513             drawHLine(x1, y1, x2);
00514             return;
00515         }
00516 
00517         if (mClipStack.empty())
00518         {
00519             throw GCN_EXCEPTION("Clip stack is empty, perhaps you called a draw funtion outside of _beginDraw() and _endDraw()?");
00520         }
00521 
00522         const ClipRectangle& top = mClipStack.top();
00523 
00524         x1 += top.xOffset;
00525         y1 += top.yOffset;
00526         x2 += top.xOffset;
00527         y2 += top.yOffset;
00528 
00529         // Draw a line with Bresenham
00530 
00531         int dx = ABS(x2 - x1);
00532         int dy = ABS(y2 - y1);
00533 
00534         if (dx > dy)
00535         {
00536             if (x1 > x2)
00537             {
00538                 // swap x1, x2
00539                 x1 ^= x2;
00540                 x2 ^= x1;
00541                 x1 ^= x2;
00542 
00543                 // swap y1, y2
00544                 y1 ^= y2;
00545                 y2 ^= y1;
00546                 y1 ^= y2;
00547             }
00548 
00549             if (y1 < y2)
00550             {
00551                 int y = y1;
00552                 int p = 0;
00553 
00554                 for (int x = x1; x <= x2; x++)
00555                 {
00556                     if (top.isPointInRect(x, y))
00557                     {
00558                         if (mAlpha)
00559                         {
00560                             SDLputPixelAlpha(mTarget, x, y, mColor);
00561                         }
00562                         else
00563                         {
00564                             SDLputPixel(mTarget, x, y, mColor);
00565                         }
00566                     }
00567 
00568                     p += dy;
00569 
00570                     if (p * 2 >= dx)
00571                     {
00572                         y++;
00573                         p -= dx;
00574                     }
00575                 }
00576             }
00577             else
00578             {
00579                 int y = y1;
00580                 int p = 0;
00581 
00582                 for (int x = x1; x <= x2; x++)
00583                 {
00584                     if (top.isPointInRect(x, y))
00585                     {
00586                         if (mAlpha)
00587                         {
00588                             SDLputPixelAlpha(mTarget, x, y, mColor);
00589                         }
00590                         else
00591                         {
00592                             SDLputPixel(mTarget, x, y, mColor);
00593                         }
00594                     }
00595 
00596                     p += dy;
00597 
00598                     if (p * 2 >= dx)
00599                     {
00600                         y--;
00601                         p -= dx;
00602                     }
00603                 }
00604             }
00605         }
00606         else
00607         {
00608             if (y1 > y2)
00609             {
00610                 // swap y1, y2
00611                 y1 ^= y2;
00612                 y2 ^= y1;
00613                 y1 ^= y2;
00614 
00615                 // swap x1, x2
00616                 x1 ^= x2;
00617                 x2 ^= x1;
00618                 x1 ^= x2;
00619             }
00620 
00621             if (x1 < x2)
00622             {
00623                 int x = x1;
00624                 int p = 0;
00625 
00626                 for (int y = y1; y <= y2; y++)
00627                 {
00628                     if (top.isPointInRect(x, y))
00629                     {
00630                         if (mAlpha)
00631                         {
00632                             SDLputPixelAlpha(mTarget, x, y, mColor);
00633                         }
00634                         else
00635                         {
00636                             SDLputPixel(mTarget, x, y, mColor);
00637                         }
00638                     }
00639 
00640                     p += dx;
00641 
00642                     if (p * 2 >= dy)
00643                     {
00644                         x++;
00645                         p -= dy;
00646                     }
00647                 }
00648             }
00649             else
00650             {
00651                 int x = x1;
00652                 int p = 0;
00653 
00654                 for (int y = y1; y <= y2; y++)
00655                 {
00656                     if (top.isPointInRect(x, y))
00657                     {
00658                         if (mAlpha)
00659                         {
00660                             SDLputPixelAlpha(mTarget, x, y, mColor);
00661                         }
00662                         else
00663                         {
00664                             SDLputPixel(mTarget, x, y, mColor);
00665                         }
00666                     }
00667 
00668                     p += dx;
00669 
00670                     if (p * 2 >= dy)
00671                     {
00672                         x--;
00673                         p -= dy;
00674                     }
00675                 }
00676             }
00677         }
00678     }
00679 
00680     void SDLGraphics::setColor(const Color& color)
00681     {
00682         mColor = color;
00683 
00684         mAlpha = color.a != 255;
00685     }
00686 
00687     const Color& SDLGraphics::getColor() const
00688     {
00689         return mColor;
00690     }
00691 
00692     void SDLGraphics::drawSDLSurface(SDL_Surface* surface,
00693                                      SDL_Rect source,
00694                                      SDL_Rect destination)
00695     {
00696         if (mClipStack.empty())
00697         {
00698             throw GCN_EXCEPTION("Clip stack is empty, perhaps you called a draw funtion outside of _beginDraw() and _endDraw()?");
00699         }
00700 
00701         const ClipRectangle& top = mClipStack.top();
00702 
00703         destination.x += top.xOffset;
00704         destination.y += top.yOffset;
00705 
00706         SDL_BlitSurface(surface, &source, mTarget, &destination);
00707     }
00708 }

Generated on Wed Jul 11 14:50:01 2007 for Guichan by  doxygen 1.5.2