gfx.h
IntroductionCanvas-like API for X-Plane 12.4.4+ XPLMPanelGraphics drawing. GFX abstracts away XPLMPanelGraphics' vertices, and instead provides you with a stateful canvas-like API to draw lines, arcs, Bézier curves, TTF- and bitmap-font text. GroupsShared state managementGroup members:
Transformations
When using the path API, all co-ordinates are specified in _user space_. Drawing commands submitted to X-Plane's Panel Graphics API are in _device space_: for avionics, this means the x and y axes originate at the bottom left of the device screen, and increase up and right; for windows, the x and y axes originate at the bottom left of the desktop space, and increase up and right. When a new frame starts (by calling `gfx_new_frame()`), user space and device space are the same. However, it can be useful to _transform_ user space, so co-ordinates are easier to work with. For example, when drawing a map, it is easier to rotate the the axes by the current aircraft's heading, than doing the rotation maths for each point drawn on the map. The transformation API is how the user space to device space relation is modified. The current transform state can be saved and restored, much like the OpenGL transorm stack. Group members:
Vector Drawing
Vector drawing using GFX is done using paths. You first construct one or multiple paths using the various functions of the path API; then call `gfx_stroke()`, `gfx_clip()`, or `gfx_fill()` to paint the path onto the context. A path can contain multiple _sub paths_: every time `gfx_move_to()` is called, a new sub-path is created. When calling `gfx_stroke()`, distinct sub-paths are drawn disconnected. Calls to `gfx_set_color()`, `gfx_set_line_width()`, take effect when `gfx_stroke() or `gfx_fill()` is called. Group members:
Context & Frame HandlingGroup members:
Color utilities.Group members:
Resource LoadingGroup members:
Text DrawingGroup members:Functions
gfx_arcParametersDiscussionAdds an circular arc of the given `radius` to the current path. The arc is centered at `(x, y)` at the centre, begins at `angle_start`, and progresses in the direction of increasing angles to `angle_end`. Angles are in radians; the point at 0 radians lies on the x-axis, and angles increase counter-clockwise. After this call, the path's current point is the point on the arc at `angle_end`. gfx_arc_negativevoid gfx_arc_negative( gfx_ctx_t *ctx, float x, float y, float radius, float angle_start, float angle_end ); ParametersDiscussionAdds an circular arc of the given `radius` to the current path. The arc is centered at `(x, y)` at the centre, begins at `angle_start`, and progresses in the direction of decreasing angles to `angle_end`. gfx_bake_sharedvoid gfx_bake_shared(); DiscussionBakes any texture atlases needed by GFX objects. Call once all your GFX contexts are done with initialisation, and you do not need to create any more resources. gfx_begin_framevoid gfx_begin_frame( gfx_ctx_t *ctx); ParametersDiscussionStarts a new rendering frame. gfx_clipParametersDiscussionUses a context's current path as a convex polygon to create a clipping area, and discard the path. Any drawing submitted after `gfx_clip()` and before `gfx_clip_reset()` is only shown when it lies within the polygon's area. gfx_clip_preservevoid gfx_clip_preserve( gfx_ctx_t *ctx); ParametersDiscussionUses a context's current path as a convex polygon to create a clipping area, and keep the path. Any drawing submitted after `gfx_clip()` and before `gfx_clip_reset()` is only shown when it lies within the polygon's area. gfx_clip_resetvoid gfx_clip_reset( gfx_ctx_t *ctx); ParametersDiscussionDiscard a context's current clipping area. gfx_close_pathvoid gfx_close_path( gfx_ctx_t *ctx); ParametersDiscussionCloses the current sub-path, by adding a point in the same location as the sub-path's first point. gfx_color_multuint32_t gfx_color_mult( uint32_t c, float m); DiscussionMultiplies each component of a colour by a scalar. gfx_curve_tovoid gfx_curve_to( gfx_ctx_t *ctx, float x1, float y1, float x2, float y2, float x3, float y3); ParametersDiscussionAdds a cubic bezier curve to the current path. The curve joins the current point to `(x3, y3)`, with `(x1, y1)` and `(x2, y2)` as control points. If there is no current point, the curve begins at `(x1, y1)`. gfx_destroyvoid gfx_destroy( gfx_ctx_t *ctx); ParametersDiscussionDestroys a GFX context. gfx_end_framevoid gfx_end_frame( gfx_ctx_t *ctx); ParametersDiscussionEnds the rendering frame. No work is done, but this allows the context to check that the rendering calls were well-formed, and that context save & restores are well balanced. gfx_fillParametersDiscussionFills a context's current path as a convex polygon, and discards the path. gfx_fill_preservevoid gfx_fill_preserve( gfx_ctx_t *ctx); ParametersDiscussionFills a context's current path as a convex polygon, and keep the path. gfx_fini_sharedvoid gfx_fini_shared(); DiscussionFrees any memory used by GFX shared resources. Call once at plugin teardown. gfx_init_sharedvoid gfx_init_shared(); DiscussionInitialises shared resources used by the GFX layer. Call once at plugin start. gfx_line_tovoid gfx_line_to( gfx_ctx_t *ctx, float x, float y); ParametersDiscussionAdds a line to the current path, from the current position to the new co-ordinates. If there is no current position, calling `gfx_line_to()` is equivalent to calling `gfx_move_to()`. After this call, the current point will be `(x, y)`. gfx_load_bitmap_fontint32_t gfx_load_bitmap_font( const char *path, const gfx_bitmap_font_desc_t *desc); ParametersReturn Valuethe font index, used when drawing text. DiscussionLoads a bitmap font from an atlas file. The atlas is assumed to contain 16 columns by 4 rows. gfx_load_texint32_t gfx_load_tex( const char *path); ParametersReturn Valuea valid texture index, or -1 if texture load failed. DiscussionLoads one texture from a PNG file, and adds it to a shared atlas. gfx_load_tex_atlasint32_t gfx_load_tex_atlas( const char *path, int col, int row, int *w, int *h); ParametersReturn Valuethe texture index of the first atlas tile, or -1 if texture load failed. DiscussionLoads one texture atlas from a PNG file. gfx_move_tovoid gfx_move_to( gfx_ctx_t *ctx, float x, float y); ParametersDiscussionBegin a new sub-path in a context. If the current path already has point, there will be a gap between the last point and the new co-ordinates. After this call, the current point will be `(x, y)`. gfx_newReturn ValueA pointer to the new context. DiscussionCreates a new GFX context. Contexts keep track of the transform stack, draw colour, line width, and active bitmap and TTF fonts. gfx_rectanglevoid gfx_rectangle( gfx_ctx_t *ctx, float x, float y, float w, float h); ParametersDiscussionAdds a rectangle to the current path. This is a convenience function, which is exactly equivalent to calling: gfx_move_to(ctx, x, y); gfx_line_to(ctx, x, y + h); gfx_line_to(ctx, x + w, y + h); gfx_line_to(ctx, x + w, y); gfx_close_path(ctx); gfx_resetParametersDiscussionResets the user-space transform to the default (coincident with device space). gfx_restorevoid gfx_restore( gfx_ctx_t *ctx); ParametersDiscussionPops the user-space transform from the stack, and make it current. gfx_rgbuint32_t gfx_rgb( uint8_t r, uint8_t g, uint8_t b); DiscussionCreates a color object from red, green, and blue 8-bit components (0-255). gfx_rgbauint32_t gfx_rgba( uint8_t r, uint8_t g, uint8_t b, uint8_t a); DiscussionCreates a color object from red, green, blue, and alpha 8-bit components (0-255). gfx_rgbafuint32_t gfx_rgbaf( float r, float g, float b, float a); DiscussionCreates a color object from red, green, blue, and alpha floating point components (0.0 - 1.0). gfx_rgbfuint32_t gfx_rgbf( float r, float g, float b); DiscussionCreates a color object from red, green, and blue floating point components (0.0 - 1.0). gfx_rotatevoid gfx_rotate( gfx_ctx_t *ctx, float angle); ParametersDiscussionRotate the user-space axes by a given angles. gfx_saveParametersDiscussionSaves the current user-space transform, and pushes it on the transform stack. gfx_scaleParametersDiscussionScales the user-space x- and y-axes by given factors. gfx_set_bitmap_fontvoid gfx_set_bitmap_font( gfx_ctx_t *ctx, int32_t font_id); ParametersDiscussionSets the font used for bitmap text drawing. gfx_set_colorvoid gfx_set_color( gfx_ctx_t *ctx, uint32_t color); ParametersDiscussionSets the color used to draw in a context. gfx_set_font_facevoid gfx_set_font_face( gfx_ctx_t *ctx, int32_t font_id); ParametersDiscussionSets the TTF font face used for TTF text drawing. gfx_set_font_sizevoid gfx_set_font_size( gfx_ctx_t *ctx, float size); ParametersDiscussionSets the character size used for TTF text drawing. gfx_set_line_capvoid gfx_set_line_cap( gfx_ctx_t *ctx, gfx_line_cap_t cap); ParametersDiscussionSets the caps with which lines should be drawn in a context. gfx_set_line_widthvoid gfx_set_line_width( gfx_ctx_t *ctx, float width); ParametersDiscussionSets the width at which lines should be drawn in a context. gfx_strokevoid gfx_stroke( gfx_ctx_t *ctx); ParametersDiscussionDraws a context's current path as a line, then discards the path. gfx_stroke_fastvoid gfx_stroke_fast( gfx_ctx_t *ctx); ParametersDiscussionDraws a context's current path as a line, then discards the path. Unlike `gfx_stroke(gfx_ctx_t *)`, this does check whether the path is closed or not, and just submits the vertices as-is to `XPLMLinesWithWidth()`. gfx_stroke_preservevoid gfx_stroke_preserve( gfx_ctx_t *ctx); ParametersDiscussionDraws a context's current path as a line, and keep the path. gfx_texParametersDiscussionDraws a rectangle at a given point, with a given texture applied to it. gfx_translatevoid gfx_translate( gfx_ctx_t *ctx, float x, float y); ParametersDiscussionMoves the user-space origin by a given `(x, y)` vector. Typedefsgfx_bitmap_font_desc_ttypedef struct { /** The ASCII code for the first character represented in the font atlas. */ char first_char; /** The number of characters contained in the font atlas. */ int32_t char_count; /** Whether the dot character is drawn at full width, or half width. */ bool short_dot; } gfx_bitmap_font_desc_t; DiscussionParameters used when loading a bitmap font file. gfx_ctx_ttypedef struct gfx_ctx_t gfx_ctx_t; DiscussionContext object, used to store the current drawing state for one window or avionics device. gfx_line_cap_ttypedef enum gfx_line_cap_t { /** Lines start and stop exactly at their end points, in a straight line. */ GFX_LINE_CAP_BUTT = 0, /** Lines have round endings, centered on the end points. */ GFX_LINE_CAP_ROUND = 1, /** Lines have square endings, centered on the end points. */ GFX_LINE_CAP_SQUARE = 2, } gfx_line_cap_t; DiscussionSpecifies how end points of lines are drawn. gfx_text_align_ttypedef enum { /** The text's left boundary is aligned with the drawing position. */ GFX_ALIGN_LEFT, /** Text is centered on the drawing position. */ GFX_ALIGN_CENTER, /** The text's right boundary is aligned with the drawing position. */ GFX_ALIGN_RIGHT, } gfx_text_align_t; DiscussionA text alignment |