feat: initial commit
This commit is contained in:
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
zig-out/
|
||||||
|
.zig-cache/
|
||||||
53
build.zig
Normal file
53
build.zig
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
const std = @import("std");
|
||||||
|
|
||||||
|
pub fn build(b: *std.Build) void {
|
||||||
|
const target = b.standardTargetOptions(.{});
|
||||||
|
const optimize = b.standardOptimizeOption(.{});
|
||||||
|
|
||||||
|
const exe_mod = b.createModule(.{
|
||||||
|
.root_source_file = b.path("src/main.zig"),
|
||||||
|
.target = target,
|
||||||
|
.optimize = optimize,
|
||||||
|
});
|
||||||
|
|
||||||
|
const exe = b.addExecutable(.{
|
||||||
|
.name = "snake",
|
||||||
|
.root_module = exe_mod,
|
||||||
|
});
|
||||||
|
|
||||||
|
const raylib_dep = b.dependency("raylib_zig", .{
|
||||||
|
.target = target,
|
||||||
|
.optimize = optimize,
|
||||||
|
});
|
||||||
|
|
||||||
|
const raylib = raylib_dep.module("raylib"); // main raylib module
|
||||||
|
const raygui = raylib_dep.module("raygui"); // raygui module
|
||||||
|
const raylib_artifact = raylib_dep.artifact("raylib"); // raylib C library
|
||||||
|
|
||||||
|
raylib_artifact.root_module.addCMacro("SUPPORT_FILEFORMAT_JPG", "");
|
||||||
|
|
||||||
|
exe.linkLibrary(raylib_artifact);
|
||||||
|
exe.root_module.addImport("raylib", raylib);
|
||||||
|
exe.root_module.addImport("raygui", raygui);
|
||||||
|
|
||||||
|
b.installArtifact(exe);
|
||||||
|
|
||||||
|
const run_cmd = b.addRunArtifact(exe);
|
||||||
|
run_cmd.step.dependOn(b.getInstallStep());
|
||||||
|
|
||||||
|
if (b.args) |args| {
|
||||||
|
run_cmd.addArgs(args);
|
||||||
|
}
|
||||||
|
|
||||||
|
const run_step = b.step("run", "Run the app");
|
||||||
|
run_step.dependOn(&run_cmd.step);
|
||||||
|
|
||||||
|
const exe_unit_tests = b.addTest(.{
|
||||||
|
.root_module = exe_mod,
|
||||||
|
});
|
||||||
|
|
||||||
|
const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests);
|
||||||
|
|
||||||
|
const test_step = b.step("test", "Run unit tests");
|
||||||
|
test_step.dependOn(&run_exe_unit_tests.step);
|
||||||
|
}
|
||||||
44
build.zig.zon
Normal file
44
build.zig.zon
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
.{
|
||||||
|
// This is the default name used by packages depending on this one. For
|
||||||
|
// example, when a user runs `zig fetch --save <url>`, this field is used
|
||||||
|
// as the key in the `dependencies` table. Although the user can choose a
|
||||||
|
// different name, most users will stick with this provided value.
|
||||||
|
//
|
||||||
|
// It is redundant to include "zig" in this name because it is already
|
||||||
|
// within the Zig package namespace.
|
||||||
|
.name = .snake,
|
||||||
|
|
||||||
|
// 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",
|
||||||
|
|
||||||
|
// Together with name, this represents a globally unique package
|
||||||
|
// identifier. This field is generated by the Zig toolchain when the
|
||||||
|
// package is first created, and then *never changes*. This allows
|
||||||
|
// unambiguous detection of one package being an updated version of
|
||||||
|
// another.
|
||||||
|
//
|
||||||
|
// When forking a Zig project, this id should be regenerated (delete the
|
||||||
|
// field and run `zig build`) if the upstream project is still maintained.
|
||||||
|
// Otherwise, the fork is *hostile*, attempting to take control over the
|
||||||
|
// original project's identity. Thus it is recommended to leave the comment
|
||||||
|
// on the following line intact, so that it shows up in code reviews that
|
||||||
|
// modify the field.
|
||||||
|
.fingerprint = 0x516d6b9e32258eaf, // Changing this has security and trust implications.
|
||||||
|
|
||||||
|
// Tracks the earliest Zig version that the package considers to be a
|
||||||
|
// supported use case.
|
||||||
|
.minimum_zig_version = "0.14.1",
|
||||||
|
|
||||||
|
.dependencies = .{
|
||||||
|
.raylib_zig = .{
|
||||||
|
.url = "git+https://github.com/Not-Nik/raylib-zig?ref=devel#5013830647196ba938a3a25a36b8245606e9a9cd",
|
||||||
|
.hash = "raylib_zig-5.6.0-dev-KE8REM0tBQAHVn9Xjqlgu9l1qgfTmP8aJa1kLhD584bV",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
.paths = .{
|
||||||
|
"build.zig",
|
||||||
|
"build.zig.zon",
|
||||||
|
"src",
|
||||||
|
},
|
||||||
|
}
|
||||||
78
src/game.zig
Normal file
78
src/game.zig
Normal file
@@ -0,0 +1,78 @@
|
|||||||
|
const std = @import("std");
|
||||||
|
|
||||||
|
const Direction = enum {
|
||||||
|
up,
|
||||||
|
down,
|
||||||
|
left,
|
||||||
|
right,
|
||||||
|
};
|
||||||
|
|
||||||
|
/// x, y
|
||||||
|
const Vec2 = @Vector(2, u32);
|
||||||
|
|
||||||
|
const Element = struct {
|
||||||
|
position: Vec2,
|
||||||
|
|
||||||
|
prev: ?Element,
|
||||||
|
next: ?Element,
|
||||||
|
|
||||||
|
pub fn hasPrev(self: Element) bool {
|
||||||
|
return self.prev != null;
|
||||||
|
}
|
||||||
|
pub fn hasNext(self: Element) bool {
|
||||||
|
return self.next != null;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const GameState = struct {
|
||||||
|
playFieldSize: Vec2,
|
||||||
|
|
||||||
|
food: Vec2,
|
||||||
|
|
||||||
|
headDirection: Direction,
|
||||||
|
|
||||||
|
head: Element,
|
||||||
|
tail: Element,
|
||||||
|
|
||||||
|
pub fn create() GameState {
|
||||||
|
const pos1 = Element{
|
||||||
|
.position = Vec2{ 10, 10 },
|
||||||
|
.prev = null,
|
||||||
|
.next = null,
|
||||||
|
};
|
||||||
|
const pos2 = Element{
|
||||||
|
.position = Vec2{ 9, 10 },
|
||||||
|
.prev = pos1,
|
||||||
|
.next = null,
|
||||||
|
};
|
||||||
|
const pos3 = Element{
|
||||||
|
.position = Vec2{ 8, 10 },
|
||||||
|
.prev = pos2,
|
||||||
|
.next = null,
|
||||||
|
};
|
||||||
|
|
||||||
|
pos1.next = pos2;
|
||||||
|
pos2.next = pos3;
|
||||||
|
|
||||||
|
return GameState{
|
||||||
|
.playFieldSize = Vec2{ 20, 20 },
|
||||||
|
.food = Vec2{ 2, 5 },
|
||||||
|
.headDirection = Direction.left,
|
||||||
|
.head = pos1,
|
||||||
|
.tail = pos3,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn tick() void {}
|
||||||
|
|
||||||
|
fn calculateSnakePositions(self: GameState, allocator: std.Allocator) []Vec2 {
|
||||||
|
var array = std.ArrayList(u8).initCapacity(allocator, 1000);
|
||||||
|
|
||||||
|
var current = self.head;
|
||||||
|
while (current.hasNext()) {
|
||||||
|
array.add(current.position);
|
||||||
|
}
|
||||||
|
|
||||||
|
return array;
|
||||||
|
}
|
||||||
|
};
|
||||||
27
src/main.zig
Normal file
27
src/main.zig
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
const std = @import("std");
|
||||||
|
const rl = @import("raylib");
|
||||||
|
|
||||||
|
pub fn main() anyerror!void {
|
||||||
|
const alloc = std.heap.page_allocator;
|
||||||
|
|
||||||
|
const screenWidth = 800;
|
||||||
|
const screenHeight = 450;
|
||||||
|
|
||||||
|
rl.initWindow(screenWidth, screenHeight, "Snake");
|
||||||
|
defer rl.closeWindow();
|
||||||
|
|
||||||
|
rl.setTargetFPS(100);
|
||||||
|
|
||||||
|
while (!rl.windowShouldClose()) {
|
||||||
|
rl.beginDrawing();
|
||||||
|
defer rl.endDrawing();
|
||||||
|
|
||||||
|
rl.clearBackground(.white);
|
||||||
|
|
||||||
|
const info = try std.fmt.allocPrintZ(alloc, "FPS: {}\n", .{rl.getFPS()});
|
||||||
|
|
||||||
|
rl.drawText("Congrats! You created your first window!", 190, 200, 20, .light_gray);
|
||||||
|
rl.drawText(info, 0, 0, 20, .light_gray);
|
||||||
|
rl.drawRectangle(10, 10, 10, 10, .black);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user