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

Base class for textured shapes with outline. More...

#include <gf/Shape.h>

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

Public Member Functions

 Shape ()
 Default constructor. More...
 
void setTexture (const Texture &texture, bool resetRect=false)
 Change the source texture of the shape. More...
 
const TexturegetTexture () const
 Get the source texture of the shape. More...
 
bool hasTexture () const
 Check if a texture is set. More...
 
void unsetTexture ()
 Unset the source texture of the shape. More...
 
void setTextureRect (const RectF &rect)
 Set the sub-rectangle of the texture that the shape will display. More...
 
const RectFgetTextureRect () const
 Get the sub-rectangle of the texture displayed by the shape. More...
 
void setColor (const Color4f &color)
 Set the fill color of the shape. More...
 
const Color4fgetColor () const
 Get the fill color of the shape. More...
 
void setOutlineColor (const Color4f &color)
 Set the outline color of the shape. More...
 
const Color4fgetOutlineColor () const
 Get the outline color of the shape. More...
 
void setOutlineThickness (float thickness)
 Set the thickness of the shape's outline. More...
 
float getOutlineThickness () const
 Get the outline thickness of the shape. More...
 
virtual std::size_t getPointCount () const =0
 Get the total number of points of the shape. More...
 
virtual Vector2f getPoint (std::size_t index) const =0
 Get a point of the shape. More...
 
RectF getLocalBounds () const
 Get the local bounding rectangle of the entity. More...
 
void setAnchor (Anchor anchor)
 Set the anchor origin of the entity. More...
 
VertexBuffer commitGeometry () const
 Create a buffer with the current geometry. More...
 
VertexBuffer commitOutlineGeometry () const
 Create a buffer with the current outline geometry. More...
 
virtual void draw (RenderTarget &target, RenderStates states) override
 Draw the object to a render target. More...
 
- Public Member Functions inherited from gf::Transformable
 Transformable ()
 Default constructor. More...
 
void setOrigin (Vector2f origin)
 Set the local origin of the object. More...
 
Vector2f getOrigin () const
 Get the local origin of the object. More...
 
void setPosition (Vector2f position)
 Set the position of the object. More...
 
Vector2f getPosition () const
 Get the position of the object. More...
 
void move (Vector2f offset)
 Move the object by a given offset. More...
 
void setRotation (float angle)
 Set the orientation of the object. More...
 
float getRotation () const
 Get the orientation of the object. More...
 
void rotate (float angle)
 Rotate the object. More...
 
void setScale (Vector2f factors)
 Set the scale factors of the object. More...
 
void setScale (float factor)
 Set the scale factor of the object. More...
 
Vector2f getScale () const
 Get the current scale of the object. More...
 
void scale (Vector2f factors)
 Scale the object. More...
 
void scale (float factor)
 Scale the object. More...
 
Matrix3f getTransform () const
 Get the combined transform of the object. More...
 
Matrix3f getInverseTransform () const
 Get the inverse of the combined transform of the object. More...
 
- Public Member Functions inherited from gf::Drawable
virtual ~Drawable ()
 Virtual desctructor. More...
 

Protected Member Functions

void updateGeometry ()
 Recompute the internal geometry of the shape. More...
 
- Protected Member Functions inherited from gf::Transformable
void setOriginFromAnchorAndBounds (Anchor anchor, const RectF &bounds)
 Set the origin from an anchor and bounds. More...
 

Detailed Description

Base class for textured shapes with outline.

gf::Shape is a drawable class that allows to define and display a custom convex shape on a render target. It's only an abstract base, it needs to be specialized for concrete types of shapes (circle, rectangle, convex polygon, star, ...).

In addition to the attributes provided by the specialized shape classes, a shape always has the following attributes:

Each feature is optional, and can be disabled easily:

You can write your own derived shape class, there are only two virtual functions to override:

See also
gf::RectangleShape, gf::CircleShape, gf::ConvexShape
gf::Transformable

Constructor & Destructor Documentation

◆ Shape()

gf::Shape::Shape ( )

Default constructor.

Member Function Documentation

◆ commitGeometry()

VertexBuffer gf::Shape::commitGeometry ( ) const

Create a buffer with the current geometry.

The geometry is uploaded in the graphics memory so that it's faster to draw.

Returns
A buffer with the current geometry

◆ commitOutlineGeometry()

VertexBuffer gf::Shape::commitOutlineGeometry ( ) const

Create a buffer with the current outline geometry.

The geometry is uploaded in the graphics memory so that it's faster to draw.

Returns
A buffer with the current outline geometry

◆ draw()

virtual void gf::Shape::draw ( RenderTarget target,
RenderStates  states 
)
overridevirtual

Draw the object to a render target.

This is a pure virtual function that has to be implemented by the derived class to define how the drawable should be drawn.

Parameters
targetRender target to draw to
statesCurrent render states

Implements gf::Drawable.

◆ getColor()

const Color4f& gf::Shape::getColor ( ) const
inline

Get the fill color of the shape.

Returns
Fill color of the shape
See also
setColor()

