Skip to main content

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
w
int
required
Width of the graphics context in pixels (1:1 screen)
h
int
required
Height of the graphics context in pixels (1:1 screen)
fps
int
default:"DEFAULT_FPS"
Target frame rate for redrawing
scale
float
default:"1.0"
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

virtual void 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.
color
const IColor&
required
The color to draw the line with
x1
float
required
X coordinate of the line start
y1
float
required
Y coordinate of the line start
x2
float
required
X coordinate of the line end
y2
float
required
Y coordinate of the line end
pBlend
const IBlend*
default:"nullptr"
Optional blend method
thickness
float
default:"1.0"
Line thickness in pixels
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.
cx
float
required
X coordinate of the circle center
cy
float
required
Y coordinate of the circle center
r
float
required
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.
a1
float
required
Start angle in degrees clockwise (0 = up)
a2
float
required
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.
text
const IText&
required
IText struct containing font and text properties
str
const char*
required
The text string to draw
bounds
const IRECT&
required
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.
return
float
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.
bitmap
const IBitmap&
required
The bitmap image to draw
bounds
const IRECT&
required
The rectangular region to draw the image in
srcX
int
required
X offset in the source image
srcY
int
required
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.
fileNameOrResID
const char*
required
File name or resource ID
nStates
int
default:"1"
Number of frames in a multi-frame bitmap
framesAreHorizontal
bool
default:"false"
True if frames are stacked horizontally
targetScale
int
default:"0"
Explicit scale (e.g., 2 for @2x.png). 0 = auto-detect
return
IBitmap
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.
pOwner
IControl*
required
The control that owns the layer
r
const IRECT&
required
The bounds of the layer
cacheable
bool
default:"false"
Make bitmap shareable between plugin instances

EndLayer

ILayerPtr EndLayer()
End drawing to a layer and return a pointer to it.
return
ILayerPtr
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.
pControl
IControl*
required
Pointer to the control to attach
ctrlTag
int
default:"kNoTag"
Integer tag to identify the control
group
const char*
default:"empty string"
Group name for batch operations
return
IControl*
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

int NControls() const
Get the number of controls in the context.
return
int
Number of controls

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.
return
float
The scale applied to the graphics context

GetScreenScale

float GetScreenScale() const
Get the screen/display scaling factor (e.g., 2 for Retina).

FPS

int FPS() const
Get the target frame rate.

Resize

void Resize(int w, int h, float scale, bool needsPlatformResize = true)
Resize the graphics context.
w
int
required
New width in pixels
h
int
required
New height in pixels
scale
float
required
New scale ratio
needsPlatformResize
bool
default:"true"
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.
fontID
const char*
required
String identifier for the font
fileNameOrResID
const char*
required
Absolute path or resource ID
return
bool
True on success

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
const IPattern&
required
Pattern for color or gradient
thickness
float
required
Line thickness
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

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