Gamedev Framework (gf)  0.6.0
A C++11 framework for 2D games
Classes | Public Member Functions | Protected Member Functions | List of all members
gf::RenderTarget Class Referenceabstract

Base class for all render targets (window, texture, ...) More...

#include <gf/RenderTarget.h>

Inheritance diagram for gf::RenderTarget:
Inheritance graph
[legend]

Public Member Functions

 RenderTarget ()=default
 Default constructor. More...
 
virtual ~RenderTarget ()
 Destructor. More...
 
 RenderTarget (const RenderTarget &)=delete
 Deleted copy constructor. More...
 
RenderTargetoperator= (const RenderTarget &)=delete
 Deleted copy assignment. More...
 
virtual Vector2u getSize () const =0
 Return the size of the rendering region of the target. More...
 
Drawing commands
Region getCanonicalScissorBox ()
 Get the current canonical scissor box. More...
 
void setCanonicalScissorBox (const Region &box)
 Define the canonical scissor box. More...
 
RectI getScissorBox ()
 Get the current scissor box. More...
 
void setScissorBox (const RectI &box)
 Define the scissor box. More...
 
void clear (const Color4f &color)
 Clear the entire target with a single color. More...
 
void clear ()
 Clear the entire target. More...
 
RangeF getAliasedLineWidthRange () const
 Get the range for aliased line width. More...
 
float getLineWidth () const
 Get the line width. More...
 
void draw (const Vertex *vertices, std::size_t count, PrimitiveType type, const RenderStates &states=RenderStates())
 Draw primitives defined by an array of vertices. More...
 
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. More...
 
void draw (const Vertex *vertices, int *first, const std::size_t *count, std::size_t primcount, PrimitiveType type, const RenderStates &states=RenderStates())
 Draw primitives defined by an array of vertices. More...
 
void draw (const Vertex *vertices, const uint16_t **indices, const std::size_t *count, std::size_t primcount, PrimitiveType type, const RenderStates &states=RenderStates())
 Draw primitives defined by an array of vertices and their indices. More...
 
void draw (const VertexBuffer &buffer, const RenderStates &states=RenderStates())
 Draw a vertex buffer to the render target. More...
 
void draw (Drawable &drawable, const RenderStates &states=RenderStates())
 Draw a drawable object to the render target. More...
 
View management
void setView (const View &view)
 Change the current active view. More...
 
const ViewgetView () const
 Get the view currently in use in the render target. More...
 
Region getCanonicalViewport (const View &view) const
 Get the canonical viewport of a view, applied to this render target. More...
 
RectI getViewport (const View &view) const
 Get the viewport of a view, applied to this render target. More...
 
Vector2f mapPixelToCoords (Vector2i point, const View &view) const
 Convert a point from target coordinates to world coordinates. More...
 
Vector2f mapPixelToCoords (Vector2i point) const
 Convert a point from target coordinates to world coordinates, using the current view. More...
 
Vector2i mapCoordsToPixel (Vector2f point, const View &view) const
 Convert a point from world coordinates to target coordinates. More...
 
Vector2i mapCoordsToPixel (Vector2f point) const
 Convert a point from world coordinates to target coordinates, using the current view. More...
 

Protected Member Functions

void initialize ()
 Performs the common initialization step after creation. More...
 
Image captureFramebuffer (unsigned name) const
 Capture the given framebuffer. More...
 

Detailed Description

Base class for all render targets (window, texture, ...)

gf::RenderTarget defines the common behavior of all the 2D render targets usable in the graphics module. It makes it possible to draw 2D entities like sprites, shapes, text without using any OpenGL command directly.

A gf::RenderTarget is also able to use views (gf::View), which are a kind of 2D cameras. With views you can globally scroll, rotate or zoom everything that is drawn, without having to transform every single entity. See the documentation of gf::View for more details and sample pieces of code about this class.

See also
gf::RenderWindow, gf::RenderTexture, gf::View

Constructor & Destructor Documentation

◆ RenderTarget() [1/2]

gf::RenderTarget::RenderTarget ( )
default

Default constructor.

◆ ~RenderTarget()

virtual gf::RenderTarget::~RenderTarget ( )
virtual

Destructor.

◆ RenderTarget() [2/2]

gf::RenderTarget::RenderTarget ( const RenderTarget )
delete

