renderbackend.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_RENDERBACKEND_H
00023 #define FIFE_VIDEO_RENDERBACKEND_H
00024 
00025 // Standard C++ library includes
00026 #include <string>
00027 #include <vector>
00028 
00029 // Platform specific includes
00030 #include "util/base/fife_stdint.h"
00031 
00032 // 3rd party library includes
00033 #include <SDL.h>
00034 #include <SDL_video.h>
00035 
00036 // FIFE includes
00037 // These includes are split up in two parts, separated by one empty line
00038 // First block: files included from the FIFE root src directory
00039 // Second block: files included from the same folder
00040 #include "util/base/singleton.h"
00041 #include "util/structures/point.h"
00042 #include "util/structures/rect.h"
00043 #include "video/devicecaps.h"
00044 
00045 #include "image.h"
00046 
00047 #ifdef HAVE_OPENGL
00048 #include "video/opengl/fife_opengl.h"
00049 #endif
00050 
00051 namespace FIFE {
00052 
00053     class Image;
00054 
00055 #ifdef HAVE_OPENGL
00056     enum GLConstants {
00057         KEEP = GL_KEEP,
00058         ZERO = GL_ZERO,
00059         REPLACE = GL_REPLACE,
00060         INCR = GL_INCR,
00061         DECR = GL_DECR,
00062         INVERT = GL_INVERT,
00063         NEVER = GL_NEVER,
00064         LESS = GL_LESS,
00065         LEQUAL = GL_LEQUAL,
00066         GREATER = GL_GREATER,
00067         GEQUAL = GL_GEQUAL,
00068         EQUAL = GL_EQUAL,
00069         NOTEQUAL = GL_NOTEQUAL,
00070         ALWAYS = GL_ALWAYS
00071     };
00072 #else
00073     enum GLConstants {
00074         KEEP = 0,
00075         ZERO,
00076         REPLACE,
00077         INCR,
00078         DECR,
00079         INVERT,
00080         NEVER,
00081         LESS,
00082         LEQUAL,
00083         GREATER,
00084         GEQUAL,
00085         EQUAL,
00086         NOTEQUAL,
00087         ALWAYS
00088     };
00089 #endif
00090 
00092     class RenderBackend: public DynamicSingleton<RenderBackend> {
00093     public:
00097         RenderBackend(const SDL_Color& colorkey);
00098 
00101         virtual ~RenderBackend();
00102 
00106         virtual const std::string& getName() const = 0;
00107 
00110         virtual void startFrame();
00111 
00114         virtual void endFrame();
00115 
00118         virtual void init(const std::string& driver) = 0;
00119 
00122         virtual void clearBackBuffer() = 0;
00123 
00126         virtual void setLightingModel(uint32_t lighting) = 0;
00127 
00130         virtual uint32_t getLightingModel() const = 0;
00131 
00134         virtual void setLighting(float red, float green, float blue) = 0;
00135 
00138         virtual void resetLighting() = 0;
00139 
00142         virtual void resetStencilBuffer(uint8_t buffer) = 0;
00143 
00146         virtual void changeBlending(int32_t scr, int32_t dst) = 0;
00147 
00150         void deinit();
00151 
00157         virtual void createMainScreen(const ScreenMode& mode, const std::string& title, const std::string& icon) = 0;
00158 
00162         virtual void setScreenMode(const ScreenMode& mode) = 0;
00163 
00164         virtual Image* createImage(IResourceLoader* loader = 0) = 0;
00165         virtual Image* createImage(const std::string& name, IResourceLoader* loader = 0) = 0;
00166 
00173         virtual Image* createImage(const uint8_t* data, uint32_t width, uint32_t height) = 0;
00174         virtual Image* createImage(const std::string& name, const uint8_t* data, uint32_t width, uint32_t height) = 0;
00175 
00181         virtual Image* createImage(SDL_Surface* surface) = 0;
00182         virtual Image* createImage(const std::string& name, SDL_Surface* surface) = 0;
00183 
00186         virtual void renderVertexArrays() = 0;
00187 
00190         virtual void addImageToArray(uint32_t id, const Rect& rec, float const* st, uint8_t alpha, uint8_t const* rgb) = 0;
00191 
00194         virtual void changeRenderInfos(uint16_t elements, int32_t src, int32_t dst, bool light, bool stentest, uint8_t stenref, GLConstants stenop, GLConstants stenfunc) = 0;
00195 
00198         virtual void captureScreen(const std::string& filename) = 0;
00199         
00202         virtual void captureScreen(const std::string& filename, uint32_t width, uint32_t height) = 0;
00203 
00207         const ScreenMode& getCurrentScreenMode() const;
00208 
00209         uint32_t getWidth() const;
00210         uint32_t getHeight() const;
00211         uint32_t getScreenWidth() const { return getWidth(); }
00212         uint32_t getScreenHeight() const { return getHeight(); }
00213         const Rect& getArea() const;
00214 
00219         void pushClipArea(const Rect& cliparea, bool clear=true);
00220 
00224         void popClipArea();
00225 
00229         const Rect& getClipArea() const;
00230 
00233         virtual bool putPixel(int32_t x, int32_t y, uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255) = 0;
00234 
00237         virtual void drawLine(const Point& p1, const Point& p2, uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255) = 0;
00238 
00241         virtual void drawTriangle(const Point& p1, const Point& p2, const Point& p3, uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255) = 0;
00242 
00245         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;
00246 
00249         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;
00250 
00253         virtual void drawQuad(const Point& p1, const Point& p2, const Point& p3, const Point& p4,  uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255) = 0;
00254 
00257         virtual void drawVertex(const Point& p, const uint8_t size, uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255) = 0;
00258         
00261         virtual void drawLightPrimitive(const Point& p, uint8_t intensity, float radius, int32_t subdivisions, float xstretch, float ystretch, uint8_t red, uint8_t green, uint8_t blue) = 0;
00262 
00263 
00269         void setAlphaOptimizerEnabled(bool enabled){ m_isalphaoptimized = enabled; }
00270 
00273         bool isAlphaOptimizerEnabled() const { return m_isalphaoptimized; }
00274 
00278         void setImageCompressingEnabled(bool enabled) { m_compressimages = enabled; }
00279 
00282         bool isImageCompressingEnabled() const { return m_compressimages; }
00283 
00286         void setFramebufferEnabled(bool enabled) { m_useframebuffer = enabled; }
00287 
00290         bool isFramebufferEnabled() const { return m_useframebuffer; }
00291 
00294         void setNPOTEnabled(bool enabled) { m_usenpot = enabled; }
00295 
00298         bool isNPOTEnabled() const { return m_usenpot; }
00299 
00302         void setColorKeyEnabled(bool colorkeyenable);
00303 
00306         bool isColorKeyEnabled() const;
00307 
00310         void setColorKey(const SDL_Color& colorkey);
00311 
00314         const SDL_Color& getColorKey() const;
00315 
00318         void setBackgroundColor(uint8_t r, uint8_t g, uint8_t b);
00319 
00322         void resetBackgroundColor();
00323         
00326         const SDL_PixelFormat& getPixelFormat() const;
00327 
00330         void setFrameLimitEnabled(bool limited);
00331         
00334         bool isFrameLimitEnabled() const;
00335 
00338         void setFrameLimit(uint16_t framelimit);
00339 
00342         uint16_t getFrameLimit() const;
00343 
00346         SDL_Surface* getRenderTargetSurface();
00347 
00350         virtual void attachRenderTarget(ImagePtr& img, bool discard) = 0;
00351 
00354         virtual void detachRenderTarget() = 0;
00355 
00356     protected:
00357         SDL_Surface* m_screen;
00358         SDL_Surface* m_target;
00359         bool m_compressimages;
00360         bool m_useframebuffer;
00361         bool m_usenpot;
00362         bool m_isalphaoptimized;
00363         bool m_iscolorkeyenabled;
00364         SDL_Color m_colorkey;
00365         ScreenMode m_screenMode;
00366         SDL_PixelFormat m_rgba_format;
00367 
00368         bool m_isbackgroundcolor;
00369         SDL_Color m_backgroundcolor;
00370 
00374         virtual void setClipArea(const Rect& cliparea, bool clear) = 0;
00375 
00379         void clearClipArea();
00380 
00381         class ClipInfo {
00382         public:
00383             Rect r;
00384             bool clearing;
00385         };
00386         std::stack<ClipInfo> m_clipstack;
00387 
00388     private:
00389         bool m_isframelimit;
00390         uint32_t m_frame_start;
00391         uint16_t m_framelimit;
00392     };
00393 }
00394 
00395 #endif