Drawing

This chapter describes all the functions available under the gfx table. Most functions work exactly like their nanovg counterpart.

Example usage

--Draw score
gfx.BeginPath()
gfx.LoadSkinFont("NovaMono.ttf")
gfx.Translate(-5,5) -- upper right margin
gfx.FillColor(255,255,255)
gfx.TextAlign(gfx.TEXT_ALIGN_RIGHT + gfx.TEXT_ALIGN_TOP)
gfx.FontSize(40)
gfx.Text(string.format("%08d", score),desw,0)

Constants

Most constants are from the nanovg library.

Lua Name Value
TEXT_ALIGN_BASELINE NVGalign::NVG_ALIGN_BASELINE
TEXT_ALIGN_BOTTOM NVGalign::NVG_ALIGN_BOTTOM
TEXT_ALIGN_CENTER NVGalign::NVG_ALIGN_CENTER
TEXT_ALIGN_LEFT NVGalign::NVG_ALIGN_LEFT
TEXT_ALIGN_MIDDLE NVGalign::NVG_ALIGN_MIDDLE
TEXT_ALIGN_RIGHT NVGalign::NVG_ALIGN_RIGHT
TEXT_ALIGN_TOP NVGalign::NVG_ALIGN_TOP
LINE_BEVEL NVGlineCap::NVG_BEVEL
LINE_BUTT NVGlineCap::NVG_BUTT
LINE_MITER NVGlineCap::NVG_MITER
LINE_ROUND NVGlineCap::NVG_ROUND
LINE_SQUARE NVGlineCap::NVG_SQUARE
IMAGE_GENERATE_MIPMAPS NVGimageFlags::NVG_IMAGE_GENERATE_MIPMAPS
IMAGE_REPEATX NVGimageFlags::NVG_IMAGE_REPEATX
IMAGE_REPEATY NVGimageFlags::NVG_IMAGE_REPEATY
IMAGE_FLIPY NVGimageFlags::NVG_IMAGE_FLIPY
IMAGE_PREMULTIPLIED NVGimageFlags::NVG_IMAGE_PREMULTIPLIED
IMAGE_NEAREST NVGimageFlags::NVG_IMAGE_NEAREST

BeginPath()

nanovg.h:461

Rect(float x, float y, float w, float h)

nanovg.h:491

FastRect(float x, float y, float w, float h)

FillColor(int r, int g, int b, int a = 255)

nanovg.h:251

CreateImage(const char* filename, int imageflags)

nanovg.h:372

CreateSkinImage(const char* filename, int imageflags)

Same as CreateImage but prepends "skins/[skinfolder]/textures/" to the filename.

ImageRect(float x, float y, float w, float h, int image, float alpha, float angle)

Draws an image in the specified rect stretching the image to fit.

Text(const char* s, float x, float y)

nanovg.h:584

TextAlign(int align)

nanovg.h:575

FontFace(const char* s)

nanovg.h:581

FontSize(float size)

nanovg.h:563

Translate(float x, float y)

nanovg.h:303

Scale(float x, float y)

nanovg.h:315

Rotate(float angle)

nanovg.h:306

ResetTransform()

nanovg.h:293

LoadFont(const char* name, const char* filename)

Loads a font and sets it as the current font. Only sets the current font if the font is already loaded.

LoadSkinFont(const char* name, const char* filename)

Same as LoadFont but prepends "skins/[skinfolder]/fonts/" to the filename.

FastText(const char* inputText, float x, float y)

A text rendering function that is slightly faster than the regular gfx.Text() but this text will always be drawn on top of any nanovg drawing.

CreateLabel(const char* text, int size, bool monospace)

Creates a cached text that can later be drawn with DrawLabel. This is the most resource efficient text drawing method.

DrawLabel(int labelId, float x, float y, float maxWidth = -1)

Renders an already created label.

Will resize the label to fit within the maxWidth if maxWidth > 0.