Deleted copy constructor.

Member Function Documentation

◆ captureFramebuffer()

Image gf::RenderTarget::captureFramebuffer ( unsigned  name) const
protected

Capture the given framebuffer.

Parameters
nameThe name of the framebuffer

◆ clear() [1/2]

void gf::RenderTarget::clear ( const Color4f color)

Clear the entire target with a single color.

This function is usually called once every frame, to clear the previous contents of the target.

Parameters
colorFill color to use to clear the render target

◆ clear() [2/2]

void gf::RenderTarget::clear ( )

Clear the entire target.

This function is usually called once every frame, to clear the previous contents of the target.

The color used to clear the target is the last color passed to the other version of clear().

◆ draw() [1/6]

void gf::RenderTarget::draw ( const Vertex vertices,
std::size_t  count,
PrimitiveType  type,
const RenderStates states = RenderStates() 
)

Draw primitives defined by an array of vertices.

Parameters
verticesPointer to the vertices
countNumber of vertices in the array
typeType of primitives to draw
statesRender states to use for drawing

◆ draw() [2/6]

void gf::RenderTarget::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.

Parameters
verticesPointer to the vertices
indicesPointer to the indices
countNumber of indices in the array
typeType of primitives to draw
statesRender states to use for drawing

◆ draw() [3/6]

void gf::RenderTarget::draw ( const Vertex vertices,
int *  first,
const std::size_t *  count,
std::size_t  primcount,
PrimitiveType  type,
const RenderStates states = RenderStates() 
)

Draw primitives defined by an array of vertices.

Parameters
verticesPointer to the vertices
firstArray of starting indices
countArray of number of vertices
primcountNumber of elements in first and count
typeType of primitives to draw
statesRender states to use for drawing

◆ draw() [4/6]

void gf::RenderTarget::draw ( const Vertex vertices,
const uint16_t **  indices,
const std::size_t *  count,
std::size_t  primcount,
PrimitiveType  type,
const RenderStates states = RenderStates() 
)

Draw primitives defined by an array of vertices and their indices.

Parameters
verticesPointer to the vertices
indicesArray of pointers to the indices
countArray of number of indices
primcountNumber of elements in indices and count
typeType of primitives to draw
statesRender states to use for drawing

◆ draw() [5/6]

void gf::RenderTarget::draw ( const VertexBuffer buffer,
const RenderStates states = RenderStates() 
)

Draw a vertex buffer to the render target.

Parameters
bufferA vertex buffer containing a geometry
statesRender states to use for drawing

◆ draw() [6/6]

void gf::RenderTarget::draw ( Drawable drawable,
const RenderStates states = RenderStates() 
)

Draw a drawable object to the render target.

Parameters
drawableObject to draw
statesRender states to use for drawing

◆ getAliasedLineWidthRange()

RangeF gf::RenderTarget::getAliasedLineWidthRange ( ) const

Get the range for aliased line width.

Returns
A range for the line width
See also
setLineWidth(), getLineWidth()

◆ getCanonicalScissorBox()

Region gf::RenderTarget::getCanonicalScissorBox ( )

Get the current canonical scissor box.

Returns
The current canonical scissor box

◆ getCanonicalViewport()

Region gf::RenderTarget::getCanonicalViewport ( const View view) const

Get the canonical viewport of a view, applied to this render target.

The viewport is defined in the view as a ratio, this function simply applies this ratio to the current dimensions of the render target to calculate the pixels rectangle that the viewport actually covers in the target.

Parameters
viewThe view for which we want to compute the viewport
Returns
Canonical viewport rectangle, expressed in pixels

◆ getLineWidth()

float gf::RenderTarget::getLineWidth ( ) const

Get the line width.

Returns
The current line width
See also
setLineWidth(), getAliasedLineWidthRange()

◆ getScissorBox()

RectI gf::RenderTarget::getScissorBox ( )

Get the current scissor box.

Returns
The current scissor box

◆ getSize()

virtual Vector2u gf::RenderTarget::getSize ( ) const
pure virtual

Return the size of the rendering region of the target.

Returns
Size in pixels

Implemented in gf::RenderTexture, gf::RenderPipeline, and gf::RenderWindow.

◆ getView()

const View& gf::RenderTarget::getView ( ) const
inline

Get the view currently in use in the render target.

Returns
The view object that is currently used
See also
setView()

◆ getViewport()

RectI gf::RenderTarget::getViewport ( const View view) const

Get the viewport of a view, applied to this render target.

The viewport is defined in the view as a ratio, this function simply applies this ratio to the current dimensions of the render target to calculate the pixels rectangle that the viewport actually covers in the target.

Parameters
viewThe view for which we want to compute the viewport
Returns
Viewport rectangle, expressed in pixels

◆ initialize()

void gf::RenderTarget::initialize ( )
protected

Performs the common initialization step after creation.

The derived classes must call this function after the target is created and ready for drawing.

◆ mapCoordsToPixel() [1/2]

Vector2i gf::RenderTarget::mapCoordsToPixel ( Vector2f  point,
const View view 
) const

Convert a point from world coordinates to target coordinates.

This function finds the pixel of the render target that matches the given 2D point. In other words, it goes through the same process as the graphics card, to compute the final position of a rendered point.

Initially, both coordinate systems (world units and target pixels) match perfectly. But if you define a custom view or resize your render target, this assertion is not true anymore, i.e. a point located at \((150, 75)\) in your 2D world may map to the pixel \((10, 50)\) of your render target – if the view is translated by \((140, 25)\).

This version uses a custom view for calculations, see the other overload of the function if you want to use the current view of the render target.

Parameters
pointPoint to convert
viewThe view to use for converting the point
Returns
The converted point, in target coordinates (pixels)
See also
mapPixelToCoords()

◆ mapCoordsToPixel() [2/2]

Vector2i gf::RenderTarget::mapCoordsToPixel ( Vector2f  point) const

Convert a point from world coordinates to target coordinates, using the current view.

This function is an overload of the mapCoordsToPixel function that implicitly uses the current view.

It is equivalent to:

target.mapCoordsToPixel(point, target.getView());
Parameters
pointPoint to convert
Returns
The converted point, in target coordinates (pixels)
See also
mapPixelToCoords()

◆ mapPixelToCoords() [1/2]

Vector2f gf::RenderTarget::mapPixelToCoords ( Vector2i  point,
const View view 
) const

Convert a point from target coordinates to world coordinates.

This function finds the 2D position that matches the given pixel of the render target. In other words, it does the inverse of what the graphics card does, to find the initial position of a rendered pixel.

Initially, both coordinate systems (world units and target pixels) match perfectly. But if you define a custom view or resize your render target, this assertion is not true anymore, i.e. a point located at \((10, 50)\) in your render target may map to the point \((150, 75)\) in your 2D world – if the view is translated by \((140, 25)\).

For windows, this function is typically used to find which point (or object) is located below the mouse cursor.

This version uses a custom view for calculations, see the other overload of the function if you want to use the current view of the render target.

Parameters
pointPixel to convert
viewThe view to use for converting the point
Returns
The converted point, in world coordinates
See also
mapCoordsToPixel()

◆ mapPixelToCoords() [2/2]

Vector2f gf::RenderTarget::mapPixelToCoords ( Vector2i  point) const

Convert a point from target coordinates to world coordinates, using the current view.

This function is an overload of the mapPixelToCoords() function that implicitly uses the current view.

It is equivalent to:

target.mapPixelToCoords(point, target.getView());
Parameters
pointPixel to convert
Returns
The converted point, in world coordinates
See also
mapCoordsToPixel()

◆ operator=()

RenderTarget& gf::RenderTarget::operator= ( const RenderTarget )
delete

Deleted copy assignment.

◆ setCanonicalScissorBox()

void gf::RenderTarget::setCanonicalScissorBox ( const Region box)

Define the canonical scissor box.

Parameters
boxThe new canonical scissor box

◆ setScissorBox()

void gf::RenderTarget::setScissorBox ( const RectI box)

Define the scissor box.

Parameters
boxThe new scissor box

◆ setView()

void gf::RenderTarget::setView ( const View view)

Change the current active view.

The view is like a 2D camera, it controls which part of the 2D scene is visible, and how it is viewed in the render target.

The new view will affect everything that is drawn, until another view is set.

The render target keeps its own copy of the view object, so it is not necessary to keep the original one alive after calling this function.

Parameters
viewNew view to use
See also
getView()