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 #include "color.h"
47 
48 #ifdef HAVE_OPENGL
50 #endif
51 
52 namespace FIFE {
53 
54  class Image;
55 
56 #ifdef HAVE_OPENGL
57  enum GLConstants {
58  KEEP = GL_KEEP,
59  ZERO = GL_ZERO,
60  REPLACE = GL_REPLACE,
61  INCR = GL_INCR,
62  DECR = GL_DECR,
63  INVERT = GL_INVERT,
64  NEVER = GL_NEVER,
65  LESS = GL_LESS,
66  LEQUAL = GL_LEQUAL,
67  GREATER = GL_GREATER,
68  GEQUAL = GL_GEQUAL,
69  EQUAL = GL_EQUAL,
70  NOTEQUAL = GL_NOTEQUAL,
71  ALWAYS = GL_ALWAYS
72  };
73 #else
74  enum GLConstants {
75  KEEP = 0,
89  };
90 #endif
91 
92  enum OverlayType {
97  };
98 
104  };
105 
111  };
112 
113  class GuiVertex {
114  public:
118  };
119 
121  class RenderBackend: public DynamicSingleton<RenderBackend> {
122  public:
126  RenderBackend(const SDL_Color& colorkey);
127 
130  virtual ~RenderBackend();
131 
135  virtual const std::string& getName() const = 0;
136 
139  virtual void startFrame();
140 
143  virtual void endFrame();
144 
147  virtual void init(const std::string& driver) = 0;
148 
151  virtual void clearBackBuffer() = 0;
152 
155  virtual void setLightingModel(uint32_t lighting) = 0;
156 
159  virtual uint32_t getLightingModel() const = 0;
160 
163  virtual void setLighting(float red, float green, float blue) = 0;
164 
167  virtual void resetLighting() = 0;
168 
171  virtual void resetStencilBuffer(uint8_t buffer) = 0;
172 
175  virtual void changeBlending(int32_t scr, int32_t dst) = 0;
176 
179  void deinit();
180 
186  virtual void createMainScreen(const ScreenMode& mode, const std::string& title, const std::string& icon) = 0;
187 
191  virtual void setScreenMode(const ScreenMode& mode) = 0;
192 
193  virtual Image* createImage(IResourceLoader* loader = 0) = 0;
194  virtual Image* createImage(const std::string& name, IResourceLoader* loader = 0) = 0;
195 
202  virtual Image* createImage(const uint8_t* data, uint32_t width, uint32_t height) = 0;
203  virtual Image* createImage(const std::string& name, const uint8_t* data, uint32_t width, uint32_t height) = 0;
204 
210  virtual Image* createImage(SDL_Surface* surface) = 0;
211  virtual Image* createImage(const std::string& name, SDL_Surface* surface) = 0;
212 
215  virtual void renderVertexArrays() = 0;
216 
219  virtual void addImageToArray(uint32_t id, const Rect& rec, float const* st, uint8_t alpha, uint8_t const* rgba) = 0;
220 
223  virtual void changeRenderInfos(RenderDataType type, uint16_t elements, int32_t src, int32_t dst, bool light, bool stentest, uint8_t stenref, GLConstants stenop, GLConstants stenfunc, OverlayType otype = OVERLAY_TYPE_NONE) = 0;
224 
227  virtual void captureScreen(const std::string& filename) = 0;
228 
231  virtual void captureScreen(const std::string& filename, uint32_t width, uint32_t height) = 0;
232 
236  const ScreenMode& getCurrentScreenMode() const;
237 
238  uint32_t getWidth() const;
239  uint32_t getHeight() const;
240  uint32_t getScreenWidth() const { return getWidth(); }
241  uint32_t getScreenHeight() const { return getHeight(); }
242  const Rect& getArea() const;
243 
248  void pushClipArea(const Rect& cliparea, bool clear=true);
249 
253  void popClipArea();
254 
258  const Rect& getClipArea() const;
259 
262  virtual bool putPixel(int32_t x, int32_t y, uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255) = 0;
263 
266  virtual void drawLine(const Point& p1, const Point& p2, uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255) = 0;
267 
270  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;
271 
274  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;
275 
278  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;
279 
282  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;
283 
286  virtual void drawVertex(const Point& p, const uint8_t size, uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255) = 0;
287 
290  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;
291 
294  virtual void enableScissorTest() = 0;
295 
298  virtual void disableScissorTest() = 0;
299 
305  void setAlphaOptimizerEnabled(bool enabled){ m_isalphaoptimized = enabled; }
306 
310 
314  void setImageCompressingEnabled(bool enabled) { m_compressimages = enabled; }
315 
319 
322  void setFramebufferEnabled(bool enabled) { m_useframebuffer = enabled; }
323 
326  bool isFramebufferEnabled() const { return m_useframebuffer; }
327 
330  void setNPOTEnabled(bool enabled) { m_usenpot = enabled; }
331 
334  bool isNPOTEnabled() const { return m_usenpot; }
335 
342 
346 
350  void setMipmappingEnabled(bool enabled);
351 
354  bool isMipmappingEnabled() const;
355 
358  int32_t getMaxAnisotropy() const;
359 
363  void setMonochromeEnabled(bool enabled);
364 
367  bool isMonochromeEnabled() const;
368 
372  void setDepthBufferEnabled(bool enabled);
373 
376  bool isDepthBufferEnabled() const;
377 
382  void setAlphaTestValue(float alpha);
383 
386  float getAlphaTestValue() const;
387 
390  void setColorKeyEnabled(bool colorkeyenable);
391 
394  bool isColorKeyEnabled() const;
395 
398  void setColorKey(const SDL_Color& colorkey);
399 
402  const SDL_Color& getColorKey() const;
403 
407 
410  void resetBackgroundColor();
411 
414  const SDL_PixelFormat& getPixelFormat() const;
415 
418  void setFrameLimitEnabled(bool limited);
419 
422  bool isFrameLimitEnabled() const;
423 
426  void setFrameLimit(uint16_t framelimit);
427 
430  uint16_t getFrameLimit() const;
431 
434  SDL_Surface* getRenderTargetSurface();
435 
438  virtual void attachRenderTarget(ImagePtr& img, bool discard) = 0;
439 
442  virtual void detachRenderTarget() = 0;
443 
446  virtual void renderGuiGeometry(const std::vector<GuiVertex>& vertices, const std::vector<int>& indices, const DoublePoint& translation, ImagePtr texture) = 0;
447 
448  protected:
449 
453  virtual void setClipArea(const Rect& cliparea, bool clear) = 0;
454 
455  SDL_Surface* m_screen;
456  SDL_Surface* m_target;
459  bool m_usenpot;
462  SDL_Color m_colorkey;
464  SDL_PixelFormat m_rgba_format;
465 
467  SDL_Color m_backgroundcolor;
468 
469  // mipmapping
471  // texture filter
473  // max anisotropy
475  // monochrome rendering
477  // depth buffer rendering
479  // alpha test value
481 
485  void clearClipArea();
486 
487  class ClipInfo {
488  public:
490  bool clearing;
491  };
492  std::stack<ClipInfo> m_clipstack;
493 
495  private:
499 
500  };
501 }
502 
503 #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.
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 disableScissorTest()=0
Disables scissor test on the render backend.
virtual void renderGuiGeometry(const std::vector< GuiVertex > &vertices, const std::vector< int > &indices, const DoublePoint &translation, ImagePtr texture)=0
Renders geometry required by gui.
void setMonochromeEnabled(bool enabled)
Enables or disables monochrome rendering.
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
TextureFiltering getTextureFiltering() const
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
int32_t getMaxAnisotropy() const
Gets max antisotropy for antisotropic filtering.
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.
DoublePoint texCoords
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.
bool isMipmappingEnabled() const
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
virtual void changeRenderInfos(RenderDataType type, uint16_t elements, int32_t src, int32_t dst, bool light, bool stentest, uint8_t stenref, GLConstants stenop, GLConstants stenfunc, OverlayType otype=OVERLAY_TYPE_NONE)=0
Dirty helper function to change the render infos.
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 setMipmappingEnabled(bool enabled)
Enables or disables the usage of mipmapping.
void setAlphaTestValue(float alpha)
Sets the value for alpha test.
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 setTextureFiltering(TextureFiltering filter)
Sets the texture filtering method.
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 'optimizing' code.
void setDepthBufferEnabled(bool enabled)
Enables or disables depth buffer rendering.
SDL_Color m_backgroundcolor
bool isMonochromeEnabled() const
virtual void enableScissorTest()=0
Enables scissor test on the render backend.
uint32_t getWidth() const
virtual void endFrame()
Called when a frame is finished and ready to be displayed.
shared pointer implementation to provide automatic reference counting and deletion when last referenc...
Definition: sharedptr.h:42
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.
TextureFiltering m_textureFilter
virtual Image * createImage(IResourceLoader *loader=0)=0
virtual void attachRenderTarget(ImagePtr &img, bool discard)=0
Attaches given image as a new render surface.
TextureFiltering
Definition: renderbackend.h:99
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
RenderDataType
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
float getAlphaTestValue() const
bool isDepthBufferEnabled() 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
Draws a light primitive that based on a triangle fan.
DoublePoint position
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.