Skip to content

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

  • x float32
  • y float32
  • u float32
  • v float32
  • r float32
  • g float32
  • b float32
  • a float32

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

  • tex ptr SDL_GPUTexture
  • width int32
  • height int32
  • tint Color – color/alpha modulation applied when drawn
  • filter Filter – linear by default
  • wrap Wrap – 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

  • depth ptr 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

  • u0 float32
  • v0 float32
  • u1 float32
  • v1 float32
  • w float32
  • h float32

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

  • engine pointer – TTF_TextEngine (GPU text engine)
  • font pointer – TTF_Font (nil for a bitmap/image font)
  • size cint
  • img Image – glyph sheet, set for a bitmap/image font
  • glyphSet string – the characters, in image order
  • glyphX seq[int32] – each glyph's x and width in the sheet
  • glyphW seq[int32] – each glyph's x and width in the sheet
  • imgH int32 – glyph height (the sheet height)
  • spacing int32 – 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

  • pipelines array[BlendMode, ptr SDL_GPUGraphicsPipeline]
  • uniform seq[byte]
  • hasUniform bool

Scissor

type Scissor = object

Fields

  • on bool
  • x int32
  • y int32
  • w int32
  • h int32

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

  • saveDir string
  • sourceDir string
  • mounts seq[string]
  • identitySet bool

DrawCmd

type DrawCmd = object

A run of indices sharing pipeline/blend/texture/scissor/shader state.

Fields

  • kind PipelineKind
  • blend BlendMode
  • texture ptr SDL_GPUTexture
  • sampler ptr SDL_GPUSampler
  • shader Shader
  • scissor Scissor
  • stencil uint8
  • firstIndex uint32
  • indexCount uint32

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

  • target ptr SDL_GPUTexture
  • depth ptr SDL_GPUTexture
  • w int32
  • h int32
  • doClear bool
  • clearColor Color
  • projection array[16, float32]
  • cmds seq[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

  • device ptr SDL_GPUDevice
  • window ptr SDL_Window
  • swFormat SDL_GPUTextureFormat – swapchain format; render targets must match it
  • shaderFormat SDL_GPUShaderFormat – MSL or SPIR-V, chosen from the device backend
  • sampler ptr SDL_GPUSampler – default sampler (linear, clamp)
  • samplers array[Filter, array[Wrap, ptr SDL_GPUSampler]] – cache for other combos
  • ssFactor int32 – supersample factor for anti-aliasing (1 = off)
  • ssTex ptr SDL_GPUTexture – the high-res offscreen target when supersampling
  • ssW int32 – supersample target size
  • ssH int32 – supersample target size
  • frameW int32 – logical frame size, for the downscale blit
  • frameH int32 – logical frame size, for the downscale blit
  • stencilEnabled bool – whether the depth-stencil machinery is built
  • depthFormat SDL_GPUTextureFormat – chosen depth-stencil format
  • screenDepth ptr SDL_GPUTexture – depth-stencil targets for screen/SS
  • ssDepth ptr SDL_GPUTexture – depth-stencil targets for screen/SS
  • screenDepthW int32 – size the screen depth target was made at
  • screenDepthH int32 – size the screen depth target was made at
  • stencilMode uint8 – 0 none, 1 write the mask, 2 test against it
  • stencilWritePipe ptr SDL_GPUGraphicsPipeline
  • stencilTestPipes array[PipelineKind, array[BlendMode, ptr SDL_GPUGraphicsPipeline]]
  • whiteTex ptr SDL_GPUTexture – 1x1 white, bound when a shader draw has no texture
  • pipelines array[PipelineKind, array[BlendMode, ptr SDL_GPUGraphicsPipeline]]
  • vertices seq[Vertex]
  • indices seq[uint32]
  • vbuf ptr SDL_GPUBuffer
  • ibuf ptr SDL_GPUBuffer
  • vbufCap int
  • ibufCap int
  • transferBuf ptr SDL_GPUTransferBuffer
  • transferCap int
  • cmd ptr SDL_GPUCommandBuffer
  • swTex ptr SDL_GPUTexture
  • passes seq[RenderPassRec]
  • tempTextures seq[ptr SDL_GPUTexture] – released after the frame submits
  • transform Transform
  • transformStack seq[Transform]
  • curScissor Scissor
  • curShader Shader

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

  • width int32
  • height int32
  • gpu GpuContext
  • background Color
  • color Color
  • font Font
  • fs Filesystem
  • blend BlendMode
  • running bool
  • perfFreq uint64
  • lastCounter uint64
  • dt float
  • fps float
  • load proc (nim2d: Nim2d)
  • draw proc (nim2d: Nim2d)
  • quit proc (nim2d: Nim2d)
  • update proc (nim2d: Nim2d; dt: float)
  • keydown proc (nim2d: Nim2d; key: Key)
  • keyup proc (nim2d: Nim2d; key: Key)
  • mousemove proc (nim2d: Nim2d; x, y, dx, dy: float)
  • mousepressed proc (nim2d: Nim2d; x, y: float; button: MouseButton; clicks: uint8)
  • mousereleased proc (nim2d: Nim2d; x, y: float; button: MouseButton; clicks: uint8)
  • mousewheel proc (nim2d: Nim2d; x, y: float)
  • textinput proc (nim2d: Nim2d; text: string)
  • gamepadpressed proc (nim2d: Nim2d; id: GamepadId; button: GamepadButton)
  • gamepadreleased proc (nim2d: Nim2d; id: GamepadId; button: GamepadButton)
  • gamepadaxis proc (nim2d: Nim2d; id: GamepadId; axis: GamepadAxis; value: float)
  • touchpressed proc (nim2d: Nim2d; id: int64; x, y, pressure: float)
  • touchmoved proc (nim2d: Nim2d; id: int64; x, y, pressure: float)
  • touchreleased proc (nim2d: Nim2d; id: int64; x, y, pressure: float)
  • window_shown proc (nim2d: Nim2d)
  • window_hidden proc (nim2d: Nim2d)
  • window_moved proc (nim2d: Nim2d)
  • window_resized proc (nim2d: Nim2d)
  • window_minimized proc (nim2d: Nim2d)
  • window_maximized proc (nim2d: Nim2d)
  • window_restored proc (nim2d: Nim2d)
  • window_enter proc (nim2d: Nim2d)
  • window_leave proc (nim2d: Nim2d)
  • window_focus_gained proc (nim2d: Nim2d)
  • window_focus_lost proc (nim2d: Nim2d)
  • window_close proc (nim2d: Nim2d)
Generated with mkdocstrings-nim