◆ getLocalBounds()

RectF gf::Shape::getLocalBounds ( ) const

Get the local bounding rectangle of the entity.

The returned rectangle is in local coordinates, which means that it ignores the transformations (translation, rotation, scale, ...) that are applied to the entity. In other words, this function returns the bounds of the entity in the entity's coordinate system.

Returns
Local bounding rectangle of the entity

◆ getOutlineColor()

const Color4f& gf::Shape::getOutlineColor ( ) const
inline

Get the outline color of the shape.

Returns
Outline color of the shape
See also
setOutlineColor()

◆ getOutlineThickness()

float gf::Shape::getOutlineThickness ( ) const
inline

Get the outline thickness of the shape.

Returns
Outline thickness of the shape
See also
setOutlineThickness()

◆ getPoint()

virtual Vector2f gf::Shape::getPoint ( std::size_t  index) const
pure virtual

Get a point of the shape.

The returned point is in local coordinates, that is, the shape's transforms (position, rotation, scale) are not taken into account.

The result is undefined if index is out of the valid range.

Parameters
indexIndex of the point to get, in range \( [0, n-1] \) where \( n \) is the number of points of the shape.
Returns
index-th point of the shape
See also
getPointCount()

Implemented in gf::RoundedRectangleShape, gf::StarShape, gf::ConvexShape, gf::CircleShape, and gf::RectangleShape.

◆ getPointCount()

virtual std::size_t gf::Shape::getPointCount ( ) const
pure virtual

Get the total number of points of the shape.

Returns
Number of points of the shape
See also
getPoint()

Implemented in gf::RoundedRectangleShape, gf::StarShape, gf::ConvexShape, gf::CircleShape, and gf::RectangleShape.

◆ getTexture()

const Texture& gf::Shape::getTexture ( ) const
inline

Get the source texture of the shape.

The returned reference is const, which means that you can't modify the texture when you retrieve it with this function.

Returns
Reference to the shape's texture
See also
setTexture()

◆ getTextureRect()

const RectF& gf::Shape::getTextureRect ( ) const
inline

Get the sub-rectangle of the texture displayed by the shape.

Returns
Texture rectangle of the shape
See also
setTextureRect()

◆ hasTexture()

bool gf::Shape::hasTexture ( ) const
inline

Check if a texture is set.

Returns
True if a texture is already set
See also
setTexture(), getTexture()

◆ setAnchor()

void gf::Shape::setAnchor ( Anchor  anchor)

Set the anchor origin of the entity.

Compute the origin of the entity based on the local bounds and the specified anchor. Internally, this function calls Transformable::setOrigin().

Parameters
anchorThe anchor of the entity
See also
getLocalBounds(), Transformable::setOrigin()

◆ setColor()

void gf::Shape::setColor ( const Color4f color)

Set the fill color of the shape.

This color is modulated (multiplied) with the shape's texture if any. It can be used to colorize the shape, or change its global opacity.

You can use gf::Color::Transparent to make the inside of the shape transparent, and have the outline alone.

By default, the shape's fill color is opaque white.

Parameters
colorNew color of the shape
See also
getColor(), setOutlineColor()

◆ setOutlineColor()

void gf::Shape::setOutlineColor ( const Color4f color)

Set the outline color of the shape.

By default, the shape's outline color is opaque white.

Parameters
colorNew outline color of the shape
See also
getOutlineColor(), setColor()

◆ setOutlineThickness()

void gf::Shape::setOutlineThickness ( float  thickness)

Set the thickness of the shape's outline.

By default, the outline thickness is \( 0 \).

Parameters
thicknessNew outline thickness
See also
getOutlineThickness()

◆ setTexture()

void gf::Shape::setTexture ( const Texture texture,
bool  resetRect = false 
)

Change the source texture of the shape.

The texture must exist as long as the shape uses it. Indeed, the shape doesn't store its own copy of the texture, but rather keeps a pointer to the one that you passed to this function. If the source texture is destroyed and the shape tries to use it, the behavior is undefined.

If resetRect is true, the texture rect property of the shape is automatically adjusted to the size of the new texture. If it is false, the texture rect is left unchanged.

Parameters
textureNew texture
resetRectShould the texture rect be reset to the size of the new texture?
See also
getTexture(), setTextureRect()

◆ setTextureRect()

void gf::Shape::setTextureRect ( const RectF rect)

Set the sub-rectangle of the texture that the shape will display.

The texture rect is useful when you don't want to display the whole texture, but rather a part of it.

By default, the texture rect covers the entire texture.

Parameters
rectRectangle defining the region of the texture to display
See also
getTextureRect(), setTexture()

◆ unsetTexture()

void gf::Shape::unsetTexture ( )

Unset the source texture of the shape.

After a call to this function, the shape has no source texture.

See also
setTexture()

◆ updateGeometry()

void gf::Shape::updateGeometry ( )
protected

Recompute the internal geometry of the shape.

This function must be called by the derived class everytime the shape's points change (i.e. the result of either getPointCount() or getPoint() is different).