MoveTo(float x, float y)

nanovg.h:465

LineTo(float x, float y)

nanovg.h:468

BezierTo(float c1x, float c1y, float c2x, float c2y, float x, float y)

nanovg.h:471

QuadTo(float cx, float cy, float x, float y)

nanovg.h:474

ArcTo(float x1, float y1, float x2, float y2, float radius)

nanovg.h:477

ClosePath()

nanovg.h:480

MiterLimit(float limit)

nanovg.h:258

StrokeWidth(float size)

nanovg.h:261

LineCap(int cap)

nanovg.h:265

LineJoin(int join)

nanovg.h:269

Stroke()

nanovg.h:509

StrokeColor(int r, int g, int b, int a = 255)

nanovg.h:245

UpdateLabel(int labelId, const char* text, int size)

Updates an already existing label.

DrawGauge(float rate, float x, float y, float w, float h, float deltaTime)

Draws the currently loaded gauge, the game loads the gauge on its own.

SetGaugeColor(int colorindex, int r, int g, int b)

Sets the gauge color for the specified colorindex. The color indexes are:

0 = Normal gauge fail
1 = Normal gauge clear
2 = Hard gauge low (<30%)
3 = Hard gauge high (>30%)

Example:

gfx.SetGaugeColor(0,50,50,50) --Normal gauge fail = dark grey
gfx.SetGaugeColor(1,255,255,255) --Normal gauge clear = white
gfx.SetGaugeColor(2,50,0,0) --Hard gauge low (<30%) = dark red
gfx.SetGaugeColor(3,255,0,0) --Hard gauge high (>30%) = red

RoundedRect(float x, float y, float w, float h, float r)

nanovg.h:494

RoundedRectVarying(float x, float y, float w, float h, float radTopLeft, float radTopRight, float radBottomRight, float radBottomLeft)

nanovg.h:497

Ellipse(float cx, float cy, float rx, float ry)

nanovg.h:500

Circle(float cx, float cy, float r)

nanovg.h:503

SkewX(float angle)

nanovg.h:309

SkewY(float angle)

nanovg.h:312

LinearGradient(float sx, float sy, float ex, float ey)

nanovg.h:400

BoxGradient(float x, float y, float w, float h, float r, float f)

nanovg.h:408

RadialGradient(float cx, float cy, float inr, float outr)

nanovg.h:414

ImagePattern(float ox, float oy, float ex, float ey, float angle, int image, float alpha)

nanovg.h:420

UpdateImagePattern(int paint, float ox, float oy, float ex, float ey, float angle, float alpha)

Updates the parameters of an already existing ImagePattern.

GradientColors(int ri, int gi, int bi, int ai, int ro, int go, int bo, int ao)

Sets icol (inner color) and ocol (outer color) for the gradient functions.

FillPaint(int paint)

nanovg.h:254

StrokePaint(int paint)

nanovg.h:248

Restore()

nanovg.h:227

Reset()

nanovg.h:230

PathWinding(int dir)

nanovg.h:483

ForceRender()

Forces the current render queue to be processed which makes it possible to put any Fast* and Label drawing calls made before a ForceRender call under regular drawing functions called after a ForceRender call.

This function might have a more than insignificant performance impact. under regular

LoadImageJob(char* path, int placeholder, int w = 0, int h = 0)

Loads an image outside the main thread to not lock up the rendering. If w and h are greater than 0 then the image will be resized if it is larger than (w,h), if w or h are 0 the image will be loaded at its original size.

Returns placeholder until the image has been loaded.

Example:

if not songCache[song.id][selectedDiff] or songCache[song.id][selectedDiff] == jacketFallback then
    songCache[song.id][selectedDiff] = gfx.LoadImageJob(diff.jacketPath, jacketFallback, 200,200)
end

LoadWebImageJob(char* url, int placeholder, int w = 0, int h = 0)

Works the same as LoadImageJob but the path is a web url instead.

