Skip to content

Commit

Permalink
Fix imgui+hidpi
Browse files Browse the repository at this point in the history
  • Loading branch information
OndraVoves committed Apr 15, 2024
1 parent c1ef349 commit a32f700
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 17 deletions.
10 changes: 6 additions & 4 deletions examples/00-minimal/src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,10 @@ pub fn main() anyerror!u8 {
// This force renderer type.
// bgfx_init.type = .Vulkan

bgfx_init.resolution.width = WIDTH;
bgfx_init.resolution.height = HEIGHT;
const framebufferSize = window.getFramebufferSize();

bgfx_init.resolution.width = @intCast(framebufferSize[0]);
bgfx_init.resolution.height = @intCast(framebufferSize[1]);
bgfx_init.platformData.ndt = null;
bgfx_init.debug = true;

Expand Down Expand Up @@ -188,7 +190,7 @@ pub fn main() anyerror!u8 {
//
// Reset and clear
//
bgfx.reset(WIDTH, HEIGHT, reset_flags, bgfx_init.resolution.format);
bgfx.reset(@intCast(framebufferSize[0]), @intCast(framebufferSize[1]), reset_flags, bgfx_init.resolution.format);

// Set view 0 clear state.
bgfx.setViewClear(0, bgfx.ClearFlags_Color | bgfx.ClearFlags_Depth, 0x303030ff, 1.0, 0);
Expand Down Expand Up @@ -246,7 +248,7 @@ pub fn main() anyerror!u8 {
//
// If resolution or flags is changed set new matrix and reset.
//
const size = window.getSize();
const size = window.getFramebufferSize();
if (old_flags != reset_flags or old_size[0] != size[0] or old_size[1] != size[1]) {
const aspect_ratio = @as(f32, @floatFromInt(size[0])) / @as(f32, @floatFromInt(size[1]));
projMtx = zm.perspectiveFovRhGl(
Expand Down
3 changes: 3 additions & 0 deletions examples/01-zgui/src/Roboto-Medium.ttf
Git LFS file not shown
3 changes: 2 additions & 1 deletion examples/01-zgui/src/backend_glfw_bgfx.zig
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ pub fn newFrame(fb_width: u32, fb_height: u32) void {
const w = fb_width;
const h = fb_height;

zgui.backend.newFrame();

zgui.io.setDisplaySize(@floatFromInt(w), @floatFromInt(h));
zgui.io.setDisplayFramebufferScale(1.0, 1.0);

zgui.backend.newFrame();
backend_bgfx.newFrame(255);
}

Expand Down
25 changes: 21 additions & 4 deletions examples/01-zgui/src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const bgfx = zbgfx.bgfx;

const backend_glfw_bgfx = @import("backend_glfw_bgfx.zig");

const MAIN_FONT = @embedFile("Roboto-Medium.ttf");

const WIDTH = 1280;
const HEIGHT = 720;

Expand Down Expand Up @@ -50,8 +52,10 @@ pub fn main() anyerror!u8 {
// This force renderer type.
// bgfx_init.type = .Vulkan

bgfx_init.resolution.width = WIDTH;
bgfx_init.resolution.height = HEIGHT;
const framebufferSize = window.getFramebufferSize();

bgfx_init.resolution.width = @intCast(framebufferSize[0]);
bgfx_init.resolution.height = @intCast(framebufferSize[1]);
bgfx_init.platformData.ndt = null;
bgfx_init.debug = true;

Expand Down Expand Up @@ -96,7 +100,7 @@ pub fn main() anyerror!u8 {
//
// Reset and clear
//
bgfx.reset(WIDTH, HEIGHT, reset_flags, bgfx_init.resolution.format);
bgfx.reset(@intCast(framebufferSize[0]), @intCast(framebufferSize[1]), reset_flags, bgfx_init.resolution.format);

// Set view 0 clear state.
bgfx.setViewClear(0, bgfx.ClearFlags_Color | bgfx.ClearFlags_Depth, 0x303030ff, 1.0, 0);
Expand All @@ -111,8 +115,21 @@ pub fn main() anyerror!u8 {
const gpa_allocator = gpa.allocator();
defer _ = gpa.deinit();

const scale_factor = scale_factor: {
const scale = window.getContentScale();
break :scale_factor @max(scale[0], scale[1]);
};

zgui.init(gpa_allocator);
defer zgui.deinit();

// Load main font
var main_cfg = zgui.FontConfig.init();
main_cfg.font_data_owned_by_atlas = false;
_ = zgui.io.addFontFromMemoryWithConfig(MAIN_FONT, std.math.floor(16 * scale_factor), main_cfg, null);

zgui.getStyle().scaleAllSizes(scale_factor);

backend_glfw_bgfx.init(window);
defer backend_glfw_bgfx.deinit();

Expand Down Expand Up @@ -159,7 +176,7 @@ pub fn main() anyerror!u8 {
//
// If resolution or flags is changed reset.
//
const size = window.getSize();
const size = window.getFramebufferSize();
if (old_flags != reset_flags or old_size[0] != size[0] or old_size[1] != size[1]) {
bgfx.reset(
@intCast(size[0]),
Expand Down
9 changes: 5 additions & 4 deletions examples/02-runtime-shaderc/src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,9 @@ pub fn main() anyerror!u8 {
// This force renderer type.
// bgfx_init.type = .Vulkan;

bgfx_init.resolution.width = WIDTH;
bgfx_init.resolution.height = HEIGHT;
const framebufferSize = window.getFramebufferSize();
bgfx_init.resolution.width = @intCast(framebufferSize[0]);
bgfx_init.resolution.height = @intCast(framebufferSize[1]);
bgfx_init.platformData.ndt = null;
bgfx_init.debug = true;

Expand Down Expand Up @@ -237,7 +238,7 @@ pub fn main() anyerror!u8 {
//
// Reset and clear
//
bgfx.reset(WIDTH, HEIGHT, reset_flags, bgfx_init.resolution.format);
bgfx.reset(@intCast(framebufferSize[0]), @intCast(framebufferSize[1]), reset_flags, bgfx_init.resolution.format);

// Set view 0 clear state.
bgfx.setViewClear(0, bgfx.ClearFlags_Color | bgfx.ClearFlags_Depth, 0x303030ff, 1.0, 0);
Expand Down Expand Up @@ -305,7 +306,7 @@ pub fn main() anyerror!u8 {
//
// If resolution or flags is changed set new matrix and reset.
//
const size = window.getSize();
const size = window.getFramebufferSize();
if (old_flags != reset_flags or old_size[0] != size[0] or old_size[1] != size[1]) {
const aspect_ratio = @as(f32, @floatFromInt(size[0])) / @as(f32, @floatFromInt(size[1]));
projMtx = zm.perspectiveFovRhGl(
Expand Down
9 changes: 5 additions & 4 deletions examples/03-debugdraw/src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ pub fn main() anyerror!u8 {
// This force renderer type.
// bgfx_init.type = .Vulkan

bgfx_init.resolution.width = WIDTH;
bgfx_init.resolution.height = HEIGHT;
const framebufferSize = window.getFramebufferSize();
bgfx_init.resolution.width = @intCast(framebufferSize[0]);
bgfx_init.resolution.height = @intCast(framebufferSize[1]);
bgfx_init.platformData.ndt = null;
bgfx_init.debug = true;

Expand Down Expand Up @@ -95,7 +96,7 @@ pub fn main() anyerror!u8 {
//
// Reset and clear
//
bgfx.reset(WIDTH, HEIGHT, reset_flags, bgfx_init.resolution.format);
bgfx.reset(@intCast(framebufferSize[0]), @intCast(framebufferSize[1]), reset_flags, bgfx_init.resolution.format);

// Set view 0 clear state.
bgfx.setViewClear(0, bgfx.ClearFlags_Color | bgfx.ClearFlags_Depth, 0x303030ff, 1.0, 0);
Expand Down Expand Up @@ -163,7 +164,7 @@ pub fn main() anyerror!u8 {
//
// I resolution or flags is changed set new matrix and reset.
//
const size = window.getSize();
const size = window.getFramebufferSize();
if (old_flags != reset_flags or old_size[0] != size[0] or old_size[1] != size[1]) {
const aspect_ratio = @as(f32, @floatFromInt(size[0])) / @as(f32, @floatFromInt(size[1]));
projMtx = zm.perspectiveFovRhGl(
Expand Down

0 comments on commit a32f700

Please sign in to comment.