Drawing Functions

#include <eglib/drawing.h>

These are generic drawing functions.

See also

eglib_Init() or eglib_Init_FrameBuffer().

Color

Various drawing functions do not take a color argument and instead use the color from a previously defined index.

void eglib_SetIndexColor(eglib_t * eglib, size_t idx, color_channel_t r, color_channel_t g, color_channel_t b)

Set color for given index, which other drawing functions may use.

Pixel

void eglib_DrawPixelColor(eglib_t * eglib, coordinate_t x, coordinate_t y, color_t color)

Draw given pixel coordinates with given color.

Example:

eglib_SetIndexColor(&eglib, 0, 0, 255, 0);
eglib_DrawPixelColor(
	&eglib,
	10, 10,
	(color_t){
		.r = 0,
		.g = 255,
		.b = 0,
	}
);

Output:

../../_images/eglib_DrawPixelColor.png
void eglib_DrawPixel(eglib_t * eglib, coordinate_t x, coordinate_t y)

Draw given pixel coordinates using color from index 0.

Example:

eglib_SetIndexColor(&eglib, 0, 0, 255, 0);
eglib_DrawPixel(&eglib, 10, 10);

Output:

../../_images/eglib_DrawPixel.png
See also

eglib_SetIndexColor().

Lines

void eglib_DrawLine(eglib_t * eglib, coordinate_t x1, coordinate_t y1, coordinate_t x2, coordinate_t y2)

Draw line from coordinates (x1, y1) to (x2, y2) using color from index 0.

Example:

eglib_SetIndexColor(&eglib, 0, 0, 255, 0);

eglib_DrawLine(&eglib, 0, 0, 99, 0);
eglib_DrawLine(&eglib, 0, 0, 50, 50);
eglib_DrawLine(&eglib, 0, 0, 0, 99);

eglib_DrawLine(&eglib, 99, 99, 99, 0);
eglib_DrawLine(&eglib, 99, 99, 50, 50);
eglib_DrawLine(&eglib, 99, 99, 0, 99);

eglib_DrawLine(&eglib, 0, 99, 50, 50);
eglib_DrawLine(&eglib, 99, 0, 50, 50);

Output:

../../_images/eglib_DrawLine.png
See also

eglib_SetIndexColor().

eglib_DrawHLine(eglib, x, y, len)

Draw horizontal line starting at (x, y) with length len using color from index 0.

Example:

eglib_SetIndexColor(&eglib, 0, 0, 255, 0);
eglib_DrawHLine(&eglib, 10, 20, 30);

Output:

../../_images/eglib_DrawHLine.png
See also

eglib_SetIndexColor().

eglib_DrawVLine(eglib, x, y, len)

Draw vertical line starting at (x, y) with length len using color from index 0.

Example:

eglib_SetIndexColor(&eglib, 0, 0, 255, 0);
eglib_DrawVLine(&eglib, 10, 20, 30);

Output:

../../_images/eglib_DrawVLine.png
See also

eglib_SetIndexColor().

void eglib_DrawGradientLine(eglib_t * eglib, coordinate_t x1, coordinate_t y1, coordinate_t x2, coordinate_t y2)

Draw line from coordinates (x1, y1) to (x2, y2).

Line color will be a gradient from index 0 at (x1, y1) to color from index 1 at (x2, y2).

Example:

eglib_SetIndexColor(&eglib, 0, 0, 255, 0);
eglib_SetIndexColor(&eglib, 1, 255, 255, 255);

eglib_DrawGradientLine(&eglib, 0, 0, 99, 0);
eglib_DrawGradientLine(&eglib, 0, 0, 50, 50);
eglib_DrawGradientLine(&eglib, 0, 0, 0, 99);

eglib_DrawGradientLine(&eglib, 99, 99, 99, 0);
eglib_DrawGradientLine(&eglib, 99, 99, 50, 50);
eglib_DrawGradientLine(&eglib, 99, 99, 0, 99);

eglib_DrawGradientLine(&eglib, 0, 99, 50, 50);
eglib_DrawGradientLine(&eglib, 99, 0, 50, 50);

