diff options
-rw-r--r-- | src/ecs/entity.zig | 44 | ||||
-rw-r--r-- | src/frontend/ncurses.zig | 39 | ||||
-rw-r--r-- | src/main.zig | 4 | ||||
-rw-r--r-- | src/root.zig | 8 | ||||
-rw-r--r-- | src/state_machine/state_machine.zig | 17 |
5 files changed, 91 insertions, 21 deletions
diff --git a/src/ecs/entity.zig b/src/ecs/entity.zig index 6131cff..2ad8556 100644 --- a/src/ecs/entity.zig +++ b/src/ecs/entity.zig @@ -1,3 +1,45 @@ const std = @import("std"); -pub const Entity = struct {}; +pub const Entity = struct { + id: usize, +}; + +pub const EntityCollection = struct { + allocator: std.mem.Allocator, + next_id: usize, + entities: std.AutoHashMap(usize, Entity), + + pub fn addEntity(self: *EntityCollection) !usize { + try self.entities.put(self.next_id, .{ + .id = self.next_id, + }); + + self.next_id += 1; + + return self.next_id - 1; + } + + pub fn init(allocator: std.mem.Allocator) EntityCollection { + return .{ + .allocator = allocator, + .next_id = 0, + .entities = std.AutoHashMap(usize, Entity).init(allocator), + }; + } + + pub fn deinit(self: *EntityCollection) void { + self.entities.deinit(); + } +}; + +test "create entities" { + try std.testing.expect(true); + + var ec = EntityCollection.init(std.testing.allocator); + defer ec.deinit(); + + for (0..128) |i| { + const id = ec.addEntity(); + try std.testing.expectEqual(i, id); + } +} diff --git a/src/frontend/ncurses.zig b/src/frontend/ncurses.zig index a8c6f71..e0421c4 100644 --- a/src/frontend/ncurses.zig +++ b/src/frontend/ncurses.zig @@ -33,7 +33,6 @@ pub const Display = struct { inst: ?*ncurses.WINDOW, msgs: ?*ncurses.WINDOW, stat: ?*ncurses.WINDOW, - allocator: std.mem.Allocator, fn validTermSize() bool { return ncurses.LINES >= MIN_HEIGHT and @@ -104,6 +103,8 @@ pub const Display = struct { } pub fn displayMessage(self: *Display, comptime fmt: []const u8, args: anytype) void { + if (self.msgs == null) return; + for (1..MESSAGE_PANEL_WIDTH - 1) |i| { const i_32 = @as(i32, @intCast(i)); _ = ncurses.mvwaddch(self.msgs, 1, i_32, ' '); @@ -193,11 +194,6 @@ pub const Display = struct { return error.CursesInitFail; } - if (!validTermSize()) { - _ = ncurses.endwin(); - return error.InvalidTermSize; - } - if (!colorSupport()) { _ = ncurses.endwin(); return error.NoColorSupport; @@ -208,30 +204,45 @@ pub const Display = struct { _ = ncurses.noecho(); _ = ncurses.curs_set(0); _ = ncurses.start_color(); + _ = ncurses.timeout(0); _ = ncurses.init_pair(1, ncurses.COLOR_WHITE, ncurses.COLOR_BLACK); _ = ncurses.init_pair(2, ncurses.COLOR_BLACK, ncurses.COLOR_RED); _ = ncurses.wattron(ncurses.stdscr, ncurses.COLOR_PAIR(1)); _ = ncurses.refresh(); - var d = Display{ - .inst = createInstructionPanel(), - .msgs = createMessagePanel(), - .stat = createStatisticsPanel(), - .main = createMainPanel(), - .allocator = undefined, - }; + var d: Display = undefined; - d.displayInstructions(); + if (!validTermSize()) { + _ = ncurses.mvprintw(0, 0, "Terminal must be at least %dx%d", @as(i32, @intCast(MIN_WIDTH)), @as(i32, @intCast(MIN_HEIGHT))); + d = Display{ + .inst = null, + .msgs = null, + .stat = null, + .main = null, + }; + } else { + d = Display{ + .inst = createInstructionPanel(), + .msgs = createMessagePanel(), + .stat = createStatisticsPanel(), + .main = createMainPanel(), + }; + d.displayInstructions(); + } return d; } fn deleteWindows(self: *Display) void { _ = ncurses.delwin(self.main); + self.main = null; _ = ncurses.delwin(self.inst); + self.inst = null; _ = ncurses.delwin(self.msgs); + self.msgs = null; _ = ncurses.delwin(self.stat); + self.stat = null; } pub fn deinit(self: *Display) void { diff --git a/src/main.zig b/src/main.zig index 9c6e5e2..9fef982 100644 --- a/src/main.zig +++ b/src/main.zig @@ -3,16 +3,18 @@ const Entity = @import("ecs/entity.zig").Entity; const Display = @import("frontend/ncurses.zig").Display; const Action = @import("actions.zig").Action; +const state_machine = @import("state_machine/state_machine.zig"); + pub fn main() u8 { var d = Display.init() catch |err| { std.log.err("{}", .{err}); return 1; }; d.displayMessage("Initialized", .{}); - d.displayStatus(&Entity{}); var action = Action.illegal; while (action != Action.exit) { action = d.processInput(); + state_machine.nextTick(&d); } d.deinit(); diff --git a/src/root.zig b/src/root.zig index ecfeade..62b3398 100644 --- a/src/root.zig +++ b/src/root.zig @@ -1,10 +1,8 @@ const std = @import("std"); const testing = std.testing; -export fn add(a: i32, b: i32) i32 { - return a + b; -} +const entity = @import("ecs/entity.zig"); -test "basic add functionality" { - try testing.expect(add(3, 7) == 10); +test { + _ = entity; } diff --git a/src/state_machine/state_machine.zig b/src/state_machine/state_machine.zig new file mode 100644 index 0000000..4388a28 --- /dev/null +++ b/src/state_machine/state_machine.zig @@ -0,0 +1,17 @@ +const std = @import("std"); +const Display = @import("../frontend/ncurses.zig").Display; + +const TICK = 33; + +var x: u64 = 0; +var prev: i64 = 0; + +pub fn nextTick(display: *Display) void { + const new = std.time.milliTimestamp(); + + if (new - prev > TICK) { + display.displayMessage("{}", .{x}); + x += 1; + prev = new; + } +} |