renderbackendopengl.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_RENDERBACKENSD_OPENGL_RENDERBACKENDOPENGL_H
00023 #define FIFE_VIDEO_RENDERBACKENSD_OPENGL_RENDERBACKENDOPENGL_H
00024 
00025 // Standard C++ library includes
00026 
00027 // 3rd party library includes
00028 
00029 // FIFE includes
00030 // These includes are split up in two parts, separated by one empty line
00031 // First block: files included from the FIFE root src directory
00032 // Second block: files included from the same folder
00033 #include "video/renderbackend.h"
00034 
00035 #include "fife_opengl.h"
00036 
00037 namespace FIFE {
00038     class ScreenMode;
00039 
00043     class RenderBackendOpenGL : public RenderBackend {
00044     public:
00045         RenderBackendOpenGL(const SDL_Color& colorkey);
00046         virtual ~RenderBackendOpenGL();
00047         virtual const std::string& getName() const;
00048         virtual void startFrame();
00049         virtual void endFrame();
00050         virtual void init(const std::string& driver);
00051         virtual void clearBackBuffer();
00052         virtual void setLightingModel(uint32_t lighting);
00053         virtual uint32_t getLightingModel() const;
00054         virtual void setLighting(float red, float green, float blue);
00055         virtual void resetLighting();
00056         virtual void resetStencilBuffer(uint8_t buffer);
00057         virtual void changeBlending(int32_t scr, int32_t dst);
00058 
00059         virtual void createMainScreen(const ScreenMode& mode, const std::string& title, const std::string& icon);
00060         virtual void setScreenMode(const ScreenMode& mode);
00061 
00062         virtual Image* createImage(IResourceLoader* loader = 0);
00063         virtual Image* createImage(const std::string& name, IResourceLoader* loader = 0);
00064         virtual Image* createImage(const uint8_t* data, uint32_t width, uint32_t height);
00065         virtual Image* createImage(const std::string& name, const uint8_t* data, uint32_t width, uint32_t height);
00066         virtual Image* createImage(SDL_Surface* surface);
00067         virtual Image* createImage(const std::string& name, SDL_Surface* surface);
00068 
00069         virtual void renderVertexArrays();
00070         virtual void addImageToArray(uint32_t id, const Rect& rec, float const* st, uint8_t alpha, uint8_t const* rgb);
00071         virtual void changeRenderInfos(uint16_t elements, int32_t src, int32_t dst, bool light, bool stentest, uint8_t stenref, GLConstants stenop, GLConstants stenfunc);
00072         virtual void captureScreen(const std::string& filename);
00073         virtual void captureScreen(const std::string& filename, uint32_t width, uint32_t height);
00074 
00075         virtual bool putPixel(int32_t x, int32_t y, uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255);
00076         virtual void drawLine(const Point& p1, const Point& p2, uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255);
00077         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);
00078         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);
00079         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);
00080         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);
00081         virtual void drawVertex(const Point& p, const uint8_t size, uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255);
00082         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);
00083 
00084         virtual void attachRenderTarget(ImagePtr& img, bool discard);
00085         virtual void detachRenderTarget();
00086 
00087         void enableTextures(uint32_t texUnit);
00088         void disableTextures(uint32_t texUnit);
00089         void bindTexture(uint32_t texUnit, GLuint texId);
00090         void bindTexture(GLuint textId);
00091 
00092     protected:
00093         virtual void setClipArea(const Rect& cliparea, bool clear);
00094 
00095         void enableLighting();
00096         void disableLighting();
00097         void enableStencilTest();
00098         void disableStencilTest();
00099         void setStencilTest(uint8_t stencil_ref, GLenum stencil_op, GLenum stencil_func);
00100         uint8_t getStencilRef() const;
00101         void enableAlphaTest();
00102         void disableAlphaTest();
00103         void setAlphaTest(float ref_alpha);
00104         void setEnvironmentalColor(const uint8_t* rgb);
00105         void setVertexPointer(GLsizei stride, const GLvoid* ptr);
00106         void setColorPointer(GLsizei stride, const GLvoid* ptr);
00107         void setTexCoordPointer(uint32_t texUnit, GLsizei stride, const GLvoid* ptr);
00108         void enableScissorTest();
00109         void disableScissorTest();
00110 
00111         GLuint m_mask_overlays;
00112         void prepareForOverlays();
00113 
00114         class RenderObject;
00115 
00116         struct renderData2T {
00117             GLfloat vertex[2];
00118             GLfloat texel[2];
00119             GLfloat texel2[2];
00120             GLubyte color[4];
00121         };
00122 
00123         struct renderData {
00124             GLfloat vertex[2];
00125             GLfloat texel[2];
00126             GLubyte color[4];
00127         };
00128         std::vector<renderData> m_render_datas;
00129         std::vector<renderData2T> m_render_datas2T;
00130         std::vector<RenderObject> m_render_objects;
00131 
00132         struct currentState {
00133             // Textures
00134             bool tex_enabled[2];
00135             GLuint texture[2];
00136             uint32_t active_tex;
00137             uint32_t active_client_tex;
00138 
00139             // Pointers
00140             const void* vertex_pointer;
00141             const void* tex_pointer[2];
00142             const void* color_pointer;
00143 
00144             // Stencil
00145             bool sten_enabled;
00146             uint8_t sten_ref;
00147             GLint sten_buf;
00148             GLenum sten_op;
00149             GLenum sten_func;
00150 
00151             // Light
00152             uint32_t lightmodel;
00153             bool light_enabled;
00154 
00155             // The rest
00156             uint8_t env_color[3];
00157             GLenum blend_src;
00158             GLenum blend_dst;
00159             bool alpha_enabled;
00160             bool scissor_test;
00161         } m_state;
00162 
00163         GLuint m_fbo_id;
00164         ImagePtr m_img_target;
00165         bool m_target_discard;
00166     };
00167 
00168 }
00169 
00170 #endif
00171 /* vim: set noexpandtab: set shiftwidth=2: set tabstop=2: */