image.h

00001 /***************************************************************************
00002  *   Copyright (C) 2005-2011 by the FIFE team                              *
00003  *   http://www.fifengine.net                                              *
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/resource/resource.h"
00038 #include "util/structures/point.h"
00039 #include "util/structures/rect.h"
00040 
00041 namespace FIFE {
00042     class Image;
00043     typedef SharedPtr<Image> ImagePtr;
00044 
00047     class Image : public IResource {
00048     public:
00051         Image(IResourceLoader* loader = 0);
00052         Image(const std::string& name, IResourceLoader* loader = 0);
00053 
00058         Image(SDL_Surface* surface);
00059         Image(const std::string& name, SDL_Surface* surface);
00060 
00066         Image(const uint8_t* data, uint32_t width, uint32_t height);
00067         Image(const std::string& name, const uint8_t* data, uint32_t width, uint32_t height);
00068 
00071         virtual ~Image();
00072 
00075         virtual void invalidate() = 0;
00076 
00083         virtual void render(const Rect& rect, uint8_t alpha = 255, uint8_t const* rgb = 0) = 0;
00084         virtual void renderZ(const Rect& rect, float vertexZ, uint8_t alpha = 255, bool forceNewBatch = false, uint8_t const* rgb = 0) {}
00085 
00089         SDL_Surface* detachSurface();
00090 
00091         SDL_Surface* getSurface() { assert(m_surface); return m_surface; }
00092         const SDL_Surface* getSurface() const { assert(m_surface); return m_surface; }
00093 
00099         virtual void setSurface(SDL_Surface* surface) = 0;
00100 
00103         void saveImage(const std::string& filename);
00104 
00107         static void saveAsPng(const std::string& filename, const SDL_Surface& surface);
00108         static bool putPixel(SDL_Surface* surface, int32_t x, int32_t y, uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255);
00109 
00110         uint32_t getWidth() const;
00111         uint32_t getHeight() const;
00112         const Rect& getArea() const;
00113 
00114         void setXShift(int32_t xshift) {
00115             m_xshift = xshift;
00116         }
00117         int32_t getXShift() const {
00118             return m_xshift;
00119         }
00120         void setYShift(int32_t yshift) {
00121             m_yshift = yshift;
00122         }
00123         int32_t getYShift() const {
00124             return m_yshift;
00125         }
00126 
00127         void getPixelRGBA(int32_t x, int32_t y, uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a);
00128 
00129         virtual size_t getSize();
00130         virtual void load();
00131         virtual void free();
00132 
00135         virtual void useSharedImage(const ImagePtr& shared, const Rect& region) = 0;
00136 
00139         virtual void forceLoadInternal() = 0;
00140 
00143         bool isSharedImage() const { return m_shared; }
00144 
00147         const Rect& getSubImageRect() const { return m_subimagerect; }
00148 
00151         virtual void copySubimage(uint32_t xoffset, uint32_t yoffset, const ImagePtr& img);
00152 
00153     protected:
00154         // The SDL Surface used.
00155         SDL_Surface* m_surface;
00156         // The X shift of the Image
00157         int32_t m_xshift;
00158         // The Y shift of the Image
00159         int32_t m_yshift;
00160 
00167         void reset(SDL_Surface* surface);
00168 
00169         // Does this image share data with another
00170         bool m_shared;
00171         // Area which this image occupy in shared image
00172         Rect m_subimagerect;
00173 
00174     private:
00175         std::string createUniqueImageName();
00176     };
00177 }
00178 
00179 #endif