ezgl  1.0.1
An Easy Graphics & GUI Library
Public Member Functions | Static Public Member Functions | Protected Types | Protected Member Functions | Friends | List of all members
ezgl::renderer Class Reference

Provides functions to draw primitives (e.g., lines, shapes) to a rendering context (the MainCanvas). More...

#include <graphics.hpp>

Public Member Functions

void set_coordinate_system (t_coordinate_system new_coordinate_system)
 Change the current coordinate system. More...
 
void set_visible_world (rectangle new_world)
 Set the visible bounds of the world. More...
 
rectangle get_visible_world ()
 Get the current visible bounds of the world. More...
 
rectangle get_visible_screen ()
 Get the current visible bounds of the screen in pixel coordinates. More...
 
rectangle world_to_screen (const rectangle &box)
 Get the screen coordinates (pixel locations) of the world coordinate rectangle box. More...
 
void set_color (color new_color)
 Change the color for subsequent draw calls. More...
 
void set_color (color new_color, uint_fast8_t alpha)
 Change the color for subsequent draw calls. More...
 
void set_color (uint_fast8_t red, uint_fast8_t green, uint_fast8_t blue, uint_fast8_t alpha=255)
 Change the color for subsequent draw calls. More...
 
void set_line_cap (line_cap cap)
 Change how line endpoints will be rendered in subsequent draw calls. More...
 
void set_line_dash (line_dash dash)
 Change the dash style of the line for subsequent draw calls. More...
 
void set_line_width (int width)
 Set the line width. More...
 
void set_font_size (double new_size)
 Change the font size. More...
 
void format_font (std::string const &family, font_slant slant, font_weight weight)
 Change the font. More...
 
void format_font (std::string const &family, font_slant slant, font_weight weight, double new_size)
 Change the font. More...
 
void set_text_rotation (double degrees)
 set the rotation_angle at which subsequent text drawing should render. More...
 
void set_horiz_justification (justification horiz_just)
 set horizontal justification; used for text and surfaces. More...
 
void set_vert_justification (justification vert_just)
 set vertical justification; used for text and surfaces. More...
 
void draw_line (point2d start, point2d end)
 Draw a line. More...
 
void draw_rectangle (point2d start, point2d end)
 Draw the outline a rectangle. More...
 
void draw_rectangle (point2d start, double width, double height)
 Draw the outline of a rectangle. More...
 
void draw_rectangle (rectangle r)
 Draw the outline of a rectangle. More...
 
void fill_rectangle (point2d start, point2d end)
 Draw a filled in rectangle. More...
 
void fill_rectangle (point2d start, double width, double height)
 Draw a filled in rectangle. More...
 
void fill_rectangle (rectangle r)
 Draw a filled in rectangle. More...
 
void fill_poly (std::vector< point2d > const &points)
 Draw a filled polygon The polygon can have an arbitrary shape and be convex or non-convex, but must be simple (no holes). More...
 
void draw_elliptic_arc (point2d center, double radius_x, double radius_y, double start_angle, double extent_angle)
 Draw the outline of an elliptic arc. More...
 
void draw_arc (point2d center, double radius, double start_angle, double extent_angle)
 Draw the outline of an arc. More...
 
void fill_elliptic_arc (point2d center, double radius_x, double radius_y, double start_angle, double extent_angle)
 Draw a filled in elliptic arc. More...
 
void fill_arc (point2d center, double radius, double start_angle, double extent_angle)
 Draw a filled in arc. More...
 
void draw_text (point2d point, std::string const &text)
 Draw text justified at the chosen point, according to the current justification. More...
 
void draw_text (point2d point, std::string const &text, double bound_x, double bound_y)
 Draw text if it fits in the specified bounds; otherwise not drawn. More...
 
void draw_surface (surface *p_surface, point2d anchor_point, double scale_factor=1)
 Draw a surface. More...
 
 ~renderer ()
 Destructor.
 

Static Public Member Functions

static surfaceload_png (const char *file_path)
 load a png image into a bitmap surface More...
 
static void free_surface (surface *surface)
 Free a surface. More...
 

