FifeGUI 0.2.0
A C++ GUI library designed for games.
fcn::sdl2::Graphics Class Reference

#include <graphics.hpp>

Inheritance diagram for fcn::sdl2::Graphics:
fcn::Graphics

Public Member Functions

void _beginDraw () override
void _endDraw () override
std::shared_ptr< FontcreateFont (std::string const &filename, int size) override
void drawBezier (PointVector const &points, int segments, unsigned int width) override
void drawCircle (Point const &center, unsigned int radius) override
void drawCircleSegment (Point const &center, unsigned int radius, int startAngle, int endAngle) override
void drawFillCircle (Point const &center, unsigned int radius) override
void drawFillCircleSegment (Point const &center, unsigned int radius, int startAngle, int endAngle) override
void drawImage (fcn::Image const *image, int srcX, int srcY, int dstX, int dstY, int width, int height) override
void drawLine (int x1, int y1, int x2, int y2) override
void drawLine (int x1, int y1, int x2, int y2, unsigned int width) override
void drawPoint (int x, int y) override
void drawPolyLine (PointVector const &points, unsigned int width) override
void drawRectangle (Rectangle const &rectangle) override
void drawRoundStroke (int x1, int y1, int x2, int y2, unsigned int width) override
virtual void drawSDLTexture (SDL_Texture *texture, SDL_Rect source, SDL_Rect destination)
void fillRectangle (Rectangle const &rectangle) override
Color const & getColor () const override
virtual SDL_Renderer * getRenderTarget () const
 Graphics (Graphics &&)=delete
 Graphics (Graphics const &)=delete
Graphics & operator= (Graphics &&)=delete
Graphics & operator= (Graphics const &)=delete
void popClipArea () override
bool pushClipArea (fcn::Rectangle area) override
void setColor (Color const &color) override
virtual void setTarget (SDL_Renderer *renderer, int width, int height)
Public Member Functions inherited from fcn::Graphics
virtual void drawImage (Image const *image, int dstX, int dstY)
void drawRectangle (int x, int y, int width, int height)
void drawText (std::string const &text, int x, int y)
virtual void drawText (std::string const &text, int x, int y, Alignment alignment)
void fillRectangle (int x, int y, int width, int height)
virtual ClipRectangle const & getCurrentClipArea ()
 Graphics (Graphics &&)=delete
 Graphics (Graphics const &)=delete
Graphics & operator= (Graphics &&)=delete
Graphics & operator= (Graphics const &)=delete
virtual void setFont (Font *font)

Protected Member Functions

void drawHorizontalLine (int x1, int y, int x2)
void drawVerticalLine (int x, int y1, int y2)
void restoreRenderColor ()
void saveRenderColor ()

Protected Attributes

Uint8 a {}
Uint8 b {}
Uint8 g {}
bool mAlpha
Color mColor
int mHeight = 0
SDL_Renderer * mRenderTarget {}
int mWidth = 0
Uint8 r {}
Protected Attributes inherited from fcn::Graphics
std::stack< ClipRectanglemClipStack
FontmFont {nullptr}

Additional Inherited Members

Public Types inherited from fcn::Graphics
enum class  Alignment : uint8_t { Left = 0 , Center , Right }

Detailed Description

SDL2 renderer-specific implementation of the Graphics interface.

This is the primary graphics backend - it uses SDL_Renderer for all drawing operations.

Definition at line 33 of file backends/sdl2/graphics.hpp.

Constructor & Destructor Documentation

◆ Graphics()

fcn::sdl2::Graphics::Graphics ( )

Definition at line 27 of file backends/sdl2/graphics.cpp.

◆ ~Graphics()

fcn::sdl2::Graphics::~Graphics ( )
overridevirtualdefault

Reimplemented from fcn::Graphics.

Member Function Documentation

◆ _beginDraw()

void fcn::sdl2::Graphics::_beginDraw ( )
overridevirtual

Initializes drawing.

Called by the Gui when Gui::draw() is called. It is needed by some implementations of Graphics to perform preparations before drawing. An example of such an implementation is the OpenGLGraphics.

NOTE: You will never need to call this function yourself, unless you use a Graphics object outside the library.

See also
_endDraw, Gui::draw

Reimplemented from fcn::Graphics.

Definition at line 31 of file backends/sdl2/graphics.cpp.

References _beginDraw(), fcn::Rectangle::height, mHeight, mWidth, pushClipArea(), fcn::Rectangle::width, fcn::Rectangle::x, and fcn::Rectangle::y.

