ezgl
1.0.1
An Easy Graphics & GUI Library
|
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 surface * | load_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 |
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.
|
protected |
Constructor.
cairo | The cairo graphics state. |
transform | The function to use to transform points to cairo's coordinate system. |
void ezgl::renderer::draw_arc | ( | point2d | center, |
double | radius, | ||
double | start_angle, | ||
double | extent_angle | ||
) |
Draw the outline of an arc.
center | The center of the arc, in the current coordinate system |
radius | The radius of the arc, in the current coordinate system |
start_angle | The starting angle of the arc, in degrees from the positive x axis |
extent_angle | The extent angle of the arc, in degrees from the positive x axis |
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.
center | The center of the arc, in the current coordinate system |
radius_x | The x radius of the elliptic arc, in the current coordinate system |
radius_y | The y radius of the elliptic arc. |
start_angle | The starting angle of the arc, in degrees from the positive x axis |
extent_angle | The extent angle of the arc, in degrees from the positive x axis |
Draw a line.
start | The start point of the line, in the current coordinate system |
end | The end point of the line |
void ezgl::renderer::draw_rectangle | ( | point2d | start, |
double | width, | ||
double | height | ||
) |
Draw the outline of a rectangle.
start | The lower left corner of the rectangle, in the current coordinate system |
width | How wide the rectangle is, in the current coordinate system |
height | How high the rectangle is |
Draw the outline a rectangle.
start | A corner point of the rectangle, in the current coordinate system |
end | The diagonally opposite point of the rectangle |
void ezgl::renderer::draw_rectangle | ( | rectangle | r | ) |
Draw the outline of a rectangle.
r | The rectangle |
void ezgl::renderer::draw_surface | ( | surface * | p_surface, |
point2d | anchor_point, | ||
double | scale_factor = 1 |
||
) |
Draw a surface.
surface | The surface (bitmap) to draw |
anchor_point | The 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. |
void ezgl::renderer::draw_text | ( | point2d | point, |
std::string const & | text | ||
) |
Draw text justified at the chosen point, according to the current justification.
point | The point where the text is drawn, in the current coordinate system |
text | The text to draw |
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.
point | The point where the text is drawn (justified according to the current justification), in the current coordinate system. |
text | The text to draw |
bound_x | The maximum allowed width of the text, in the current coordinate system. |
bound_y | The maximum allowed height of the text, in the current coordinate system. |
void ezgl::renderer::fill_arc | ( | point2d | center, |
double | radius, | ||
double | start_angle, | ||
double | extent_angle | ||
) |
Draw a filled in arc.
center | The center of the arc, in the current coordinate system |
radius | The radius of the arc, in the current coordinate system |
start_angle | The starting angle of the arc, in degrees from the positive x axis |
extent_angle | The extent angle of the arc, in degrees from the positive x axis |
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.
center | The center of the arc, in pixels. |
radius_x | The x radius of the elliptic arc, in the current coordinate sstem. |
radius_y | The y radius of the elliptic arc, in the current coordinate system. |
start_angle | The starting angle of the arc, in degrees from the positive x axis. |
extent_angle | The extent angle of the arc, in degrees from the positive x axis. |
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).
points | The 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. |
void ezgl::renderer::fill_rectangle | ( | point2d | start, |
double | width, | ||
double | height | ||
) |
Draw a filled in rectangle.
start | The lower left corner of the rectangle, the current coordinate system |
width | How wide the rectangle is, in the current coordinate system |
height | How high the rectangle is |
Draw a filled in rectangle.
start | One corner of the rectangle, in the current coordinate system |
end | The diagonally opposite corner of the rectangle |
void ezgl::renderer::fill_rectangle | ( | rectangle | r | ) |
Draw a filled in rectangle.
r | The rectangle |
void ezgl::renderer::format_font | ( | std::string const & | family, |
font_slant | slant, | ||
font_weight | weight | ||
) |
Change the font.
family | The font family to use (e.g., serif). Use an empty string to request the default font. |
slant | The slant to use (e.g., italic) |
weight | The weight of the font (e.g., bold) |
void ezgl::renderer::format_font | ( | std::string const & | family, |
font_slant | slant, | ||
font_weight | weight, | ||
double | new_size | ||
) |
Change the font.
family | The font family to use (e.g., serif). Use an empty string to request the default font. |
slant | The slant to use (e.g., italic) |
weight | The weight of the font (e.g., bold) |
new_size | The new size text should be drawn at. |
|
static |
Free a surface.
surface | The surface to destroy |
rectangle ezgl::renderer::get_visible_screen | ( | ) |
Get the current visible bounds of the screen in pixel coordinates.
rectangle ezgl::renderer::get_visible_world | ( | ) |
Get the current visible bounds of the world.
|
static |
load a png image into a bitmap surface
file_path | The path to the png image. |
void ezgl::renderer::set_color | ( | color | new_color | ) |
Change the color for subsequent draw calls.
new_color | The new color to use. |
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.
new_color | The new color to use. |
alpha | The transparency level. 0 is fully tranparent, 255 is opaque. |
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.
red | The amount of red to use, between 0 and 255. |
green | The amount of green to use, between 0 and 255. |
blue | The 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. |
void ezgl::renderer::set_coordinate_system | ( | t_coordinate_system | new_coordinate_system | ) |
Change the current coordinate system.
new_coordinate_system | The drawing coordinate system SCREEN or WORLD |
void ezgl::renderer::set_font_size | ( | double | new_size | ) |
Change the font size.
new_size | The 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. |
void ezgl::renderer::set_horiz_justification | ( | justification | horiz_just | ) |
set horizontal justification; used for text and surfaces.
horiz_just | Options: center, left and right justification. |
void ezgl::renderer::set_line_cap | ( | line_cap | cap | ) |
Change how line endpoints will be rendered in subsequent draw calls.
cap | The line_cap style |
void ezgl::renderer::set_line_dash | ( | line_dash | dash | ) |
Change the dash style of the line for subsequent draw calls.
dash | The line_dash style |
void ezgl::renderer::set_line_width | ( | int | width | ) |
Set the line width.
width | The 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. |
void ezgl::renderer::set_text_rotation | ( | double | degrees | ) |
set the rotation_angle at which subsequent text drawing should render.
degrees | The angle by which the text should rotate, in degrees from the x-axis. |
void ezgl::renderer::set_vert_justification | ( | justification | vert_just | ) |
set vertical justification; used for text and surfaces.
vert_just | Options: center, top and bottom justification. |
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.
new_world | The 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. |
|
protected |
Update the renderer when the cairo surface/context changes.
cairo | The new cairo graphics state |
m_surface | The new cairo surface |
Get the screen coordinates (pixel locations) of the world coordinate rectangle box.
box | A rectangle in world coordinates |