summaryrefslogtreecommitdiff
path: root/src/context.zig
diff options
context:
space:
mode:
authorAlec Goncharow <alec@goncharow.dev>2024-01-19 22:39:43 -0500
committerAlec Goncharow <alec@goncharow.dev>2024-01-19 22:39:43 -0500
commitdb229ae38f04e8a6b9759c1e5208af75b81344aa (patch)
tree1cdd2eda0b2c71d0c5f5f16e25a362cdd9d41904 /src/context.zig
initial stuff
got some hexes, got some mouse picking
Diffstat (limited to 'src/context.zig')
-rw-r--r--src/context.zig54
1 files changed, 54 insertions, 0 deletions
diff --git a/src/context.zig b/src/context.zig
new file mode 100644
index 0000000..f3e88f2
--- /dev/null
+++ b/src/context.zig
@@ -0,0 +1,54 @@
+const std = @import("std");
+const rl = @cImport(@cInclude("raylib.h"));
+const root = @import("root.zig");
+
+pub var camera: rl.Camera2D = undefined;
+pub var grid: []root.MyRect = undefined;
+
+pub const Grid = struct {
+ buffer: std.MultiArrayList(root.Hex),
+ cursor: usize,
+ size: usize,
+ const Self = @This();
+
+ const GridError = error{
+ OutOfBounds,
+ };
+
+ //TODO think about what size really means
+ pub fn init(
+ allocator: std.mem.Allocator,
+ size: usize,
+ ) !Grid {
+ var buffer: std.MultiArrayList(root.Hex) = .{};
+ try buffer.ensureTotalCapacity(allocator, size * size);
+ return .{
+ .buffer = buffer,
+ .cursor = 0,
+ .size = size,
+ };
+ }
+
+ // should only be used when standing up the buffer, afterwards should use q, r indexing
+ pub fn initPush(self: *Self, hex: root.Hex) GridError!void {
+ if (self.cursor >= self.size * 2) {
+ return GridError.OutOfBounds;
+ }
+ self.buffer.insertAssumeCapacity(self.cursor, hex);
+ }
+
+ pub fn set(self: *Self, hex: root.Hex) GridError!void {
+ const idx = hex.qr[1] * self.size + hex.qr[0];
+ if (idx >= self.size * 2) {
+ return GridError.OutOfBounds;
+ }
+
+ self.buffer.set(idx, hex);
+ }
+};
+pub var hex_grid: Grid = undefined;
+pub var hovered_coords: root.HexCoord = root.HexCoord{ .q = 0, .r = 0 };
+pub var hovered_handle: ?usize = null;
+
+var Gpa = std.heap.GeneralPurposeAllocator(.{}){};
+pub var gpa: std.mem.Allocator = Gpa.allocator();