mach: wasm: Adjust width and height for HiDpi/Retina

Also implements getFramebufferSize() and getWindowSize() properly.
This commit is contained in:
iddev5 2022-05-23 23:49:11 +05:30 committed by Stephen Gutekanst
parent 017b469e2f
commit 488131ecbb
2 changed files with 31 additions and 12 deletions

View file

@ -42,8 +42,10 @@ const mach = {
machCanvasInit(width, height, id) { machCanvasInit(width, height, id) {
let canvas = document.createElement("canvas"); let canvas = document.createElement("canvas");
canvas.id = "#mach-canvas-" + mach.canvases.length; canvas.id = "#mach-canvas-" + mach.canvases.length;
canvas.width = width; canvas.style.width = width + "px";
canvas.height = height; canvas.style.height = height + "px";
canvas.width = Math.floor(width * window.devicePixelRatio);
canvas.height = Math.floor(height * window.devicePixelRatio);
canvas.tabIndex = 1; canvas.tabIndex = 1;
mach.setString(canvas.id, id); mach.setString(canvas.id, id);
@ -71,17 +73,29 @@ const mach = {
machCanvasSetSize(canvas, width, height) { machCanvasSetSize(canvas, width, height) {
const cv = mach.canvases[canvas]; const cv = mach.canvases[canvas];
if (width > 0 && height > 0) { if (width > 0 && height > 0) {
cv.canvas.width = width; cv.canvas.style.width = width + "px";
cv.canvas.height = height; cv.canvas.style.height = height + "px";
cv.canvas.width = width * window.devicePixelRatio;
cv.canvas.height = height * window.devicePixelRatio;
} }
}, },
machCanvasGetWidth(canvas) { machCanvasGetWindowWidth(canvas) {
const cv = mach.canvases[canvas];
return cv.canvas.width / window.devicePixelRatio;
},
machCanvasGetWindowHeight(canvas) {
const cv = mach.canvases[canvas];
return cv.canvas.height / window.devicePixelRatio;
},
machCanvasGetFramebufferWidth(canvas) {
const cv = mach.canvases[canvas]; const cv = mach.canvases[canvas];
return cv.canvas.width; return cv.canvas.width;
}, },
machCanvasGetHeight(canvas) { machCanvasGetFramebufferHeight(canvas) {
const cv = mach.canvases[canvas]; const cv = mach.canvases[canvas];
return cv.canvas.height; return cv.canvas.height;
}, },

View file

@ -9,8 +9,10 @@ const js = struct {
extern fn machCanvasDeinit(canvas: CanvasId) void; extern fn machCanvasDeinit(canvas: CanvasId) void;
extern fn machCanvasSetTitle(canvas: CanvasId, title: [*]const u8, len: u32) void; extern fn machCanvasSetTitle(canvas: CanvasId, title: [*]const u8, len: u32) void;
extern fn machCanvasSetSize(canvas: CanvasId, width: u32, height: u32) void; extern fn machCanvasSetSize(canvas: CanvasId, width: u32, height: u32) void;
extern fn machCanvasGetWidth(canvas: CanvasId) u32; extern fn machCanvasGetWindowWidth(canvas: CanvasId) u32;
extern fn machCanvasGetHeight(canvas: CanvasId) u32; extern fn machCanvasGetWindowHeight(canvas: CanvasId) u32;
extern fn machCanvasGetFramebufferWidth(canvas: CanvasId) u32;
extern fn machCanvasGetFramebufferHeight(canvas: CanvasId) u32;
extern fn machPerfNow() f64; extern fn machPerfNow() f64;
extern fn machLog(str: [*]const u8, len: u32) void; extern fn machLog(str: [*]const u8, len: u32) void;
@ -41,14 +43,17 @@ pub const Core = struct {
pub fn setShouldClose(_: *Core, _: bool) void {} pub fn setShouldClose(_: *Core, _: bool) void {}
pub fn getFramebufferSize(_: *Core) !structs.Size { pub fn getFramebufferSize(core: *Core) !structs.Size {
return structs.Size{ .width = 0, .height = 0 }; return structs.Size{
.width = js.machCanvasGetFramebufferWidth(core.id),
.height = js.machCanvasGetFramebufferHeight(core.id),
};
} }
pub fn getWindowSize(core: *Core) !structs.Size { pub fn getWindowSize(core: *Core) !structs.Size {
return structs.Size{ return structs.Size{
.width = js.machCanvasGetWidth(core.id), .width = js.machCanvasGetWindowWidth(core.id),
.height = js.machCanvasGetHeight(core.id), .height = js.machCanvasGetWindowHeight(core.id),
}; };
} }