graphics
graphics
¶
Immediate-mode 2D shapes, broken into triangles for the batch renderer.
SDL2_gfx, the old primitive backend, has no SDL3 successor, so every shape is turned into triangles or edges here and fed to the GPU batcher. Angles are in radians. Outlines are drawn as quad strips.
Coordinates use Nim's default float for ergonomics and get converted to
float32 only when building GPU vertices.
setColor ¶
proc setColor(nim2d: Nim2d; r, g, b: uint8; a: uint8 = 255)
Set the color used by shapes and text until it is changed again. Channels
are bytes from 0 to 255, and alpha defaults to fully opaque.
Parameters
-
nim2d(Nim2d) -
r(uint8) -
g(uint8) -
b(uint8) -
a(uint8)
setColor ¶
proc setColor(nim2d: Nim2d; c: Color)
Set the draw color from a Color, so named colors, rgb, gray and hex all
work: setColor(orange), setColor(rgb(255, 120, 60)), setColor(color("#ff7a3c")).
Parameters
-
nim2d(Nim2d) -
c(Color)
setBackgroundColor ¶
proc setBackgroundColor(nim2d: Nim2d; r, g, b: uint8; a: uint8 = 255)
Set the color the window clears to at the start of every frame.
Parameters
-
nim2d(Nim2d) -
r(uint8) -
g(uint8) -
b(uint8) -
a(uint8)
setBackgroundColor ¶
proc setBackgroundColor(nim2d: Nim2d; c: Color)
Set the background color from a Color.
Parameters
-
nim2d(Nim2d) -
c(Color)
setBlendMode ¶
proc setBlendMode(nim2d: Nim2d; mode: BlendMode)
Set how drawing mixes with what is already there: bmAlpha (the default),
bmAdd for brightening glow effects, bmMod to darken, bmNone to
overwrite without blending.
Parameters
-
nim2d(Nim2d) -
mode(BlendMode)
setBlendMode ¶
proc setBlendMode(nim2d: Nim2d; mode: string)
Set the blend mode by name: "blend" or "alpha", "add", "mod" or
"multiply". Anything else turns blending off.
Parameters
-
nim2d(Nim2d) -
mode(string)
push ¶
proc push(nim2d: Nim2d)
Save the current transform so a later pop restores it.
Parameters
-
nim2d(Nim2d)
pop ¶
proc pop(nim2d: Nim2d)
Restore the transform saved by the matching push.
Parameters
-
nim2d(Nim2d)
origin ¶
proc origin(nim2d: Nim2d)
Reset the current transform to the identity.
Parameters
-
nim2d(Nim2d)
translate ¶
proc translate(nim2d: Nim2d; dx, dy: float)
Shift the coordinate system by (dx, dy). Everything drawn afterwards moves
with it.
Parameters
-
nim2d(Nim2d) -
dx(float) -
dy(float)
rotate ¶
proc rotate(nim2d: Nim2d; radians: float)
Turn the coordinate system around the current origin.
Parameters
-
nim2d(Nim2d) -
radians(float)
scale ¶
proc scale(nim2d: Nim2d; sx, sy: float)
Stretch the coordinate system by sx and sy.
Parameters
-
nim2d(Nim2d) -
sx(float) -
sy(float)
shear ¶
proc shear(nim2d: Nim2d; kx, ky: float)
Slant the coordinate system by the factors kx and ky.
Parameters
-
nim2d(Nim2d) -
kx(float) -
ky(float)
setScissor ¶
proc setScissor(nim2d: Nim2d; x, y, w, h: float)
Clip drawing to a rectangle until the scissor is cleared.
Parameters
-
nim2d(Nim2d) -
x(float) -
y(float) -
w(float) -
h(float)
setScissor ¶
proc setScissor(nim2d: Nim2d)
circle ¶
proc circle(nim2d: Nim2d; x, y, radius: float; filled = false; segments = 48)
A circle centered at (x, y), outlined by default and solid with filled.
More segments make it rounder, fewer make it cheaper.
Parameters
-
nim2d(Nim2d) -
x(float) -
y(float) -
radius(float) -
filled(auto) -
segments(auto)
ellipse ¶
proc ellipse(nim2d: Nim2d; x, y, rx, ry: float; filled = false; segments = 48)
An ellipse centered at (x, y) with the radii rx and ry.
Parameters
-
nim2d(Nim2d) -
x(float) -
y(float) -
rx(float) -
ry(float) -
filled(auto) -
segments(auto)
arc ¶
proc arc(nim2d: Nim2d; x, y, radius, a1, a2: float; segments = 48)
Part of a circle outline between the angles a1 and a2, in radians measured
clockwise from the right (y points down).
Parameters
-
nim2d(Nim2d) -
x(float) -
y(float) -
radius(float) -
a1(float) -
a2(float) -
segments(auto)
pie ¶
proc pie(nim2d: Nim2d; x, y, radius, a1, a2: float; filled = false; segments = 48)
A wedge from the center (x, y) between the angles a1 and a2, like a slice
of pie. Outlined by default, solid with filled.
Parameters
-
nim2d(Nim2d) -
x(float) -
y(float) -
radius(float) -
a1(float) -
a2(float) -
filled(auto) -
segments(auto)
rectangle ¶
proc rectangle(nim2d: Nim2d; x, y, w, h: float; filled = false; roundness = 0.0)
A rectangle with its top-left corner at (x, y). A roundness above zero
rounds the corners by that radius.
Parameters
-
nim2d(Nim2d) -
x(float) -
y(float) -
w(float) -
h(float) -
filled(auto) -
roundness(auto)
triangle ¶
proc triangle(nim2d: Nim2d; x1, y1, x2, y2, x3, y3: float; filled = false)
A triangle through three points.
Parameters
-
nim2d(Nim2d) -
x1(float) -
y1(float) -
x2(float) -
y2(float) -
x3(float) -
y3(float) -
filled(auto)
polygon ¶
proc polygon(nim2d: Nim2d; xs, ys: openArray[float]; filled = false)
A closed polygon through the points given as parallel x and y arrays. The
fill works for concave outlines too, so a star or an arrow fills correctly. Raises ValueError when the arrays differ in length or hold fewer than 3 points.
Parameters
-
nim2d(Nim2d) -
xs(openArray[float]) -
ys(openArray[float]) -
filled(auto)
line ¶
proc line(nim2d: Nim2d; points: openArray[Vec2]; width = 1.0)
A polyline through the points. Lines wider than a couple of pixels get
round joins where segments meet.
Parameters
-
nim2d(Nim2d) -
points(openArray[Vec2]) -
width(auto)
points ¶
proc points(nim2d: Nim2d; pts: openArray[Vec2]; size = 1.0)
A square dot of the given size at each point.
Parameters
-
nim2d(Nim2d) -
pts(openArray[Vec2]) -
size(auto)
stencil ¶
proc stencil(nim2d: Nim2d; mask: proc (nim2d: Nim2d))
Draw mask into the stencil buffer, then clip following drawing to where the
mask drew. The mask shapes themselves are not visible. Call stencilStop to
stop clipping. Needs a window created with stencil = true; without it this
does nothing and drawing is unclipped.
Parameters
-
nim2d(Nim2d) -
mask(proc (nim2d: Nim2d))
stencilStop ¶
proc stencilStop(nim2d: Nim2d)
Stop clipping to the stencil mask; drawing appears everywhere again.
Parameters
-
nim2d(Nim2d)