Skip to content

Commit

Permalink
new example: sgl-points
Browse files Browse the repository at this point in the history
  • Loading branch information
kassane committed Feb 5, 2024
1 parent 8c99c8e commit d3e738b
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 12 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ jobs:
run: |
dub build :clear
dub build :sgl_context
dub build :sgl_points
dub build :debugtext_print
# dub build :cube
# dub build :blend
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ zig build run-debugtext_print -Doptimize=ReleaseFast
zig build run-mrt -Doptimize=ReleaseFast
zig build run-saudio -Doptimize=ReleaseFast
zig build run-sgl_context -Doptimize=ReleaseFast
zig build run-sgl_points -Doptimize=ReleaseFast
zig build run-user_data -Doptimize=ReleaseFast
zig build run-triangle -Doptimize=ReleaseFast

Expand Down
1 change: 1 addition & 0 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ pub fn build(b: *Build) !void {
"mrt",
"saudio",
"sgl_context",
"sgl_points",
"debugtext_print",
"user_data", // Need GC for user data [associative array]
};
Expand Down
29 changes: 20 additions & 9 deletions dub.sdl
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ subPackage {
libs "sokol"
lflags "-Lzig-out/lib" platform="posix"
lflags "/LIBPATH:zig-out/lib" platform="windows"
excludedSourceFiles "src/examples/sgl_context.d" "src/examples/triangle.d" "src/examples/saudio.d" "src/examples/debugtext_print.d" "src/examples/mrt.d" "src/examples/user_data.d" "src/examples/cube.d" "src/examples/blend.d" "src/shaders/*.d"
excludedSourceFiles "src/examples/sgl_context.d" "src/examples/triangle.d" "src/examples/sgl_points.d" "src/examples/saudio.d" "src/examples/debugtext_print.d" "src/examples/mrt.d" "src/examples/user_data.d" "src/examples/cube.d" "src/examples/blend.d" "src/shaders/*.d"
preBuildCommands "zig build -Dshared -Doptimize=ReleaseFast"
}
subPackage {
Expand All @@ -47,7 +47,18 @@ subPackage {
libs "sokol"
lflags "-Lzig-out/lib" platform="posix"
lflags "/LIBPATH:zig-out/lib" platform="windows"
excludedSourceFiles "src/examples/clear.d" "src/examples/triangle.d" "src/examples/saudio.d" "src/examples/debugtext_print.d" "src/examples/mrt.d" "src/examples/user_data.d" "src/examples/cube.d" "src/examples/blend.d" "src/shaders/*.d"
excludedSourceFiles "src/examples/clear.d" "src/examples/triangle.d" "src/examples/sgl_points.d" "src/examples/saudio.d" "src/examples/debugtext_print.d" "src/examples/mrt.d" "src/examples/user_data.d" "src/examples/cube.d" "src/examples/blend.d" "src/shaders/*.d"
preBuildCommands "zig build -Dshared -Doptimize=ReleaseFast"
}
subPackage {
name "sgl_points"
targetType "executable"
targetPath "build"
sourceFiles "src/examples/sgl_points.d"
libs "sokol"
lflags "-Lzig-out/lib" platform="posix"
lflags "/LIBPATH:zig-out/lib" platform="windows"
excludedSourceFiles "src/examples/clear.d" "src/examples/triangle.d" "src/examples/sgl_context.d" "src/examples/saudio.d" "src/examples/debugtext_print.d" "src/examples/mrt.d" "src/examples/user_data.d" "src/examples/cube.d" "src/examples/blend.d" "src/shaders/*.d"
preBuildCommands "zig build -Dshared -Doptimize=ReleaseFast"
}
subPackage {
Expand All @@ -58,7 +69,7 @@ subPackage {
libs "sokol"
lflags "-Lzig-out/lib" platform="posix"
lflags "/LIBPATH:zig-out/lib" platform="windows"
excludedSourceFiles "src/examples/sgl_context.d" "src/examples/triangle.d" "src/examples/saudio.d" "src/examples/clear.d" "src/examples/mrt.d" "src/examples/user_data.d" "src/examples/cube.d" "src/examples/blend.d" "src/shaders/*.d"
excludedSourceFiles "src/examples/sgl_context.d" "src/examples/triangle.d" "src/examples/sgl_points.d" "src/examples/saudio.d" "src/examples/clear.d" "src/examples/mrt.d" "src/examples/user_data.d" "src/examples/cube.d" "src/examples/blend.d" "src/shaders/*.d"
preBuildCommands "zig build -Dshared -Doptimize=ReleaseFast"
}
subPackage {
Expand All @@ -69,7 +80,7 @@ subPackage {
libs "sokol"
lflags "-Lzig-out/lib" platform="posix"
lflags "/LIBPATH:zig-out/lib" platform="windows"
excludedSourceFiles "src/examples/sgl_context.d" "src/examples/clear.d" "src/examples/saudio.d" "src/examples/debugtext_print.d" "src/examples/mrt.d" "src/examples/user_data.d" "src/examples/cube.d" "src/examples/blend.d" "src/shaders/*.d"
excludedSourceFiles "src/examples/sgl_context.d" "src/examples/clear.d" "src/examples/sgl_points.d" "src/examples/saudio.d" "src/examples/debugtext_print.d" "src/examples/mrt.d" "src/examples/user_data.d" "src/examples/cube.d" "src/examples/blend.d" "src/shaders/*.d"
preBuildCommands "zig build -Dshared -Doptimize=ReleaseFast"
}
subPackage {
Expand All @@ -80,7 +91,7 @@ subPackage {
libs "sokol"
lflags "-Lzig-out/lib" platform="posix"
lflags "/LIBPATH:zig-out/lib" platform="windows"
excludedSourceFiles "src/examples/sgl_context.d" "src/examples/clear.d" "src/examples/saudio.d" "src/examples/debugtext_print.d" "src/examples/mrt.d" "src/examples/user_data.d" "src/examples/cube.d" "src/examples/triangle.d" "src/shaders/*.d"
excludedSourceFiles "src/examples/sgl_context.d" "src/examples/clear.d" "src/examples/saudio.d" "src/examples/debugtext_print.d" "src/examples/mrt.d" "src/examples/user_data.d" "src/examples/cube.d" "src/examples/triangle.d" "src/examples/sgl_points.d" "src/shaders/*.d"
preBuildCommands "zig build -Dshared -Doptimize=ReleaseFast"
}
subPackage {
Expand All @@ -91,7 +102,7 @@ subPackage {
libs "sokol"
lflags "-Lzig-out/lib" platform="posix"
lflags "/LIBPATH:zig-out/lib" platform="windows"
excludedSourceFiles "src/examples/sgl_context.d" "src/examples/clear.d" "src/examples/triangle.d" "src/examples/debugtext_print.d" "src/examples/mrt.d" "src/examples/user_data.d" "src/examples/cube.d" "src/examples/blend.d" "src/shaders/*.d"
excludedSourceFiles "src/examples/sgl_context.d" "src/examples/clear.d" "src/examples/triangle.d" "src/examples/sgl_points.d" "src/examples/debugtext_print.d" "src/examples/mrt.d" "src/examples/user_data.d" "src/examples/cube.d" "src/examples/blend.d" "src/shaders/*.d"
preBuildCommands "zig build -Dshared -Doptimize=ReleaseFast"
}
subPackage {
Expand All @@ -102,7 +113,7 @@ subPackage {
libs "sokol"
lflags "-Lzig-out/lib" platform="posix"
lflags "/LIBPATH:zig-out/lib" platform="windows"
excludedSourceFiles "src/examples/sgl_context.d" "src/examples/clear.d" "src/examples/saudio.d" "src/examples/debugtext_print.d" "src/examples/triangle.d" "src/examples/user_data.d" "src/examples/cube.d" "src/examples/blend.d" "src/shaders/*.d"
excludedSourceFiles "src/examples/sgl_context.d" "src/examples/clear.d" "src/examples/saudio.d" "src/examples/debugtext_print.d" "src/examples/triangle.d" "src/examples/sgl_points.d" "src/examples/user_data.d" "src/examples/cube.d" "src/examples/blend.d" "src/shaders/*.d"
preBuildCommands "zig build -Dshared -Doptimize=ReleaseFast"
}
subPackage {
Expand All @@ -113,7 +124,7 @@ subPackage {
libs "sokol"
lflags "-Lzig-out/lib" platform="posix"
lflags "/LIBPATH:zig-out/lib" platform="windows"
excludedSourceFiles "src/examples/sgl_context.d" "src/examples/clear.d" "src/examples/saudio.d" "src/examples/debugtext_print.d" "src/examples/triangle.d" "src/examples/user_data.d" "src/examples/mrt.d" "src/examples/blend.d" "src/shaders/*.d"
excludedSourceFiles "src/examples/sgl_context.d" "src/examples/clear.d" "src/examples/saudio.d" "src/examples/debugtext_print.d" "src/examples/triangle.d" "src/examples/sgl_points.d" "src/examples/user_data.d" "src/examples/mrt.d" "src/examples/blend.d" "src/shaders/*.d"
preBuildCommands "zig build -Dshared -Doptimize=ReleaseFast"
}
subPackage {
Expand All @@ -124,6 +135,6 @@ subPackage {
libs "sokol"
lflags "-Lzig-out/lib" platform="posix"
lflags "/LIBPATH:zig-out/lib" platform="windows"
excludedSourceFiles "src/examples/sgl_context.d" "src/examples/clear.d" "src/examples/saudio.d" "src/examples/debugtext_print.d" "src/examples/triangle.d" "src/examples/cube.d" "src/examples/mrt.d" "src/examples/blend.d" "src/shaders/*.d"
excludedSourceFiles "src/examples/sgl_context.d" "src/examples/clear.d" "src/examples/saudio.d" "src/examples/debugtext_print.d" "src/examples/triangle.d" "src/examples/sgl_points.d" "src/examples/cube.d" "src/examples/mrt.d" "src/examples/blend.d" "src/shaders/*.d"
preBuildCommands "zig build -Dshared -Doptimize=ReleaseFast"
}
131 changes: 131 additions & 0 deletions src/examples/sgl_points.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
//------------------------------------------------------------------------------
// sgl_points.d
//
// Test sokol-gl point rendering.
//
// (port of this C sample: https://floooh.github.io/sokol-html5/sgl-points-sapp.html)
//------------------------------------------------------------------------------

module examples.sgl_points;

import sg = sokol.gfx;
import sgapp = sokol.glue;
import sapp = sokol.app;
import slog = sokol.log;
import sgl = sokol.gl;
import handmade.math : sin, cos, floor;

extern (C):
@safe:

struct RGB
{
float r = 0.0, g = 0.0, b = 0.0;
}

struct State
{
static sg.PassAction pass_action = {
colors: [
{
load_action: sg.LoadAction.Clear,
clear_value: {r: 0.0, g: 0.0, b: 0.0, a: 1.0},
}
]
};
}

immutable float[3][16] pallete = [
[0.957, 0.263, 0.212],
[0.914, 0.118, 0.388],
[0.612, 0.153, 0.690],
[0.404, 0.227, 0.718],
[0.247, 0.318, 0.710],
[0.129, 0.588, 0.953],
[0.012, 0.663, 0.957],
[0.000, 0.737, 0.831],
[0.000, 0.588, 0.533],
[0.298, 0.686, 0.314],
[0.545, 0.765, 0.290],
[0.804, 0.863, 0.224],
[1.000, 0.922, 0.231],
[1.000, 0.757, 0.027],
[1.000, 0.596, 0.000],
[1.000, 0.341, 0.133],
];

void init()
{
sg.Desc gfxd = {context: sgapp.context(),
logger: {func: &slog.func}};
sg.setup(gfxd);

sgl.Desc gld = {logger: {func: &slog.func}};
sgl.setup(gld);
}

void frame()
{
immutable float angle = sapp.frameCount() % 360.0;
State state;

sgl.defaults();
sgl.beginPoints();
float psize = 5.0;
foreach (i; 0 .. 360)
{
auto a = sgl.asRadians(angle + i);
auto color = computeColor(((sapp.frameCount() + i) % 300) / 300.0);
auto r = sin(a * 4.0);
auto s = sin(a);
auto c = cos(a);
auto x = s * r;
auto y = c * r;
sgl.c3f(color.r, color.g, color.b);
sgl.pointSize(psize);
sgl.v2f(x, y);
psize *= 1.005;
}
sgl.end();

sg.beginDefaultPass(state.pass_action, sapp.width, sapp.height);
sgl.draw();
sg.endPass();
sg.commit();
}

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

void main()
{
sapp.Desc runner = {
window_title: "sgl-points.d",
init_cb: &init,
frame_cb: &frame,
cleanup_cb: &cleanup,
width: 512,
height: 512,
logger: {func: &slog.func},
icon: {sokol_default: true}
};
sapp.run(runner);
}

RGB computeColor(float t)
{
const(size_t) idx0 = cast(size_t)(t * 16) % 16;
const(size_t) idx1 = cast(size_t)(idx0 + 1) % 16;
const l = (t * 16) - floor(t * 16);
const c0 = pallete[idx0];
const c1 = pallete[idx1];
RGB rgb = {
r: (c0[0] * (1 - l)) + (c1[0] * l),
g: (c0[1] * (1 - l)) + (c1[1] * l),
b: (c0[2] * (1 - l)) + (c1[2] * l),
};
return rgb;
}
8 changes: 5 additions & 3 deletions src/handmade/math.d
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ version (WebAssembly)
{
// zig stdlib no-libc math functions
enum PI = 3.14159265358979323846264338327950288419716939937510;
double zig_sqrt(size_t value) @nogc nothrow @trusted;
double zig_sqrtf(double value) @nogc nothrow @trusted;
double zig_cos(double value) @nogc nothrow @trusted;
double zig_floor(double value) @nogc nothrow @trusted;
double zig_sin(double value) @nogc nothrow @trusted;
double zig_sqrt(size_t value) @nogc nothrow @trusted;
double zig_sqrtf(double value) @nogc nothrow @trusted;
double zig_tan(double value) @nogc nothrow @trusted;
alias cos = zig_cos;
alias floor = zig_floor;
alias sin = zig_sin;
alias tan = zig_tan;

Expand All @@ -39,7 +41,7 @@ version (WebAssembly)
}
else
{
public import core.stdc.math : sqrt, cos, sin, tan;
public import core.stdc.math : sqrt, cos, sin, tan, floor;
public import std.math : PI;
}

Expand Down
3 changes: 3 additions & 0 deletions src/handmade/math.zig
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@ export fn zig_sin(x: f64) callconv(.C) f64 {
export fn zig_tan(x: f64) callconv(.C) f64 {
return std.math.tan(x);
}
export fn zig_floor(x: f64) callconv(.C) f64 {
return std.math.floor(x);
}

0 comments on commit d3e738b

Please sign in to comment.