sokol

Cross-platform graphics and audio library bindings for Kit using sokol-zig

Files

FileDescription
.editorconfigEditor formatting configuration
.gitignoreGit ignore rules for build artifacts and dependencies
.tool-versionsasdf tool versions (Zig, Kit)
LICENSEMIT license file
README.mdThis file
examples/blend.kitBlend mode grid example
examples/buffer-offsets.kitBuffer offset example
examples/clear.kitMinimal animated framebuffer clear example
examples/cube.kitIndexed cube example
examples/debug-text-print.kitDebug text print API example
examples/debug-text-userfont.kitDebug text user font example
examples/debug-text.kitDebug text rendering example
examples/defer-pattern.kitResource cleanup pattern example
examples/instancing-compute.kitCompute and instancing example
examples/instancing.kitParticle instancing example
examples/mrt.kitMultiple render targets example
examples/noninterleaved.kitNon-interleaved vertex buffer example
examples/offscreen.kitOffscreen render target example
examples/quad.kitTextured quad example
examples/saudio.kitsokol_audio example
examples/sgl-context.kitsokol_gl context example
examples/sgl-points.kitsokol_gl points example
examples/sgl.kitsokol_gl immediate-mode rendering example
examples/shapes.kitProcedural shape generation example
examples/tex-cube.kitTextured cube example
examples/triangle.kitTriangle example
examples/vertex-pull.kitVertex pulling example
kit.tomlPackage manifest with metadata, capabilities, and tasks
src/sokol.kitKit API surface for sokolapp, sokolgfx, sokol_gl, debug text, audio, and shapes
tests/sokol.test.kitTests for exported API members and helper functions
zig/kit_ffi.zigKit FFI compatibility types and helpers
zig/sokol_wrapper.zigZig FFI wrapper used by extern-zig bindings

Dependencies

No Kit package dependencies.

This package requires the ffi capability:

[capabilities]
requires = ["ffi"]

The package is declared as kind = "ffi-zig" and uses zig/sokol_wrapper.zig as the bridge between Kit values and the Sokol-facing API.

Installation

kit add gitlab.com/kit-lang/packages/kit-sokol.git

Usage

import Kit.Sokol as Sokol

state = {
  g: 0.0
}

init = fn =>
  Sokol.Gfx.setup {
    environment: Sokol.Glue.environment
  }

frame = fn(st) =>
  g = st.g + 0.01
  new-g = if g > 1.0 then 0.0 else g

  pass-action = Sokol.Gfx.pass-action {
    colors: [{
      load-action: :clear,
      clear-value: {r: 1.0, g: new-g, b: 0.0, a: 1.0}
    }]
  }

  Sokol.Gfx.begin-pass {
    action: pass-action,
    swapchain: Sokol.Glue.swapchain
  }
  Sokol.Gfx.end-pass
  Sokol.Gfx.commit

  {g: new-g}

cleanup = fn =>
  Sokol.Gfx.shutdown

main = fn =>
  Sokol.App.run {
    init: init,
    frame: frame,
    cleanup: cleanup,
    initial-state: state,
    width: 640,
    height: 480,
    window-title: "kit-sokol",
    icon: {sokol-default: true}
  }

main

When working inside this repository, examples import the local source file:

import "../src/sokol" as Sokol

Installed package users should import Kit.Sokol.

API Overview

The public module is organized into records that mirror the Sokol libraries:

APIPurpose
Sokol.AppApplication lifecycle, window size, frame timing, and quit requests
Sokol.GfxGraphics setup, resources, passes, pipelines, bindings, uniforms, draw, and dispatch
Sokol.GlueEnvironment and swapchain helpers for connecting app and graphics
Sokol.GLImmediate-mode rendering helpers from sokol_gl
Sokol.DebugTextBuilt-in debug text rendering and font handles
Sokol.AudioAudio setup, device queries, and sample push
Sokol.ShapesProcedural mesh helpers for boxes, planes, spheres, cylinders, and torus shapes
Sokol.rgb, Sokol.rgba, Sokol.colorsColor helpers

The package also exports SokolError variants for typed error handling:

  • SokolConfigError
  • SokolCallbackError
  • SokolGraphicsError
  • SokolNotImplementedError

Development

Running Examples

Run examples with the interpreter:

kit run --allow=ffi examples/clear.kit

Compile examples to a native binary:

kit build --allow=ffi examples/clear.kit -o /tmp/kit-sokol-clear
/tmp/kit-sokol-clear

Running Tests

Run the test suite:

kit test

Run the test suite with coverage:

kit test --coverage

Running Parity

Check that interpreter and compiled example outputs match:

kit parity --no-spinner --failures-only

Parity writes timestamped *-results.txt files in the package root. They can be removed with the clean task.

Running kit dev

Run the standard development workflow:

kit dev

This will format, check, and test the package according to the Kit development workflow.

Generating Documentation

Generate API documentation from doc comments:

kit doc

Note: Kit sources with doc comments (##) will generate HTML documents in docs/*.html.

Cleaning Build Artifacts

Remove generated files, caches, parity results, and build artifacts:

kit task clean

Note: Defined in kit.toml.

Local Installation

To install this package locally for development:

kit install

This installs the package to ~/.kit/packages/@kit/sokol/, making it available for import as Kit.Sokol in other projects.

License

This package is released under the MIT License - see LICENSE for details.

Exported Functions & Types

SokolError

Sokol error type for typed error handling.

Variants

SokolConfigError {message}
SokolCallbackError {message}
SokolGraphicsError {message}
SokolNotImplementedError {message}

App

Application lifecycle and window query functions.

Gfx

Graphics API functions for setup, resources, passes, and draw calls.

Glue

Integration helpers connecting sokol_app and sokol_gfx.

GL

Immediate-mode rendering functions from sokol_gl.

DebugText

Debug text rendering functions and built-in font handles.

Audio

Audio setup, query, and sample push functions.

Shapes

Procedural shape generation functions.

rgb

Create a color from RGB values (0-255).

Int -> Int -> Int -> {r: Float, g: Float, b: Float, a: Float}

rgba

Create a color from RGBA values (0-255).

Int -> Int -> Int -> Int -> {r: Float, g: Float, b: Float, a: Float}

colors

Common colors.