Scissor(float x, float y, float w, float h)

nanovg.h:431

IntersectScissor(float x, float y, float w, float h)

nanovg.h:439

ResetScissor()

nanovg.h:442

TextBounds(float x, float y, char* s)

Returns xmin,ymin, xmax,ymax for a gfx.Text text.

LabelSize(int label)

Returns w,h for a label created with gfx.CreateLabel

FastTextSize(char* text)

Returns w,h for a gfx.FastText text.

ImageSize(int image)

Returns w,h for the given image.

Arc(float cx, float cy, float r, float a0, float a1, int dir)

nanovg.h:488

SetImageTint(int r, int g, int b)

Sets the color to tint all coming image drawing calls with. (Multiplies the color given with the image colors)

GlobalCompositeOperation(int op)

nanovg.h:171

The available op are:

gfx.BLEND_OP_SOURCE_OVER <--- default
gfx.BLEND_OP_SOURCE_IN
gfx.BLEND_OP_SOURCE_OUT
gfx.BLEND_OP_ATOP
gfx.BLEND_OP_DESTINATION_OVER
gfx.BLEND_OP_DESTINATION_IN
gfx.BLEND_OP_DESTINATION_OUT
gfx.BLEND_OP_DESTINATION_ATOP
gfx.BLEND_OP_LIGHTER
gfx.BLEND_OP_COPY
gfx.BLEND_OP_XOR

GlobalCompositeBlendFunc(int sfactor, int dfactor)

nanovg.h:174

The available blend factors are:

gfx.BLEND_ZERO = 1<<0
gfx.BLEND_ONE = 1<<1
gfx.BLEND_SRC_COLOR = 1<<2
gfx.BLEND_ONE_MINUS_SRC_COLOR = 1<<3
gfx.BLEND_DST_COLOR = 1<<4
gfx.BLEND_ONE_MINUS_DST_COLOR = 1<<5
gfx.BLEND_SRC_ALPHA = 1<<6
gfx.BLEND_ONE_MINUS_SRC_ALPHA = 1<<7
gfx.BLEND_DST_ALPHA = 1<<8
gfx.BLEND_ONE_MINUS_DST_ALPHA = 1<<9
gfx.BLEND_SRC_ALPHA_SATURATE = 1<<10

GlobalCompositeBlendFuncSeparate(int srcRGB, int dstRGB, int srcAlpha, int dstAlpha)

nanovg.h:177

Uses the same parameter values as GlobalCompositeBlendFunc

LoadAnimation(char* path, float frametime, int loopcount = 0, bool compressed = false)

Loads all the images in a specified folder as an animation. frametime is used for the speed of the animation and if loopcount is set to something that isn’t 0 then the animation will stop after playing that many times.

If compressed is set to true then the animation will be stored in memory in a compressed format and each frame will be decoded on-demand which means that the animation uses much less RAM but it uses more CPU and the animation might stutter or skip frames if it’s too heavy.

Returns a numer that is used the same way a regular image is used.

GlobalAlpha(float alpha)

Sets a global alpha value for all nanovg drawing. Already transparent objects will have their tranparency adjusted relative to the global value.

LoadSkinAnimation(char* path, float frametime, int loopcount = 0, bool compressed = false)

Same as LoadAnimation but prepends "skins/[skinfolder]/textures/" to the path.

TickAnimation(int animation, float deltaTime)

Progresses the given animation. Returns -1 if animation not found, 0 if the animation is still loading, and 1 if the animation is loaded.

LoadSharedTexture(string key, string path)

Load a persistent texture that can be accessed by the key in GetSharedTexture and ShadedMesh:AddSharedTexture

LoadSharedSkinTexture(string key, string path)

Same as LoadSharedTexture but prepends "skins/[skinfolder]/textures/" to the path.

GetSharedTexture(string key)

Returns the shared texture ID generated by LoadSharedTexture which is used the same as images loaded with CreateImage.