Skip to content

Commit

Permalink
Add more examples (#46)
Browse files Browse the repository at this point in the history
* add texcube sample
* vertexpull example added
* noninterleaved and quad examples added
* shapes and bufferoffsets exampels added
* instancing example added
math module add `@nogc` & `nothrow` attributes
* offscreen example added
* some minor change
- all examples modules have private content module
- build-system detect by module name
  • Loading branch information
kassane authored Dec 29, 2024
1 parent 98f8c03 commit 4f9ad69
Show file tree
Hide file tree
Showing 28 changed files with 2,709 additions and 204 deletions.
8 changes: 8 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,18 @@ jobs:
dub build :sgl_points
dub build :debugtext
dub build :cube
dub build :texcube
dub build :blend
dub build :triangle
dub build :instancing
dub build :mrt
dub build :noninterleaved
dub build :quad
dub build :offscreen
dub build :bufferoffsets
dub build :user_data
dub build :vertexpull
dub build :shapes
dub build :imgui
dub build :droptest
Expand Down
41 changes: 26 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Auto-generated [D](https://dlang.org) bindings for the [sokol headers](https://g
#### Targets

- Native
- Wasm (`-Dtarget=wasm32-emscripten-none`)
- Wasm (`-Dtarget=wasm32-emscripten-none`) - LTO enabled on release-mode.

By default, the backend 3D API will be selected based on the target platform:

Expand All @@ -27,24 +27,35 @@ On Linux install the following packages: libglu1-mesa-dev, mesa-common-dev, xorg

```bash
# build sokol library + all examples [default: static library]
zig build -Doptimize=ReleaseSafe
zig build

# build sokol shared library + all examples
zig build -Doptimize=ReleaseSafe -Dshared
zig build -Dshared

# build sokol library only
zig build -Dartifact

# Run Examples
zig build run-blend -Doptimize=ReleaseSafe
zig build run-clear -Doptimize=ReleaseSafe
zig build run-cube -Doptimize=ReleaseSafe
zig build run-debugtext -Doptimize=ReleaseSafe
zig build run-mrt -Doptimize=ReleaseSafe
zig build run-saudio -Doptimize=ReleaseSafe
zig build run-sgl_context -Doptimize=ReleaseSafe
zig build run-sgl_points -Doptimize=ReleaseSafe
zig build run-user_data -Doptimize=ReleaseSafe
zig build run-triangle -Doptimize=ReleaseSafe
zig build run-imgui -Doptimize=ReleaseSafe -Dimgui # optional: -Dimgui-version=docking
zig build run-droptest -Doptimize=ReleaseSafe -Dimgui # optional: -Dimgui-version=docking
zig build run-blend
zig build run-bufferoffsets
zig build run-clear
zig build run-cube
zig build run-debugtext
zig build run-mrt
zig build run-saudio
zig build run-instancing
zig build run-offscreen
zig build run-sgl_context
zig build run-sgl_points
zig build run-user_data
zig build run-noninterleaved
zig build run-texcube
zig build run-quad
zig build run-triangle
zig build run-shapes
zig build run-vertexpull
zig build run-imgui -Dimgui # optional: -Dimgui-version=docking
zig build run-droptest -Dimgui # optional: -Dimgui-version=docking

zig build --help
# Project-Specific Options:
Expand Down
26 changes: 20 additions & 6 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -239,18 +239,26 @@ pub fn build(b: *Build) !void {
} else {
// build examples
const examples = .{
"blend",
"bufferoffsets",
"clear",
"triangle",
"cube",
"blend",
"debugtext",
"instancing",
"mrt",
"noninterleaved",
"offscreen",
"quad",
"saudio",
"sgl_context",
"sgl_points",
"debugtext",
"shapes",
"texcube",
"triangle",
"user_data", // Need GC for user data [associative array]
"imgui",
"vertexpull",
"droptest",
"imgui",
};

inline for (examples) |example| {
Expand Down Expand Up @@ -402,8 +410,13 @@ pub fn ldcBuildStep(b: *Build, options: DCompileStep) !*std.Build.Step.InstallDi
// keep all function bodies in .di files
ldc_exec.addArg("-Hkeep-all-bodies");

// automatically finds needed library files and builds
ldc_exec.addArg("-i");
// automatically finds needed modules
ldc_exec.addArgs(&.{
"-i=sokol",
"-i=shaders",
"-i=handmade",
"-i=cimgui",
});

// sokol include path
ldc_exec.addArg(b.fmt("-I{s}", .{b.pathJoin(&.{ rootPath(), "src" })}));
Expand Down Expand Up @@ -757,6 +770,7 @@ fn buildShaders(b: *Build, target: Build.ResolvedTarget) void {
"shapes.glsl",
"texcube.glsl",
"blend.glsl",
"vertexpull.glsl",
};
const optional_shdc: ?[:0]const u8 = comptime switch (builtin.os.tag) {
.windows => "win32/sokol-shdc.exe",
Expand Down
2 changes: 1 addition & 1 deletion build.zig.zon
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.{
.name = "sokol-d",
.version = "0.1.6",
.version = "0.1.7",
.min_zig_version = "0.13.0",
.paths = .{
"src",
Expand Down
176 changes: 153 additions & 23 deletions dub.sdl

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions src/examples/blend.d
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
//------------------------------------------------------------------------------
module examples.blend;

private:

import sg = sokol.gfx;
import app = sokol.app;
import log = sokol.log;
Expand Down
131 changes: 131 additions & 0 deletions src/examples/bufferoffsets.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
//------------------------------------------------------------------------------
// bufferoffsets.d
//
// Render separate geometries in vertex- and index-buffers with
// buffer offsets.
//------------------------------------------------------------------------------
module examples.bufferoffsets;

private:

import sg = sokol.gfx;
import app = sokol.app;
import log = sokol.log;
import sglue = sokol.glue;
import shd = examples.shaders.bufferoffsets;

extern (C):
@safe:

struct State
{
sg.Pipeline pip;
sg.Bindings bind;
sg.PassAction passAction = {};
}

struct Vertex
{
float x = 0.0f, y = 0.0f;
float r = 0.0f, g = 0.0f, b = 0.0f;
}

static State state;

void init()
{
sg.Desc gfxd = {environment: sglue.environment,
logger: {func: &log.func}};
sg.setup(gfxd);

state.passAction.colors[0].load_action = sg.LoadAction.Clear;
state.passAction.colors[0].clear_value.r = 0.5;
state.passAction.colors[0].clear_value.g = 0.5;
state.passAction.colors[0].clear_value.b = 1.0;
state.passAction.colors[0].clear_value.a = 1.0;

Vertex[7] vertices = [
Vertex(0.0, 0.55, 1.0, 0.0, 0.0),
Vertex(0.25, 0.05, 0.0, 1.0, 0.0),
Vertex(-0.25, 0.05, 0.0, 0.0, 1.0),

Vertex(-0.25, -0.05, 0.0, 0.0, 1.0),
Vertex(0.25, -0.05, 0.0, 1.0, 0.0),
Vertex(0.25, -0.55, 1.0, 0.0, 0.0),
Vertex(-0.25, -0.55, 1.0, 1.0, 0.0),
];

sg.BufferDesc vbufd = {data: {ptr: vertices.ptr, size: vertices.sizeof},};
state.bind.vertex_buffers[0] = sg.makeBuffer(vbufd);

ushort[9] indices = [
0, 1, 2,
0, 1, 2,
0, 2, 3,
];

// dfmt off
sg.BufferDesc ibufd = {
type: sg.BufferType.Indexbuffer,
data: {ptr: indices.ptr, size: indices.sizeof},
};
state.bind.index_buffer = sg.makeBuffer(ibufd);

sg.PipelineDesc pld = {
layout: {
attrs: [
shd.ATTR_BUFFEROFFSETS_POSITION: {format: sg.VertexFormat.Float2},
shd.ATTR_BUFFEROFFSETS_COLOR0: {format: sg.VertexFormat.Float3},
],
},
shader: sg.makeShader(shd.bufferoffsetsShaderDesc(sg.queryBackend())),
index_type: sg.IndexType.Uint16,
};
// dfmt on
state.pip = sg.makePipeline(pld);
}

void frame()
{
sg.Pass pass = {action: state.passAction, swapchain: sglue.swapchain()};
sg.beginPass(pass);
sg.applyPipeline(state.pip);

// render the triangle
state.bind.vertex_buffer_offsets[0] = 0;
state.bind.index_buffer_offset = 0;
sg.applyBindings(state.bind);
sg.draw(0, 3, 1);

// render the quad
state.bind.vertex_buffer_offsets[0] = 3 * Vertex.sizeof;
state.bind.index_buffer_offset = 3 * ushort.sizeof;
sg.applyBindings(state.bind);
sg.draw(0, 6, 1);

sg.endPass();
sg.commit();
}

void cleanup()
{
sg.shutdown();
}

// dfmt off
void main()
{
app.Desc runner = {
window_title: "bufferoffsets.d",
init_cb: &init,
frame_cb: &frame,
cleanup_cb: &cleanup,
width: 800,
height: 600,
sample_count: 4,
icon: {sokol_default: true},
logger: {func: &log.func}
};
app.run(runner);
}
// dfmt on
42 changes: 21 additions & 21 deletions src/examples/clear.d
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
//------------------------------------------------------------------------------
module examples.clear;

private:

import sg = sokol.gfx;
import sglue = sokol.glue;
import sapp = sokol.app;
Expand All @@ -15,36 +17,32 @@ extern (C):

sg.PassAction pass_action = {
colors: [
{ load_action: sg.LoadAction.Clear, clear_value: { r: 1, g: 1, b: 0, a: 1 } }
{load_action: sg.LoadAction.Clear, clear_value: {r: 1, g: 1, b: 0, a: 1}}
]
};

static void init() {
static void init()
{
sg.Desc gfx = {
environment: sglue.environment(),
logger: { func: &log.slog_func },
logger: {func: &log.slog_func},
};
sg.setup(gfx);
debug

version (WebAssembly)
{ /* none */ }
else version (D_BetterC)
{ /* none */ }
else
{
version (WebAssembly)
{/* none */}
else
{
import std.stdio : writeln;
import std.stdio : writeln;

try
{
writeln("Backend: ", sg.queryBackend());
}
catch (Exception)
{
}
}
debug writeln("Backend: ", sg.queryBackend());
}
}

static void frame() {
static void frame()
{
const g = pass_action.colors[0].clear_value.g + 0.01;
pass_action.colors[0].clear_value.g = g > 1.0 ? 0.0 : g;
sg.Pass pass = {action: pass_action, swapchain: sglue.swapchain};
Expand All @@ -53,11 +51,13 @@ static void frame() {
sg.commit();
}

static void cleanup() {
static void cleanup()
{
sg.shutdown();
}

void main() {
void main()
{
sapp.Desc runner = {
window_title: "clear.d",
init_cb: &init,
Expand All @@ -66,7 +66,7 @@ void main() {
width: 640,
height: 480,
win32_console_attach: true,
icon: { sokol_default: true },
icon: {sokol_default: true},
logger: {func: &log.func}
};
sapp.run(runner);
Expand Down
2 changes: 2 additions & 0 deletions src/examples/cube.d
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
//------------------------------------------------------------------------------
module examples.cube;

private:

import sg = sokol.gfx;
import app = sokol.app;
import log = sokol.log;
Expand Down
Loading

0 comments on commit 4f9ad69

Please sign in to comment.