Gamedev Framework (gf) 1.2.0
A C++17 framework for 2D games
RenderTarget.h
1/*
2 * Gamedev Framework (gf)
3 * Copyright (C) 2016-2022 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
41namespace gf {
42#ifndef DOXYGEN_SHOULD_SKIP_THIS
43inline namespace v1 {
44#endif
45
46 class Drawable;
47 class VertexBuffer;
48 struct Vertex;
49
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;
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
111
115 virtual ~RenderTarget();
116
120 RenderTarget(const RenderTarget&) = delete;
121
126
132 virtual Vector2i getSize() const = 0;
133
134
141 virtual void setActive();
142
154
161
168
174 void setScissorBox(const RectI& box);
175
184 void clear(const Color4f& color);
185
195 void clear();
196
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
401
427 Vector2i mapCoordsToPixel(Vector2f point, const View& view) const;
428
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
Abstract base class for objects that can be drawn to a render window.
Definition: Drawable.h:57
Class for loading, manipulating and saving images.
Definition: Image.h:81
Base class for all render targets (window, texture, ...)
Definition: RenderTarget.h:102
void customDraw(const void *vertices, std::size_t size, std::size_t count, PrimitiveType type, Span< const RenderAttributeInfo > attributes, const RenderStates &states=RenderStates())
Draw primitives defined by an array of custom vertices.
RectI getScissorBox()
Get the current scissor box.
void clear(const Color4f &color)
Clear the entire target with a single color.
void draw(const Vertex *vertices, std::size_t count, PrimitiveType type, const RenderStates &states=RenderStates())
Draw primitives defined by an array of vertices.
void draw(Drawable &drawable, const RenderStates &states=RenderStates())
Draw a drawable object to the render target.
RenderTarget & operator=(const RenderTarget &)=delete
Deleted copy assignment.
Image captureFramebuffer(unsigned name) const
Capture the given framebuffer.
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())
Draw primitives defined by an array of custom vertices and their indices.
RectI getViewport(const View &view) const
Get the viewport of a view, applied to this render target.
void draw(const Vertex *vertices, const uint16_t *indices, std::size_t count, PrimitiveType type, const RenderStates &states=RenderStates())
Draw primitives defined by an array of vertices and their indices.
RenderTarget(Vector2i size)
Constructor.
Region getCanonicalScissorBox()
Get the current canonical scissor box.
Vector2f mapPixelToCoords(Vector2i point) const
Convert a point from target coordinates to world coordinates, using the current view.
void draw(const VertexBuffer &buffer, const RenderStates &states=RenderStates())
Draw a vertex buffer to the render target.
virtual ~RenderTarget()
Destructor.
void customDraw(const VertexBuffer &buffer, Span< const RenderAttributeInfo > attributes, const RenderStates &states=RenderStates())
Draw a custom vertex buffer to the render target.
void clear()
Clear the entire target.
virtual void setActive()
Activate the render target for rendering.
RangeF getAliasedLineWidthRange() const
Get the range for aliased line width.
void setCanonicalScissorBox(const Region &box)
Define the canonical scissor box.
void setScissorBox(const RectI &box)
Define the scissor box.
void setView(const View &view)
Change the current active view.
const View & getView() const
Get the view currently in use in the render target.
Definition: RenderTarget.h:318
Vector2f mapPixelToCoords(Vector2i point, const View &view) const
Convert a point from target coordinates to world coordinates.
Vector2i mapCoordsToPixel(Vector2f point) const
Convert a point from world coordinates to target coordinates, using the current view.
Vector2i mapCoordsToPixel(Vector2f point, const View &view) const
Convert a point from world coordinates to target coordinates.
RenderTarget(const RenderTarget &)=delete
Deleted copy constructor.
float getLineWidth() const
Get the line width.
Region getCanonicalViewport(const View &view) const
Get the canonical viewport of a view, applied to this render target.
virtual Vector2i getSize() const =0
Return the size of the rendering region of the target.
An OpenGL vertex and/or fragment shader.
Definition: Shader.h:119
A span.
Definition: Span.h:414
A texture for colored images.
Definition: Texture.h:313
Data in the graphics memory.
Definition: VertexBuffer.h:81
2D camera that defines what region is shown on framebuffer
Definition: View.h:94
GraphicsTag
A tag to represent various GPU resources.
Definition: GraphicsHandle.h:37
@ Framebuffer
A GPU framebuffer.
PrimitiveType
Kind of primitives to render.
Definition: PrimitiveType.h:43
RenderAttributeType
The type of an attribute.
Definition: RenderTarget.h:54
The namespace for gf classes.
static void del(int n, const unsigned *resources)
static void gen(int n, unsigned *resources)
A trait to handle creation and deletion of GPU resources.
Definition: GraphicsHandle.h:48
A half-open range of values.
Definition: Range.h:44
A region of a window.
Definition: Region.h:40
Attribute info.
Definition: RenderTarget.h:66
std::size_t offset
Offset of the attribute in the vertex.
Definition: RenderTarget.h:71
int size
Size of the attribute.
Definition: RenderTarget.h:68
RenderAttributeType type
Type of the attribute.
Definition: RenderTarget.h:69
const char * name
Name of the attribute.
Definition: RenderTarget.h:67
bool normalized
Is the attribute normalized?
Definition: RenderTarget.h:70
Define the states used for drawing to a RenderTarget.
Definition: RenderStates.h:82
A 4D vector.
Definition: Vector.h:852
A point associated with a color and a texture coordinate.
Definition: Vertex.h:75