Skip to content

Commit 36ff153

Browse files
committed
[0729] 使用社区版 GLFW && 修复 WASM 版本光标不闪烁的问题
1 parent c980b20 commit 36ff153

5 files changed

Lines changed: 53 additions & 9 deletions

File tree

bin/wasm_server.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,35 @@
1010
#if not os.path.exists(html) and os.path.exists(js):
1111
with open(html, "w", encoding="utf-8") as f:
1212
f.write("""<!doctype html>
13+
<!doctype html>
1314
<html>
1415
<head>
1516
<meta charset="utf-8">
1617
<title>stem</title>
1718
<style>
18-
html, body, canvas {
19+
html, body {
1920
margin: 0;
2021
width: 100%;
2122
height: 100%;
2223
overflow: hidden;
24+
background-color: #9f9f9f;
25+
}
26+
#main-canvas {
27+
display: block;
28+
width: 100%;
29+
height: 100%;
30+
outline: none;
2331
}
2432
</style>
2533
</head>
2634
<body>
27-
<canvas id="canvas"></canvas>
28-
35+
<canvas id="main-canvas" tabindex="1"></canvas>
36+
<!-- tabindex="1" 允许 main-canvas 被获得 focus -->
2937
<script>
3038
var Module = {
31-
canvas: document.getElementById('canvas')
39+
canvas: document.getElementById("main-canvas")
3240
};
3341
</script>
34-
3542
<script src="stem.js"></script>
3643
</body>
3744
</html>

devel/0729.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# [0729] 使用社区版 GLFW && 修复 WASM 版本光标不闪烁的问题
2+
3+
## What
4+
- 更改了编译器选项,使用社区维护的 C++ 版 GLFW
5+
- 修改 html 适配社区版 GLFW
6+
- 修改获取窗口大小的逻辑,resize相关逻辑,适配社区版 GLFW
7+
- 修复 WASM 版本光标不闪烁的问题
8+
9+
## Why
10+
- 社区版支持的 GLFW API 更多,具体参考(https://github.com/pongasoft/emscripten-glfw/blob/master/docs/Usage.md)
11+
- 改用社区版后,构建产物大小与先前没有区别
12+
- 社区版对窗口大小的处理逻辑与 Emscripten 提供的 JS 版本不同,因此修改相关逻辑做适配
13+
- 社区版实现了 `glfwSetWindowFocusCallback` API,因此虽然没有代码修改,但是解决了 WASM 版本光标不闪烁的问题

src/Plugins/ImGui/im_gui.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
#ifndef GL_SILENCE_DEPRECATION
2727
#define GL_SILENCE_DEPRECATION
2828
#endif
29+
#ifdef __EMSCRIPTEN__
30+
#include <emscripten/html5.h>
31+
#endif
2932
#include <GLFW/glfw3.h>
3033
#include <cstdio>
3134

@@ -458,6 +461,15 @@ void
458461
gui_root_extents (SI& width, SI& height) {
459462
// get the screen size: the GLFW counterpart of Qt's
460463
// QGuiApplication::primaryScreen()->size()
464+
#ifdef __EMSCRIPTEN__
465+
// Browsers do not expose a meaningful primary monitor to GLFW.
466+
// Use the current canvas CSS size as the root extent instead.
467+
double css_w, css_h;
468+
emscripten_get_element_css_size ("#main-canvas", &css_w, &css_h);
469+
width = (SI) css_w * PIXEL;
470+
height= (SI) css_h * PIXEL;
471+
return;
472+
#else
461473
int w= 1920, h= 1080;
462474
if (s_glfw_initialized) {
463475
GLFWmonitor* monitor= glfwGetPrimaryMonitor ();
@@ -469,6 +481,7 @@ gui_root_extents (SI& width, SI& height) {
469481
}
470482
width = w * PIXEL;
471483
height= h * PIXEL;
484+
#endif
472485
}
473486

474487
void

src/Plugins/ImGui/im_tm_widget.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include <GLFW/glfw3.h> // defines GLFWwindow, drags in system OpenGL headers
3737

3838
#ifdef __EMSCRIPTEN__
39+
#include <GLFW/emscripten_glfw3.h>
3940
#include <emscripten.h>
4041
#endif
4142

@@ -233,14 +234,24 @@ im_tm_widget_rep::im_tm_widget_rep (int mask, command _quit)
233234

234235
float main_scale=
235236
ImGui_ImplGlfw_GetContentScaleForMonitor (glfwGetPrimaryMonitor ());
236-
const int win_w = 800; // size for init
237-
const int win_h = 600;
237+
SI win_w, win_h;
238+
gui_root_extents (win_w, win_h);
239+
win_w = win_w / PIXEL;
240+
win_h = win_h / PIXEL;
238241
GLFWmonitor* monitor= glfwGetPrimaryMonitor ();
239242
int wa_x= 0, wa_y= 0, wa_w= win_w, wa_h= win_h;
240243
if (monitor != nullptr)
241244
glfwGetMonitorWorkarea (monitor, &wa_x, &wa_y, &wa_w, &wa_h);
245+
#ifdef __EMSCRIPTEN__
246+
glfwWindowHint (GLFW_SCALE_FRAMEBUFFER, GLFW_TRUE); // Enable retina
247+
emscripten_glfw_set_next_window_canvas_selector ("#main-canvas");
248+
#endif
242249
window= glfwCreateWindow ((int) win_w * main_scale, (int) win_h * main_scale,
243250
"Mogan (ImGui)", nullptr, nullptr);
251+
#ifdef __EMSCRIPTEN__
252+
emscripten_glfw_make_canvas_resizable (window, "window",
253+
nullptr); // emscripten 3.1.56 specific
254+
#endif
244255
if (window == nullptr) {
245256
return;
246257
}

xmake/targets/stem.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,9 @@ target("stem") do
101101

102102
if is_plat("wasm") then
103103
add_cxxflags("-O3")
104-
add_cxxflags("-sUSE_GLFW=3")
104+
add_cxxflags("--use-port=contrib.glfw3")
105105
add_ldflags("-O3")
106-
add_ldflags("-sUSE_GLFW=3")
106+
add_ldflags("--use-port=contrib.glfw3")
107107
add_ldflags("-sINITIAL_MEMORY=512MB")
108108
add_ldflags("-sALLOW_MEMORY_GROWTH=1")
109109
add_ldflags("-sSTACK_SIZE=32MB", {force = true})

0 commit comments

Comments
 (0)