Output:

../../_images/eglib_DrawGradientLine.png
See also

eglib_SetIndexColor().

eglib_DrawGradientHLine(eglib, x, y, len)

Draw horizontal gradient line from coordinates (x, y) to (x + len, y).

Example:

eglib_SetIndexColor(&eglib, 0, 0, 255, 0);
eglib_SetIndexColor(&eglib, 1, 255, 255, 255);
eglib_DrawGradientHLine(&eglib, 10, 20, 30);

Output:

../../_images/eglib_DrawGradientHLine.png
See also

eglib_DrawGradientLine().

eglib_DrawGradientVLine(eglib, x, y, len)

Draw vertical gradient line from coordinates (x, y) to (x, y + len).

Example:

eglib_SetIndexColor(&eglib, 0, 0, 255, 0);
eglib_SetIndexColor(&eglib, 1, 255, 255, 255);
eglib_DrawGradientVLine(&eglib, 10, 20, 30);

Output:

../../_images/eglib_DrawGradientVLine.png
See also

eglib_DrawGradientLine().

Triangles

void eglib_DrawTriangle(eglib_t * eglib, coordinate_t x1, coordinate_t y1, coordinate_t x2, coordinate_t y2, coordinate_t x3, coordinate_t y3)

Draw triangle for coordinates (x1, y1), (x2, y2) and (x3, y3) using color from index 0.

Example:

eglib_SetIndexColor(&eglib, 0, 0, 255, 0);
eglib_DrawTriangle(
	&eglib,
	50, 0,
	0, 99,
	99, 99
);

Output:

../../_images/eglib_DrawTriangle.png
See also

eglib_SetIndexColor().

Frames

void eglib_DrawFrame(eglib_t * eglib, coordinate_t x, coordinate_t y, coordinate_t width, coordinate_t height)

Draw frame starting at (x, y) with width and height using color from index 0.

Example:

eglib_SetIndexColor(&eglib, 0, 0, 255, 0);
eglib_DrawFrame(&eglib, 10, 10, 80, 80);

Output:

../../_images/eglib_DrawFrame.png
See also

eglib_SetIndexColor().

void eglib_DrawGradientFrame(eglib_t * eglib, coordinate_t x, coordinate_t y, coordinate_t width, coordinate_t height)

Draw frame starting at (x, y) with width and height.

Its colors will be a gradient:

  • Top left: color index 0.

  • Top right: color index 1.

  • Bottom left: color index 2.

  • Bottom right: color index 3.

Example:

eglib_SetIndexColor(&eglib, 0, 255, 0, 0);
eglib_SetIndexColor(&eglib, 1, 0, 255, 0);
eglib_SetIndexColor(&eglib, 2, 0, 0, 255);
eglib_SetIndexColor(&eglib, 3, 255, 255, 255);
eglib_DrawGradientFrame(&eglib, 10, 10, 80, 80);

Output:

../../_images/eglib_DrawGradientFrame.png
See also

eglib_SetIndexColor().

void eglib_DrawRoundFrame(eglib_t * eglib, coordinate_t x, coordinate_t y, coordinate_t width, coordinate_t height, coordinate_t radius)

Draw frame starting at (x, y), with width and height and rounded edges with radius, using color from index 0.

Both width and height must be greater than 2 * radius.

Example:

eglib_SetIndexColor(&eglib, 0, 0, 255, 0);
eglib_DrawRoundFrame(&eglib, 10, 10, 80, 80, 10);

Output:

../../_images/eglib_DrawRoundFrame.png
See also

eglib_SetIndexColor().

Boxes

void eglib_DrawBox(eglib_t * eglib, coordinate_t x, coordinate_t y, coordinate_t width, coordinate_t height)

Draw box starting at (x, y) with width and height using color from index 0.

Example:

eglib_SetIndexColor(&eglib, 0, 0, 255, 0);
eglib_DrawBox(&eglib, 10, 10, 80, 80);

Output:

../../_images/eglib_DrawBox.png
See also

eglib_SetIndexColor().

void eglib_DrawGradientBox(eglib_t * eglib, coordinate_t x, coordinate_t y, coordinate_t width, coordinate_t height)

Draw box starting at (x, y) with width and height.

Its colors will be a gradient:

  • Top left: color index 0.

  • Top right: color index 1.

  • Bottom left: color index 2.

  • Bottom right: color index 3.

Example:

eglib_SetIndexColor(&eglib, 0, 255, 0, 0);
eglib_SetIndexColor(&eglib, 1, 0, 255, 0);
eglib_SetIndexColor(&eglib, 2, 0, 0, 255);
eglib_SetIndexColor(&eglib, 3, 255, 255, 255);
eglib_DrawGradientBox(&eglib, 10, 10, 80, 80);

Output:

../../_images/eglib_DrawGradientBox.png
See also

eglib_SetIndexColor().

void eglib_DrawRoundBox(eglib_t * eglib, coordinate_t x, coordinate_t y, coordinate_t width, coordinate_t height, coordinate_t radius)

Draw frame starting at (x, y), with width and height and rounded edges with radius, using color from index 0.

Both width and height must be greater than 2 * radius.

Example:

eglib_SetIndexColor(&eglib, 0, 0, 255, 0);
eglib_DrawRoundBox(&eglib, 10, 10, 80, 80, 10);

Output:

../../_images/eglib_DrawRoundBox.png
See also

eglib_SetIndexColor().

void eglib_ClearScreen(eglib_t * eglib)

Set all pixels to the color from index 0.

Example:

eglib_SetIndexColor(&eglib, 0, 0, 255, 0);
eglib_DrawBox(&eglib, 10, 10, 80, 80);
eglib_ClearScreen(&eglib);

Output:

../../_images/eglib_ClearScreen.png
See also

eglib_SetIndexColor().

Round things

void eglib_DrawArc(eglib_t * eglib, coordinate_t x, coordinate_t y, coordinate_t radius, float start_angle, float end_angle)

Draw an arc with color from index 0

Parameters
  • x – Center x.

  • y – Center y.

  • radius – Radius.

  • start_angle – Start angle from 0 to 360, where 0 is “up” and 90 is “right”.

  • end_angle – End angle, bigger than start_angle and less or equal to 360.

Example:

eglib_SetIndexColor(&eglib, 0, 0, 255, 0);
eglib_DrawArc(&eglib, 50, 50, 25, 0, 135);

Output:

../../_images/eglib_DrawArc.png
void eglib_DrawGradientArc(eglib_t * eglib, coordinate_t x, coordinate_t y, coordinate_t radius, float start_angle, float end_angle)

Draw an arc with color gradient from index 0 to index 1.

Parameters
  • x – Center x.

  • y – Center y.

  • radius – Radius.

  • start_angle – Start angle from 0 to 360, where 0 is “up” and 90 is “right”.

  • end_angle – End angle, bigger than start_angle and less or equal to 360.

Example:

eglib_SetIndexColor(&eglib, 0, 0, 255, 0);
eglib_SetIndexColor(&eglib, 1, 0, 0, 255);
eglib_DrawGradientArc(&eglib, 50, 50, 25, 0, 135);

Output:

../../_images/eglib_DrawGradientArc.png
eglib_DrawCircle(eglib, x, y, radius)

Draw an arc with color from index 0

Parameters
  • x – Center x.

  • y – Center y.

  • radius – Radius.

Example:

eglib_SetIndexColor(&eglib, 0, 0, 255, 0);
eglib_DrawCircle(&eglib, 50, 50, 25);

Output:

../../_images/eglib_DrawCircle.png
void eglib_DrawFilledArc(eglib_t * eglib, coordinate_t x, coordinate_t y, coordinate_t radius, float start_angle, float end_angle)

Draw a filled arc with color from index 0

Parameters
  • x – Center x.

  • y – Center y.

  • radius – Radius.

  • start_angle – Start angle from 0 to 360, where 0 is “up” and 90 is “right”.

  • end_angle – End angle, bigger than start_angle and less or equal to 360.

Example:

eglib_SetIndexColor(&eglib, 0, 0, 255, 0);
eglib_DrawFilledArc(&eglib, 50, 50, 25, 0, 135);

Output:

../../_images/eglib_DrawFilledArc.png
void eglib_DrawGradientFilledArc(eglib_t * eglib, coordinate_t x, coordinate_t y, coordinate_t radius, float start_angle, float end_angle)

Draw a filled arc with color gradient from index 0 at the center and index 1 at the radius.

Parameters
  • x – Center x.

  • y – Center y.

  • radius – Radius.

  • start_angle – Start angle from 0 to 360, where 0 is “up” and 90 is “right”.

  • end_angle – End angle, bigger than start_angle and less or equal to 360.

Example:

eglib_SetIndexColor(&eglib, 0, 0, 255, 0);
eglib_SetIndexColor(&eglib, 1, 0, 0, 255);
eglib_DrawGradientFilledArc(&eglib, 50, 50, 25, 0, 135);

Output:

../../_images/eglib_DrawGradientFilledArc.png
eglib_DrawDisc(eglib, x, y, radius)

Draw a disc with color from index 0

Parameters
  • x – Center x.

  • y – Center y.

  • radius – Radius.

Example:

eglib_SetIndexColor(&eglib, 0, 0, 255, 0);
eglib_DrawDisc(&eglib, 50, 50, 25);

Output:

../../_images/eglib_DrawDisc.png
eglib_DrawGradientDisc(eglib, x, y, radius)

Draw a disc with color gradient from index 0 at the center and index 1 at the radius.

Parameters
  • x – Center x.

  • y – Center y.

  • radius – Radius.

Example:

eglib_SetIndexColor(&eglib, 0, 0, 255, 0);
eglib_SetIndexColor(&eglib, 1, 0, 0, 255);
eglib_DrawGradientDisc(&eglib, 50, 50, 25);

Output:

../../_images/eglib_DrawGradientDisc.png

Bitmaps

enum bitmap_format_t

Format of bitmap data.

BITMAP_BW

1 bit per pixel black and white.

Tip

this is the same data format as XBM.

BITMAP_RGB24

8bit per channel RGB

struct bitmap_t

A bitmap definition.

See also

eglib_DrawBitmap().

coordinate_t width

Width

coordinate_t height

Height

enum bitmap_format_t format

Data format

uint8_t * data

Pointer to bitmap data

void eglib_DrawBitmap(eglib_t * eglib, coordinate_t x, coordinate_t y, const struct bitmap_t * bitmap)

Draw given bitmap at (x, y).

Example:

const struct bitmap_t bitmap_bw = {
	.width = 12,
	.height = 8,
	.format = BITMAP_BW,
	.data = (uint8_t []){0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f},
};

const struct bitmap_t bitmap_rgb = {
	.width = 9,
	.height = 9,
	.format = BITMAP_RGB24,
	.data = (uint8_t []){
		0xff, 0x00, 0x00,  0xff, 0x00, 0x00,  0xff, 0x00, 0x00,   0xff, 0x00, 0x00,  0xff, 0x00, 0x00,  0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0x00,
		0xff, 0x00, 0x00,  0xff, 0x00, 0x00,  0xff, 0x00, 0x00,   0xff, 0x00, 0x00,  0xff, 0x00, 0x00,  0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0x00,
		0xff, 0x00, 0x00,  0xff, 0x00, 0x00,  0xff, 0x00, 0x00,   0xff, 0x00, 0x00,  0xff, 0x00, 0x00,  0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0x00,
		0x00, 0xff, 0x00,  0x00, 0xff, 0x00,  0x00, 0xff, 0x00,   0x00, 0xff, 0x00,  0x00, 0xff, 0x00,  0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00,
		0x00, 0xff, 0x00,  0x00, 0xff, 0x00,  0x00, 0xff, 0x00,   0x00, 0xff, 0x00,  0x00, 0xff, 0x00,  0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00,
		0x00, 0xff, 0x00,  0x00, 0xff, 0x00,  0x00, 0xff, 0x00,   0x00, 0xff, 0x00,  0x00, 0xff, 0x00,  0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00,
		0x00, 0x00, 0xff,  0x00, 0x00, 0xff,  0x00, 0x00, 0xff,   0x00, 0x00, 0xff,  0x00, 0x00, 0xff,  0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff,
		0x00, 0x00, 0xff,  0x00, 0x00, 0xff,  0x00, 0x00, 0xff,   0x00, 0x00, 0xff,  0x00, 0x00, 0xff,  0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff,
		0x00, 0x00, 0xff,  0x00, 0x00, 0xff,  0x00, 0x00, 0xff,   0x00, 0x00, 0xff,  0x00, 0x00, 0xff,  0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff,
	},
};

