bgfx xDDD
This commit is contained in:
parent
29197efd44
commit
ef0ba9bcd4
3 changed files with 122 additions and 38 deletions
|
@ -32,6 +32,13 @@ pub fn build(b: *std.Build) void {
|
|||
const sdl_lib = sdl_dep.artifact("SDL3");
|
||||
exe_mod.linkLibrary(sdl_lib);
|
||||
|
||||
const bgfx_dep = b.dependency("zig_bgfx", .{
|
||||
.target = target,
|
||||
.optimize = .ReleaseFast,
|
||||
});
|
||||
const bgfx_lib = bgfx_dep.artifact("bgfx");
|
||||
exe_mod.linkLibrary(bgfx_lib);
|
||||
|
||||
const exe = b.addExecutable(.{
|
||||
.name = "NouVeL",
|
||||
.root_module = exe_mod,
|
||||
|
|
|
@ -40,6 +40,10 @@
|
|||
.url = "git+https://github.com/castholm/SDL.git#f6bbe8ac5e7b901db69ba62f017596090c362d84",
|
||||
.hash = "sdl-0.2.1+3.2.10-7uIn9PLkfQHKJO7TvSXbVa0VnySCHbLz28PDZIlKWF4Y",
|
||||
},
|
||||
.zig_bgfx = .{
|
||||
.url = "git+https://github.com/jean-dao/zig-bgfx#75f0a7cab7a4513a48ede67ccb4f93816b2f9f0f",
|
||||
.hash = "zig_bgfx-0.0.0-5XhMYGqwAACrsMUW9pIsSJHTzYiCHD8AXKxGa67zExGX",
|
||||
},
|
||||
},
|
||||
.paths = .{
|
||||
"build.zig",
|
||||
|
|
149
src/main.zig
149
src/main.zig
|
@ -1,10 +1,12 @@
|
|||
const std = @import("std");
|
||||
const builtin = @import("builtin");
|
||||
const nvl = @import("NouVeL_lib");
|
||||
const c = @cImport({
|
||||
@cDefine("SDL_DISABLE_OLD_NAMES", {});
|
||||
@cInclude("SDL3/SDL.h");
|
||||
@cDefine("SDL_MAIN_HANDLED", {});
|
||||
@cInclude("SDL3/SDL_main.h");
|
||||
@cInclude("bgfx/c99/bgfx.h");
|
||||
});
|
||||
|
||||
pub fn main() u8 {
|
||||
|
@ -13,16 +15,16 @@ pub fn main() u8 {
|
|||
return @bitCast(@as(i8, @truncate(status)));
|
||||
}
|
||||
|
||||
const width = 1280;
|
||||
const height = 720;
|
||||
var window: ?*c.SDL_Window = null;
|
||||
var renderer: ?*c.SDL_Renderer = null;
|
||||
var device: ?*c.SDL_GPUDevice = null;
|
||||
var frame_number: u64 = 0;
|
||||
|
||||
var content: [128:0]u8 = [_:0]u8{0} ** 128;
|
||||
var speaker: [20:0]u8 = [_:0]u8{0} ** 20;
|
||||
var curr: usize = 0;
|
||||
var exec_next_n: usize = 1;
|
||||
var index_buffer: [20]u8 = [_]u8{0} ** 20;
|
||||
var index_str: [:0]const u8 = "";
|
||||
|
||||
const Vtable = struct {
|
||||
Say: fn ([]const u8) void,
|
||||
|
@ -50,7 +52,6 @@ const fns = blk: {
|
|||
break :blk nvl.compile_aot(Vtable{ .Say = Say, .Speaker = Speaker, .ClearDialog = ClearDialog }, commands);
|
||||
};
|
||||
fn advance() void {
|
||||
index_str = std.fmt.bufPrintZ(&index_buffer, "{}", .{curr}) catch "-1";
|
||||
while (exec_next_n > 0) : (exec_next_n -= 1) {
|
||||
fns[curr]();
|
||||
curr += 1;
|
||||
|
@ -69,24 +70,79 @@ fn init(appstate: ?*?*anyopaque, argc: c_int, argv: ?[*:null]?[*:0]u8) callconv(
|
|||
return c.SDL_APP_FAILURE;
|
||||
}
|
||||
|
||||
device = c.SDL_CreateGPUDevice(c.SDL_GPU_SHADERFORMAT_SPIRV | c.SDL_GPU_SHADERFORMAT_DXIL | c.SDL_GPU_SHADERFORMAT_MSL, false, null);
|
||||
if (device != null) {
|
||||
window = c.SDL_CreateWindow("zig sdl test", 1280, 720, 0);
|
||||
if (window == null) {
|
||||
c.SDL_Log("Couldn't create window: %s", c.SDL_GetError());
|
||||
return c.SDL_APP_FAILURE;
|
||||
}
|
||||
if (!c.SDL_ClaimWindowForGPUDevice(device, window)) {
|
||||
c.SDL_Log("Couldn't claim window for GPU: %s", c.SDL_GetError());
|
||||
return c.SDL_APP_FAILURE;
|
||||
}
|
||||
} else {
|
||||
if (!c.SDL_CreateWindowAndRenderer("zig sdl test", 1280, 720, 0, &window, &renderer)) {
|
||||
c.SDL_Log("Couldn't create window/renderer: %s", c.SDL_GetError());
|
||||
return c.SDL_APP_FAILURE;
|
||||
}
|
||||
window = c.SDL_CreateWindow("zig sdl bgfx test", width, height, 0);
|
||||
if (window == null) {
|
||||
c.SDL_Log("Couldn't create window: %s", c.SDL_GetError());
|
||||
return c.SDL_APP_FAILURE;
|
||||
}
|
||||
|
||||
const w = window.?;
|
||||
|
||||
var bgfx_init = std.mem.zeroes(c.bgfx_init_t);
|
||||
bgfx_init.type = c.BGFX_RENDERER_TYPE_COUNT;
|
||||
bgfx_init.resolution.width = width;
|
||||
bgfx_init.resolution.height = height;
|
||||
bgfx_init.resolution.reset = c.BGFX_RESET_VSYNC;
|
||||
|
||||
bgfx_init.vendorId = c.BGFX_PCI_ID_NONE;
|
||||
bgfx_init.capabilities = std.math.maxInt(u64);
|
||||
bgfx_init.resolution.format = c.BGFX_TEXTURE_FORMAT_RGBA8;
|
||||
bgfx_init.resolution.numBackBuffers = 2;
|
||||
bgfx_init.limits.maxEncoders = 8;
|
||||
bgfx_init.limits.minResourceCbSize = 64 << 10;
|
||||
bgfx_init.limits.transientVbSize = 6 << 20;
|
||||
bgfx_init.limits.transientIbSize = 2 << 20;
|
||||
|
||||
bgfx_init.platformData = blk: {
|
||||
var data = std.mem.zeroes(c.bgfx_platform_data_t);
|
||||
switch (builtin.os.tag) {
|
||||
.linux => {
|
||||
const video_driver = std.mem.span(c.SDL_GetCurrentVideoDriver() orelse {
|
||||
c.SDL_Log("Couldn't get video driver: %s", c.SDL_GetError());
|
||||
return c.SDL_APP_FAILURE;
|
||||
});
|
||||
if (std.mem.eql(u8, video_driver, "x11")) {
|
||||
data.type = c.BGFX_NATIVE_WINDOW_HANDLE_TYPE_DEFAULT;
|
||||
data.ndt = get_window_ptr_prop(w, c.SDL_PROP_WINDOW_X11_DISPLAY_POINTER).?;
|
||||
data.nwh = get_window_int_prop(w, c.SDL_PROP_WINDOW_X11_WINDOW_NUMBER).?;
|
||||
} else if (std.mem.eql(u8, video_driver, "wayland")) {
|
||||
data.type = c.BGFX_NATIVE_WINDOW_HANDLE_TYPE_WAYLAND;
|
||||
data.ndt = get_window_ptr_prop(w, c.SDL_PROP_WINDOW_WAYLAND_DISPLAY_POINTER).?;
|
||||
data.nwh = get_window_ptr_prop(w, c.SDL_PROP_WINDOW_WAYLAND_SURFACE_POINTER).?;
|
||||
} else {
|
||||
c.SDL_Log("Couldn't get video driver: %s", video_driver);
|
||||
return c.SDL_APP_FAILURE;
|
||||
}
|
||||
},
|
||||
.windows => {
|
||||
data.nwh = get_window_ptr_prop(w, c.SDL_PROP_WINDOW_WIN32_HWND_POINTER).?;
|
||||
},
|
||||
.macos => {
|
||||
data.nwh = get_window_ptr_prop(w, c.SDL_PROP_WINDOW_COCOA_WINDOW_POINTER).?;
|
||||
},
|
||||
else => {
|
||||
c.SDL_Log("Unsupported os: %s", @tagName(builtin.os.tag));
|
||||
return c.SDL_APP_FAILURE;
|
||||
},
|
||||
}
|
||||
break :blk data;
|
||||
};
|
||||
c.bgfx_set_platform_data(&bgfx_init.platformData);
|
||||
|
||||
// macos needs this
|
||||
_ = c.bgfx_render_frame(-1);
|
||||
|
||||
if (!c.bgfx_init(&bgfx_init)) {
|
||||
c.SDL_Log("Couldn't initialize BGFX");
|
||||
return c.SDL_APP_FAILURE;
|
||||
}
|
||||
|
||||
c.bgfx_set_debug(c.BGFX_DEBUG_TEXT);
|
||||
c.bgfx_set_view_clear(0, c.BGFX_CLEAR_COLOR | c.BGFX_CLEAR_DEPTH, 0xdeadbeff, 1, 0);
|
||||
c.bgfx_set_view_rect(0, 0, 0, width, height);
|
||||
|
||||
advance();
|
||||
|
||||
return c.SDL_APP_CONTINUE;
|
||||
}
|
||||
|
||||
|
@ -106,20 +162,23 @@ fn on_event(appstate: ?*anyopaque, event: ?*c.SDL_Event) callconv(.c) c.SDL_AppR
|
|||
fn iterate(appstate: ?*anyopaque) callconv(.c) c.SDL_AppResult {
|
||||
_ = appstate;
|
||||
|
||||
if (device == null) {
|
||||
_ = c.SDL_SetRenderDrawColor(renderer, 0, 0, 0, c.SDL_ALPHA_OPAQUE);
|
||||
_ = c.SDL_RenderClear(renderer);
|
||||
c.bgfx_set_view_rect(0, 0, 0, width, height);
|
||||
c.bgfx_touch(0);
|
||||
c.bgfx_dbg_text_clear(0, false);
|
||||
c.bgfx_dbg_text_printf(1, 1, 0x4f, "Frame#: %d", frame_number);
|
||||
c.bgfx_dbg_text_printf(1, 20, 0x4f, "BGFX xDDDDDD");
|
||||
c.bgfx_dbg_text_printf(1, 21, 0x4f, "%d", curr);
|
||||
c.bgfx_dbg_text_printf(1, 23, 0x4f, "%s", &speaker);
|
||||
c.bgfx_dbg_text_printf(1, 24, 0x4f, "%s", &content);
|
||||
c.bgfx_dbg_text_printf(1, 26, 0x4f, "SDL %i.%i.%2i :: bgfx 1.%i :: %s",
|
||||
c.SDL_MAJOR_VERSION,
|
||||
c.SDL_MINOR_VERSION,
|
||||
c.SDL_MICRO_VERSION,
|
||||
c.BGFX_API_VERSION,
|
||||
c.bgfx_get_renderer_name(c.bgfx_get_renderer_type())
|
||||
);
|
||||
|
||||
_ = c.SDL_SetRenderScale(renderer, 4.0, 4.0);
|
||||
_ = c.SDL_SetRenderDrawColor(renderer, 255, 255, 255, c.SDL_ALPHA_OPAQUE);
|
||||
|
||||
_ = c.SDL_RenderDebugText(renderer, 20, 20, index_str.ptr);
|
||||
_ = c.SDL_RenderDebugText(renderer, 20, 120, &speaker);
|
||||
_ = c.SDL_SetRenderScale(renderer, 1.5, 1.5);
|
||||
_ = c.SDL_RenderDebugText(renderer, 50, 370, &content);
|
||||
|
||||
_ = c.SDL_RenderPresent(renderer);
|
||||
}
|
||||
frame_number = c.bgfx_frame(false);
|
||||
|
||||
return c.SDL_APP_CONTINUE;
|
||||
}
|
||||
|
@ -128,9 +187,23 @@ fn quit(appstate: ?*anyopaque, result: c.SDL_AppResult) callconv(.c) void {
|
|||
_ = appstate;
|
||||
_ = result;
|
||||
|
||||
if (device != null) {
|
||||
c.SDL_ReleaseWindowFromGPUDevice(device, window);
|
||||
c.SDL_DestroyWindow(window);
|
||||
c.SDL_DestroyGPUDevice(device);
|
||||
}
|
||||
c.bgfx_shutdown();
|
||||
c.SDL_DestroyWindow(window);
|
||||
}
|
||||
|
||||
fn get_window_ptr_prop(w: *c.SDL_Window, prop: [:0]const u8) ?*anyopaque {
|
||||
const properties = c.SDL_GetWindowProperties(w);
|
||||
if (properties == 0)
|
||||
return null;
|
||||
|
||||
return c.SDL_GetPointerProperty(properties, prop, null);
|
||||
}
|
||||
|
||||
fn get_window_int_prop(w: *c.SDL_Window, prop: [:0]const u8) ?*anyopaque {
|
||||
const properties = c.SDL_GetWindowProperties(w);
|
||||
if (properties == 0)
|
||||
return null;
|
||||
|
||||
// No idea if 0 is a valid property
|
||||
return @ptrFromInt(@as(usize, @intCast(c.SDL_GetNumberProperty(properties, prop, 0))));
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue