FIFE
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
renderbackend.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2013 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
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,
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* rgba) = 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 
274 
278  void setImageCompressingEnabled(bool enabled) { m_compressimages = enabled; }
279 
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 
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;
361  bool m_usenpot;
364  SDL_Color m_colorkey;
366  SDL_PixelFormat m_rgba_format;
367 
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:
384  bool clearing;
385  };
386  std::stack<ClipInfo> m_clipstack;
387 
388  private:
392  };
393 }
394 
395 #endif
virtual void addImageToArray(uint32_t id, const Rect &rec, float const *st, uint8_t alpha, uint8_t const *rgba)=0
Add the Image data to the array.
Abstract interface for all the renderbackends.
Definition: renderbackend.h:92
virtual void setLighting(float red, float green, float blue)=0
Set colors for lighting.
SDL_Surface * m_target
virtual void setClipArea(const Rect &cliparea, bool clear)=0
Sets given clip area into image.
RenderBackend(const SDL_Color &colorkey)
Constructor.
void setColorKey(const SDL_Color &colorkey)
Sets the global colorkey to use for images.
uint32_t getScreenHeight() const
virtual void changeRenderInfos(uint16_t elements, int32_t src, int32_t dst, bool light, bool stentest, uint8_t stenref, GLConstants stenop, GLConstants stenfunc)=0
Dirty helper function to change the render infos.
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
Draws an axis parallel rectangle.
Base Class for Images.
Definition: image.h:47
SDL_PixelFormat m_rgba_format
void setBackgroundColor(uint8_t r, uint8_t g, uint8_t b)
Set the background color.
virtual const std::string & getName() const =0
The name of the renderbackend.
uint32_t getHeight() const
virtual void createMainScreen(const ScreenMode &mode, const std::string &title, const std::string &icon)=0
Creates the mainscreen (the display window).
virtual void detachRenderTarget()=0
Detaches current render surface.
bool isFrameLimitEnabled() const
Gets whether the frame limiter is in use.
bool isFramebufferEnabled() const
void setColorKeyEnabled(bool colorkeyenable)
Sets whether to use the colorkey feature.
void setFramebufferEnabled(bool enabled)
Enables or disable the usage of the framebuffer, if available.
virtual void init(const std::string &driver)=0
Initializes the backend.
uint16_t getFrameLimit() const
Gets the frame limit.
virtual void clearBackBuffer()=0
Forces a clear of the backbuffer.
virtual void drawVertex(const Point &p, const uint8_t size, uint8_t r, uint8_t g, uint8_t b, uint8_t a=255)=0
Draws a quad that represents a vertex with given RGBA.
virtual uint32_t getLightingModel() const =0
Gets the current light model.
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
Draws triangle between given points with given RGBA.
void resetBackgroundColor()
Reset the background color to black.
bool isColorKeyEnabled() const
Gets whether the colorkey feature is in use.
unsigned char uint8_t
Definition: core.h:38
void pushClipArea(const Rect &cliparea, bool clear=true)
Pushes clip area to clip stack Clip areas define which area is drawn on screen.
const SDL_Color & getColorKey() const
Gets the global colorkey setting.
virtual void startFrame()
Called when a new frame starts.
void setFrameLimit(uint16_t framelimit)
Sets the frame limit.
void setNPOTEnabled(bool enabled)
Enables or disable the usage of npot, if available.
Another Singleton.
Definition: singleton.h:82
uint32_t getScreenWidth() const
virtual bool putPixel(int32_t x, int32_t y, uint8_t r, uint8_t g, uint8_t b, uint8_t a=255)=0
Writes pixel to given position.
virtual ~RenderBackend()
Destructor.
void popClipArea()
Pops clip area from clip stack.
virtual void drawLine(const Point &p1, const Point &p2, uint8_t r, uint8_t g, uint8_t b, uint8_t a=255)=0
Draws line between given points with given RGBA.
unsigned short uint16_t
Definition: core.h:39
SDL_Surface * m_screen
void setAlphaOptimizerEnabled(bool enabled)
Enable or disable the alpha &#39;optimizing&#39; code.
SDL_Color m_backgroundcolor
uint32_t getWidth() const
virtual void endFrame()
Called when a frame is finished and ready to be displayed.
void clearClipArea()
Clears any possible clip areas.
virtual void changeBlending(int32_t scr, int32_t dst)=0
Change the Blendingmodel.
void setFrameLimitEnabled(bool limited)
Sets whether to use the frame limiter.
virtual void captureScreen(const std::string &filename)=0
Creates a Screenshot and saves it to a file.
SDL_Surface * getRenderTargetSurface()
Returns currently attached render surface.
virtual Image * createImage(IResourceLoader *loader=0)=0
virtual void attachRenderTarget(ImagePtr &img, bool discard)=0
Attaches given image as a new render surface.
bool isImageCompressingEnabled() const
virtual void renderVertexArrays()=0
Render the Vertex Arrays, only for primitives (points, lines,...)
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
Draws a filled axis parallel rectangle.
const SDL_PixelFormat & getPixelFormat() const
Gets the current screen rgba format.
ScreenMode m_screenMode
const Rect & getClipArea() const
Gets the current clip area.
void deinit()
Performs cleanup actions.
std::stack< ClipInfo > m_clipstack
unsigned int uint32_t
Definition: core.h:40
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
Draws a light primitive that based on a triangle fan.
virtual void resetLighting()=0
Reset lighting with default values.
virtual void resetStencilBuffer(uint8_t buffer)=0
Reset stencil buffer with given value.
void setImageCompressingEnabled(bool enabled)
Enables or disable compressing images by video driver.
bool isNPOTEnabled() const
virtual void setScreenMode(const ScreenMode &mode)=0
Sets the mainscreen display mode.
const ScreenMode & getCurrentScreenMode() const
Get current screen mode.
virtual void setLightingModel(uint32_t lighting)=0
Initializes the light.
bool isAlphaOptimizerEnabled() const
const Rect & getArea() 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
Draws quad between given points with given RGBA.