FIFE  2008.0
 All Classes Namespaces Functions Variables Enumerations Enumerator
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 
00044     class AbstractImage {
00045     public:
00046         virtual ~AbstractImage() {}
00047 
00051         virtual SDL_Surface* getSurface() = 0;
00052 
00057         virtual unsigned int getWidth() const = 0;
00058 
00061         virtual unsigned int getHeight() const = 0;
00062 
00066         virtual const Rect& getArea() = 0;
00067 
00070         virtual bool putPixel(int x, int y, int r, int g, int b, int a = 255) = 0;
00071 
00074         virtual void drawLine(const Point& p1, const Point& p2, int r, int g, int b, int a = 255) = 0;
00075 
00078         virtual void drawTriangle(const Point& p1, const Point& p2, const Point& p3, int r, int g, int b, int a = 255) = 0;
00079 
00082         virtual void drawRectangle(const Point& p, uint16_t w, uint16_t h, uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255) = 0;
00083 
00086         virtual void fillRectangle(const Point& p, uint16_t w, uint16_t h, uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255)  = 0;
00087 
00090         virtual void drawQuad(const Point& p1, const Point& p2, const Point& p3, const Point& p4,  int r, int g, int b, int a = 255) = 0;
00091 
00094         virtual void drawVertex(const Point& p, const uint8_t size, int r, int g, int b, int a = 255) = 0;
00095 
00098         virtual void drawLightPrimitive(const Point& p, uint8_t intensity, float radius, int subdivisions, float xstretch, float ystretch, uint8_t red, uint8_t green, uint8_t blue) = 0;
00099 
00102         virtual void getPixelRGBA(int x, int y, uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a) = 0;
00103 
00108         virtual void pushClipArea(const Rect& cliparea, bool clear=true) = 0;
00109 
00113         virtual void popClipArea() = 0;
00114 
00118         virtual const Rect& getClipArea() const = 0;
00119 
00122         virtual void saveImage(const std::string& filename) = 0;
00123 
00129         virtual void setAlphaOptimizerEnabled(bool enabled) = 0;
00130 
00133         virtual bool isAlphaOptimizerEnabled() = 0;
00134     };
00135 
00138     class Image : public ResourceClass, public AbstractImage {
00139     public:
00144         Image(SDL_Surface* surface);
00145 
00151         Image(const uint8_t* data, unsigned int width, unsigned int height);
00152 
00155         virtual void invalidate() = 0;
00156 
00163         virtual void render(const Rect& rect, SDL_Surface* dst, unsigned char alpha = 255) = 0;
00164 
00170         void render(const Rect& rect, unsigned char alpha = 255);
00171 
00175         SDL_Surface* detachSurface();
00176 
00177         virtual ~Image();
00178         SDL_Surface* getSurface() { return m_surface; }
00179         unsigned int getWidth() const;
00180         unsigned int getHeight() const;
00181         const Rect& getArea();
00182         void setXShift(int xshift);
00183         inline int getXShift() const {
00184             return m_xshift;
00185         }
00186         void setYShift(int yshift);
00187         inline int getYShift() const {
00188             return m_yshift;
00189         }
00190         void getPixelRGBA(int x, int y, uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a);
00191         void pushClipArea(const Rect& cliparea, bool clear=true);
00192         void popClipArea();
00193         const Rect& getClipArea() const;
00194         void setAlphaOptimizerEnabled(bool enabled) { m_isalphaoptimized = enabled; }
00195         bool isAlphaOptimizerEnabled() { return m_isalphaoptimized; }
00196 
00197     protected:
00201         virtual void setClipArea(const Rect& cliparea, bool clear) = 0;
00202         //saves images to png format
00203         virtual void saveAsPng(const std::string& filename, SDL_Surface& surface);
00207         virtual void clearClipArea();
00208 
00209         // The SDL Surface used.
00210         SDL_Surface* m_surface;
00211         // The X shift of the Image
00212         int m_xshift;
00213         // The Y shift of the Image
00214         int m_yshift;
00215 
00216         class ClipInfo {
00217         public:
00218             Rect r;
00219             bool clearing;
00220         };
00221         std::stack<ClipInfo> m_clipstack;
00222 
00223         // image area
00224         Rect m_area;
00225         bool m_isalphaoptimized;
00226 
00227     private:
00228         void reset(SDL_Surface* surface);
00229     };
00230 
00231 }
00232 
00233 #endif