diff options
Diffstat (limited to 'src/context.zig')
| -rw-r--r-- | src/context.zig | 54 |
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(); |
