code-disaster / sokol-rs

Rust bindings to sokol (https://github.com/floooh/sokol).
zlib License
35 stars 8 forks source link
rust-bindings rust-ffi

This code is terribly outdated.

There are "official" (and more importantly, auto-generated) Rust bindings now. Head over to sokol-rust to check them out.

sokol-rs

This repository contains source code of the following Rust library crates:

To complement the crates above:

How to build

The current version compiles (and has been tested) with stable Rust (v1.34.1) on Windows (both MSVC and GNU toolchains), MacOS and Linux.

> git clone --recursive https://github.com/code-disaster/sokol-rs
> cd sokol-rs
> cargo build

The sokol-samples folder contains some examples ported from sokol-samples/sapp.

> cargo run --bin clear-sapp 

About the implementation

The SApp program loop

In the C version of sokol_app, when compiled with SOKOL_NO_ENTRY, you call sapp_run(), passing callback function pointers for setup, frame updates, and cleanup.

In the Rust version, you call sokol::app::sapp_run(). This hands over control to the C library, which then will operate as usual. The C callbacks are implemented by sokol-rs. They are forwarded to your application via the SApp trait. User applications implement this trait to power the application loop.

Check the clear-sapp sample for a minimal implementation.

API style and implementation details

I tried to stay as close as possible to the source, while adjusting to the Rust naming conventions, as well as making the public API more convenient in places a direct port would be too cumbersome.

In the Rust library, the app, gfx and audio modules are not as separable as their C counterparts. Essentially, sokol-rs assumes that you use them in conjunction.

Status

The following libraries are implemented:

header Rust module status notes
sokol_app.h sokol::app done
sokol_args.h n/a n/a low priority: there are many cmdline parsers for Rust already
sokol_audio.h sokol::audio done callback API via trait in sokol::app
sokol_gfx.h sokol::gfx mostly done trace hooks are enabled (and consumed by the sokol_gfx_imgui implementation in the sokol-imgui crate)

missing: separate resource management, render contexts, user-provided buffers

not implemented: sg_query_*_info() functions
sokol_time.h sokol::time done

Additionally, some utility libraries are available:

header Rust module status notes
sokol_gfx_imgui.h sokol_imgui::gfx partially
done
sg_imgui_t is wrapped opaquely instead of replicated in Rust

missing: sg_imgui_draw_*_content and sg_imgui_draw_*_window functions
sokol_gl.h n/a n/a no plans yet
sokol_imgui.h sokol_imgui::imgui done

Remarks