Referenced by _beginDraw().

◆ _endDraw()

void fcn::sdl2::Graphics::_endDraw ( )
overridevirtual

Deinitializes the drawing process.

Called by the GUI when Gui::draw() is complete. It should reset any state changes made by _beginDraw().

NOTE: You generally won't need to call this function yourself unless you are using a Graphics object outside of the library.

See also
_beginDraw, Gui::draw

Reimplemented from fcn::Graphics.

Definition at line 41 of file backends/sdl2/graphics.cpp.

References _endDraw(), and popClipArea().

Referenced by _endDraw().

◆ createFont()

std::shared_ptr< Font > fcn::sdl2::Graphics::createFont ( std::string const & filename,
int size )
overridevirtual

Creates a font for this graphics backend.

Backends that do not provide runtime font loading may return nullptr.

Parameters
filenamePath to the font file.
sizeRequested point size.
Returns
Shared pointer to a created font, or nullptr.

Reimplemented from fcn::Graphics.

Definition at line 670 of file backends/sdl2/graphics.cpp.

References createFont().

Referenced by createFont().

◆ drawBezier()

void fcn::sdl2::Graphics::drawBezier ( PointVector const & points,
int steps,
unsigned int width )
overridevirtual

Draws a bezier curve.

Parameters
pointsThe coordinates as points in a vector.
stepsThe steps for each line between two points.
widthThe line width.

Implements fcn::Graphics.

Definition at line 612 of file backends/sdl2/graphics.cpp.

References drawBezier(), drawLine(), fcn::Graphics::mClipStack, mColor, mRenderTarget, restoreRenderColor(), and saveRenderColor().

Referenced by drawBezier().

◆ drawCircle()

void fcn::sdl2::Graphics::drawCircle ( Point const & p,
unsigned int radius )
overridevirtual

Draws a simple, non-filled circle with a one pixel width.

Parameters
pThe circle center coordinate as point.
radiusThe circle radius.

Implements fcn::Graphics.

Definition at line 498 of file backends/sdl2/graphics.cpp.

References drawCircle(), fcn::Graphics::mClipStack, mColor, mRenderTarget, restoreRenderColor(), saveRenderColor(), fcn::ClipRectangle::xOffset, and fcn::ClipRectangle::yOffset.

Referenced by drawCircle().

◆ drawCircleSegment()

void fcn::sdl2::Graphics::drawCircleSegment ( Point const & p,
unsigned int radius,
int sangle,
int eangle )
overridevirtual

Draws a simple, non-filled circle segment with a one pixel width.

Note: The start angle must be less than the end angle. 0 angle is right side.

Parameters
pThe circle center coordinate as point.
radiusThe circle radius.
sangleThe start angle of the segment.
eangleThe end angle of the segment.

Implements fcn::Graphics.

Definition at line 540 of file backends/sdl2/graphics.cpp.

References drawCircleSegment(), fcn::Graphics::mClipStack, mColor, mRenderTarget, restoreRenderColor(), saveRenderColor(), fcn::ClipRectangle::xOffset, and fcn::ClipRectangle::yOffset.

Referenced by drawCircleSegment().

◆ drawFillCircle()

void fcn::sdl2::Graphics::drawFillCircle ( Point const & p,
unsigned int radius )
overridevirtual

Draws a filled circle.

Parameters
pThe circle center coordinate as point.
radiusThe circle radius.

Implements fcn::Graphics.

Definition at line 397 of file backends/sdl2/graphics.cpp.

References drawFillCircle(), fcn::Graphics::mClipStack, mColor, mRenderTarget, restoreRenderColor(), saveRenderColor(), fcn::ClipRectangle::xOffset, and fcn::ClipRectangle::yOffset.

Referenced by drawFillCircle().

◆ drawFillCircleSegment()

void fcn::sdl2::Graphics::drawFillCircleSegment ( Point const & p,
unsigned int radius,
int sangle,
int eangle )
overridevirtual

Draws a filled circle segment.

Note: The start angle must be less than the end angle. 0 angle is right side.

Parameters
pThe circle center coordinate as point.
radiusThe circle radius.
sangleThe start angle of the segment.
eangleThe end angle of the segment.

Implements fcn::Graphics.

Definition at line 446 of file backends/sdl2/graphics.cpp.

References drawFillCircleSegment(), fcn::Graphics::mClipStack, mColor, mRenderTarget, restoreRenderColor(), saveRenderColor(), fcn::ClipRectangle::xOffset, and fcn::ClipRectangle::yOffset.

