Debian Codegraph @

engine/core/video/renderbackend.h

raw
1 /***************************************************************************
2 * Copyright (C) 2005-2019 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
49 #include "video/opengl/fife_opengl.h"
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,
76 ZERO,
77 REPLACE,
78 INCR,
79 DECR,
80 INVERT,
81 NEVER,
82 LESS,
83 LEQUAL,
84 GREATER,
85 GEQUAL,
86 EQUAL,
87 NOTEQUAL,
88 ALWAYS
89 };
90 #endif
91
92 enum OverlayType {
93 OVERLAY_TYPE_NONE = 0,
94 OVERLAY_TYPE_COLOR = 1,
95 OVERLAY_TYPE_COLOR_AND_TEXTURE = 2,
96 OVERLAY_TYPE_TEXTURES_AND_FACTOR = 3
97 };
98
99 enum TextureFiltering {
100 TEXTURE_FILTER_NONE = 0,
101 TEXTURE_FILTER_BILINEAR = 1,
102 TEXTURE_FILTER_TRILINEAR = 2,
103 TEXTURE_FILTER_ANISOTROPIC = 3
104 };
105
106 enum RenderDataType {
107 RENDER_DATA_WITHOUT_Z = 0,
108 RENDER_DATA_TEXTURE_Z = 1,
109 RENDER_DATA_TEXCOLOR_Z = 2,
110 RENDER_DATA_MULTITEXTURE_Z = 3
111 };
112
113 class GuiVertex {
114 public:
115 DoublePoint position;
116 Color color;
117 DoublePoint texCoords;
118 };
119
120 /** Abstract interface for all the renderbackends. */
121 class RenderBackend: public DynamicSingleton<RenderBackend> {
122 public:
123 /** Constructor.
124 * @param colorkey The colorkey to use.
125 */
126 RenderBackend(const SDL_Color& colorkey);
127
128 /** Destructor.
129 */
130 virtual ~RenderBackend();
131
132 /** The name of the renderbackend.
133 * @return The name of this renderbackend.
134 */
135 virtual const std::string& getName() const = 0;
136
137 /** Called when a new frame starts.
138 */
139 virtual void startFrame();
140
141 /** Called when a frame is finished and ready to be displayed.
142 */
143 virtual void endFrame();
144
145 /** Initializes the backend.
146 */
147 virtual void init(const std::string& driver) = 0;
148
149 /** Forces a clear of the backbuffer
150 */
151 virtual void clearBackBuffer() = 0;
152
153 /** Initializes the light.
154 */
155 virtual void setLightingModel(uint32_t lighting) = 0;
156
157 /** Gets the current light model.
158 */
159 virtual uint32_t getLightingModel() const = 0;
160
161 /** Set colors for lighting
162 */
163 virtual void setLighting(float red, float green, float blue) = 0;
164
165 /** Reset lighting with default values.
166 */
167 virtual void resetLighting() = 0;
168
169 /** Reset stencil buffer with given value.
170 */
171 virtual void resetStencilBuffer(uint8_t buffer) = 0;
172
173 /** Change the Blendingmodel.
174 */
175 virtual void changeBlending(int32_t scr, int32_t dst) = 0;
176
177 /** Performs cleanup actions.
178 */
179 void deinit();
180
181 /** Creates the mainscreen (the display window).
182 * @param mode The ScreenMode to use. @see FIFE::ScreenMode.
183 * @param title The window title to use.
184 * @param icon The window icon to use.
185 */
186 virtual void createMainScreen(const ScreenMode& mode, const std::string& title, const std::string& icon) = 0;
187
188 /** Sets the mainscreen display mode.
189 * @param mode The ScreenMode to change the display to. @see FIFE::ScreenMode.
190 */
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
196 /** Creates an Image suitable for this renderbackend.
197 * @param data Pointer to the imagedata (needs to be in RGBA, 8 bits per channel).
198 * @param width Width of the image.
199 * @param height Height of the image.
200 * @return The new Image.
201 */
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
205 /** Helper function to create images from SDL_Surfaces.
206 * Takes ownership over the surface.
207 * @param surface The surface to convert.
208 * @return The new Image.
209 */
210 virtual Image* createImage(SDL_Surface* surface) = 0;
211 virtual Image* createImage(const std::string& name, SDL_Surface* surface) = 0;
212
213 /** Render the Vertex Arrays, only for primitives (points, lines,...)
214 */
215 virtual void renderVertexArrays() = 0;
216
217 /** Add the Image data to the array
218 */
219 virtual void addImageToArray(uint32_t id, const Rect& rec, float const* st, uint8_t alpha, uint8_t const* rgba) = 0;
220
221 /** Dirty helper function to change the render infos
222 */
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
225 /** Creates a Screenshot and saves it to a file.
226 */
227 virtual void captureScreen(const std::string& filename) = 0;
228
229 /** Creates a Screenshot with the given size(w,h) and saves it to a file.
230 */
231 virtual void captureScreen(const std::string& filename, uint32_t width, uint32_t height) = 0;
232
233 SDL_Window* getWindow() {return m_window; }
234
235 /** Get current screen mode
236 * @return The current screen mode
237 */
238 const ScreenMode& getCurrentScreenMode() const;
239
240 uint32_t getWidth() const;
241 uint32_t getHeight() const;
242 uint32_t getScreenWidth() const { return getWidth(); }
243 uint32_t getScreenHeight() const { return getHeight(); }
244 const Rect& getArea() const;
245
246 /** Pushes clip area to clip stack
247 * Clip areas define which area is drawn on screen. Usable e.g. with viewports
248 * note that previous items in stack do not affect the latest area pushed
249 */
250 void pushClipArea(const Rect& cliparea, bool clear=true);
251
252 /** Pops clip area from clip stack
253 * @see pushClipArea
254 */
255 void popClipArea();
256
257 /** Gets the current clip area
258 * @see pushClipArea
259 */
260 const Rect& getClipArea() const;
261
262 /** Writes pixel to given position. Returns true, if pixel was written (not out of bounds)
263 */
264 virtual bool putPixel(int32_t x, int32_t y, uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255) = 0;
265
266 /** Draws line between given points with given RGBA
267 */
268 virtual void drawLine(const Point& p1, const Point& p2, uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255) = 0;
269
270 /** Draws line between given points with given RGBA and width.
271 */
272 virtual void drawThickLine(const Point& p1, const Point& p2, uint8_t width, uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255) = 0;
273
274 /** Draws lines between given points with given RGBA and width.
275 */
276 virtual void drawPolyLine(const std::vector<Point>& points, uint8_t width, uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255) = 0;
277
278 /** Draws bezier curve between given points with given RGBA and width.
279 */
280 virtual void drawBezier(const std::vector<Point>& points, int32_t steps, uint8_t width, uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255) = 0;
281
282 /** Draws triangle between given points with given RGBA
283 */
284 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;
285
286 /** Draws an axis parallel rectangle.
287 */
288 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;
289
290 /** Draws a filled axis parallel rectangle.
291 */
292 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;
293
294 /** Draws quad between given points with given RGBA
295 */
296 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;
297
298 /** Draws a quad that represents a vertex with given RGBA
299 */
300 virtual void drawVertex(const Point& p, const uint8_t size, uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255) = 0;
301
302 /** Draws a circle.
303 */
304 virtual void drawCircle(const Point& p, uint32_t radius, uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255) = 0;
305
306 /** Draws a filled circle.
307 */
308 virtual void drawFillCircle(const Point& p, uint32_t radius, uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255) = 0;
309
310 /** Draws a circle segment.
311 * Note: The start angle must be less than the end angle. 0 angle is right side.
312 */
313 virtual void drawCircleSegment(const Point& p, uint32_t radius, int32_t sangle, int32_t eangle, uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255) = 0;
314
315 /** Draws a filled circle segment. 0 angle is right side.
316 * Note: The start angle must be less than the end angle.
317 */
318 virtual void drawFillCircleSegment(const Point& p, uint32_t radius, int32_t sangle, int32_t eangle, uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255) = 0;
319
320 /** Draws a light primitive that based on a triangle fan
321 */
322 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;
323
324 /** Enables scissor test on the render backend.
325 */
326 virtual void enableScissorTest() = 0;
327
328 /** Disables scissor test on the render backend.
329 */
330 virtual void disableScissorTest() = 0;
331
332 /** Enable or disable the alpha 'optimizing' code
333 *
334 * @param enabled Optimize whether the image shall be analysed for 'fake' alpha images.
335 * Only implemented by and useful for the SDL backend at the moment.
336 */
337 void setAlphaOptimizerEnabled(bool enabled){ m_isalphaoptimized = enabled; }
338
339 /** @see setAlphaOptimizerEnabled
340 */
341 bool isAlphaOptimizerEnabled() const { return m_isalphaoptimized; }
342
343 /** Enables or disable compressing images by video driver.
344 * @remarks This is relevant for in OpenGL renderbackend
345 */
346 void setImageCompressingEnabled(bool enabled) { m_compressimages = enabled; }
347
348 /** @see setImageCompressingEnabled
349 */
350 bool isImageCompressingEnabled() const { return m_compressimages; }
351
352 /** Enables or disable the usage of the framebuffer, if available
353 */
354 void setFramebufferEnabled(bool enabled) { m_useframebuffer = enabled; }
355
356 /** @see setFramebufferEnabled
357 */
358 bool isFramebufferEnabled() const { return m_useframebuffer; }
359
360 /** Enables or disable the usage of npot, if available
361 */
362 void setNPOTEnabled(bool enabled) { m_usenpot = enabled; }
363
364 /** @see setNPOTEnabled
365 */
366 bool isNPOTEnabled() const { return m_usenpot; }
367
368 /** Sets the texture filtering method.
369 * Supports none, bilinear, trilinear and anisotropic filtering.
370 * Note! Works only for OpenGL backends.
371 * @see TextureFiltering
372 */
373 void setTextureFiltering(TextureFiltering filter);
374
375 /** @see setTextureFiltering
376 */
377 TextureFiltering getTextureFiltering() const;
378
379 /** Enables or disables the usage of mipmapping.
380 * Note! Works only for OpenGL backends.
381 */
382 void setMipmappingEnabled(bool enabled);
383
384 /** @see setMipmappingEnabled
385 */
386 bool isMipmappingEnabled() const;
387
388 /** Gets max antisotropy for antisotropic filtering.
389 */
390 int32_t getMaxAnisotropy() const;
391
392 /** Enables or disables monochrome rendering.
393 * Note! Works only for OpenGL backends.
394 */
395 void setMonochromeEnabled(bool enabled);
396
397 /** @see setMonochromeEnabled
398 */
399 bool isMonochromeEnabled() const;
400
401 /** Enables or disables depth buffer rendering.
402 * Note! Works only for OpenGL backend.
403 */
404 void setDepthBufferEnabled(bool enabled);
405
406 /** @see setMonochromeEnabled
407 */
408 bool isDepthBufferEnabled() const;
409
410 /** Sets the value for alpha test.
411 * Discards a fragment if its value is not greater. Only used if depth buffer is enabled.
412 * Note! Works only for OpenGL backend.
413 */
414 void setAlphaTestValue(float alpha);
415
416 /** @see setAlphaValue
417 */
418 float getAlphaTestValue() const;
419
420 /** Sets whether to use the colorkey feature
421 */
422 void setColorKeyEnabled(bool colorkeyenable);
423
424 /** Gets whether the colorkey feature is in use
425 */
426 bool isColorKeyEnabled() const;
427
428 /** Sets the global colorkey to use for images
429 */
430 void setColorKey(const SDL_Color& colorkey);
431
432 /** Gets the global colorkey setting
433 */
434 const SDL_Color& getColorKey() const;
435
436 /** Set the background color
437 */
438 void setBackgroundColor(uint8_t r, uint8_t g, uint8_t b);
439
440 /** Reset the background color to black
441 */
442 void resetBackgroundColor();
443
444 /** Gets the current screen rgba format
445 */
446 const SDL_PixelFormat& getPixelFormat() const;
447
448 /** Sets whether to use VSync
449 */
450 void setVSyncEnabled(bool vsync);
451
452 /** Gets whether VSync is in use
453 */
454 bool isVSyncEnabled() const;
455
456 /** Sets whether to use the frame limiter
457 */
458 void setFrameLimitEnabled(bool limited);
459
460 /** Gets whether the frame limiter is in use
461 */
462 bool isFrameLimitEnabled() const;
463
464 /** Sets the frame limit
465 */
466 void setFrameLimit(uint16_t framelimit);
467
468 /** Gets the frame limit
469 */
470 uint16_t getFrameLimit() const;
471
472 /** Returns screen render surface
473 */
474 SDL_Surface* getScreenSurface();
475
476 /** Returns currently attached render surface
477 */
478 SDL_Surface* getRenderTargetSurface();
479
480 /** Attaches given image as a new render surface
481 */
482 virtual void attachRenderTarget(ImagePtr& img, bool discard) = 0;
483
484 /** Detaches current render surface
485 */
486 virtual void detachRenderTarget() = 0;
487
488 /** Renders geometry required by gui.
489 */
490 virtual void renderGuiGeometry(const std::vector<GuiVertex>& vertices, const std::vector<int>& indices, const DoublePoint& translation, ImagePtr texture) = 0;
491
492 /** Helper that returns an interpolated Point
493 */
494 Point getBezierPoint(const std::vector<Point>& points, int32_t elements, float t);
495
496 /** Helper that adds the control points for bezier curves.
497 */
498 void addControlPoints(const std::vector<Point>& points, std::vector<Point>& newPoints);
499
500 protected:
501
502 /** Sets given clip area into image
503 * @see pushClipArea
504 */
505 virtual void setClipArea(const Rect& cliparea, bool clear) = 0;
506
507 SDL_Window* m_window;
508 SDL_Surface* m_screen;
509 SDL_Surface* m_target;
510 bool m_compressimages;
511 bool m_useframebuffer;
512 bool m_usenpot;
513 bool m_isalphaoptimized;
514 bool m_iscolorkeyenabled;
515 SDL_Color m_colorkey;
516 ScreenMode m_screenMode;
517 SDL_PixelFormat m_rgba_format;
518
519 bool m_isbackgroundcolor;
520 SDL_Color m_backgroundcolor;
521
522 // mipmapping
523 bool m_isMipmapping;
524 // texture filter
525 TextureFiltering m_textureFilter;
526 // max anisotropy
527 int32_t m_maxAnisotropy;
528 // monochrome rendering
529 bool m_monochrome;
530 // depth buffer rendering
531 bool m_isDepthBuffer;
532 // alpha test value
533 float m_alphaValue;
534 // vsync value
535 bool m_vSync;
536
537 /** Clears any possible clip areas
538 * @see pushClipArea
539 */
540 void clearClipArea();
541
542 class ClipInfo {
543 public:
544 Rect r;
545 bool clearing;
546 };
547 std::stack<ClipInfo> m_clipstack;
548
549 ClipInfo m_guiClip;
550 private:
551 bool m_isframelimit;
552 uint32_t m_frame_start;
553 uint16_t m_framelimit;
554
555 };
556 }
557
558 #endif