Made ECS a separate module and implemented basic input handling.
This commit is contained in:
parent
64c9d32905
commit
536c927613
6 changed files with 172 additions and 7 deletions
10
build.zig
10
build.zig
|
|
@ -68,12 +68,21 @@ pub fn build(b: *std.Build) void {
|
|||
});
|
||||
mods.addImport("sideros", sideros);
|
||||
|
||||
const ecs = b.addModule("ecs", .{
|
||||
.root_source_file = b.path("src/ecs/ecs.zig"),
|
||||
.target = target,
|
||||
.optimize = optimize,
|
||||
});
|
||||
ecs.addImport("sideros", sideros);
|
||||
|
||||
const renderer = b.addModule("renderer", .{
|
||||
.root_source_file = b.path("src/renderer/Renderer.zig"),
|
||||
.target = target,
|
||||
.optimize = optimize,
|
||||
});
|
||||
renderer.addImport("sideros", sideros);
|
||||
renderer.addImport("ecs", ecs);
|
||||
ecs.addImport("renderer", renderer);
|
||||
|
||||
renderer.addIncludePath(b.path("ext/glfw/include"));
|
||||
compileAllShaders(b, renderer);
|
||||
|
|
@ -87,6 +96,7 @@ pub fn build(b: *std.Build) void {
|
|||
exe.root_module.addImport("mods", mods);
|
||||
exe.root_module.addImport("sideros", sideros);
|
||||
exe.root_module.addImport("renderer", renderer);
|
||||
exe.root_module.addImport("ecs", ecs);
|
||||
|
||||
exe.linkSystemLibrary("vulkan");
|
||||
exe.linkLibrary(glfw);
|
||||
|
|
|
|||
133
src/Input.zig
Normal file
133
src/Input.zig
Normal file
|
|
@ -0,0 +1,133 @@
|
|||
const std = @import("std");
|
||||
const Allocator = std.mem.Allocator;
|
||||
|
||||
const Input = @This();
|
||||
|
||||
pub const KeyCode = enum(u32) {
|
||||
space = 32,
|
||||
apostrophe = 39,
|
||||
comma = 44,
|
||||
minus = 45,
|
||||
period = 46,
|
||||
slash = 47,
|
||||
@"0" = 48,
|
||||
@"1" = 49,
|
||||
@"2" = 50,
|
||||
@"3" = 51,
|
||||
@"4" = 52,
|
||||
@"5" = 53,
|
||||
@"6" = 54,
|
||||
@"7" = 55,
|
||||
@"8" = 56,
|
||||
@"9" = 57,
|
||||
semicolon = 59,
|
||||
equal = 61,
|
||||
a = 65,
|
||||
b = 66,
|
||||
c = 67,
|
||||
d = 68,
|
||||
e = 69,
|
||||
f = 70,
|
||||
g = 71,
|
||||
h = 72,
|
||||
i = 73,
|
||||
j = 74,
|
||||
k = 75,
|
||||
l = 76,
|
||||
m = 77,
|
||||
n = 78,
|
||||
o = 79,
|
||||
p = 80,
|
||||
q = 81,
|
||||
r = 82,
|
||||
s = 83,
|
||||
t = 84,
|
||||
u = 85,
|
||||
v = 86,
|
||||
w = 87,
|
||||
x = 88,
|
||||
y = 89,
|
||||
z = 90,
|
||||
left_bracket = 91,
|
||||
backslash = 92,
|
||||
right_bracket = 93,
|
||||
grave_accent = 96,
|
||||
world_1 = 161,
|
||||
world_2 = 162,
|
||||
escape = 256,
|
||||
enter = 257,
|
||||
tab = 258,
|
||||
backspace = 259,
|
||||
insert = 260,
|
||||
delete = 261,
|
||||
right = 262,
|
||||
left = 263,
|
||||
down = 264,
|
||||
up = 265,
|
||||
page_up = 266,
|
||||
page_down = 267,
|
||||
home = 268,
|
||||
end = 269,
|
||||
caps_lock = 280,
|
||||
scroll_lock = 281,
|
||||
num_lock = 282,
|
||||
print_screen = 283,
|
||||
pause = 284,
|
||||
f1 = 290,
|
||||
f2 = 291,
|
||||
f3 = 292,
|
||||
f4 = 293,
|
||||
f5 = 294,
|
||||
f6 = 295,
|
||||
f7 = 296,
|
||||
f8 = 297,
|
||||
f9 = 298,
|
||||
f10 = 299,
|
||||
f11 = 300,
|
||||
f12 = 301,
|
||||
f13 = 302,
|
||||
f14 = 303,
|
||||
f15 = 304,
|
||||
f16 = 305,
|
||||
f17 = 306,
|
||||
f18 = 307,
|
||||
f19 = 308,
|
||||
f20 = 309,
|
||||
f21 = 310,
|
||||
f22 = 311,
|
||||
f23 = 312,
|
||||
f24 = 313,
|
||||
f25 = 314,
|
||||
kp_0 = 320,
|
||||
kp_1 = 321,
|
||||
kp_2 = 322,
|
||||
kp_3 = 323,
|
||||
kp_4 = 324,
|
||||
kp_5 = 325,
|
||||
kp_6 = 326,
|
||||
kp_7 = 327,
|
||||
kp_8 = 328,
|
||||
kp_9 = 329,
|
||||
kp_decimal = 330,
|
||||
kp_divide = 331,
|
||||
kp_multiply = 332,
|
||||
kp_subtract = 333,
|
||||
kp_add = 334,
|
||||
kp_enter = 335,
|
||||
kp_equal = 336,
|
||||
left_shift = 340,
|
||||
left_control = 341,
|
||||
left_alt = 342,
|
||||
left_super = 343,
|
||||
right_shift = 344,
|
||||
right_control = 345,
|
||||
right_alt = 346,
|
||||
right_super = 347,
|
||||
menu = 348,
|
||||
};
|
||||
|
||||
key_pressed: [@intFromEnum(KeyCode.menu)]bool,
|
||||
|
||||
pub fn isKeyDown(self: Input, key: KeyCode) bool {
|
||||
return self.key_pressed[@intFromEnum(key)];
|
||||
}
|
||||
|
|
@ -3,6 +3,7 @@ const Allocator = std.mem.Allocator;
|
|||
const components = @import("components.zig");
|
||||
const sparse = @import("sparse.zig");
|
||||
const Renderer = @import("renderer");
|
||||
const Input = @import("sideros").Input;
|
||||
|
||||
pub const System = *const fn (*Pool) void;
|
||||
pub const SystemGroup = []const System;
|
||||
|
|
@ -10,6 +11,7 @@ pub const SystemGroup = []const System;
|
|||
pub const Resources = struct {
|
||||
window: Renderer.Window,
|
||||
renderer: Renderer,
|
||||
input: Input,
|
||||
};
|
||||
|
||||
pub const Human = struct {
|
||||
|
|
|
|||
13
src/main.zig
13
src/main.zig
|
|
@ -1,16 +1,13 @@
|
|||
const std = @import("std");
|
||||
const config = @import("config");
|
||||
const math = @import("sideros").math;
|
||||
const Input = @import("sideros").Input;
|
||||
const mods = @import("mods");
|
||||
const ecs = @import("ecs/ecs.zig");
|
||||
const ecs = @import("ecs");
|
||||
pub const Renderer = @import("renderer");
|
||||
|
||||
fn testSystem2(pool: *ecs.Pool) void {
|
||||
const slice = pool.humans.slice();
|
||||
|
||||
for (slice.items(.position), slice.items(.speed)) |position, speed| {
|
||||
std.debug.print("entity: {any} {any} {any}: {any}\n", .{ position.x, position.y, position.z, speed.speed });
|
||||
}
|
||||
std.debug.print("{any}\n", .{pool.resources.input.isKeyDown(.a)});
|
||||
}
|
||||
|
||||
pub fn main() !void {
|
||||
|
|
@ -38,7 +35,7 @@ pub fn main() !void {
|
|||
|
||||
//var parameters = [_]usize{};
|
||||
//try runtime.callExternal(allocator, "preinit", ¶meters);
|
||||
const w = try Renderer.Window.create(800, 600, "sideros");
|
||||
var w = try Renderer.Window.create(800, 600, "sideros");
|
||||
defer w.destroy();
|
||||
|
||||
// TODO(luccie-cmd): Renderer.create shouldn't return an error
|
||||
|
|
@ -48,10 +45,12 @@ pub fn main() !void {
|
|||
const resources = ecs.Resources{
|
||||
.window = w,
|
||||
.renderer = r,
|
||||
.input = .{ .key_pressed = .{false} ** @intFromEnum(Input.KeyCode.menu) },
|
||||
};
|
||||
|
||||
var pool = try ecs.Pool.init(allocator, resources);
|
||||
defer pool.deinit();
|
||||
w.setResources(&pool.resources);
|
||||
|
||||
_ = try pool.createEntity(ecs.entities.Human{
|
||||
.position = .{ .x = 0.0, .y = 1.0, .z = 0.0 },
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
const c = @import("c.zig");
|
||||
const ecs = @import("ecs");
|
||||
const std = @import("std");
|
||||
|
||||
const Window = @This();
|
||||
|
|
@ -36,6 +37,7 @@ pub fn create(width: usize, height: usize, title: []const u8) !Window {
|
|||
c.glfwWindowHint(c.GLFW_CLIENT_API, c.GLFW_NO_API);
|
||||
const raw = c.glfwCreateWindow(@intCast(width), @intCast(height), title.ptr, null, null);
|
||||
c.glfwShowWindow(raw);
|
||||
_ = c.glfwSetKeyCallback(raw, keyCallback);
|
||||
|
||||
return Window{
|
||||
.title = title,
|
||||
|
|
@ -45,6 +47,10 @@ pub fn create(width: usize, height: usize, title: []const u8) !Window {
|
|||
};
|
||||
}
|
||||
|
||||
pub fn setResources(self: *Window, resources: *ecs.Resources) void {
|
||||
c.glfwSetWindowUserPointer(self.raw, resources);
|
||||
}
|
||||
|
||||
pub fn pollEvents() void {
|
||||
c.glfwPollEvents();
|
||||
}
|
||||
|
|
@ -66,3 +72,17 @@ pub fn destroy(self: Window) void {
|
|||
c.glfwDestroyWindow(self.raw);
|
||||
c.glfwTerminate();
|
||||
}
|
||||
|
||||
pub fn keyCallback(window: ?*c.GLFWwindow, key: c_int, scancode: c_int, action: c_int, mods: c_int) callconv(.c) void {
|
||||
_ = scancode;
|
||||
_ = mods;
|
||||
std.debug.print("test {d}\n", .{key});
|
||||
if (c.glfwGetWindowUserPointer(window)) |r| {
|
||||
const resources: *ecs.Resources = @alignCast(@ptrCast(r));
|
||||
if (action == c.GLFW_PRESS) {
|
||||
resources.input.key_pressed[@intCast(key)] = true;
|
||||
} else if (action == c.GLFW_RELEASE) {
|
||||
resources.input.key_pressed[@intCast(key)] = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1 +1,2 @@
|
|||
pub const math = @import("math.zig");
|
||||
pub const Input = @import("Input.zig");
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue