Gamedev Framework (gf)  0.17.0
A C++14 framework for 2D games
RenderTarget.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2019 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 "ArrayRef.h"
30 #include "GraphicsHandle.h"
31 #include "Image.h"
32 #include "Matrix.h"
33 #include "Portability.h"
34 #include "PrimitiveType.h"
35 #include "Range.h"
36 #include "Region.h"
37 #include "RenderStates.h"
38 #include "Shader.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 
50  enum class RenderAttributeType {
51  Byte = 0x1400,
52  UByte = 0x1401,
53  Short = 0x1402,
54  UShort = 0x1403,
55  Float = 0x1406,
56  };
57 
59  const char *name;
60  int size;
62  bool normalized;
63  std::size_t offset;
64  };
65 
66  template<>
68  static void gen(int n, unsigned* resources);
69  static void del(int n, const unsigned* resources);
70  };
71 
90  class GF_API RenderTarget {
91  public:
92 
98  RenderTarget(Vector2i size);
99 
103  virtual ~RenderTarget();
104 
108  RenderTarget(const RenderTarget&) = delete;
109 
113  RenderTarget& operator=(const RenderTarget&) = delete;
114 
120  virtual Vector2i getSize() const = 0;
121 
132  Region getCanonicalScissorBox();
133 
139  void setCanonicalScissorBox(const Region& box);
140 
146  RectI getScissorBox();
147 
153  void setScissorBox(const RectI& box);
154 
163  void clear(const Color4f& color);
164 
174  void clear();
175 
182  RangeF getAliasedLineWidthRange() const;
183 
190  float getLineWidth() const;
191 
200  void draw(const Vertex *vertices, std::size_t count, PrimitiveType type, const RenderStates& states = RenderStates());
201 
211  void draw(const Vertex *vertices, const uint16_t *indices, std::size_t count, PrimitiveType type, const RenderStates& states = RenderStates());
212 
219  void draw(const VertexBuffer& buffer, const RenderStates& states = RenderStates());
220 
227  void draw(Drawable& drawable, const RenderStates& states = RenderStates());
228 
239  void customDraw(const void *vertices, std::size_t size, std::size_t count, PrimitiveType type, ArrayRef<RenderAttributeInfo> attributes, const RenderStates& states = RenderStates());
240 
252  void customDraw(const void *vertices, std::size_t size, const uint16_t *indices, std::size_t count, PrimitiveType type, ArrayRef<RenderAttributeInfo> attributes, const RenderStates& states = RenderStates());
253 
261  void customDraw(const VertexBuffer& buffer, ArrayRef<RenderAttributeInfo> attributes, const RenderStates& states = RenderStates());
262 
288  void setView(const View& view);
289 
297  const View& getView() const {
298  return m_view;
299  }
300 
313  Region getCanonicalViewport(const View& view) const;
314 
327  RectI getViewport(const View& view) const;
328 
358  Vector2f mapPixelToCoords(Vector2i point, const View& view) const;
359 
379  Vector2f mapPixelToCoords(Vector2i point) const;
380 
406  Vector2i mapCoordsToPixel(Vector2f point, const View& view) const;
407 
427  Vector2i mapCoordsToPixel(Vector2f point) const;
428 
431  protected:
437  Image captureFramebuffer(unsigned name) const;
438 
439  private:
440  struct Locations {
441  static constexpr std::size_t CountMax = 5;
442  int data[CountMax];
443  std::size_t count = 0;
444  };
445 
446  void drawStart(const RenderStates& states, Locations& locations, std::size_t size, ArrayRef<RenderAttributeInfo> attributes);
447  void drawFinish(const Locations& locations);
448 
449  private:
450  View m_view;
451  Shader m_defaultShader;
452  Shader m_defaultAlphaShader;
453  Texture m_defaultTexture;
454  };
455 
456 #ifndef DOXYGEN_SHOULD_SKIP_THIS
457 }
458 #endif
459 }
460 
461 #endif // GF_RENDER_TARGET_H
RenderAttributeType
Definition: RenderTarget.h:50
2D camera that defines what region is shown on framebuffer
Definition: View.h:94
A half-open range of values.
Definition: Range.h:43
const char * name
Definition: RenderTarget.h:59
Base class for all render targets (window, texture, ...)
Definition: RenderTarget.h:90
Define the states used for drawing to a RenderTarget.
Definition: RenderStates.h:82
A point associated with a color and a texture coordinate.
Definition: Vertex.h:75
PrimitiveType
Kind of primitives to render.
Definition: PrimitiveType.h:43
Data in the graphics memory.
Definition: VertexBuffer.h:77
constexpr BufferRef< T > buffer(T *data, std::size_t size)
Create a reference to a buffer.
Definition: BufferRef.h:211
Definition: GraphicsHandle.h:41
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:120
bool normalized
Definition: RenderTarget.h:62
RenderAttributeType type
Definition: RenderTarget.h:61
A texture for colored images.
Definition: Texture.h:309
Definition: RenderTarget.h:58
std::size_t offset
Definition: RenderTarget.h:63
GraphicsTag
Definition: GraphicsHandle.h:34
Class for loading, manipulating and saving images.
Definition: Image.h:80
The namespace for gf classes.
Definition: Action.h:35
A constant reference to an array and its size.
Definition: ArrayRef.h:43
A 4D vector.
Definition: Vector.h:852
A region of a window.
Definition: Region.h:40
int size
Definition: RenderTarget.h:60
const View & getView() const
Get the view currently in use in the render target.
Definition: RenderTarget.h:297