NYXGFX/src/gfx.zig

114 lines
2.9 KiB
Zig
Raw Normal View History

2026-08-09 14:17:49 +01:00
//! ----------------------------------------------------
//! Low cortisol graphics
//! ----------------------------------------------------
const std = @import("std");
const gtl = @import("gtl");
const vk = @import("vulkan");
const Window = @import("rhi/root.zig").Window;
const VulkanRHI = @import("rhi/vulkan/vulkan.zig");
const Self = @This();
/// ----------------------------------------------------
/// ----------------------------------------------------
pub const Backend = enum { vulkan };
/// ----------------------------------------------------
/// ----------------------------------------------------
const BackendCTX = union(Backend) {
vulkan: VulkanRHI,
};
/// ----------------------------------------------------
/// ----------------------------------------------------
pub const Config = struct {
app_name: []const u8 = "NYXGFX",
engine_name: []const u8 = "NYX",
enable_validation: bool = true,
window: Window,
backend: Backend = .vulkan,
};
//
// FIELDS
//
ctx: BackendCTX,
config: Config,
alloc: std.mem.Allocator,
/// ----------------------------------------------------
/// ----------------------------------------------------
pub fn init(
config: Config,
alloc: std.mem.Allocator,
) !Self {
const backend_ctx: BackendCTX = switch (config.backend) {
.vulkan => .{ .vulkan = try .init(config, alloc) },
};
return .{
.config = config,
.ctx = backend_ctx,
.alloc = alloc,
};
}
/// ----------------------------------------------------
/// ----------------------------------------------------
pub fn deinit(self: *Self) void {
switch (self.ctx) {
.vulkan => |*v| v.deinit(),
}
}
//
// TESTS
//
const sdl = @import("sdl");
test "Main" {
// --- # ---
if (!sdl.SDL_Init(sdl.SDL_INIT_VIDEO)) return error.FailedToInitSDL;
defer sdl.SDL_Quit();
// --- # ---
const window = sdl.SDL_CreateWindow("NYXGFX", 800, 600, 0) orelse return error.FailedToCreateWindow;
defer sdl.SDL_DestroyWindow(window);
// --- # ---
const props = sdl.SDL_GetWindowProperties(window);
const display = sdl.SDL_GetPointerProperty(props, sdl.SDL_PROP_WINDOW_X11_DISPLAY_POINTER, null);
const xwindow = sdl.SDL_GetNumberProperty(props, sdl.SDL_PROP_WINDOW_X11_WINDOW_NUMBER, 0);
// --- # ---
var gfx: Self = try .init(Config{
.window = .{
.xlib = .{
.window = @intCast(xwindow),
.display = display.?,
},
},
}, std.testing.allocator);
defer gfx.deinit();
// --- # ---
var count: u32 = 0;
_ = vk.vkEnumerateInstanceExtensionProperties(null, &count, null);
// --- # ---
if (gtl.log.print(.debug, "WINDOW", gtl.ansi.blue)) |v| {
defer v.end();
v.write("Title: {s}", .{"NYXGFX"});
v.write("Width: {}", .{800});
v.write("Height: {}", .{600});
v.write("Count: {}", .{count});
}
}