Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

CEGUIRenderableImage.cpp

Go to the documentation of this file.
00001 /************************************************************************
00002         filename:       CEGUIRenderableImage.cpp
00003         created:        17/4/2004
00004         author:         Paul D Turner
00005         
00006         purpose:        Implementation of RenderableImage UI entity
00007 *************************************************************************/
00008 /*************************************************************************
00009     Crazy Eddie's GUI System (http://www.cegui.org.uk)
00010     Copyright (C)2004 - 2005 Paul D Turner (paul@cegui.org.uk)
00011 
00012     This library is free software; you can redistribute it and/or
00013     modify it under the terms of the GNU Lesser General Public
00014     License as published by the Free Software Foundation; either
00015     version 2.1 of the License, or (at your option) any later version.
00016 
00017     This library is distributed in the hope that it will be useful,
00018     but WITHOUT ANY WARRANTY; without even the implied warranty of
00019     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00020     Lesser General Public License for more details.
00021 
00022     You should have received a copy of the GNU Lesser General Public
00023     License along with this library; if not, write to the Free Software
00024     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00025 *************************************************************************/
00026 #include "CEGUIRenderableImage.h"
00027 #include "CEGUIImage.h"
00028 #include "CEGUIExceptions.h"
00029 
00030 // Start of CEGUI namespace section
00031 namespace CEGUI
00032 {
00033 
00034 /*************************************************************************
00035         Constructor             
00036 *************************************************************************/
00037 RenderableImage::RenderableImage(void) :
00038         d_horzFormat(LeftAligned),
00039         d_vertFormat(TopAligned),
00040         d_quadSplitMode(TopLeftToBottomRight),
00041         d_image(NULL)
00042 {
00043 }
00044 
00045 
00046 /*************************************************************************
00047         Destructor
00048 *************************************************************************/
00049 RenderableImage::~RenderableImage(void)
00050 {
00051 }
00052 
00053 
00054 /*************************************************************************
00055         Renders the imagery for a RenderableImage element.
00056 *************************************************************************/
00057 void RenderableImage::draw_impl(const Vector3& position, const Rect& clip_rect) const
00058 {
00059         // do not draw anything if image is not set.
00060         if (d_image == NULL)
00061                 return;
00062 
00063         // calculate final clipping rect which is intersection of RenderableImage area and supplied clipping area
00064         Rect final_clipper(position.d_x, position.d_y, 0, 0);
00065         final_clipper.setSize(d_area.getSize());
00066         final_clipper = clip_rect.getIntersection(final_clipper);
00067 
00068         Size imgSize(getDestinationSize());
00069         // calculate number of times to tile image based of formatting options
00070         uint horzTiles = getHorzTileCount();
00071         uint vertTiles = getVertTileCount();
00072         // calculate 'base' X co-ordinate, depending upon formatting
00073         float baseX = getBaseXCoord(imgSize) + position.d_x;
00074         // calculate 'base' Y co-ordinate, depending upon formatting
00075         float baseY = getBaseYCoord(imgSize) + position.d_y;
00076 
00077         Vector3 drawpos(0,baseY, position.d_z);
00078         ColourRect final_colours(d_colours);
00079         bool calcColoursPerImage = !(d_useColoursPerImage || d_colours.isMonochromatic());
00080 
00081         // perform actual rendering
00082         for (uint row = 0; row < vertTiles; ++row)
00083         {
00084                 drawpos.d_x = baseX;
00085 
00086                 for (uint col = 0; col < horzTiles; ++col)
00087                 {
00088                         if (calcColoursPerImage)
00089                         {
00090                                 float leftfactor = (drawpos.d_x - baseX + d_image->getOffsetX()) / d_area.getWidth();
00091                                 float rightfactor = leftfactor + imgSize.d_width / d_area.getWidth();
00092                                 float topfactor = (drawpos.d_y - baseY + d_image->getOffsetY()) / d_area.getHeight();
00093                                 float bottomfactor = topfactor + imgSize.d_height / d_area.getHeight();
00094 
00095                                 final_colours = d_colours.getSubRectangle( leftfactor, rightfactor, topfactor, bottomfactor);
00096                         }
00097 
00098                         d_image->draw(drawpos, imgSize, final_clipper, final_colours, d_quadSplitMode);
00099                         drawpos.d_x += imgSize.d_width;
00100                 }
00101 
00102                 drawpos.d_y += imgSize.d_height;
00103         }
00104 
00105 }
00106 
00107 void RenderableImage::draw_impl(RenderCache& renderCache) const
00108 {
00109     // do not do anything if image is not set.
00110     if (!d_image)
00111         return;
00112 
00113     // get size of destination image(s)
00114     Size imgSize(getDestinationSize());
00115     // get starting co-ords for image(s)
00116     float baseX = getBaseXCoord(imgSize) + d_area.d_left;
00117     float baseY = getBaseYCoord(imgSize) + d_area.d_top;
00118     // get number of image (tiles) to draw
00119     uint horzTiles = getHorzTileCount();
00120     uint vertTiles = getVertTileCount();
00121 
00122     // get initial colours and decide how to handle those for each drawn tile.
00123     ColourRect final_colours(d_colours);
00124     bool calcColoursPerImage = !(d_useColoursPerImage || d_colours.isMonochromatic());
00125 
00126     // init target area vertically
00127     Rect targetRect;
00128     targetRect.d_top    = baseY;
00129     targetRect.d_bottom = baseY + imgSize.d_height;
00130 
00131     // perform actual rendering
00132     for (uint row = 0; row < vertTiles; ++row)
00133     {
00134         // reset horizontal target position
00135         targetRect.d_left  = baseX;
00136         targetRect.d_right = baseX + imgSize.d_width;
00137 
00138         for (uint col = 0; col < horzTiles; ++col)
00139         {
00140             // calculate colours to be used as required.
00141             if (calcColoursPerImage)
00142             {
00143                 float leftfactor = (targetRect.d_left - baseX + d_image->getOffsetX()) / d_area.getWidth();
00144                 float rightfactor = leftfactor + imgSize.d_width / d_area.getWidth();
00145                 float topfactor = (targetRect.d_top - baseY + d_image->getOffsetY()) / d_area.getHeight();
00146                 float bottomfactor = topfactor + imgSize.d_height / d_area.getHeight();
00147                 final_colours = d_colours.getSubRectangle( leftfactor, rightfactor, topfactor, bottomfactor);
00148             }
00149 
00150             // cache the image
00151             renderCache.cacheImage(*d_image, targetRect, 0, final_colours);
00152             // advance to next horizontal position
00153             targetRect.d_left  += imgSize.d_width;
00154             targetRect.d_right += imgSize.d_width;
00155         }
00156 
00157         // advance to next vertical position.
00158         targetRect.d_top    += imgSize.d_height;
00159         targetRect.d_bottom += imgSize.d_height;
00160     }
00161 }
00162 
00163 uint RenderableImage::getHorzTileCount() const
00164 {
00165     return (d_horzFormat == HorzTiled) ? (uint)((d_area.getWidth() + (d_image->getWidth() - 1)) / d_image->getWidth()) : 1;
00166 }
00167 
00168 uint RenderableImage::getVertTileCount() const
00169 {
00170     return (d_vertFormat == VertTiled) ? (uint)((d_area.getHeight() + (d_image->getHeight() - 1)) / d_image->getHeight()) : 1;
00171 }
00172 
00173 float RenderableImage::getBaseXCoord(const Size& sz) const
00174 {
00175     switch (d_horzFormat)
00176     {
00177         case HorzStretched:
00178         case HorzTiled:
00179         case LeftAligned:
00180             return 0;
00181             break;
00182 
00183         case HorzCentred:
00184             return PixelAligned((d_area.getWidth() - sz.d_width) * 0.5f);
00185             break;
00186 
00187         case RightAligned:
00188             return d_area.getWidth() - sz.d_width;
00189             break;
00190 
00191         default:
00192             throw InvalidRequestException("An unknown horizontal formatting value was specified in a RenderableImage object.");
00193     }
00194 }
00195 
00196 float RenderableImage::getBaseYCoord(const Size& sz) const
00197 {
00198     switch (d_vertFormat)
00199     {
00200         case VertStretched:
00201         case VertTiled:
00202         case TopAligned:
00203             return 0;
00204             break;
00205 
00206         case VertCentred:
00207             return PixelAligned((d_area.getHeight() - sz.d_height) * 0.5f);
00208             break;
00209 
00210         case BottomAligned:
00211             return d_area.getHeight() - sz.d_height;
00212             break;
00213 
00214         default:
00215             throw InvalidRequestException("An unknown vertical formatting value was specified in a RenderableImage object.");
00216     }
00217 }
00218 
00219 Size RenderableImage::getDestinationSize() const
00220 {
00221     return Size (
00222         (d_horzFormat == HorzStretched) ? d_area.getWidth() : d_image->getWidth(),
00223         (d_vertFormat == VertStretched) ? d_area.getHeight() : d_image->getHeight()
00224     );
00225 }
00226 
00227 } // End of  CEGUI namespace section

Generated on Wed Sep 7 09:56:33 2005 for Crazy Eddies GUI System by  doxygen 1.4.3