Skip to main content

Python ctypes bindings for the Sokol cross-platform graphics library

Project description

Sokol Python Bindings

PyPI version CI Build Sokol Python 3.10+ License: MIT

Automatically generated Python ctypes bindings for the Sokol cross-platform graphics library using libclang.

Installation

pip install sokol

Or with uv:

uv add sokol

Features

  • Cross-platform support: Windows (D3D11), macOS (Metal), Linux (OpenGL)
  • Automatic platform detection: Just call load_sokol() and it works
  • Pre-built shared libraries: Included for all supported platforms
  • Automatic binding generation from Sokol C headers using libclang
  • Complete type coverage: enums, structs, function pointers, and API functions
  • Zero manual binding code - everything is parsed from headers

Supported Platforms

Platform Architecture Backend Library
Windows x64 D3D11 sokol-windows-x64.dll
macOS x64 Metal libsokol-macos-x64.dylib
macOS ARM64 Metal libsokol-macos-arm64.dylib
Linux x64 OpenGL libsokol-linux-x64.so

Generated Statistics

Type Count
Enums 42
Structs 199
Function Pointer Types 80
API Functions 207

Requirements

  • Python 3.10+
  • Pre-built shared libraries are included in the package

For Development/Binding Generation

  • libclang (pip install libclang)

Quick Start

1. Install Dependencies

uv pip install libclang

2. Generate Bindings

python setup_and_generate.py

This will:

  • Download Sokol headers from GitHub to ./sokol/
  • Parse headers with libclang
  • Generate sokol_bindings.py (~6000 lines)

3. Run the Demo

python main.py

Project Structure

├── generate_bindings.py    # Binding generator using libclang
├── setup_and_generate.py   # Downloads headers & runs generator
├── sokol_bindings.py       # Auto-generated ctypes bindings
├── main.py                 # Triangle demo application
├── sokol-dll.dll           # Pre-built Sokol DLL (you provide)
├── sokol/                  # Downloaded Sokol headers
│   ├── sokol_app.h
│   ├── sokol_gfx.h
│   ├── sokol_glue.h
│   └── sokol_log.h
└── README.md

Usage

Basic Usage

from sokol.sokol_ahmedaliadeel import *

# Auto-detect platform and load the appropriate library
lib = load_sokol()

# Now use Sokol API
# lib.sg_setup(), lib.sapp_run(), etc.

Explicit Library Path

from sokol.sokol_ahmedaliadeel import load_sokol

# Load a specific library file
lib = load_sokol('/path/to/libsokol.so')

Creating Structs

# All Sokol structs are available as ctypes Structures
desc = sg_desc()
ctypes.memset(ctypes.addressof(desc), 0, ctypes.sizeof(desc))

# Set fields
desc.environment = lib.sglue_environment()

Callbacks

# Use generated function pointer types for callbacks
init_callback = _FuncPtr_init_cb_66(my_init_function)
frame_callback = _FuncPtr_frame_cb_67(my_frame_function)

desc = sapp_desc()
desc.init_cb = init_callback
desc.frame_cb = frame_callback

Vertex Buffers

# Create vertex data
vertices = (ctypes.c_float * 24)(
    # x, y, z, w,    r, g, b, a
    0.0,  0.5, 0.5, 1.0,  1.0, 0.0, 0.0, 1.0,  # top
    0.5, -0.5, 0.5, 1.0,  0.0, 1.0, 0.0, 1.0,  # right
   -0.5, -0.5, 0.5, 1.0,  0.0, 0.0, 1.0, 1.0,  # left
)

# Create buffer
buf_desc = sg_buffer_desc()
buf_desc.data.ptr = ctypes.cast(vertices, ctypes.c_void_p)
buf_desc.data.size = ctypes.sizeof(vertices)
vbuf = lib.sg_make_buffer(ctypes.byref(buf_desc))

Generator Details

The generate_bindings.py script:

  1. Parses headers with libclang in combined mode (all headers together for proper dependency resolution)
  2. Extracts types:
    • Enums → Python constants
    • Structs → ctypes.Structure subclasses
    • Function pointers → ctypes.CFUNCTYPE types
    • Functions → DLL function bindings with argtypes/restype
  3. Handles edge cases:
    • Anonymous structs (filtered out)
    • Nested struct arrays
    • Function pointer fields in structs
    • Proper type ordering for forward declarations

Command Line Usage

# Generate with custom paths
python generate_bindings.py --sokol-dir ./sokol --output sokol_bindings.py

# Specify libclang path (if not auto-detected)
python generate_bindings.py --libclang /path/to/libclang.dll

Sokol Headers

The generator supports these Sokol headers:

Header Description
sokol_log.h Logging utilities
sokol_gfx.h 3D graphics API abstraction
sokol_app.h Application/window handling
sokol_glue.h Glue between sokol_app and sokol_gfx

Building sokol-dll.dll

If you need to build the DLL yourself:

// sokol_dll.c
#define SOKOL_DLL
#define SOKOL_D3D11
#define SOKOL_NO_ENTRY
#define SOKOL_IMPL
#include "sokol_log.h"
#include "sokol_gfx.h"
#include "sokol_app.h"
#include "sokol_glue.h"

Compile with MSVC:

cl /LD /O2 /DSOKOL_DLL sokol_dll.c /Fe:sokol-dll.dll

License

This binding generator is provided as-is. Sokol itself is licensed under the zlib license - see sokol for details.

Links

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sokol-0.1.0.tar.gz (559.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sokol-0.1.0-py3-none-any.whl (558.8 kB view details)

Uploaded Python 3

File details

Details for the file sokol-0.1.0.tar.gz.

File metadata

  • Download URL: sokol-0.1.0.tar.gz
  • Upload date:
  • Size: 559.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for sokol-0.1.0.tar.gz
Algorithm Hash digest
SHA256 edc92db90023b0a8a45f41ae631109c308b8c512165f59a6db0ca2652efaa078
MD5 783d67e77a80a882ff83c7643d0ee883
BLAKE2b-256 f4d56755f1e6a50fe828f6338d199b941bac2d1ebf864cf9aac8a949b7c35b24

See more details on using hashes here.

Provenance

The following attestation bundles were made for sokol-0.1.0.tar.gz:

Publisher: publish.yml on ahmadaliadeel/sokol-py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file sokol-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: sokol-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 558.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for sokol-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fd8beca2011ae6e150b30cdb9f6758a8697acd29cc3333590bbe71a13117d7d2
MD5 cfe50dbf30f212b828189c9e8fdbe118
BLAKE2b-256 73e3276300d71247765f5fa50761368a99eb98099c33ec36934954ede31678eb

See more details on using hashes here.

Provenance

The following attestation bundles were made for sokol-0.1.0-py3-none-any.whl:

Publisher: publish.yml on ahmadaliadeel/sokol-py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page