Skip to content

types

types

Core nim2d types.

This module is mostly data, with the behaviour living in the backend renderer and the public modules. The two exceptions are the device-liveness flag and the =destroy hooks for the texture, font and shader resources, which Nim wants in the same module as the type they belong to. It stays a leaf of the dependency graph, since types is imported by everything and itself depends only on the SDL shims 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 DrawableObj

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 TextureObj

A GPU texture with its size and sampling state. Images and canvases are

both textures, which is why they draw the same way. Its texture frees itself when the texture is collected; destroy frees it early.

Image

type Image = ref ImageObj

A texture loaded from a file or uploaded from an ImageData.

Canvas

type Canvas = ref CanvasObj

A render target (GPU texture created with COLOR_TARGET usage). It frees

its color target, and the paired depth target when present, on collection; destroy frees them early.

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 FontObj

A font for print: either a TrueType font opened through SDL_ttf, or a

bitmap font built from a glyph sheet by newImageFont. Its handles free themselves when the font is collected; destroy frees them early.

Shader

type Shader = ref ShaderObj

A user fragment shader compiled into one pipeline per blend mode, with an

optional fragment uniform buffer filled by send. Its pipelines free themselves when the shader is collected; destroy frees them early.

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
  • teardownRan bool – set once teardown has run, so it never runs twice
  • 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)

gpuLiveDevice

var gpuLiveDevice: ptr SDL_GPUDevice

The live GPU device. The renderer sets this when the context is created and

clears it on teardown. Device-bound destructors check it first, so a font, shader or other resource collected after the engine has shut down frees nothing and the device's own teardown reclaims it instead. This sidesteps the unspecified order in which ORC frees globals at exit.

Type: ptr SDL_GPUDevice

Generated with mkdocstrings-nim