fix web focus issue

This commit is contained in:
Rokas Puzonas 2025-12-31 04:45:37 +02:00
parent 201bd26b80
commit 44968f35f2
7 changed files with 79 additions and 13 deletions

View File

@ -61,7 +61,6 @@ pub fn build(b: *std.Build) void {
options.addOption(bool, "has_tracy", has_tracy);
mod_main.addOptions("build_options", options);
// from here on different handling for native vs wasm builds
if (target.result.cpu.arch.isWasm()) {
try buildWasm(b, .{
@ -73,8 +72,8 @@ pub fn build(b: *std.Build) void {
} else {
try buildNative(b, "sokol_template", mod_main, has_console);
}
}
fn buildNative(b: *std.Build, name: []const u8, mod: *std.Build.Module, has_console: bool) !void {
const exe = b.addExecutable(.{
.name = name,
@ -170,18 +169,18 @@ fn patchWasmIncludeDirs(
// C compilation is attempted (since the sokol C library depends on the
// Emscripten SDK setup step)
lib.step.dependOn(depend_step);
std.debug.print("{s}\n", .{lib.name});
}
patchWasmIncludeDirs(lib.root_module, path, depend_step);
}
}
fn buildWasm(b: *std.Build, opts: BuildWasmOptions) !void {
opts.mod_main.sanitize_c = .off;
// build the main file into a library, this is because the WASM 'exe'
// needs to be linked in a separate build step with the Emscripten linker
const main_lib = b.addLibrary(.{
.name = opts.name,
.name = "index",
.root_module = opts.mod_main,
});
@ -202,14 +201,16 @@ fn buildWasm(b: *std.Build, opts: BuildWasmOptions) !void {
.use_webgl2 = true,
.use_emmalloc = true,
.use_filesystem = false,
.shell_file_path = opts.dep_sokol.path("src/sokol/web/shell.html"),
.shell_file_path = b.path("src/engine/shell.html"),
});
// attach to default target
b.getInstallStep().dependOn(&link_step.step);
// ...and a special run step to start the web build output via 'emrun'
const run = sokol.emRunStep(b, .{ .name = opts.name, .emsdk = dep_emsdk });
const run = sokol.emRunStep(b, .{ .name = "index", .emsdk = dep_emsdk });
run.step.dependOn(&link_step.step);
b.step("run", "Run game").dependOn(&run.step);
// TODO: Create a zip archive of all of the files. Would be useful for easier itch.io upload
}
const AddExecutableIcon = struct {

View File

@ -9,8 +9,8 @@
.hash = "sokol-0.1.0-pb1HK4iDNgCom5dkY66eUBm_bYBHEl8KWFDAiqwWgpEy",
},
.cimgui = .{
.url = "git+https://github.com/floooh/dcimgui.git#6c2c7f0452d9f44c95094c3f7dc2969c2c6a5e1a",
.hash = "cimgui-0.1.0-44ClkQYjlQCl0iQX5wlquJ-mFZMqSxXA3hE2MG_3i1BT",
.url = "git+https://github.com/floooh/dcimgui.git#33c99ef426b68030412b5a4b11487a23da9d4f13",
.hash = "cimgui-0.1.0-44ClkQRJlABdFMKRqIG8KDD6jy1eQbgPO335NziPYjmL",
.lazy = true
},
.tracy = .{

View File

@ -8,7 +8,7 @@ pub fn build(b: *std.Build) void {
.target = target,
.optimize = optimize,
.root_source_file = b.path("src/root.zig"),
.link_libc = true
.link_libc = true,
});
const stb_dependency = b.dependency("stb", .{});
@ -18,7 +18,6 @@ pub fn build(b: *std.Build) void {
.flags = &.{}
});
{
const tests = b.addTest(.{
.root_module = mod

View File

@ -520,7 +520,6 @@ pub const Lexer = struct {
try self.skipWhiteSpace();
if (try self.takeByte() != '=') {
std.debug.print("{s}\n", .{self.buffers.text.items[self.cursor..]});
return error.MissingAttributeEquals;
}
try self.skipWhiteSpace();

View File

@ -27,8 +27,14 @@ const tracy = @import("tracy");
// * https://github.com/nimgl/nimgl/issues/59
const Options = struct {
const ImguiFont = struct {
ttf_data: []const u8,
size: f32 = 16
};
allocator: std.mem.Allocator,
logger: sg.Logger = .{},
imgui_font: ?ImguiFont = null
};
const DrawFrame = struct {
@ -88,6 +94,7 @@ pub fn init(options: Options) !void {
.func = options.logger.func,
.user_data = options.logger.user_data
},
.no_default_font = options.imgui_font != null,
// TODO: Figure out a way to make imgui play nicely with UI
// Ideally when mouse is inside a Imgui window, then the imgui cursor should be used.
@ -95,6 +102,10 @@ pub fn init(options: Options) !void {
.disable_set_mouse_cursor = true
});
if (options.imgui_font) |imgui_font| {
imgui.addFont(imgui_font.ttf_data, imgui_font.size);
}
linear_sampler = sg.makeSampler(.{
.min_filter = .LINEAR,
.mag_filter = .LINEAR,

View File

@ -142,7 +142,10 @@ fn sokolInit(self: *Engine) !void {
try Gfx.init(.{
.allocator = self.allocator,
.logger = .{ .func = sokolLogCallback }
.logger = .{ .func = sokolLogCallback },
.imgui_font = .{
.ttf_data = @embedFile("../assets/roboto-font/Roboto-Regular.ttf"),
}
});
self.assets = try Assets.init();

53
src/engine/shell.html Normal file
View File

@ -0,0 +1,53 @@
<!doctype html>
<html lang="en-us">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no"/>
<title>Sokol</title>
<style>
body { margin: 0; background-color: black }
.game {
position: absolute;
top: 0px;
left: 0px;
margin: 0px;
border: 0;
width: 100%;
height: 100%;
overflow: hidden;
display: block;
image-rendering: optimizeSpeed;
image-rendering: -moz-crisp-edges;
image-rendering: -o-crisp-edges;
image-rendering: -webkit-optimize-contrast;
image-rendering: optimize-contrast;
image-rendering: crisp-edges;
image-rendering: pixelated;
-ms-interpolation-mode: nearest-neighbor;
}
</style>
</head>
<body>
<canvas class="game" id="canvas" oncontextmenu="event.preventDefault()"></canvas>
<script type='text/javascript'>
var Module = {
preRun: [],
print: (function() {
return function(text) {
text = Array.prototype.slice.call(arguments).join(' ');
console.log(text);
};
})(),
printErr: function(text) {
text = Array.prototype.slice.call(arguments).join(' ');
console.error(text);
},
};
window.onerror = function() {
console.log("onerror: " + event.message);
};
window.addEventListener("click", () => window.focus())
</script>
{{{ SCRIPT }}}
</body>
</html>