Gamedev Framework (gf)  0.20.0
A C++17 framework for 2D games
RenderTarget.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2021 Julien Bernard
4  *
5  * This software is provided 'as-is', without any express or implied
6  * warranty. In no event will the authors be held liable for any damages
7  * arising from the use of this software.
8  *
9  * Permission is granted to anyone to use this software for any purpose,
10  * including commercial applications, and to alter it and redistribute it
11  * freely, subject to the following restrictions:
12  *
13  * 1. The origin of this software must not be misrepresented; you must not
14  * claim that you wrote the original software. If you use this software
15  * in a product, an acknowledgment in the product documentation would be
16  * appreciated but is not required.
17  * 2. Altered source versions must be plainly marked as such, and must not be
18  * misrepresented as being the original software.
19  * 3. This notice may not be removed or altered from any source distribution.
20  *
21  * Part of this file comes from SFML, with the same license:
22  * Copyright (C) 2007-2015 Laurent Gomila (laurent@sfml-dev.org)
23  */
24 #ifndef GF_RENDER_TARGET_H
25 #define GF_RENDER_TARGET_H
26 
27 #include <cstdint>
28 
29 #include "GraphicsApi.h"
30 #include "GraphicsHandle.h"
31 #include "Image.h"
32 #include "Matrix.h"
33 #include "PrimitiveType.h"
34 #include "Range.h"
35 #include "Region.h"
36 #include "RenderStates.h"
37 #include "Shader.h"
38 #include "Span.h"
39 #include "View.h"
40 
41 namespace gf {
42 #ifndef DOXYGEN_SHOULD_SKIP_THIS
43 inline namespace v1 {
44 #endif
45 
46  class Drawable;
47  class VertexBuffer;
48  struct Vertex;
49 
54  enum class RenderAttributeType {
55  Byte = 0x1400,
56  UByte = 0x1401,
57  Short = 0x1402,
58  UShort = 0x1403,
59  Float = 0x1406,
60  };
61 
66  struct GF_GRAPHICS_API RenderAttributeInfo {
67  const char *name;
68  int size;
70  bool normalized;
71  std::size_t offset;
72  };
73 
78  template<>
79  struct GF_GRAPHICS_API GraphicsTrait<GraphicsTag::Framebuffer> {
80  static void gen(int n, unsigned* resources);
81  static void del(int n, const unsigned* resources);
82  };
83 
102  class GF_GRAPHICS_API RenderTarget {
103  public:
104 
110  RenderTarget(Vector2i size);
111 
115  virtual ~RenderTarget();
116 
120  RenderTarget(const RenderTarget&) = delete;
121 
125  RenderTarget& operator=(const RenderTarget&) = delete;
126 
132  virtual Vector2i getSize() const = 0;
133 
134 
141  virtual void setActive();
142 
153  Region getCanonicalScissorBox();
154 
160  void setCanonicalScissorBox(const Region& box);
161 
167  RectI getScissorBox();
168 
174  void setScissorBox(const RectI& box);
175 
184  void clear(const Color4f& color);
185 
195  void clear();
196 
203  RangeF getAliasedLineWidthRange() const;
204 
211  float getLineWidth() const;
212 
221  void draw(const Vertex *vertices, std::size_t count, PrimitiveType type, const RenderStates& states = RenderStates());
222 
232  void draw(const Vertex *vertices, const uint16_t *indices, std::size_t count, PrimitiveType type, const RenderStates& states = RenderStates());
233 
240  void draw(const VertexBuffer& buffer, const RenderStates& states = RenderStates());
241 
248  void draw(Drawable& drawable, const RenderStates& states = RenderStates());
249 
260  void customDraw(const void *vertices, std::size_t size, std::size_t count, PrimitiveType type, Span<const RenderAttributeInfo> attributes, const RenderStates& states = RenderStates());
261 
273  void customDraw(const void *vertices, std::size_t size, const uint16_t *indices, std::size_t count, PrimitiveType type, Span<const RenderAttributeInfo> attributes, const RenderStates& states = RenderStates());
274 
282  void customDraw(const VertexBuffer& buffer, Span<const RenderAttributeInfo> attributes, const RenderStates& states = RenderStates());
283 
309  void setView(const View& view);
310 
318  const View& getView() const {
319  return m_view;
320  }
321 
334  Region getCanonicalViewport(const View& view) const;
335 
348  RectI getViewport(const View& view) const;
349 
379  Vector2f mapPixelToCoords(Vector2i point, const View& view) const;
380 
400  Vector2f mapPixelToCoords(Vector2i point) const;
401 
427  Vector2i mapCoordsToPixel(Vector2f point, const View& view) const;
428 
448  Vector2i mapCoordsToPixel(Vector2f point) const;
449 
452  protected:
458  Image captureFramebuffer(unsigned name) const;
459 
460  private:
461  struct Locations {
462  static constexpr std::size_t CountMax = 5;
463  int data[CountMax];
464  std::size_t count = 0;
465  };
466 
467  void drawStart(const RenderStates& states, Locations& locations, std::size_t size, Span<const RenderAttributeInfo> attributes);
468  void drawFinish(const Locations& locations);
469 
470  private:
471  View m_view;
472  Shader m_defaultShader;
473  Shader m_defaultAlphaShader;
474  Texture m_defaultTexture;
475  };
476 
477 #ifndef DOXYGEN_SHOULD_SKIP_THIS
478 }
479 #endif
480 }
481 
482 #endif // GF_RENDER_TARGET_H
A GPU framebuffer.
PrimitiveType
Kind of primitives to render.
Definition: PrimitiveType.h:43
2D camera that defines what region is shown on framebuffer
Definition: View.h:94
A half-open range of values.
Definition: Range.h:44
const char * name
Name of the attribute.
Definition: RenderTarget.h:67
Base class for all render targets (window, texture, ...)
Definition: RenderTarget.h:102
Define the states used for drawing to a RenderTarget.
Definition: RenderStates.h:82
A span.
Definition: Span.h:36
A point associated with a color and a texture coordinate.
Definition: Vertex.h:75
Data in the graphics memory.
Definition: VertexBuffer.h:81
A trait to handle creation and deletion of GPU resources.
Definition: GraphicsHandle.h:48
Abstract base class for objects that can be drawn to a render window.
Definition: Drawable.h:57
An OpenGL vertex and/or fragment shader.
Definition: Shader.h:119
bool normalized
Is the attribute normalized?
Definition: RenderTarget.h:70
RenderAttributeType type
Type of the attribute.
Definition: RenderTarget.h:69
A texture for colored images.
Definition: Texture.h:313
Attribute info.
Definition: RenderTarget.h:66
std::size_t offset
Offset of the attribute in the vertex.
Definition: RenderTarget.h:71
Class for loading, manipulating and saving images.
Definition: Image.h:81
The namespace for gf classes.
Definition: Action.h:35
RenderAttributeType
The type of an attribute.
Definition: RenderTarget.h:54
A 4D vector.
Definition: Vector.h:852
A region of a window.
Definition: Region.h:40
int size
Size of the attribute.
Definition: RenderTarget.h:68
GraphicsTag
A tag to represent various GPU resources.
Definition: GraphicsHandle.h:37
const View & getView() const
Get the view currently in use in the render target.
Definition: RenderTarget.h:318