Protected Types

using transform_fn = std::function< point2d(point2d)>
 A callback for transforming points from one coordinate system to another.
 

Protected Member Functions

 renderer (cairo_t *cairo, transform_fn transform, camera *m_camera, cairo_surface_t *m_surface)
 Constructor. More...
 
void update_renderer (cairo_t *cairo, cairo_surface_t *m_surface)
 Update the renderer when the cairo surface/context changes. More...
 

Friends

class canvas
 

Detailed Description

Provides functions to draw primitives (e.g., lines, shapes) to a rendering context (the MainCanvas).

The various set_ functions are sticky; they remain in effect for all graphics drawing calls after them, until overrideen by a later set_ call.

The renderer modifies a cairo_t context based on draw calls. The renderer uses an ezgl::camera object to convert world coordinates into cairo's expected coordinate system.

Constructor & Destructor Documentation

◆ renderer()

ezgl::renderer::renderer ( cairo_t *  cairo,
transform_fn  transform,
camera m_camera,
cairo_surface_t *  m_surface 
)
protected

Constructor.

Parameters
cairoThe cairo graphics state.
transformThe function to use to transform points to cairo's coordinate system.

Member Function Documentation

◆ draw_arc()

void ezgl::renderer::draw_arc ( point2d  center,
double  radius,
double  start_angle,
double  extent_angle 
)

Draw the outline of an arc.

Parameters
centerThe center of the arc, in the current coordinate system
radiusThe radius of the arc, in the current coordinate system
start_angleThe starting angle of the arc, in degrees from the positive x axis
extent_angleThe extent angle of the arc, in degrees from the positive x axis

◆ draw_elliptic_arc()

void ezgl::renderer::draw_elliptic_arc ( point2d  center,
double  radius_x,
double  radius_y,
double  start_angle,
double  extent_angle 
)

Draw the outline of an elliptic arc.

Parameters
centerThe center of the arc, in the current coordinate system
radius_xThe x radius of the elliptic arc, in the current coordinate system
radius_yThe y radius of the elliptic arc.
start_angleThe starting angle of the arc, in degrees from the positive x axis
extent_angleThe extent angle of the arc, in degrees from the positive x axis

◆ draw_line()

void ezgl::renderer::draw_line ( point2d  start,
point2d  end 
)

Draw a line.

Parameters
startThe start point of the line, in the current coordinate system
endThe end point of the line

◆ draw_rectangle() [1/3]

void ezgl::renderer::draw_rectangle ( point2d  start,
double  width,
double  height 
)

Draw the outline of a rectangle.

Parameters
startThe lower left corner of the rectangle, in the current coordinate system
widthHow wide the rectangle is, in the current coordinate system
heightHow high the rectangle is

◆ draw_rectangle() [2/3]

void ezgl::renderer::draw_rectangle ( point2d  start,
point2d  end 
)

Draw the outline a rectangle.

Parameters
startA corner point of the rectangle, in the current coordinate system
endThe diagonally opposite point of the rectangle

◆ draw_rectangle() [3/3]

void ezgl::renderer::draw_rectangle ( rectangle  r)

Draw the outline of a rectangle.

Parameters
rThe rectangle

◆ draw_surface()

void ezgl::renderer::draw_surface ( surface p_surface,
point2d  anchor_point,
double  scale_factor = 1 
)

Draw a surface.

Parameters
surfaceThe surface (bitmap) to draw
anchor_pointThe anchor_point point of the drawn surface The surface will be justified at this point accordinate to the current justification.
scale_factor(optional) The scaling factor of the drawn surface. If specified, the width and height of the surface are each scaled by scale_factor.

◆ draw_text() [1/2]

void ezgl::renderer::draw_text ( point2d  point,
std::string const &  text 
)

Draw text justified at the chosen point, according to the current justification.

Parameters
pointThe point where the text is drawn, in the current coordinate system
textThe text to draw

◆ draw_text() [2/2]

void ezgl::renderer::draw_text ( point2d  point,
std::string const &  text,
double  bound_x,
double  bound_y 
)