Referenced by drawFillCircleSegment().

◆ drawHorizontalLine()

void fcn::sdl2::Graphics::drawHorizontalLine ( int x1,
int y,
int x2 )
protected

Draws a horizontal line.

Parameters
x1the start coordinate of the line.
ythe y coordinate of the line.
x2the end coordinate of the line.

Definition at line 180 of file backends/sdl2/graphics.cpp.

References drawHorizontalLine(), fcn::Rectangle::height, fcn::Graphics::mClipStack, mColor, mRenderTarget, restoreRenderColor(), saveRenderColor(), fcn::Rectangle::width, fcn::Rectangle::x, fcn::ClipRectangle::xOffset, fcn::Rectangle::y, and fcn::ClipRectangle::yOffset.

Referenced by drawHorizontalLine(), and drawRectangle().

◆ drawImage()

void fcn::sdl2::Graphics::drawImage ( fcn::Image const * image,
int srcX,
int srcY,
int dstX,
int dstY,
int width,
int height )
overridevirtual

Draws a part of an image.

NOTE: Width and height arguments will not scale the image but specifies the size of the part to be drawn. If you want to draw the whole image there is a simplified version of this function.

EXAMPLE:

drawImage(myImage, 10, 10, 20, 20, 40, 40);
void drawImage(fcn::Image const *image, int srcX, int srcY, int dstX, int dstY, int width, int height) override
Draws a part of an image.

Will draw a rectangular piece of myImage starting at coordinate (10, 10) in myImage, with width and height 40. The piece will be drawn with it's top left corner at coordinate (20, 20).

Parameters
imageThe image to draw.
srcXThe source image x coordinate.
srcYThe source image y coordinate.
dstXThe destination x coordinate.
dstYThe destination y coordinate.
widthThe width of the piece.
heightThe height of the piece.

Implements fcn::Graphics.

Definition at line 95 of file backends/sdl2/graphics.cpp.

References drawImage(), fcn::sdl2::Image::getTexture(), fcn::Graphics::mClipStack, mRenderTarget, fcn::ClipRectangle::xOffset, and fcn::ClipRectangle::yOffset.

Referenced by drawImage().

◆ drawLine() [1/2]

void fcn::sdl2::Graphics::drawLine ( int x1,
int y1,
int x2,
int y2 )
overridevirtual

Draws a line.

Parameters
x1The first x coordinate.
y1The first y coordinate.
x2The second x coordinate.
y2The second y coordinate.

Implements fcn::Graphics.

Definition at line 280 of file backends/sdl2/graphics.cpp.

References drawLine(), fcn::Graphics::mClipStack, mColor, mRenderTarget, restoreRenderColor(), saveRenderColor(), fcn::ClipRectangle::xOffset, and fcn::ClipRectangle::yOffset.

Referenced by drawBezier(), drawLine(), drawLine(), drawPolyLine(), and drawRoundStroke().

◆ drawLine() [2/2]

void fcn::sdl2::Graphics::drawLine ( int x1,
int y1,
int x2,
int y2,
unsigned int width )
overridevirtual

Draws a thick line.

Parameters
x1The first x coordinate.
y1The first y coordinate.
x2The second x coordinate.
y2The second y coordinate.
widthThe line width.

Implements fcn::Graphics.

Definition at line 301 of file backends/sdl2/graphics.cpp.

References drawLine(), drawRoundStroke(), fcn::Graphics::mClipStack, mColor, mRenderTarget, restoreRenderColor(), saveRenderColor(), fcn::ClipRectangle::xOffset, and fcn::ClipRectangle::yOffset.

◆ drawPoint()

void fcn::sdl2::Graphics::drawPoint ( int x,
int y )
overridevirtual

Draws a single point/pixel.

Parameters
xThe x coordinate.
yThe y coordinate.

Implements fcn::Graphics.

Definition at line 157 of file backends/sdl2/graphics.cpp.

References drawPoint(), fcn::Rectangle::isContaining(), fcn::Graphics::mClipStack, mColor, mRenderTarget, restoreRenderColor(), saveRenderColor(), fcn::ClipRectangle::xOffset, and fcn::ClipRectangle::yOffset.

Referenced by drawPoint().

◆ drawPolyLine()

void fcn::sdl2::Graphics::drawPolyLine ( PointVector const & points,
unsigned int width )
overridevirtual

Draws lines between points with given width.

