image.h

00001 /***************************************************************************
00002  *   Copyright (C) 2005-2008 by the FIFE team                              *
00003  *   http://www.fifengine.de                                               *
00004  *   This file is part of FIFE.                                            *
00005  *                                                                         *
00006  *   FIFE is free software; you can redistribute it and/or                 *
00007  *   modify it under the terms of the GNU Lesser General Public            *
00008  *   License as published by the Free Software Foundation; either          *
00009  *   version 2.1 of the License, or (at your option) any later version.    *
00010  *                                                                         *
00011  *   This library is distributed in the hope that it will be useful,       *
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00014  *   Lesser General Public License for more details.                       *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Lesser General Public      *
00017  *   License along with this library; if not, write to the                 *
00018  *   Free Software Foundation, Inc.,                                       *
00019  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
00020  ***************************************************************************/
00021 
00022 #ifndef FIFE_VIDEO_IMAGE_H
00023 #define FIFE_VIDEO_IMAGE_H
00024 
00025 // Standard C++ library includes
00026 #include <stack>
00027 
00028 // 3rd party library includes
00029 #include <SDL.h>
00030 #include <png.h>
00031 
00032 // FIFE includes
00033 // These includes are split up in two parts, separated by one empty line
00034 // First block: files included from the FIFE root src directory
00035 // Second block: files included from the same folder
00036 #include "util/base/fife_stdint.h"
00037 #include "util/base/resourceclass.h"
00038 #include "util/resource/resource.h"
00039 #include "util/structures/point.h"
00040 #include "util/structures/rect.h"
00041 
00042 namespace FIFE {
00043     class Rect;
00044 
00045     class AbstractImage {
00046     public:
00047         virtual ~AbstractImage() {}
00048 
00052         virtual SDL_Surface* getSurface() = 0;
00053 
00058         virtual unsigned int getWidth() const = 0;
00059 
00062         virtual unsigned int getHeight() const = 0;
00063 
00067         virtual const Rect& getArea() = 0;
00068 
00071         virtual bool putPixel(int x, int y, int r, int g, int b) = 0;
00072 
00075         virtual void drawLine(const Point& p1, const Point& p2, int r, int g, int b) = 0;
00076 
00079         virtual void drawQuad(const Point& p1, const Point& p2, const Point& p3, const Point& p4,  int r, int g, int b) = 0;
00080 
00083         virtual void drawVertex(const Point& p, const uint8_t size, int r, int g, int b) = 0;
00084 
00087         virtual void getPixelRGBA(int x, int y, uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a) = 0;
00088 
00093         virtual void pushClipArea(const Rect& cliparea, bool clear=true) = 0;
00094 
00098         virtual void popClipArea() = 0;
00099 
00103         virtual const Rect& getClipArea() const = 0;
00104 
00107         virtual void saveImage(const std::string& filename) = 0;
00108 
00114         virtual void setAlphaOptimizerEnabled(bool enabled) = 0;
00115 
00118         virtual bool isAlphaOptimizerEnabled() = 0;
00119     };
00120 
00123     class Image : public ResourceClass, public AbstractImage {
00124     public:
00129         Image(SDL_Surface* surface);
00130 
00136         Image(const uint8_t* data, unsigned int width, unsigned int height);
00137 
00144         virtual void render(const Rect& rect, SDL_Surface* dst, unsigned char alpha = 255) = 0;
00145 
00151         void render(const Rect& rect, unsigned char alpha = 255);
00152 
00156         SDL_Surface* detachSurface();
00157 
00158         virtual ~Image();
00159         SDL_Surface* getSurface() { return m_surface; }
00160         unsigned int getWidth() const;
00161         unsigned int getHeight() const;
00162         const Rect& getArea();
00163         void setXShift(int xshift);
00164         inline int getXShift() const {
00165             return m_xshift;
00166         }
00167         void setYShift(int yshift);
00168         inline int getYShift() const {
00169             return m_yshift;
00170         }
00171         void getPixelRGBA(int x, int y, uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a);
00172         void pushClipArea(const Rect& cliparea, bool clear=true);
00173         void popClipArea();
00174         const Rect& getClipArea() const;
00175         void setAlphaOptimizerEnabled(bool enabled) { m_isalphaoptimized = enabled; }
00176         bool isAlphaOptimizerEnabled() { return m_isalphaoptimized; }
00177 
00178     protected:
00182         virtual void setClipArea(const Rect& cliparea, bool clear) = 0;
00183         //saves images to png format
00184         virtual void saveAsPng(const std::string& filename, SDL_Surface& surface);
00188         virtual void clearClipArea();
00189 
00190         // The SDL Surface used.
00191         SDL_Surface* m_surface;
00192         // The X shift of the Image
00193         int m_xshift;
00194         // The Y shift of the Image
00195         int m_yshift;
00196 
00197         class ClipInfo {
00198         public:
00199             Rect r;
00200             bool clearing;
00201         };
00202         std::stack<ClipInfo> m_clipstack;
00203 
00204         // image area
00205         Rect m_area;
00206         bool m_isalphaoptimized;
00207 
00208     private:
00209         void reset(SDL_Surface* surface);
00210     };
00211 
00212 }
00213 
00214 #endif
Generated by  doxygen 1.6.2-20100208