Draw text if it fits in the specified bounds; otherwise not drawn.

Parameters
pointThe point where the text is drawn (justified according to the current justification), in the current coordinate system.
textThe text to draw
bound_xThe maximum allowed width of the text, in the current coordinate system.
bound_yThe maximum allowed height of the text, in the current coordinate system.

◆ fill_arc()

void ezgl::renderer::fill_arc ( point2d  center,
double  radius,
double  start_angle,
double  extent_angle 
)

Draw a filled in arc.

Parameters
centerThe center of the arc, in the current coordinate system
radiusThe radius of the arc, in the current coordinate system
start_angleThe starting angle of the arc, in degrees from the positive x axis
extent_angleThe extent angle of the arc, in degrees from the positive x axis

◆ fill_elliptic_arc()

void ezgl::renderer::fill_elliptic_arc ( point2d  center,
double  radius_x,
double  radius_y,
double  start_angle,
double  extent_angle 
)

Draw a filled in elliptic arc.

Parameters
centerThe center of the arc, in pixels.
radius_xThe x radius of the elliptic arc, in the current coordinate sstem.
radius_yThe y radius of the elliptic arc, in the current coordinate system.
start_angleThe starting angle of the arc, in degrees from the positive x axis.
extent_angleThe extent angle of the arc, in degrees from the positive x axis.

◆ fill_poly()

void ezgl::renderer::fill_poly ( std::vector< point2d > const &  points)

Draw a filled polygon The polygon can have an arbitrary shape and be convex or non-convex, but must be simple (no holes).

Parameters
pointsThe points to draw, which must be in the current coordinate system (world or screen). The first and last points are connected to close the polygon. There must be at least 2 points.

◆ fill_rectangle() [1/3]

void ezgl::renderer::fill_rectangle ( point2d  start,
double  width,
double  height 
)

Draw a filled in rectangle.

Parameters
startThe lower left corner of the rectangle, the current coordinate system
widthHow wide the rectangle is, in the current coordinate system
heightHow high the rectangle is

◆ fill_rectangle() [2/3]

void ezgl::renderer::fill_rectangle ( point2d  start,
point2d  end 
)

Draw a filled in rectangle.

Parameters
startOne corner of the rectangle, in the current coordinate system
endThe diagonally opposite corner of the rectangle

◆ fill_rectangle() [3/3]

void ezgl::renderer::fill_rectangle ( rectangle  r)

Draw a filled in rectangle.

Parameters
rThe rectangle

◆ format_font() [1/2]

void ezgl::renderer::format_font ( std::string const &  family,
font_slant  slant,
font_weight  weight 
)

Change the font.

Parameters
familyThe font family to use (e.g., serif). Use an empty string to request the default font.
slantThe slant to use (e.g., italic)
weightThe weight of the font (e.g., bold)

◆ format_font() [2/2]

void ezgl::renderer::format_font ( std::string const &  family,
font_slant  slant,
font_weight  weight,
double  new_size 
)

Change the font.

Parameters
familyThe font family to use (e.g., serif). Use an empty string to request the default font.
slantThe slant to use (e.g., italic)
weightThe weight of the font (e.g., bold)
new_sizeThe new size text should be drawn at.

◆ free_surface()

static void ezgl::renderer::free_surface ( surface surface)
static

Free a surface.

Parameters
surfaceThe surface to destroy

◆ get_visible_screen()

rectangle ezgl::renderer::get_visible_screen ( )

Get the current visible bounds of the screen in pixel coordinates.

Returns
A rectangle where rectangle.first is the lower left MainCanvas corner and rectangle.second is the upper right

◆ get_visible_world()

rectangle ezgl::renderer::get_visible_world ( )

Get the current visible bounds of the world.

Returns
A rectangle where rectangle.first is the lower left MainCanvas corner and rectangle.second is the upper right

◆ load_png()

static surface* ezgl::renderer::load_png ( const char *  file_path)
static

load a png image into a bitmap surface

Parameters
file_pathThe path to the png image.
Returns
a pointer to the created surface. This should later be freed using free_surface()

