Skip to content

Commit 0f057ec

Browse files
committed
feat(app): support hidpi
1 parent 339b1cf commit 0f057ec

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

src/appevent.h

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,22 @@ mouse_message(struct event_message *em, const sapp_event* ev) {
1515
switch (ev->type) {
1616
case SAPP_EVENTTYPE_MOUSE_MOVE:
1717
em->typestr = "mouse_move";
18-
em->p1 = ev->mouse_x;
19-
em->p2 = ev->mouse_y;
18+
{
19+
float dpi_scale = sapp_dpi_scale();
20+
if (dpi_scale <= 0.0f) {
21+
dpi_scale = 1.0f;
22+
}
23+
float logical_x = ev->mouse_x / dpi_scale;
24+
float logical_y = ev->mouse_y / dpi_scale;
25+
if (logical_x >= 0.0f)
26+
em->p1 = (int)(logical_x + 0.5f);
27+
else
28+
em->p1 = (int)(logical_x - 0.5f);
29+
if (logical_y >= 0.0f)
30+
em->p2 = (int)(logical_y + 0.5f);
31+
else
32+
em->p2 = (int)(logical_y - 0.5f);
33+
}
2034
break;
2135
case SAPP_EVENTTYPE_MOUSE_DOWN:
2236
case SAPP_EVENTTYPE_MOUSE_UP:

src/entry.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -792,9 +792,15 @@ static const char *code = "local embed = require 'soluna.embedsource' ; local f
792792
static void
793793
set_app_info(lua_State *L, int index) {
794794
lua_newtable(L);
795-
lua_pushinteger(L, sapp_width());
795+
const float dpi_scale = sapp_dpi_scale();
796+
const float safe_scale = dpi_scale > 0.0f ? dpi_scale : 1.0f;
797+
const int fb_width = sapp_width();
798+
const int fb_height = sapp_height();
799+
const int logical_width = (int)((float)fb_width / safe_scale + 0.5f);
800+
const int logical_height = (int)((float)fb_height / safe_scale + 0.5f);
801+
lua_pushinteger(L, logical_width);
796802
lua_setfield(L, -2, "width");
797-
lua_pushinteger(L, sapp_height());
803+
lua_pushinteger(L, logical_height);
798804
lua_setfield(L, -2, "height");
799805
lua_setfield(L, index, "app");
800806
}
@@ -999,7 +1005,8 @@ sokol_main(int argc, char* argv[]) {
9991005

10001006
sapp_desc d;
10011007
memset(&d, 0, sizeof(d));
1002-
1008+
1009+
d.high_dpi = true;
10031010
d.width = 1024;
10041011
d.height = 768;
10051012
d.init_cb = app_init;

0 commit comments

Comments
 (0)