summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/Input.zig133
-rw-r--r--src/ecs/entities.zig2
-rw-r--r--src/main.zig49
-rw-r--r--src/renderer/Window.zig20
-rw-r--r--src/sideros.zig1
5 files changed, 178 insertions, 27 deletions
diff --git a/src/Input.zig b/src/Input.zig
new file mode 100644
index 0000000..854d54a
--- /dev/null
+++ b/src/Input.zig
@@ -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)];
+}
diff --git a/src/ecs/entities.zig b/src/ecs/entities.zig
index 0f1c58d..b50055e 100644
--- a/src/ecs/entities.zig
+++ b/src/ecs/entities.zig
@@ -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 {
diff --git a/src/main.zig b/src/main.zig
index e1b400f..1584b1e 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -1,26 +1,18 @@
const std = @import("std");
-const c = @import("c.zig");
-const window = @import("rendering/window.zig");
-
const config = @import("config");
-const Renderer = @import("rendering/renderer_vulkan.zig");
-const math = @import("math.zig");
+const math = @import("sideros").math;
+const Input = @import("sideros").Input;
const mods = @import("mods");
const ecs = @import("ecs");
-const gltf = @import("rendering/gltf.zig");
+pub const Renderer = @import("renderer");
fn testSystem2(pool: *ecs.Pool) void {
- for (pool.getQuery(ecs.components.Position), 0..) |position, i| {
- const entity = pool.getEntity(i, ecs.components.Position);
- if (pool.getComponent(entity, ecs.components.Speed)) |speed| {
- std.debug.print("entity{d}: {any},{any},{any} {any}\n", .{ i, position.x, position.y, position.z, speed.speed });
- }
- }
+ std.debug.print("{any}\n", .{pool.resources.input.isKeyDown(.a)});
}
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
- //const allocator = gpa.allocator();
+ const allocator = gpa.allocator();
defer if (gpa.deinit() != .ok) @panic("Leaked memory");
//var global_runtime = mods.GlobalRuntime.init(allocator);
@@ -43,15 +35,21 @@ pub fn main() !void {
//var parameters = [_]usize{17};
//try runtime.callExternal(allocator, "preinit", &parameters);
- //const result = runtime.stack.pop().?;
- //std.debug.print("Result of preinit: {any}\n", .{result});
-
- const w = try window.Window.create(800, 600, "sideros");
+ var w = try Renderer.Window.create(800, 600, "sideros");
defer w.destroy();
- //var pool = try ecs.Pool.init(allocator);
- //defer pool.deinit(allocator);
+ var r = try Renderer.create(allocator, w);
+ defer r.destroy();
+
+ 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.addSystemGroup(&[_]entities.System{
// testSystem,
//});
@@ -65,12 +63,9 @@ pub fn main() !void {
// try pool.addComponent(entity, ecs.components.Speed{ .speed = 5.0 });
// }
- // var r = try Renderer.create(allocator, w);
- // defer r.destroy();
-
- // while (!w.shouldClose()) {
- // c.glfwPollEvents();
- // try r.tick();
- // pool.tick();
- // }
+ while (!w.shouldClose()) {
+ Renderer.Window.pollEvents();
+ try r.tick();
+ pool.tick();
+ }
}
diff --git a/src/renderer/Window.zig b/src/renderer/Window.zig
index a80f277..9ef389d 100644
--- a/src/renderer/Window.zig
+++ b/src/renderer/Window.zig
@@ -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;
+ }
+ }
+}
diff --git a/src/sideros.zig b/src/sideros.zig
index 6ac5dbc..3109a0a 100644
--- a/src/sideros.zig
+++ b/src/sideros.zig
@@ -1 +1,2 @@
pub const math = @import("math.zig");
+pub const Input = @import("Input.zig");