Gamedev Framework (gf)  0.19.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 
144  Region getCanonicalScissorBox();
145 
151  void setCanonicalScissorBox(const Region& box);
152 
158  RectI getScissorBox();
159 
165  void setScissorBox(const RectI& box);
166 
175  void clear(const Color4f& color);
176 
186  void clear();
187 
194  RangeF getAliasedLineWidthRange() const;
195 
202  float getLineWidth() const;
203 
212  void draw(const Vertex *vertices, std::size_t count, PrimitiveType type, const RenderStates& states = RenderStates());
213 
223  void draw(const Vertex *vertices, const uint16_t *indices, std::size_t count, PrimitiveType type, const RenderStates& states = RenderStates());
224 
231  void draw(const VertexBuffer& buffer, const RenderStates& states = RenderStates());
232 
239  void draw(Drawable& drawable, const RenderStates& states = RenderStates());
240 
251  void customDraw(const void *vertices, std::size_t size, std::size_t count, PrimitiveType type, Span<const RenderAttributeInfo> attributes, const RenderStates& states = RenderStates());
252 
264  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());
265 
273  void customDraw(const VertexBuffer& buffer, Span<const RenderAttributeInfo> attributes, const RenderStates& states = RenderStates());
274 
300  void setView(const View& view);
301 
309  const View& getView() const {
310  return m_view;
311  }
312 
325  Region getCanonicalViewport(const View& view) const;
326 
339  RectI getViewport(const View& view) const;
340 
370  Vector2f mapPixelToCoords(Vector2i point, const View& view) const;
371 
391  Vector2f mapPixelToCoords(Vector2i point) const;
392 
418  Vector2i mapCoordsToPixel(Vector2f point, const View& view) const;
419 
439  Vector2i mapCoordsToPixel(Vector2f point) const;
440 
443  protected:
449  Image captureFramebuffer(unsigned name) const;
450 
451  private:
452  struct Locations {
453  static constexpr std::size_t CountMax = 5;
454  int data[CountMax];
455  std::size_t count = 0;
456  };
457 
458  void drawStart(const RenderStates& states, Locations& locations, std::size_t size, Span<const RenderAttributeInfo> attributes);
459  void drawFinish(const Locations& locations);
460 
461  private:
462  View m_view;
463  Shader m_defaultShader;
464  Shader m_defaultAlphaShader;
465  Texture m_defaultTexture;
466  };
467 
468 #ifndef DOXYGEN_SHOULD_SKIP_THIS
469 }
470 #endif
471 }
472 
473 #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:80
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:309