FIFE  2008.0
 All Classes Namespaces Functions Variables Enumerations Enumerator Pages
renderbackendopengl.h
1 /***************************************************************************
2  * Copyright (C) 2005-2011 by the FIFE team *
3  * http://www.fifengine.net *
4  * This file is part of FIFE. *
5  * *
6  * FIFE is free software; you can redistribute it and/or *
7  * modify it under the terms of the GNU Lesser General Public *
8  * License as published by the Free Software Foundation; either *
9  * version 2.1 of the License, or (at your option) any later version. *
10  * *
11  * This library is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14  * Lesser General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU Lesser General Public *
17  * License along with this library; if not, write to the *
18  * Free Software Foundation, Inc., *
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
20  ***************************************************************************/
21 
22 #ifndef FIFE_VIDEO_RENDERBACKENSD_OPENGL_RENDERBACKENDOPENGL_H
23 #define FIFE_VIDEO_RENDERBACKENSD_OPENGL_RENDERBACKENDOPENGL_H
24 
25 // Standard C++ library includes
26 
27 // 3rd party library includes
28 
29 // FIFE includes
30 // These includes are split up in two parts, separated by one empty line
31 // First block: files included from the FIFE root src directory
32 // Second block: files included from the same folder
33 #include "video/renderbackend.h"
34 
35 #include "fife_opengl.h"
36 
37 namespace FIFE {
38  class ScreenMode;
39 
44  public:
45  RenderBackendOpenGL(const SDL_Color& colorkey);
46  virtual ~RenderBackendOpenGL();
47  virtual const std::string& getName() const;
48  virtual void startFrame();
49  virtual void endFrame();
50  virtual void init(const std::string& driver);
51  virtual void clearBackBuffer();
52  virtual void setLightingModel(uint32_t lighting);
53  virtual uint32_t getLightingModel() const;
54  virtual void setLighting(float red, float green, float blue);
55  virtual void resetLighting();
56  virtual void resetStencilBuffer(uint8_t buffer);
57  virtual void changeBlending(int32_t scr, int32_t dst);
58 
59  virtual void createMainScreen(const ScreenMode& mode, const std::string& title, const std::string& icon);
60  virtual void setScreenMode(const ScreenMode& mode);
61 
62  virtual Image* createImage(IResourceLoader* loader = 0);
63  virtual Image* createImage(const std::string& name, IResourceLoader* loader = 0);
64  virtual Image* createImage(const uint8_t* data, uint32_t width, uint32_t height);
65  virtual Image* createImage(const std::string& name, const uint8_t* data, uint32_t width, uint32_t height);
66  virtual Image* createImage(SDL_Surface* surface);
67  virtual Image* createImage(const std::string& name, SDL_Surface* surface);
68 
69  virtual void renderVertexArrays();
70  virtual void addImageToArray(uint32_t id, const Rect& rec, float const* st, uint8_t alpha, uint8_t const* rgb);
71  virtual void changeRenderInfos(uint16_t elements, int32_t src, int32_t dst, bool light, bool stentest, uint8_t stenref, GLConstants stenop, GLConstants stenfunc);
72  virtual void captureScreen(const std::string& filename);
73  virtual void captureScreen(const std::string& filename, uint32_t width, uint32_t height);
74 
75  virtual bool putPixel(int32_t x, int32_t y, uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255);
76  virtual void drawLine(const Point& p1, const Point& p2, uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255);
77  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);
78  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);
79  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);
80  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);
81  virtual void drawVertex(const Point& p, const uint8_t size, uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255);
82  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);
83 
84  virtual void attachRenderTarget(ImagePtr& img, bool discard);
85  virtual void detachRenderTarget();
86 
87  void enableTextures(uint32_t texUnit);
88  void disableTextures(uint32_t texUnit);
89  void bindTexture(uint32_t texUnit, GLuint texId);
90  void bindTexture(GLuint textId);
91 
92  protected:
93  virtual void setClipArea(const Rect& cliparea, bool clear);
94 
95  void enableLighting();
96  void disableLighting();
97  void enableStencilTest();
98  void disableStencilTest();
99  void setStencilTest(uint8_t stencil_ref, GLenum stencil_op, GLenum stencil_func);
100  uint8_t getStencilRef() const;
101  void enableAlphaTest();
102  void disableAlphaTest();
103  void setAlphaTest(float ref_alpha);
104  void setEnvironmentalColor(const uint8_t* rgb);
105  void setVertexPointer(GLsizei stride, const GLvoid* ptr);
106  void setColorPointer(GLsizei stride, const GLvoid* ptr);
107  void setTexCoordPointer(uint32_t texUnit, GLsizei stride, const GLvoid* ptr);
108  void enableScissorTest();
109  void disableScissorTest();
110 
111  GLuint m_mask_overlays;
112  void prepareForOverlays();
113 
114  class RenderObject;
115 
116  struct renderData2T {
117  GLfloat vertex[2];
118  GLfloat texel[2];
119  GLfloat texel2[2];
120  GLubyte color[4];
121  };
122 
123  struct renderData {
124  GLfloat vertex[2];
125  GLfloat texel[2];
126  GLubyte color[4];
127  };
128  std::vector<renderData> m_render_datas;
129  std::vector<renderData2T> m_render_datas2T;
130  std::vector<RenderObject> m_render_objects;
131 
132  struct currentState {
133  // Textures
134  bool tex_enabled[2];
135  GLuint texture[2];
136  uint32_t active_tex;
137  uint32_t active_client_tex;
138 
139  // Pointers
140  const void* vertex_pointer;
141  const void* tex_pointer[2];
142  const void* color_pointer;
143 
144  // Stencil
145  bool sten_enabled;
146  uint8_t sten_ref;
147  GLint sten_buf;
148  GLenum sten_op;
149  GLenum sten_func;
150 
151  // Light
152  uint32_t lightmodel;
153  bool light_enabled;
154 
155  // The rest
156  uint8_t env_color[3];
157  GLenum blend_src;
158  GLenum blend_dst;
159  bool alpha_enabled;
160  bool scissor_test;
161  } m_state;
162 
163  GLuint m_fbo_id;
164  ImagePtr m_img_target;
165  bool m_target_discard;
166  };
167 
168 }
169 
170 #endif
171 /* vim: set noexpandtab: set shiftwidth=2: set tabstop=2: */