Parameters
pointsContains the points that are used for drawing.
widthThe line width.

Implements fcn::Graphics.

Definition at line 636 of file backends/sdl2/graphics.cpp.

References drawLine(), drawPolyLine(), fcn::Graphics::mClipStack, mColor, mRenderTarget, restoreRenderColor(), and saveRenderColor().

Referenced by drawPolyLine().

◆ drawRectangle()

void fcn::sdl2::Graphics::drawRectangle ( Rectangle const & rectangle)
overridevirtual

Draws a simple, non-filled rectangle with a one pixel width.

Parameters
rectangleThe rectangle to draw.

Implements fcn::Graphics.

Definition at line 266 of file backends/sdl2/graphics.cpp.

References drawHorizontalLine(), drawRectangle(), drawVerticalLine(), fcn::Rectangle::height, fcn::Rectangle::width, fcn::Rectangle::x, and fcn::Rectangle::y.

Referenced by drawRectangle().

◆ drawRoundStroke()

void fcn::sdl2::Graphics::drawRoundStroke ( int x1,
int y1,
int x2,
int y2,
unsigned int width )
overridevirtual

Draws a round brush stroke along the line segment.

Unlike drawLine(..., width), this uses a round brush footprint and therefore produces round caps and a softer joint shape.

Backends that do not provide a dedicated implementation fall back to drawLine(..., width).

Parameters
x1The first x coordinate.
y1The first y coordinate.
x2The second x coordinate.
y2The second y coordinate.
widthThe brush width.

Reimplemented from fcn::Graphics.

Definition at line 345 of file backends/sdl2/graphics.cpp.

References drawLine(), drawRoundStroke(), fcn::Graphics::mClipStack, mColor, mRenderTarget, restoreRenderColor(), saveRenderColor(), fcn::ClipRectangle::xOffset, and fcn::ClipRectangle::yOffset.

Referenced by drawLine(), and drawRoundStroke().

◆ drawSDLTexture()

void fcn::sdl2::Graphics::drawSDLTexture ( SDL_Texture * texture,
SDL_Rect source,
SDL_Rect destination )
virtual

Draws an SDL_Texture on the target surface.

NOTE: The clip areas will be taken into account.

Definition at line 675 of file backends/sdl2/graphics.cpp.

References drawSDLTexture(), fcn::Graphics::mClipStack, mRenderTarget, fcn::ClipRectangle::xOffset, and fcn::ClipRectangle::yOffset.

Referenced by drawSDLTexture().

◆ drawVerticalLine()

void fcn::sdl2::Graphics::drawVerticalLine ( int x,
int y1,
int y2 )
protected

Draws a vertical line.

Parameters
xthe x coordinate of the line.
y1the start coordinate of the line.
y2the end coordinate of the line.

Definition at line 223 of file backends/sdl2/graphics.cpp.

References drawVerticalLine(), fcn::Rectangle::height, fcn::Graphics::mClipStack, mColor, mRenderTarget, restoreRenderColor(), saveRenderColor(), fcn::Rectangle::width, fcn::Rectangle::x, fcn::ClipRectangle::xOffset, fcn::Rectangle::y, and fcn::ClipRectangle::yOffset.

Referenced by drawRectangle(), and drawVerticalLine().

◆ fillRectangle()

void fcn::sdl2::Graphics::fillRectangle ( Rectangle const & rectangle)
overridevirtual

◆ getColor()

Color const & fcn::sdl2::Graphics::getColor ( ) const
overridevirtual

Gets the color to use when drawing.

Returns
The color used when drawing.
See also
setColor

Implements fcn::Graphics.

Definition at line 665 of file backends/sdl2/graphics.cpp.

References getColor(), and mColor.

Referenced by getColor().

◆ getRenderTarget()

SDL_Renderer * fcn::sdl2::Graphics::getRenderTarget ( ) const
virtual

Gets the target SDL_Renderer.

Returns
the target SDL_Renderer.

Definition at line 90 of file backends/sdl2/graphics.cpp.

References getRenderTarget(), and mRenderTarget.

Referenced by getRenderTarget().

◆ popClipArea()

void fcn::sdl2::Graphics::popClipArea ( )
overridevirtual

Removes the top most clip area from the stack.

Exceptions
Exceptionif the stack is empty.

Reimplemented from fcn::Graphics.

Definition at line 71 of file backends/sdl2/graphics.cpp.