eglib_DrawBitmap(&eglib, 44, 26, &bitmap_bw);
eglib_DrawBitmap(&eglib, 46, 57, &bitmap_rgb);

Output:

../../_images/eglib_DrawBitmap.png

Text

struct glyph_t

A glyph.

See also

glyph_block_t.

uint8_t width

Bitmap width.

uint8_t height

Bitmap height.

int8_t left

Left padding before bitmap.

uint8_t advance

Distance to increment the pen position after rendering this glyph.

int8_t top

Distance from baseline to glyph’s highest pixel.

const uint8_t * data

Bitmap data.

struct glyph_unicode_block_t

Glyphs for a Unicode block.

See also

font_t.

wchar_t charcode_start

First unicode character code this font supports.

wchar_t charcode_end

Last unicode character code this font supports.

const struct glyph_t ** glyphs

Array of glyphs for each supported unicode character.

struct font_t

A Font definition.

uint8_t pixel_size

Font pixel size.

int16_t ascent

The distance from the baseline to the highest or upper grid coordinate used to place an outline point.

int16_t descent

The distance from the baseline to the lowest grid coordinate used to place an outline point.

uint16_t line_space

The distance that must be placed between two lines of text.

const struct glyph_unicode_block_t *[5] unicode_blocks

Array of glyph unicode blocks.

uint8_t unicode_blocks_count

Number of unicode_blocks.

void eglib_SetFont(eglib_t * eglib, const struct font_t * font)

Set font to be used by other font functions.

See also

Fonts.

See also

eglib_AddUnicodeBlockToFont().

_Bool eglib_AddUnicodeBlockToFont(struct font_t * font, const struct glyph_unicode_block_t * unicode_block)

Add given unicode block to font.

Returns true in case of error, false on success.

Example:

coordinate_t x, y;

x = 40 - 14;
y = 30 + 14 / 2;

eglib_SetIndexColor(&eglib, 0, 0, 0, 128);
eglib_DrawLine(&eglib, 0, y, 100, y);
eglib_DrawLine(&eglib, 0, y * 2, 100, y * 2);
eglib_DrawLine(&eglib, x, 0, x, 100);

eglib_SetFont(&eglib, &font_Liberation_SansRegular_20px);
eglib_SetIndexColor(&eglib, 0, 255, 255, 255);
eglib_DrawText(&eglib, x, y, "Fábio");
eglib_AddUnicodeBlockToFont(
  &font_Liberation_SansRegular_20px, 
  &unicode_block_Liberation_SansRegular_20px_Latin1Supplement
);
eglib_DrawText(&eglib, x, y * 2, "Fábio");

Output:

../../_images/eglib_AddUnicodeBlockToFont.png
See also

Fonts

See also

eglib_SetFont().

const struct glyph_t * eglib_GetGlyph(eglib_t * eglib, wchar_t unicode_char)

Return given unicode character’s glyph_t or NULL if unsupported by font.

void eglib_DrawGlyph(eglib_t * eglib, coordinate_t x, coordinate_t y, const struct glyph_t * glyph)

Draw given glyph_t at (x, y).

Example:

coordinate_t x, y;

x = 50 - 14;
y = 50 + 14 / 2;

