NYXGFX/examples/simple/simple.zig

133 lines
4 KiB
Zig
Raw Normal View History

2026-08-11 21:26:15 +01:00
//! ----------------------------------------------------
//! ----------------------------------------------------
const std = @import("std");
const sdl = @import("sdl");
const Gfx = @import("gfx");
/// ----------------------------------------------------
/// ----------------------------------------------------
const Vertex = struct {
pos: [2]f32,
normal: [2]f32,
uv: [2]f32,
};
/// ----------------------------------------------------
/// ----------------------------------------------------
const vertices = [_]Vertex{
.{ .pos = .{ -0.5, -0.5 }, .normal = .{ 0, 0 }, .uv = .{ 0, 0 } },
.{ .pos = .{ 0.5, -0.5 }, .normal = .{ 0, 0 }, .uv = .{ 1, 0 } },
.{ .pos = .{ 0.5, 0.5 }, .normal = .{ 0, 0 }, .uv = .{ 1, 1 } },
.{ .pos = .{ -0.5, 0.5 }, .normal = .{ 0, 0 }, .uv = .{ 0, 1 } },
};
/// ----------------------------------------------------
/// ----------------------------------------------------
const indices = [_]u32{
0, 1, 2,
0, 2, 3,
};
/// ----------------------------------------------------
/// ----------------------------------------------------
pub fn main(init: std.process.Init) !void {
//
// SDL
//
// --- # ---
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);
//
// GFX
//
// --- # ---
var gfx: Gfx = try .init(.{
.window = .{
.xlib = .{
.window = @intCast(xwindow),
.display = display.?,
},
},
}, init.gpa, init.io);
defer gfx.deinit();
// --- # ---
const adapter = try gfx.createAdapter();
const surface = try gfx.createSurface();
const device = try gfx.createDevice(adapter, surface);
const swapchain = try gfx.createSwapchain(adapter, device, surface);
// --- BUFFERS ---
const vbuf = try gfx.createBuffer(vertices, .vertex, adapter, device);
const ibuf = try gfx.createBuffer(indices, .index, adapter, device);
// --- SHADERS ---
const simple_shader = try gfx.createShader(
"assets/shaders/compiled/simple.vert.spv",
"assets/shaders/compiled/simple.frag.spv",
device,
);
const pipeline = try gfx.createPipeline(.{
.vertex_attributes = &.{
.{ .location = 0, .format = .vec2, .offset = @offsetOf(Vertex, "pos") },
.{ .location = 1, .format = .vec2, .offset = @offsetOf(Vertex, "normal") },
.{ .location = 2, .format = .vec2, .offset = @offsetOf(Vertex, "uv") },
},
.vertex_binding = .{ .stride = @sizeOf(Vertex) },
.shader = simple_shader,
.device = device,
.swapchain = swapchain,
});
try pipeline.setUniform("uTime", 0.1);
try pipeline.setUniform("uTime", 0.1);
var event: sdl.SDL_Event = undefined;
var running: bool = true;
while (running) {
while (sdl.SDL_PollEvent(&event)) {
switch (event.type) {
sdl.SDL_EVENT_QUIT => running = false,
else => {},
}
}
try gfx.beginFrame(swapchain, device);
defer gfx.endFrame(swapchain, device) catch unreachable;
{
try gfx.beginRendering(swapchain);
defer gfx.endRendering(swapchain) catch unreachable;
try pipeline.bind();
try vbuf.bind();
try ibuf.bind();
try gfx.drawIndexed(indices.len, 1, 0, 0, 0);
}
// if (gfx.beginRendering(...)) |pass| {
// defer pass.end();
//
// try pass.bindPipeline(...);
// try pass.draw(...);
// }
}
}