From d3e738bc1557fcf9699ff45f515117efa5d4d979 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matheus=20Catarino=20Fran=C3=A7a?= Date: Mon, 5 Feb 2024 10:40:19 -0300 Subject: [PATCH] new example: sgl-points --- .github/workflows/build.yml | 1 + README.md | 1 + build.zig | 1 + dub.sdl | 29 +++++--- src/examples/sgl_points.d | 131 ++++++++++++++++++++++++++++++++++++ src/handmade/math.d | 8 ++- src/handmade/math.zig | 3 + 7 files changed, 162 insertions(+), 12 deletions(-) create mode 100644 src/examples/sgl_points.d diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 7b2c2a9..e047be3 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -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 diff --git a/README.md b/README.md index ca166ae..0886feb 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/build.zig b/build.zig index 52d61b6..cab011b 100644 --- a/build.zig +++ b/build.zig @@ -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] }; diff --git a/dub.sdl b/dub.sdl index 5a60256..edcc0bf 100644 --- a/dub.sdl +++ b/dub.sdl @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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" } diff --git a/src/examples/sgl_points.d b/src/examples/sgl_points.d new file mode 100644 index 0000000..a6b5858 --- /dev/null +++ b/src/examples/sgl_points.d @@ -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; +} diff --git a/src/handmade/math.d b/src/handmade/math.d index 41268c0..552e67b 100644 --- a/src/handmade/math.d +++ b/src/handmade/math.d @@ -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; @@ -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; } diff --git a/src/handmade/math.zig b/src/handmade/math.zig index 775f026..26ad6fa 100644 --- a/src/handmade/math.zig +++ b/src/handmade/math.zig @@ -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); +}