eglib_SetIndexColor(&eglib, 0, 0, 0, 128);
eglib_DrawLine(&eglib, 0, y, 100, y);
eglib_DrawLine(&eglib, x, 0, x, 100);

eglib_SetFont(&eglib, &font_Liberation_SansRegular_20px);
eglib_SetIndexColor(&eglib, 0, 255, 255, 255);

eglib_DrawGlyph(&eglib, x, y, eglib_GetGlyph(&eglib, 'A'));

Output:

../../_images/eglib_DrawGlyph.png
void eglib_DrawWChar(eglib_t * eglib, coordinate_t x, coordinate_t y, wchar_t unicode_char)

Draw given unicode character glyph at (x, y).

Example:

coordinate_t x, y;

x = 50 - 14;
y = 30 + 14 / 2;

eglib_SetIndexColor(&eglib, 0, 0, 0, 128);
eglib_DrawLine(&eglib, 0, y, 100, y);
eglib_DrawLine(&eglib, 0, y * 2, 100, y * 2);
eglib_DrawLine(&eglib, x, 0, x, 100);

eglib_SetFont(&eglib, &font_Liberation_SansRegular_20px);
eglib_SetIndexColor(&eglib, 0, 255, 255, 255);
eglib_DrawWChar(&eglib, x, y, 'g');
eglib_DrawWChar(&eglib, x, y * 2, 9999);  // Unsupported by font

Output:

../../_images/eglib_DrawWChar.png
void eglib_DrawFilledWChar(eglib_t * eglib, coordinate_t x, coordinate_t y, wchar_t unicode_char)

Similar to eglib_DrawWChar(), but fills the background using color from index 1.

Example:

coordinate_t x, y;

x = 50 - 14;
y = 50;

eglib_SetIndexColor(&eglib, 0, 0, 0, 128);
eglib_DrawLine(&eglib, 0, y, 100, y);
eglib_DrawLine(&eglib, x, 0, x, 100);

eglib_SetFont(&eglib, &font_Liberation_SansRegular_20px);
eglib_SetIndexColor(&eglib, 0, 255, 255, 255);
eglib_SetIndexColor(&eglib, 1, 64, 64, 64);
eglib_DrawFilledWChar(&eglib, x, y, '!');

Output:

../../_images/eglib_DrawFilledWChar.png
void eglib_DrawText(eglib_t * eglib, coordinate_t x, coordinate_t y, const char * utf8_text)

Draw given UTF-8 text starting at (x, y).

Example:

coordinate_t x, y;

x = 50 - 20;
y = 50 + 14 / 2;

eglib_SetIndexColor(&eglib, 0, 0, 0, 128);
eglib_DrawLine(&eglib, 0, y, 99, y);
eglib_DrawLine(&eglib, x, 0, x, 99);

eglib_SetFont(&eglib, &font_Liberation_SansRegular_20px);
eglib_AddUnicodeBlockToFont(
  &font_Liberation_SansRegular_20px,
  &unicode_block_Liberation_SansRegular_20px_Latin1Supplement
);
eglib_SetIndexColor(&eglib, 0, 255, 255, 255);
eglib_DrawText(&eglib, x, y, "Olá!");

Output:

../../_images/eglib_DrawText.png
eglib_DrawTextCentered(eglib, x, y, utf8_text)

Similar to eglib_DrawText(), but centers text horizontally at given coordinates

Example:

coordinate_t x, y;

x = 50 - 20;
y = 50 + 14 / 2;

eglib_SetIndexColor(&eglib, 0, 0, 0, 128);
eglib_DrawLine(&eglib, 0, y, 99, y);
eglib_DrawLine(&eglib, x, 0, x, 99);

eglib_SetFont(&eglib, &font_Liberation_SansRegular_20px);
eglib_SetIndexColor(&eglib, 0, 255, 255, 255);
eglib_DrawTextCentered(&eglib, x, y, "Hello!");

Output:

../../_images/eglib_DrawTextCentered.png
coordinate_t eglib_GetTextWidth(eglib_t * eglib, const char * utf8_text)

Return the width in pixels of the given UTF-8 text.

See also

eglib_SetFont().