IGraphics
The IGraphics class is the lowest level base class of an IGraphics context. It manages the drawing canvas, controls, and all UI interactions for audio plugins built with iPlug2.
Overview
IGraphics provides:
Cross-platform drawing API (NanoVG, Skia)
Control management and rendering
Mouse and keyboard event handling
Layer and bitmap management
Platform-specific windowing integration
Constructor
IGraphics (IGEditorDelegate & dlg, int w, int h, int fps = DEFAULT_FPS, float scale = 1. )
dlg
IGEditorDelegate&
required
The delegate that handles communication between IGraphics and the plugin
Width of the graphics context in pixels (1:1 screen)
Height of the graphics context in pixels (1:1 screen)
Target frame rate for redrawing
Initial draw scale factor
Core Drawing Methods
BeginFrame
virtual void BeginFrame ()
Called at the beginning of drawing. Override to perform pre-frame setup. Always call the base implementation.
EndFrame
Called after drawing to blit the draw bitmap onto the screen or perform cleanup.
Shape Drawing
DrawLine
virtual void DrawLine ( const IColor & color , float x1 , float y1 , float x2 , float y2 ,
const IBlend * pBlend = 0 , float thickness = 1. f )
Draw a line to the graphics context.
The color to draw the line with
X coordinate of the line start
Y coordinate of the line start
X coordinate of the line end
Y coordinate of the line end
pBlend
const IBlend*
default: "nullptr"
Optional blend method
g . DrawLine (COLOR_RED, 10 , 10 , 100 , 100 , nullptr , 2. f );
DrawRect
virtual void DrawRect ( const IColor & color , const IRECT & bounds ,
const IBlend * pBlend = 0 , float thickness = 1. f )
Draw a rectangle outline.
FillRect
virtual void FillRect ( const IColor & color , const IRECT & bounds , const IBlend * pBlend = 0 )
Fill a rectangular region with a color.
IRECT rect ( 10 , 10 , 110 , 60 );
g . FillRect (COLOR_BLUE, rect);
DrawRoundRect
virtual void DrawRoundRect ( const IColor & color , const IRECT & bounds , float cornerRadius = 5. f ,
const IBlend * pBlend = 0 , float thickness = 1. f )
Draw a rounded rectangle outline.
FillRoundRect
virtual void FillRoundRect ( const IColor & color , const IRECT & bounds , float cornerRadius = 5. f ,
const IBlend * pBlend = 0 )
Fill a rounded rectangle with a color.
DrawCircle
virtual void DrawCircle ( const IColor & color , float cx , float cy , float r ,
const IBlend * pBlend = 0 , float thickness = 1. f )
Draw a circle outline.
X coordinate of the circle center
Y coordinate of the circle center
Radius of the circle in pixels
FillCircle
virtual void FillCircle ( const IColor & color , float cx , float cy , float r , const IBlend * pBlend = 0 )
Fill a circle with a color.
DrawArc
virtual void DrawArc ( const IColor & color , float cx , float cy , float r , float a1 , float a2 ,
const IBlend * pBlend = 0 , float thickness = 1. f )
Draw an arc.
Start angle in degrees clockwise (0 = up)
End angle in degrees clockwise (0 = up)
FillArc
virtual void FillArc ( const IColor & color , float cx , float cy , float r , float a1 , float a2 ,
const IBlend * pBlend = 0 )
Fill an arc segment.
DrawTriangle
virtual void DrawTriangle ( const IColor & color , float x1 , float y1 , float x2 , float y2 ,
float x3 , float y3 , const IBlend * pBlend = 0 , float thickness = 1. f )
Draw a triangle outline.
FillTriangle
virtual void FillTriangle ( const IColor & color , float x1 , float y1 , float x2 , float y2 ,
float x3 , float y3 , const IBlend * pBlend = 0 )
Fill a triangle with a color.
DrawEllipse
virtual void DrawEllipse ( const IColor & color , const IRECT & bounds ,
const IBlend * pBlend = 0 , float thickness = 1. f )
Draw an ellipse within a rectangular region.
FillEllipse
virtual void FillEllipse ( const IColor & color , const IRECT & bounds , const IBlend * pBlend = 0 )
Fill an ellipse.
Text Drawing
DrawText
void DrawText ( const IText & text , const char* str , const IRECT & bounds , const IBlend * pBlend = 0 )
Draw text to the graphics context in a specific rectangle.
IText struct containing font and text properties
The rectangular region to draw the text in
IText text ( 16 , COLOR_BLACK , "Roboto-Regular" , EAlign :: Center , EVAlign :: Middle );
g . DrawText (text, "Hello iPlug2" , mRECT);
MeasureText
virtual float MeasureText ( const IText & text , const char* str , IRECT & bounds ) const
Measure the rectangular region that text will occupy.
The width of the measured text
Bitmap Drawing
DrawBitmap
virtual void DrawBitmap ( const IBitmap & bitmap , const IRECT & bounds , int srcX , int srcY ,
const IBlend * pBlend = 0 ) = 0
Draw a bitmap image to the graphics context.
The rectangular region to draw the image in
X offset in the source image
Y offset in the source image
LoadBitmap
virtual IBitmap LoadBitmap ( const char* fileNameOrResID , int nStates = 1 ,
bool framesAreHorizontal = false , int targetScale = 0 )
Load a bitmap from disk or Windows resource.
Number of frames in a multi-frame bitmap
True if frames are stacked horizontally
Explicit scale (e.g., 2 for @2x.png). 0 = auto-detect
An IBitmap representing the loaded image
DrawSVG
virtual void DrawSVG ( const ISVG & svg , const IRECT & bounds , const IBlend * pBlend = 0 ,
const IColor * pStrokeColor = nullptr , const IColor * pFillColor = nullptr )
Draw an SVG image to the graphics context.
pStrokeColor
const IColor*
default: "nullptr"
Optional color to override all SVG stroke commands
pFillColor
const IColor*
default: "nullptr"
Optional color to override all SVG fill commands
LoadSVG
virtual ISVG LoadSVG ( const char* fileNameOrResID , const char* units = "px" , float dpi = 72. f )
Load an SVG from disk or Windows resource.
Layer Management
StartLayer
void StartLayer ( IControl * pOwner , const IRECT & r , bool cacheable = false )
Create an IGraphics layer for offscreen drawing.
The control that owns the layer
Make bitmap shareable between plugin instances
EndLayer
End drawing to a layer and return a pointer to it.
Pointer to the layer that should be kept to draw it later
DrawLayer
void DrawLayer ( const ILayerPtr & layer , const IBlend * pBlend = nullptr )
Draw a layer to the main IGraphics context.
Control Management
AttachControl
IControl * AttachControl ( IControl * pControl , int ctrlTag = kNoTag, const char* group = "" )
Attach an IControl to the graphics context.
Pointer to the control to attach
Integer tag to identify the control
group
const char*
default: "empty string"
Group name for batch operations
Pointer to the attached control
auto * knob = new IVKnobControl (bounds, kGain);
knob = g . AttachControl (knob, kCtrlTagGainKnob, "mainControls" );
GetControl
IControl * GetControl ( int idx )
Get the control at a specific index.
GetControlWithTag
IControl * GetControlWithTag ( int ctrlTag ) const
Get a control by its tag.
RemoveControl
void RemoveControl ( IControl * pControl )
Remove a control and free its memory.
NControls
Get the number of controls in the context.
Context Properties
Width / Height
int Width () const
int Height () const
Get the dimensions of the graphics context in 1:1 pixels.
GetDrawScale
float GetDrawScale () const
Get the current draw scale factor.
The scale applied to the graphics context
GetScreenScale
float GetScreenScale () const
Get the screen/display scaling factor (e.g., 2 for Retina).
FPS
Get the target frame rate.
Resize
void Resize ( int w , int h , float scale , bool needsPlatformResize = true )
Resize the graphics context.
Set false when called from OnParentWindowResize to avoid feedback
Font Management
LoadFont
virtual bool LoadFont ( const char* fontID , const char* fileNameOrResID )
Load a font to be used by the graphics context.
String identifier for the font
Absolute path or resource ID
Path Drawing
PathClear
virtual void PathClear () = 0
Clear the stack of path drawing commands.
PathMoveTo
virtual void PathMoveTo ( float x , float y ) = 0
Move the current point in the path.
PathLineTo
virtual void PathLineTo ( float x , float y ) = 0
Add a line to the current path from the current point.
PathArc
virtual void PathArc ( float cx , float cy , float r , float a1 , float a2 ,
EWinding winding = EWinding :: CW ) = 0
Add an arc to the current path.
PathStroke
virtual void PathStroke ( const IPattern & pattern , float thickness ,
const IStrokeOptions & options = IStrokeOptions (),
const IBlend * pBlend = 0 ) = 0
Stroke the current path.
Pattern for color or gradient
options
const IStrokeOptions&
default: "IStrokeOptions()"
Dash, join, and preserve options
PathFill
virtual void PathFill ( const IPattern & pattern ,
const IFillOptions & options = IFillOptions (),
const IBlend * pBlend = 0 ) = 0
Fill the current path.
Example Usage
Complete Example
Custom Drawing
class MyPlugin : public Plugin {
public:
MyPlugin ( const InstanceInfo & info ) : Plugin (info, MakeConfig (kNumParams, kNumPrograms)) {
auto * pGraphics = MakeGraphics ( * this , PLUG_WIDTH, PLUG_HEIGHT, PLUG_FPS);
pGraphics -> AttachPanelBackground (COLOR_GRAY);
pGraphics -> LoadFont ( "Roboto-Regular" , "Roboto-Regular.ttf" );
const IRECT bounds = pGraphics -> GetBounds (). GetPadded ( - 20 );
// Attach controls
pGraphics -> AttachControl ( new IVKnobControl ( bounds . GetGridCell ( 0 , 2 , 1 ), kGain));
pGraphics -> AttachControl ( new IVSliderControl ( bounds . GetGridCell ( 1 , 2 , 1 ), kCutoff));
SetUI (pGraphics);
}
};
See Also
IControl Base class for all UI controls
IGraphics Structs Supporting data structures