Imt*_*bir 6 javascript c sdl emscripten webassembly
每当我使用 emscripten 编译 C 代码时,emcc main.c -o index.html都会生成一个 html 文件,其中包含其徽标、一些按钮和控制台。但我不想要那些。我只想要可以显示 SDL 渲染内容的画布。
我做了一些研究,并在堆栈溢出中发现了这个问题。显然你必须输入emcc --shell-file一些模板 html 作为参数。
所以我制作了一个模板html文件,如下所示
<html>
<head>
<title>Some title</title>
</head>
<body>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
但当我运行时却emcc main.c -o index.html --shell-file template.html出错了。显然 emscripten 寻找一些类似的想法 - {{{ SCRIPT }}}。
所以我添加到了{{{ SCRIPT }}}我的身体里。它编译得很好。但是当我运行时,index.html控制台localhost:3000中出现错误,显示cannot find addEventListener of undefined.
[注意,我正在运行 SDL 程序。他们的文档中提到的
我应该怎么办?提前致谢
这是一个可以用作您的的最小示例index.html,假设您的画布代码位于index.js:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<!-- Create the canvas that the C++ code will draw into -->
<canvas id="canvas" oncontextmenu="event.preventDefault()"></canvas>
<!-- Allow the C++ to access the canvas element -->
<script type='text/javascript'>
var Module = {
canvas: (function() { return document.getElementById('canvas'); })()
};
</script>
<!-- Add the javascript glue code (index.js) as generated by Emscripten -->
<script src="index.js"></script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)