1 /* _______ __ __ __ ______ __ __ _______ __ __ 2 * / _____/\ / /\ / /\ / /\ / ____/\ / /\ / /\ / ___ /\ / |\/ /\ 3 * / /\____\// / // / // / // /\___\// /_// / // /\_/ / // , |/ / / 4 * / / /__ / / // / // / // / / / ___ / // ___ / // /| ' / / 5 * / /_// /\ / /_// / // / // /_/_ / / // / // /\_/ / // / | / / 6 * /______/ //______/ //_/ //_____/\ /_/ //_/ //_/ //_/ //_/ /|_/ / 7 * \______\/ \______\/ \_\/ \_____\/ \_\/ \_\/ \_\/ \_\/ \_\/ \_\/ 8 * 9 * Copyright (c) 2004 - 2008 Olof Naess�n and Per Larsson 10 * 11 * 12 * Per Larsson a.k.a finalman 13 * Olof Naess�n a.k.a jansem/yakslem 14 * 15 * Visit: http://guichan.sourceforge.net 16 * 17 * License: (BSD) 18 * Redistribution and use in source and binary forms, with or without 19 * modification, are permitted provided that the following conditions 20 * are met: 21 * 1. Redistributions of source code must retain the above copyright 22 * notice, this list of conditions and the following disclaimer. 23 * 2. Redistributions in binary form must reproduce the above copyright 24 * notice, this list of conditions and the following disclaimer in 25 * the documentation and/or other materials provided with the 26 * distribution. 27 * 3. Neither the name of Guichan nor the names of its contributors may 28 * be used to endorse or promote products derived from this software 29 * without specific prior written permission. 30 * 31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 35 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 36 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 37 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 38 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 39 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 40 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 41 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 42 */ 43 44 #ifndef GCN_GRAPHICS_HPP 45 #define GCN_GRAPHICS_HPP 46 47 #include <iosfwd> 48 #include <stack> 49 50 #include "guichan/cliprectangle.hpp" 51 #include "guichan/platform.hpp" 52 53 namespace gcn 54 { 55 class Color; 56 class Font; 57 class Image; 58 59 /** 60 * Abstract class for providing drawing primitve functions. 61 * It contains all vital functions for drawing. 62 * 63 * Guichan contains implementations of Graphics for common 64 * libraries like the Allegro library, the HGE library, 65 * the OpenGL library, the OpenLayer library, and the SDL library. 66 * To make Guichan usable with other libraries, a Graphics class 67 * must be implemented. 68 * 69 * In Graphics you can set clip areas to limit drawing to certain 70 * areas of the screen. Clip areas are put on a stack, which 71 * means that you can push smaller and smaller clip areas onto the 72 * stack. All coordinates will be relative to the top most clip area. 73 * In most cases you won't have to worry about the clip areas, 74 * unless you want to implement some really complex widget. 75 * Pushing and poping of clip areas are handled automatically by 76 * container widgets when their child widgets are drawn. 77 * 78 * IMPORTANT: Remember to pop each clip area that you pushed on the stack 79 * after you are done with it. 80 * 81 * If you feel that Graphics is to restrictive for your needs, 82 * there is no one stopping you from using your own code for drawing 83 * in widgets. You could for instance use pure SDL in the drawing of 84 * widgets bypassing Graphics. This might however hurt portability of 85 * your application. 86 * 87 * If you implement a Graphics class not present in Guichan we would 88 * be very happy to add it to Guichan. 89 * 90 * @see AllegroGraphics, HGEGraphics, OpenLayerGraphics, OpenGLGraphics, 91 * SDLGraphics, Image 92 * @since 0.1.0 93 */ 94 class GCN_CORE_DECLSPEC Graphics 95 { 96 public: 97 /** 98 * Alignments for text drawing. 99 */ 100 enum Alignment 101 { 102 LEFT = 0, 103 CENTER, 104 RIGHT 105 }; 106 107 /** 108 * Constructor. 109 */ 110 Graphics(); 111 112 /** 113 * Destructor. 114 */ ~Graphics()115 virtual ~Graphics() { } 116 117 /** 118 * Initializes drawing. Called by the Gui when Gui::draw() is called. 119 * It is needed by some implementations of Graphics to perform 120 * preparations before drawing. An example of such an implementation 121 * is the OpenGLGraphics. 122 * 123 * NOTE: You will never need to call this function yourself, unless 124 * you use a Graphics object outside of Guichan. 125 * 126 * @see _endDraw, Gui::draw 127 */ _beginDraw()128 virtual void _beginDraw() { } 129 130 /** 131 * Deinitializes drawing. Called by the Gui when a Gui::draw() is done. 132 * done. It should reset any state changes made by _beginDraw(). 133 * 134 * NOTE: You will never need to call this function yourself, unless 135 * you use a Graphics object outside of Guichan. 136 * 137 * @see _beginDraw, Gui::draw 138 */ _endDraw()139 virtual void _endDraw() { } 140 141 /** 142 * Pushes a clip area onto the stack. The x and y coordinates in the 143 * rectangle is relative to the last pushed clip area. 144 * If the new area falls outside the current clip area, it will be 145 * clipped as necessary. 146 * 147 * If a clip area is outside of the top clip area a clip area with 148 * zero width and height will be pushed. 149 * 150 * @param area The clip area to be pushed onto the stack. 151 * @return False if the the new area lays outside the current clip 152 * area. 153 */ 154 virtual bool pushClipArea(Rectangle area); 155 156 /** 157 * Removes the top most clip area from the stack. 158 * 159 * @throws Exception if the stack is empty. 160 */ 161 virtual void popClipArea(); 162 163 /** 164 * Gets the current clip area. Usefull if you want to do drawing 165 * bypassing Graphics. 166 * 167 * @return The current clip area. 168 */ 169 virtual const ClipRectangle& getCurrentClipArea(); 170 171 /** 172 * Draws a part of an image. 173 * 174 * NOTE: Width and height arguments will not scale the image but 175 * specifies the size of the part to be drawn. If you want 176 * to draw the whole image there is a simplified version of 177 * this function. 178 * 179 * EXAMPLE: @code drawImage(myImage, 10, 10, 20, 20, 40, 40); @endcode 180 * Will draw a rectangular piece of myImage starting at 181 * coordinate (10, 10) in myImage, with width and height 40. 182 * The piece will be drawn with it's top left corner at 183 * coordinate (20, 20). 184 * 185 * @param image The image to draw. 186 * @param srcX The source image x coordinate. 187 * @param srcY The source image y coordinate. 188 * @param dstX The destination x coordinate. 189 * @param dstY The destination y coordinate. 190 * @param width The width of the piece. 191 * @param height The height of the piece. 192 */ 193 virtual void drawImage(const Image* image, 194 int srcX, 195 int srcY, 196 int dstX, 197 int dstY, 198 int width, 199 int height) = 0; 200 /** 201 * Draws an image. A simplified version of the other drawImage. 202 * It will draw a whole image at the coordinate you specify. 203 * It is equivalent to calling: 204 * @code drawImage(myImage, 0, 0, dstX, dstY, image->getWidth(), \ 205 image->getHeight()); @endcode 206 */ 207 virtual void drawImage(const Image* image, int dstX, int dstY); 208 209 /** 210 * Draws a single point/pixel. 211 * 212 * @param x The x coordinate. 213 * @param y The y coordinate. 214 */ 215 virtual void drawPoint(int x, int y) = 0; 216 217 /** 218 * Ddraws a line. 219 * 220 * @param x1 The first x coordinate. 221 * @param y1 The first y coordinate. 222 * @param x2 The second x coordinate. 223 * @param y2 The second y coordinate. 224 */ 225 virtual void drawLine(int x1, int y1, int x2, int y2) = 0; 226 227 /** 228 * Draws a simple, non-filled, rectangle with a one pixel width. 229 * 230 * @param rectangle The rectangle to draw. 231 */ 232 virtual void drawRectangle(const Rectangle& rectangle) = 0; 233 234 /** 235 * Draws a filled rectangle. 236 * 237 * @param rectangle The filled rectangle to draw. 238 */ 239 virtual void fillRectangle(const Rectangle& rectangle) = 0; 240 241 /** 242 * Sets the color to use when drawing. 243 * 244 * @param color A color. 245 * @see getColor 246 */ 247 virtual void setColor(const Color& color) = 0; 248 249 /** 250 * Gets the color to use when drawing. 251 * 252 * @return The color used when drawing. 253 * @see setColor 254 */ 255 virtual const Color& getColor() const = 0; 256 257 /** 258 * Sets the font to use when drawing text. 259 * 260 * @param font The font to use when drawing. 261 */ 262 virtual void setFont(Font* font); 263 264 /** 265 * Draws text. 266 * 267 * @param text The text to draw. 268 * @param x The x coordinate where to draw the text. 269 * @param y The y coordinate where to draw the text. 270 * @param alignment The alignemnt to use when drawing. 271 * @throws Exception when no font has been set. 272 */ 273 virtual void drawText(const std::string& text, 274 int x, 275 int y, 276 Alignment alignment = LEFT); 277 278 protected: 279 /** 280 * Holds the clip area stack. 281 */ 282 std::stack<ClipRectangle> mClipStack; 283 284 /** 285 * Holds the current font. 286 */ 287 Font* mFont; 288 }; 289 } 290 291 #endif // end GCN_GRAPHICS_HPP 292