References fcn::Rectangle::height, fcn::Graphics::mClipStack, mRenderTarget, fcn::Graphics::popClipArea(), popClipArea(), fcn::Rectangle::width, fcn::Rectangle::x, and fcn::Rectangle::y.

Referenced by _endDraw(), and popClipArea().

◆ pushClipArea()

bool fcn::sdl2::Graphics::pushClipArea ( fcn::Rectangle area)
overridevirtual

Pushes a clip area onto the stack.

The x and y coordinates in the rectangle is relative to the last pushed clip area. If the new area falls outside the current clip area, it will be clipped as necessary.

If a clip area is outside of the top clip area a clip area with zero width and height will be pushed.

Parameters
areaThe clip area to be pushed onto the stack.
Returns
False if the the new area lays outside the current clip area.

Reimplemented from fcn::Graphics.

Definition at line 53 of file backends/sdl2/graphics.cpp.

References fcn::Rectangle::height, fcn::Graphics::mClipStack, mRenderTarget, fcn::Graphics::pushClipArea(), pushClipArea(), fcn::Rectangle::width, fcn::Rectangle::x, and fcn::Rectangle::y.

Referenced by _beginDraw(), and pushClipArea().

◆ restoreRenderColor()

void fcn::sdl2::Graphics::restoreRenderColor ( )
protected

◆ saveRenderColor()

void fcn::sdl2::Graphics::saveRenderColor ( )
protected

Save the current rendering color before drawing.

Does not affect the mColor attribute.

Definition at line 693 of file backends/sdl2/graphics.cpp.

References a, b, g, mRenderTarget, r, and saveRenderColor().

Referenced by drawBezier(), drawCircle(), drawCircleSegment(), drawFillCircle(), drawFillCircleSegment(), drawHorizontalLine(), drawLine(), drawLine(), drawPoint(), drawPolyLine(), drawRoundStroke(), drawVerticalLine(), fillRectangle(), and saveRenderColor().

◆ setColor()

void fcn::sdl2::Graphics::setColor ( Color const & color)
overridevirtual

Sets the color to use when drawing.

Parameters
colorA color.
See also
getColor

Implements fcn::Graphics.

Definition at line 658 of file backends/sdl2/graphics.cpp.

References fcn::Color::a, mAlpha, mColor, and setColor().

Referenced by setColor().

◆ setTarget()

void fcn::sdl2::Graphics::setTarget ( SDL_Renderer * renderer,
int width,
int height )
virtual

Sets the target SDL_Renderer to use for drawing.

Preferably done only once.

Parameters
rendererthe SDL_Renderer to use for drawing.
widthscreen width
heightscreen height

Definition at line 46 of file backends/sdl2/graphics.cpp.

References mHeight, mRenderTarget, mWidth, and setTarget().

Referenced by setTarget().

Member Data Documentation

◆ a

Uint8 fcn::sdl2::Graphics::a {}
protected

Previous alpha component from renderer.

Definition at line 146 of file backends/sdl2/graphics.hpp.

Referenced by restoreRenderColor(), and saveRenderColor().

◆ b

Uint8 fcn::sdl2::Graphics::b {}
protected

Previous blue component from renderer.

Definition at line 144 of file backends/sdl2/graphics.hpp.

Referenced by restoreRenderColor(), and saveRenderColor().

◆ g

Uint8 fcn::sdl2::Graphics::g {}
protected

Previous green component from renderer.

Definition at line 142 of file backends/sdl2/graphics.hpp.

Referenced by restoreRenderColor(), and saveRenderColor().

◆ mAlpha

bool fcn::sdl2::Graphics::mAlpha
protected

Whether alpha blending is enabled.

Definition at line 149 of file backends/sdl2/graphics.hpp.

Referenced by setColor().

◆ mColor

◆ mHeight

int fcn::sdl2::Graphics::mHeight = 0
protected

Screen height.

Definition at line 133 of file backends/sdl2/graphics.hpp.

Referenced by _beginDraw(), and setTarget().

◆ mRenderTarget

◆ mWidth

int fcn::sdl2::Graphics::mWidth = 0
protected

Screen width.

Definition at line 130 of file backends/sdl2/graphics.hpp.

Referenced by _beginDraw(), and setTarget().

◆ r

Uint8 fcn::sdl2::Graphics::r {}
protected

Cached renderer color components (previous renderer color).

Previous red component from renderer.

Definition at line 140 of file backends/sdl2/graphics.hpp.

Referenced by restoreRenderColor(), and saveRenderColor().


The documentation for this class was generated from the following files: