FIFE  2008.0
 All Classes Namespaces Functions Variables Enumerations Enumerator Pages
renderbackend.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_RENDERBACKEND_H
23 #define FIFE_VIDEO_RENDERBACKEND_H
24 
25 // Standard C++ library includes
26 #include <string>
27 #include <vector>
28 
29 // Platform specific includes
30 #include "util/base/fife_stdint.h"
31 
32 // 3rd party library includes
33 #include <SDL.h>
34 #include <SDL_video.h>
35 
36 // FIFE includes
37 // These includes are split up in two parts, separated by one empty line
38 // First block: files included from the FIFE root src directory
39 // Second block: files included from the same folder
40 #include "util/base/singleton.h"
41 #include "util/structures/point.h"
42 #include "util/structures/rect.h"
43 #include "video/devicecaps.h"
44 
45 #include "image.h"
46 
47 #ifdef HAVE_OPENGL
48 #include "video/opengl/fife_opengl.h"
49 #endif
50 
51 namespace FIFE {
52 
53  class Image;
54 
55 #ifdef HAVE_OPENGL
56  enum GLConstants {
57  KEEP = GL_KEEP,
58  ZERO = GL_ZERO,
59  REPLACE = GL_REPLACE,
60  INCR = GL_INCR,
61  DECR = GL_DECR,
62  INVERT = GL_INVERT,
63  NEVER = GL_NEVER,
64  LESS = GL_LESS,
65  LEQUAL = GL_LEQUAL,
66  GREATER = GL_GREATER,
67  GEQUAL = GL_GEQUAL,
68  EQUAL = GL_EQUAL,
69  NOTEQUAL = GL_NOTEQUAL,
70  ALWAYS = GL_ALWAYS
71  };
72 #else
73  enum GLConstants {
74  KEEP = 0,
75  ZERO,
76  REPLACE,
77  INCR,
78  DECR,
79  INVERT,
80  NEVER,
81  LESS,
82  LEQUAL,
83  GREATER,
84  GEQUAL,
85  EQUAL,
86  NOTEQUAL,
87  ALWAYS
88  };
89 #endif
90 
92  class RenderBackend: public DynamicSingleton<RenderBackend> {
93  public:
97  RenderBackend(const SDL_Color& colorkey);
98 
101  virtual ~RenderBackend();
102 
106  virtual const std::string& getName() const = 0;
107 
110  virtual void startFrame();
111 
114  virtual void endFrame();
115 
118  virtual void init(const std::string& driver) = 0;
119 
122  virtual void clearBackBuffer() = 0;
123 
126  virtual void setLightingModel(uint32_t lighting) = 0;
127 
130  virtual uint32_t getLightingModel() const = 0;
131 
134  virtual void setLighting(float red, float green, float blue) = 0;
135 
138  virtual void resetLighting() = 0;
139 
142  virtual void resetStencilBuffer(uint8_t buffer) = 0;
143 
146  virtual void changeBlending(int32_t scr, int32_t dst) = 0;
147 
150  void deinit();
151 
157  virtual void createMainScreen(const ScreenMode& mode, const std::string& title, const std::string& icon) = 0;
158 
162  virtual void setScreenMode(const ScreenMode& mode) = 0;
163 
164  virtual Image* createImage(IResourceLoader* loader = 0) = 0;
165  virtual Image* createImage(const std::string& name, IResourceLoader* loader = 0) = 0;
166 
173  virtual Image* createImage(const uint8_t* data, uint32_t width, uint32_t height) = 0;
174  virtual Image* createImage(const std::string& name, const uint8_t* data, uint32_t width, uint32_t height) = 0;
175 
181  virtual Image* createImage(SDL_Surface* surface) = 0;
182  virtual Image* createImage(const std::string& name, SDL_Surface* surface) = 0;
183 
186  virtual void renderVertexArrays() = 0;
187 
190  virtual void addImageToArray(uint32_t id, const Rect& rec, float const* st, uint8_t alpha, uint8_t const* rgb) = 0;
191 
194  virtual void changeRenderInfos(uint16_t elements, int32_t src, int32_t dst, bool light, bool stentest, uint8_t stenref, GLConstants stenop, GLConstants stenfunc) = 0;
195 
198  virtual void captureScreen(const std::string& filename) = 0;
199 
202  virtual void captureScreen(const std::string& filename, uint32_t width, uint32_t height) = 0;
203 
207  const ScreenMode& getCurrentScreenMode() const;
208 
209  uint32_t getWidth() const;
210  uint32_t getHeight() const;
211  uint32_t getScreenWidth() const { return getWidth(); }
212  uint32_t getScreenHeight() const { return getHeight(); }
213  const Rect& getArea() const;
214 
219  void pushClipArea(const Rect& cliparea, bool clear=true);
220 
224  void popClipArea();
225 
229  const Rect& getClipArea() const;
230 
233  virtual bool putPixel(int32_t x, int32_t y, uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255) = 0;
234 
237  virtual void drawLine(const Point& p1, const Point& p2, uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255) = 0;
238 
241  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;
242 
245  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;
246 
249  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;
250 
253  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;
254 
257  virtual void drawVertex(const Point& p, const uint8_t size, uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255) = 0;
258 
261  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;
262 
263 
269  void setAlphaOptimizerEnabled(bool enabled){ m_isalphaoptimized = enabled; }
270 
273  bool isAlphaOptimizerEnabled() const { return m_isalphaoptimized; }
274 
278  void setImageCompressingEnabled(bool enabled) { m_compressimages = enabled; }
279 
282  bool isImageCompressingEnabled() const { return m_compressimages; }
283 
286  void setFramebufferEnabled(bool enabled) { m_useframebuffer = enabled; }
287 
290  bool isFramebufferEnabled() const { return m_useframebuffer; }
291 
294  void setNPOTEnabled(bool enabled) { m_usenpot = enabled; }
295 
298  bool isNPOTEnabled() const { return m_usenpot; }
299 
302  void setColorKeyEnabled(bool colorkeyenable);
303 
306  bool isColorKeyEnabled() const;
307 
310  void setColorKey(const SDL_Color& colorkey);
311 
314  const SDL_Color& getColorKey() const;
315 
318  void setBackgroundColor(uint8_t r, uint8_t g, uint8_t b);
319 
322  void resetBackgroundColor();
323 
326  const SDL_PixelFormat& getPixelFormat() const;
327 
330  void setFrameLimitEnabled(bool limited);
331 
334  bool isFrameLimitEnabled() const;
335 
338  void setFrameLimit(uint16_t framelimit);
339 
342  uint16_t getFrameLimit() const;
343 
346  SDL_Surface* getRenderTargetSurface();
347 
350  virtual void attachRenderTarget(ImagePtr& img, bool discard) = 0;
351 
354  virtual void detachRenderTarget() = 0;
355 
356  protected:
357  SDL_Surface* m_screen;
358  SDL_Surface* m_target;
359  bool m_compressimages;
360  bool m_useframebuffer;
361  bool m_usenpot;
362  bool m_isalphaoptimized;
363  bool m_iscolorkeyenabled;
364  SDL_Color m_colorkey;
365  ScreenMode m_screenMode;
366  SDL_PixelFormat m_rgba_format;
367 
368  bool m_isbackgroundcolor;
369  SDL_Color m_backgroundcolor;
370 
374  virtual void setClipArea(const Rect& cliparea, bool clear) = 0;
375 
379  void clearClipArea();
380 
381  class ClipInfo {
382  public:
383  Rect r;
384  bool clearing;
385  };
386  std::stack<ClipInfo> m_clipstack;
387 
388  private:
389  bool m_isframelimit;
390  uint32_t m_frame_start;
391  uint16_t m_framelimit;
392  };
393 }
394 
395 #endif
virtual void setLighting(float red, float green, float blue)=0
virtual void setClipArea(const Rect &cliparea, bool clear)=0
RenderBackend(const SDL_Color &colorkey)
void setColorKey(const SDL_Color &colorkey)
virtual void changeRenderInfos(uint16_t elements, int32_t src, int32_t dst, bool light, bool stentest, uint8_t stenref, GLConstants stenop, GLConstants stenfunc)=0
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
void setBackgroundColor(uint8_t r, uint8_t g, uint8_t b)
virtual const std::string & getName() const =0
virtual void createMainScreen(const ScreenMode &mode, const std::string &title, const std::string &icon)=0
virtual void detachRenderTarget()=0
bool isFrameLimitEnabled() const
bool isFramebufferEnabled() const
virtual void addImageToArray(uint32_t id, const Rect &rec, float const *st, uint8_t alpha, uint8_t const *rgb)=0
void setColorKeyEnabled(bool colorkeyenable)
void setFramebufferEnabled(bool enabled)
virtual void init(const std::string &driver)=0
uint16_t getFrameLimit() const
virtual void clearBackBuffer()=0
virtual void drawVertex(const Point &p, const uint8_t size, uint8_t r, uint8_t g, uint8_t b, uint8_t a=255)=0
virtual uint32_t getLightingModel() const =0
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
bool isColorKeyEnabled() const
void pushClipArea(const Rect &cliparea, bool clear=true)
const SDL_Color & getColorKey() const
virtual void startFrame()
void setFrameLimit(uint16_t framelimit)
void setNPOTEnabled(bool enabled)
virtual bool putPixel(int32_t x, int32_t y, uint8_t r, uint8_t g, uint8_t b, uint8_t a=255)=0
virtual void drawLine(const Point &p1, const Point &p2, uint8_t r, uint8_t g, uint8_t b, uint8_t a=255)=0
void setAlphaOptimizerEnabled(bool enabled)
virtual void endFrame()
virtual void changeBlending(int32_t scr, int32_t dst)=0
void setFrameLimitEnabled(bool limited)
virtual void captureScreen(const std::string &filename)=0
SDL_Surface * getRenderTargetSurface()
virtual void attachRenderTarget(ImagePtr &img, bool discard)=0
bool isImageCompressingEnabled() const
virtual void renderVertexArrays()=0
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
const SDL_PixelFormat & getPixelFormat() const
const Rect & getClipArea() const
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
virtual void resetLighting()=0
virtual void resetStencilBuffer(uint8_t buffer)=0
void setImageCompressingEnabled(bool enabled)
bool isNPOTEnabled() const
virtual void setScreenMode(const ScreenMode &mode)=0
const ScreenMode & getCurrentScreenMode() const
virtual void setLightingModel(uint32_t lighting)=0
bool isAlphaOptimizerEnabled() const
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