◆ set_color() [1/3]

void ezgl::renderer::set_color ( color  new_color)

Change the color for subsequent draw calls.

Parameters
new_colorThe new color to use.

◆ set_color() [2/3]

void ezgl::renderer::set_color ( color  new_color,
uint_fast8_t  alpha 
)

Change the color for subsequent draw calls.

Opaque drawing is about 2x faster than partially transparent.

Parameters
new_colorThe new color to use.
alphaThe transparency level. 0 is fully tranparent, 255 is opaque.

◆ set_color() [3/3]

void ezgl::renderer::set_color ( uint_fast8_t  red,
uint_fast8_t  green,
uint_fast8_t  blue,
uint_fast8_t  alpha = 255 
)

Change the color for subsequent draw calls.

Opaque drawing is about 2x faster than partially transparent.

Parameters
redThe amount of red to use, between 0 and 255.
greenThe amount of green to use, between 0 and 255.
blueThe amount of blue to use, between 0 and 255.
alpha(optional) The transparency level. 0 is fully transparent, 255 is opaque. Defaults to opaque is not specified.

◆ set_coordinate_system()

void ezgl::renderer::set_coordinate_system ( t_coordinate_system  new_coordinate_system)

Change the current coordinate system.

Parameters
new_coordinate_systemThe drawing coordinate system SCREEN or WORLD

◆ set_font_size()

void ezgl::renderer::set_font_size ( double  new_size)

Change the font size.

Parameters
new_sizeThe size text should be drawn at, in points. A point is 1/72 of an inch. Text sizes do not scale as graphics are zoomed in and out; they are always drawn at the chosen point size.

◆ set_horiz_justification()

void ezgl::renderer::set_horiz_justification ( justification  horiz_just)

set horizontal justification; used for text and surfaces.

Parameters
horiz_justOptions: center, left and right justification.

◆ set_line_cap()

void ezgl::renderer::set_line_cap ( line_cap  cap)

Change how line endpoints will be rendered in subsequent draw calls.

Parameters
capThe line_cap style

◆ set_line_dash()

void ezgl::renderer::set_line_dash ( line_dash  dash)

Change the dash style of the line for subsequent draw calls.

Parameters
dashThe line_dash style

◆ set_line_width()

void ezgl::renderer::set_line_width ( int  width)

Set the line width.

Parameters
widthThe width in pixels. A value of 0 is still one pixel wide but about 100x faster to draw than other line widths as small accuracy shortcuts are allowed. Line widths are in pixels (i.e. screen coordinates) and do not scale as graphics are zoomed in or out.

◆ set_text_rotation()

void ezgl::renderer::set_text_rotation ( double  degrees)

set the rotation_angle at which subsequent text drawing should render.

Parameters
degreesThe angle by which the text should rotate, in degrees from the x-axis.

◆ set_vert_justification()

void ezgl::renderer::set_vert_justification ( justification  vert_just)

set vertical justification; used for text and surfaces.

Parameters
vert_justOptions: center, top and bottom justification.

◆ set_visible_world()

void ezgl::renderer::set_visible_world ( rectangle  new_world)

Set the visible bounds of the world.

The function preserves the aspect ratio of the new world so that a single x-unit and a single y-unit map to the same distance on screen. It does this by expanding the new_world in either the x- or y-direction so it matches the MainCanvas aspect ratio.

Parameters
new_worldThe new visible bounds of the world. new_world.first is the lower left corner of the MainCanvas, and new_world.second is the upper right.

◆ update_renderer()

void ezgl::renderer::update_renderer ( cairo_t *  cairo,
cairo_surface_t *  m_surface 
)
protected

Update the renderer when the cairo surface/context changes.

Parameters
cairoThe new cairo graphics state
m_surfaceThe new cairo surface

◆ world_to_screen()

rectangle ezgl::renderer::world_to_screen ( const rectangle box)

Get the screen coordinates (pixel locations) of the world coordinate rectangle box.

Parameters
boxA rectangle in world coordinates
Returns
The corresponding rectangle in screen coordinates

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