summaryrefslogtreecommitdiff
path: root/src/context.zig
blob: f3e88f2981a5b9cd21e676d6834af32e3f7583f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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();