Graphic Library¶
Graphic Library provides functions for drawing to a display driver.
To use this library
Header: #include "gl.h"
memake: MikroSDK.GraphicLibrary
The library is responsible for common drawing tasks and uses gl_driver_t
structure to interface with underlying display driver. The library supports
drawing common graphical primitives, images and texts. The library has modular
architecture, so it can work with any driver which implements gl_driver_t
interface, including some click board drivers.
Initialization¶
Before using any of the drawing functions, a driver must be initialized and set, as shown below:
#include "gl.h"
#include "board.h"
#include "tft8.h"
static gl_driver_t driver;
static tft8_cfg_t tft_cfg;
void main ()
{
// initialize driver
TFT8_MAP_PINOUTS_16BIT(tft_cfg);
tft_cfg.board = &TFT_BOARD_7_CAPACITIVE;
tft8_init(&tft_cfg, &driver);
gl_set_driver(&driver); // Set driver before drawing
// Drawing is now available...
}
driver
instance is specific to a display driver and it must be initialized
before using any drawing functions. Component library usually provides an
initialization function for the driver
instance. For more detailed explanation
for how to properly initialize a component's driver, refer to its documentation.
Drawing¶
Drawing to a display is done by setting GL state for pen, brush, font, etc. and calling draw functions. The following example shows how to clear a display with black color, set pen to white color 3px wide, and draw a center aligned rectangle.
// Assume driver is already set
gl_clear(GL_BLACK); // Clear screen.
gl_set_pen(&gl, GL_WHITE, 3); // Set pen.
uint16_t center_x = gl_get_screen_width(&gl) / 2 - 50;
uint16_t center_y = gl_get_screen_height(&gl) / 2 - 50;
gl_draw_rect(center_x, center_y, 100, 100);
Coordinate system¶
The library uses cartesian coordinate system in plane, and each point represents one pixel on display. The origin is placed in top-left corner of a display. The x value increases to the right and the y value increases downwards.