types
types
¶
Core nim2d types.
This module holds data only. The behaviour lives in the backend renderer and
the public modules. Keeping it this way leaves the dependency graph acyclic,
since types is imported by everything and itself depends only on the SDL
shim and the transform math.
Color
¶
type Color = tuple[r, g, b, a: uint8]
A color as four bytes from 0 to 255. The color module adds named
constants and the rgb, gray and hex constructors.
Vec2
¶
type Vec2 = tuple[x, y: float]
The (x, y) float pair used for positions throughout nim2d. The math
module gives it the usual vector operators, and because it is a plain
tuple they work on bare literals like (10.0, 20.0) too.
Vertex
¶
type Vertex = object
One batched vertex: pixel-space position, texcoords, normalized color.
Fields
-
xfloat32 -
yfloat32 -
ufloat32 -
vfloat32 -
rfloat32 -
gfloat32 -
bfloat32 -
afloat32
BlendMode
¶
type BlendMode = enum
How drawing mixes with what is already on the target: no blending,
ordinary alpha blending, additive (brightens, good for glow), or multiplicative (darkens).
Values
-
bmNone -
bmAlpha -
bmAdd -
bmMod
GamepadId
¶
type GamepadId = SDL_JoystickID
Identifies a connected controller, handed to the gamepad callbacks and
the polling procs. Opaque; you do not build one yourself.
PipelineKind
¶
type PipelineKind = enum
Which built-in pipeline a draw uses: plain vertex colors or a texture.
Values
-
pkColored -
pkTextured
Drawable
¶
type Drawable = ref object
The base of everything that can be drawn to the screen.
Filter
¶
type Filter = enum
Texture sampling: smooth (the default) or sharp, for pixel art.
Values
-
filLinear -
filNearest
Wrap
¶
type Wrap = enum
How texcoords outside 0..1 are handled.
Values
-
wrapClamp -
wrapRepeat -
wrapMirror
Texture
¶
type Texture = ref object
A GPU texture with its size and sampling state. Images and canvases are
both textures, which is why they draw the same way.
Fields
-
texptr SDL_GPUTexture -
widthint32 -
heightint32 -
tintColor– color/alpha modulation applied when drawn -
filterFilter– linear by default -
wrapWrap– clamp by default
Image
¶
type Image = ref object
A texture loaded from a file or uploaded from an ImageData.
Canvas
¶
type Canvas = ref object
A render target (GPU texture created with COLOR_TARGET usage).
Fields
-
depthptr SDL_GPUTexture– paired depth-stencil target, only when stencil is enabled
Quad
¶
type Quad = object
A rectangular sub-region of a texture, as texcoords plus its pixel size.
Fields
-
u0float32 -
v0float32 -
u1float32 -
v1float32 -
wfloat32 -
hfloat32
Font
¶
type Font = ref object
A font for print: either a TrueType font opened through SDL_ttf, or a
bitmap font built from a glyph sheet by newImageFont.
Fields
-
enginepointer– TTF_TextEngine (GPU text engine) -
fontpointer– TTF_Font (nil for a bitmap/image font) -
sizecint -
imgImage– glyph sheet, set for a bitmap/image font -
glyphSetstring– the characters, in image order -
glyphXseq[int32]– each glyph's x and width in the sheet -
glyphWseq[int32]– each glyph's x and width in the sheet -
imgHint32– glyph height (the sheet height) -
spacingint32– pixels added between glyphs
Shader
¶
type Shader = ref object
A user fragment shader compiled into one pipeline per blend mode, with an
optional fragment uniform buffer filled by send.
Fields
-
pipelinesarray[BlendMode, ptr SDL_GPUGraphicsPipeline] -
uniformseq[byte] -
hasUniformbool
Scissor
¶
type Scissor = object
Fields
-
onbool -
xint32 -
yint32 -
wint32 -
hint32
Filesystem
¶
type Filesystem = ref object
A small virtual filesystem: a writable save directory, a read-only source
directory, and extra read directories added with mount. The behaviour
lives in the filesystem module.
Fields
-
saveDirstring -
sourceDirstring -
mountsseq[string] -
identitySetbool
DrawCmd
¶
type DrawCmd = object
A run of indices sharing pipeline/blend/texture/scissor/shader state.
Fields
-
kindPipelineKind -
blendBlendMode -
textureptr SDL_GPUTexture -
samplerptr SDL_GPUSampler -
shaderShader -
scissorScissor -
stenciluint8 -
firstIndexuint32 -
indexCountuint32
RenderPassRec
¶
type RenderPassRec = object
One render pass: a target texture plus the draws recorded against it.
(A new pass is started whenever the render target or clear changes.)
Fields
-
targetptr SDL_GPUTexture -
depthptr SDL_GPUTexture -
wint32 -
hint32 -
doClearbool -
clearColorColor -
projectionarray[16, float32] -
cmdsseq[DrawCmd]
GpuContext
¶
type GpuContext = ref object
The renderer's state: the GPU device, pipelines, samplers, the geometry
accumulated for the current frame, and the transform stack. Owned by the engine; games normally never touch it directly.
Fields
-
deviceptr SDL_GPUDevice -
windowptr SDL_Window -
swFormatSDL_GPUTextureFormat– swapchain format; render targets must match it -
shaderFormatSDL_GPUShaderFormat– MSL or SPIR-V, chosen from the device backend -
samplerptr SDL_GPUSampler– default sampler (linear, clamp) -
samplersarray[Filter, array[Wrap, ptr SDL_GPUSampler]]– cache for other combos -
ssFactorint32– supersample factor for anti-aliasing (1 = off) -
ssTexptr SDL_GPUTexture– the high-res offscreen target when supersampling -
ssWint32– supersample target size -
ssHint32– supersample target size -
frameWint32– logical frame size, for the downscale blit -
frameHint32– logical frame size, for the downscale blit -
stencilEnabledbool– whether the depth-stencil machinery is built -
depthFormatSDL_GPUTextureFormat– chosen depth-stencil format -
screenDepthptr SDL_GPUTexture– depth-stencil targets for screen/SS -
ssDepthptr SDL_GPUTexture– depth-stencil targets for screen/SS -
screenDepthWint32– size the screen depth target was made at -
screenDepthHint32– size the screen depth target was made at -
stencilModeuint8– 0 none, 1 write the mask, 2 test against it -
stencilWritePipeptr SDL_GPUGraphicsPipeline -
stencilTestPipesarray[PipelineKind, array[BlendMode, ptr SDL_GPUGraphicsPipeline]] -
whiteTexptr SDL_GPUTexture– 1x1 white, bound when a shader draw has no texture -
pipelinesarray[PipelineKind, array[BlendMode, ptr SDL_GPUGraphicsPipeline]] -
verticesseq[Vertex] -
indicesseq[uint32] -
vbufptr SDL_GPUBuffer -
ibufptr SDL_GPUBuffer -
vbufCapint -
ibufCapint -
transferBufptr SDL_GPUTransferBuffer -
transferCapint -
cmdptr SDL_GPUCommandBuffer -
swTexptr SDL_GPUTexture -
passesseq[RenderPassRec] -
tempTexturesseq[ptr SDL_GPUTexture]– released after the frame submits -
transformTransform -
transformStackseq[Transform] -
curScissorScissor -
curShaderShader
Nim2d
¶
type Nim2d = ref object
The engine: the window, the renderer, the current draw state, timing,
and the callbacks the main loop dispatches to. Make one with newNim2d,
assign the callbacks you care about, and hand it to play.
Fields
-
widthint32 -
heightint32 -
gpuGpuContext -
backgroundColor -
colorColor -
fontFont -
fsFilesystem -
blendBlendMode -
runningbool -
perfFrequint64 -
lastCounteruint64 -
dtfloat -
fpsfloat -
loadproc (nim2d: Nim2d) -
drawproc (nim2d: Nim2d) -
quitproc (nim2d: Nim2d) -
updateproc (nim2d: Nim2d; dt: float) -
keydownproc (nim2d: Nim2d; key: Key) -
keyupproc (nim2d: Nim2d; key: Key) -
mousemoveproc (nim2d: Nim2d; x, y, dx, dy: float) -
mousepressedproc (nim2d: Nim2d; x, y: float; button: MouseButton; clicks: uint8) -
mousereleasedproc (nim2d: Nim2d; x, y: float; button: MouseButton; clicks: uint8) -
mousewheelproc (nim2d: Nim2d; x, y: float) -
textinputproc (nim2d: Nim2d; text: string) -
gamepadpressedproc (nim2d: Nim2d; id: GamepadId; button: GamepadButton) -
gamepadreleasedproc (nim2d: Nim2d; id: GamepadId; button: GamepadButton) -
gamepadaxisproc (nim2d: Nim2d; id: GamepadId; axis: GamepadAxis; value: float) -
touchpressedproc (nim2d: Nim2d; id: int64; x, y, pressure: float) -
touchmovedproc (nim2d: Nim2d; id: int64; x, y, pressure: float) -
touchreleasedproc (nim2d: Nim2d; id: int64; x, y, pressure: float) -
window_shownproc (nim2d: Nim2d) -
window_hiddenproc (nim2d: Nim2d) -
window_movedproc (nim2d: Nim2d) -
window_resizedproc (nim2d: Nim2d) -
window_minimizedproc (nim2d: Nim2d) -
window_maximizedproc (nim2d: Nim2d) -
window_restoredproc (nim2d: Nim2d) -
window_enterproc (nim2d: Nim2d) -
window_leaveproc (nim2d: Nim2d) -
window_focus_gainedproc (nim2d: Nim2d) -
window_focus_lostproc (nim2d: Nim2d) -
window_closeproc (nim2d: Nim2d)