summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoralaric <alaric@netmythos.org>2024-04-09 04:38:20 -0700
committeralaric <alaric@netmythos.org>2024-04-09 04:38:20 -0700
commitdcbee5d6fb42dc12b6cad3b7908a172ebb2804ad (patch)
treed91448a6fdf31a7ab6a0c1ff5dafc69f76cb6fce
downloadtiny-td-dcbee5d6fb42dc12b6cad3b7908a172ebb2804ad.tar.gz
tiny-td-dcbee5d6fb42dc12b6cad3b7908a172ebb2804ad.zip
Initial Commit
-rw-r--r--.gitignore1
-rw-r--r--.gitmodules3
-rw-r--r--build.zig45
-rw-r--r--build.zig.zon35
m---------engine0
-rw-r--r--flake.lock146
-rw-r--r--flake.nix48
-rw-r--r--src/main.zig73
8 files changed, 351 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..4c80a22
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+zig-*
diff --git a/.gitmodules b/.gitmodules
new file mode 100644
index 0000000..933415a
--- /dev/null
+++ b/.gitmodules
@@ -0,0 +1,3 @@
+[submodule "engine"]
+ path = engine
+ url = git@netmythos.org:engine.git
diff --git a/build.zig b/build.zig
new file mode 100644
index 0000000..e0567c3
--- /dev/null
+++ b/build.zig
@@ -0,0 +1,45 @@
+const std = @import("std");
+
+pub fn build(b: *std.Build) void {
+ // The engine only supports wasm right now so if you set a whitelist here,
+ // you won't have to remember to specify your target each time you compile.
+ const wasm_target: std.Target.Query = .{
+ .cpu_arch = .wasm32,
+ .os_tag = .freestanding,
+ };
+ const target = b.standardTargetOptions(.{
+ .whitelist = &.{wasm_target},
+ .default_target = wasm_target,
+ });
+ const optimize = b.standardOptimizeOption(.{});
+
+ // Import the dependency via your build.zig.zon
+ const engine = b.dependency("engine", .{});
+
+ const exe = b.addExecutable(.{
+ // IMPORTANT! Right now the shell expects a file named game.wasm in the bin directory.
+ // I will make it flexible eventually. But right now if you change this line,
+ // Your project won't work right.
+ .name = "game",
+ .root_source_file = .{ .path = "src/main.zig" },
+ .target = target,
+ .optimize = optimize,
+ });
+ // WASM doesn't use a normal entry point. The HTML/JS shell code is going
+ // to handle calling our functions.
+ exe.entry = .disabled;
+ exe.root_module.addImport("engine", engine.module("engine"));
+
+ // Here we install a pre-written HTML/JS shell. After building, if you
+ // run a webserver with your zig-out dir as the root, you should see your
+ // program.
+ const shell_files = engine.namedWriteFiles("shell");
+ const install_shell = b.addInstallDirectory(.{
+ .source_dir = .{ .generated = &shell_files.generated_directory },
+ .install_dir = .prefix,
+ .install_subdir = "",
+ });
+ b.getInstallStep().dependOn(&install_shell.step);
+
+ b.installArtifact(exe);
+}
diff --git a/build.zig.zon b/build.zig.zon
new file mode 100644
index 0000000..6ae678d
--- /dev/null
+++ b/build.zig.zon
@@ -0,0 +1,35 @@
+.{
+ .name = "tiny-td",
+ // This is a [Semantic Version](https://semver.org/).
+ // In a future version of Zig it will be used for package deduplication.
+ .version = "0.0.0",
+
+ // This field is optional.
+ // This is currently advisory only; Zig does not yet do anything
+ // with this value.
+ //.minimum_zig_version = "0.11.0",
+
+ // This field is optional.
+ // Each dependency must either provide a `url` and `hash`, or a `path`.
+ // `zig build --fetch` can be used to fetch all dependencies of a package, recursively.
+ // Once all dependencies are fetched, `zig build` no longer requires
+ // internet connectivity.
+ .dependencies = .{
+ .engine = .{
+ .path = "engine/",
+ },
+ },
+ .paths = .{
+ // This makes *all* files, recursively, included in this package. It is generally
+ // better to explicitly list the files and directories instead, to insure that
+ // fetching from tarballs, file system paths, and version control all result
+ // in the same contents hash.
+ "",
+ // For example...
+ //"build.zig",
+ //"build.zig.zon",
+ //"src",
+ //"LICENSE",
+ //"README.md",
+ },
+}
diff --git a/engine b/engine
new file mode 160000
+Subproject e1ff0e3d124151c4c965f4d731c795d31732e42
diff --git a/flake.lock b/flake.lock
new file mode 100644
index 0000000..df89518
--- /dev/null
+++ b/flake.lock
@@ -0,0 +1,146 @@
+{
+ "nodes": {
+ "flake-compat": {
+ "flake": false,
+ "locked": {
+ "lastModified": 1696426674,
+ "narHash": "sha256-kvjfFW7WAETZlt09AgDn1MrtKzP7t90Vf7vypd3OL1U=",
+ "owner": "edolstra",
+ "repo": "flake-compat",
+ "rev": "0f9255e01c2351cc7d116c072cb317785dd33b33",
+ "type": "github"
+ },
+ "original": {
+ "owner": "edolstra",
+ "repo": "flake-compat",
+ "type": "github"
+ }
+ },
+ "flake-compat_2": {
+ "flake": false,
+ "locked": {
+ "lastModified": 1673956053,
+ "narHash": "sha256-4gtG9iQuiKITOjNQQeQIpoIB6b16fm+504Ch3sNKLd8=",
+ "owner": "edolstra",
+ "repo": "flake-compat",
+ "rev": "35bb57c0c8d8b62bbfd284272c928ceb64ddbde9",
+ "type": "github"
+ },
+ "original": {
+ "owner": "edolstra",
+ "repo": "flake-compat",
+ "type": "github"
+ }
+ },
+ "flake-utils": {
+ "inputs": {
+ "systems": "systems"
+ },
+ "locked": {
+ "lastModified": 1710146030,
+ "narHash": "sha256-SZ5L6eA7HJ/nmkzGG7/ISclqe6oZdOZTNoesiInkXPQ=",
+ "owner": "numtide",
+ "repo": "flake-utils",
+ "rev": "b1d9ab70662946ef0850d488da1c9019f3a9752a",
+ "type": "github"
+ },
+ "original": {
+ "owner": "numtide",
+ "repo": "flake-utils",
+ "type": "github"
+ }
+ },
+ "flake-utils_2": {
+ "locked": {
+ "lastModified": 1659877975,
+ "narHash": "sha256-zllb8aq3YO3h8B/U0/J1WBgAL8EX5yWf5pMj3G0NAmc=",
+ "owner": "numtide",
+ "repo": "flake-utils",
+ "rev": "c0e246b9b83f637f4681389ecabcb2681b4f3af0",
+ "type": "github"
+ },
+ "original": {
+ "owner": "numtide",
+ "repo": "flake-utils",
+ "type": "github"
+ }
+ },
+ "nixpkgs": {
+ "locked": {
+ "lastModified": 1704290814,
+ "narHash": "sha256-LWvKHp7kGxk/GEtlrGYV68qIvPHkU9iToomNFGagixU=",
+ "owner": "nixos",
+ "repo": "nixpkgs",
+ "rev": "70bdadeb94ffc8806c0570eb5c2695ad29f0e421",
+ "type": "github"
+ },
+ "original": {
+ "owner": "nixos",
+ "ref": "nixos-23.05",
+ "repo": "nixpkgs",
+ "type": "github"
+ }
+ },
+ "nixpkgs_2": {
+ "locked": {
+ "lastModified": 1702350026,
+ "narHash": "sha256-A+GNZFZdfl4JdDphYKBJ5Ef1HOiFsP18vQe9mqjmUis=",
+ "owner": "NixOS",
+ "repo": "nixpkgs",
+ "rev": "9463103069725474698139ab10f17a9d125da859",
+ "type": "github"
+ },
+ "original": {
+ "owner": "NixOS",
+ "ref": "nixos-23.05",
+ "repo": "nixpkgs",
+ "type": "github"
+ }
+ },
+ "root": {
+ "inputs": {
+ "flake-compat": "flake-compat",
+ "flake-utils": "flake-utils",
+ "nixpkgs": "nixpkgs",
+ "zig": "zig"
+ }
+ },
+ "systems": {
+ "locked": {
+ "lastModified": 1681028828,
+ "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
+ "owner": "nix-systems",
+ "repo": "default",
+ "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
+ "type": "github"
+ },
+ "original": {
+ "owner": "nix-systems",
+ "repo": "default",
+ "type": "github"
+ }
+ },
+ "zig": {
+ "inputs": {
+ "flake-compat": "flake-compat_2",
+ "flake-utils": "flake-utils_2",
+ "nixpkgs": "nixpkgs_2"
+ },
+ "locked": {
+ "lastModified": 1712622171,
+ "narHash": "sha256-xhzKVbMhQHGhwy6KbqKNVizXGoZ09zlvX4whQ8tyG3w=",
+ "owner": "mitchellh",
+ "repo": "zig-overlay",
+ "rev": "3f0f0ade900a74480f2e5d6b07ffb8f1c3129a24",
+ "type": "github"
+ },
+ "original": {
+ "owner": "mitchellh",
+ "repo": "zig-overlay",
+ "type": "github"
+ }
+ }
+ },
+ "root": "root",
+ "version": 7
+}
diff --git a/flake.nix b/flake.nix
new file mode 100644
index 0000000..2859429
--- /dev/null
+++ b/flake.nix
@@ -0,0 +1,48 @@
+{
+ description = "The flake for the Tiny TD";
+
+ inputs = {
+ nixpkgs.url = "github:nixos/nixpkgs/nixos-23.05";
+ flake-utils.url = "github:numtide/flake-utils";
+ zig.url = "github:mitchellh/zig-overlay";
+
+ # Used for shell.nix
+ flake-compat = {
+ url = "github:edolstra/flake-compat";
+ flake = false;
+ };
+ };
+
+ outputs = {
+ self,
+ nixpkgs,
+ flake-utils,
+ ...
+ } @ inputs: let
+ overlays = [
+ # Other overlays
+ (final: prev: {
+ zigpkgs = inputs.zig.packages.${prev.system};
+ })
+ ];
+
+ # Our supported systems are the same supported systems as the Zig binaries
+ systems = builtins.attrNames inputs.zig.packages;
+ in
+ flake-utils.lib.eachSystem systems (
+ system: let
+ pkgs = import nixpkgs {inherit overlays system;};
+ in {
+ devShells.default = pkgs.mkShell {
+ nativeBuildInputs = with pkgs; [
+ zigpkgs.master
+ python3
+ ];
+ };
+
+ # For compatibility with older versions of the `nix` binary
+ devShell = self.devShells.${system}.default;
+ }
+ );
+}
+
diff --git a/src/main.zig b/src/main.zig
new file mode 100644
index 0000000..bebb189
--- /dev/null
+++ b/src/main.zig
@@ -0,0 +1,73 @@
+const engine = @import("engine");
+const matrix = engine.matrix;
+const Mat3 = matrix.Matrix(f32, 3, 3);
+const webgl = engine.webgl;
+const input = engine.input;
+comptime {
+ _ = engine;
+}
+
+const Color = engine.Color;
+const bg: Color = Color.rgbFromScalar(0.4);
+
+var prog: webgl.General2DSolidProgram = undefined;
+var buffer: webgl.Buffer = undefined;
+var vao: webgl.VertexArrayObject = undefined;
+
+const plane_verts = [_][2]f32{
+ .{ 0, 0 },
+ .{ 1, 0 },
+ .{ 0, 1 },
+ .{ 0, 1 },
+ .{ 1, 0 },
+ .{ 1, 1 },
+};
+
+var pos: [2]f32 = .{ 0, 0 };
+var push: bool = false;
+
+const p_count = 8;
+var p_idx: usize = 0;
+var points: [p_count][2]f32 = [1][2]f32{.{ 0, 0 }} ** p_count;
+
+pub fn init() void {
+ prog = webgl.createGeneral2DSolidProgram() catch return;
+ prog.use();
+ buffer = webgl.Buffer.init();
+ buffer.bindAndFill(.array_buffer, .vec2, &plane_verts, .static_draw);
+ vao = webgl.VertexArrayObject.init();
+ prog.setVertexAttribPointer(.pos, vao, buffer);
+}
+
+pub fn update() void {
+ const dims = webgl.getScreenSize();
+ webgl.viewportToScreen();
+ prog.setUniform(.projection, matrix.projectionMatrix2D(dims[0], dims[1]));
+ prog.setUniform(.color, .{ 1, 0, 0 });
+ if (push) {
+ prog.setUniform(.color, .{ 0, 0, 1 });
+ }
+ webgl.clear(bg);
+ buffer.bind(.array_buffer);
+
+ for (points) |p| {
+ const model = Mat3.translation(p).scaled(.{ 50, 50 });
+ prog.setUniform(.model, model);
+ webgl.drawArrays(.triangles, 0, 6);
+ }
+}
+
+pub fn handleInput(event: input.InputEvent) void {
+ switch (event) {
+ .mouse => |m| {
+ const dims = webgl.getScreenSize();
+
+ const world_pos = .{ m.pos[0], dims[1] - m.pos[1] };
+ if (m.buttons.left and !push) {
+ points[p_idx] = world_pos;
+ p_idx = (p_idx + 1) % p_count;
+ }
+ push = m.buttons.left;
+ },
+ }
+}