Skip to content

Commit c1ead62

Browse files
authored
Merge pull request #26 from yuchanns/feat/hidpi
feat(app): support hidpi
2 parents 3de1f44 + 0f057ec commit c1ead62

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
@@ -1036,9 +1036,15 @@ static const char *code = "local embed = require 'soluna.embedsource' ; local f
10361036
static void
10371037
set_app_info(lua_State *L, int index) {
10381038
lua_newtable(L);
1039-
lua_pushinteger(L, sapp_width());
1039+
const float dpi_scale = sapp_dpi_scale();
1040+
const float safe_scale = dpi_scale > 0.0f ? dpi_scale : 1.0f;
1041+
const int fb_width = sapp_width();
1042+
const int fb_height = sapp_height();
1043+
const int logical_width = (int)((float)fb_width / safe_scale + 0.5f);
1044+
const int logical_height = (int)((float)fb_height / safe_scale + 0.5f);
1045+
lua_pushinteger(L, logical_width);
10401046
lua_setfield(L, -2, "width");
1041-
lua_pushinteger(L, sapp_height());
1047+
lua_pushinteger(L, logical_height);
10421048
lua_setfield(L, -2, "height");
10431049
lua_setfield(L, index, "app");
10441050
}
@@ -1245,7 +1251,8 @@ sokol_main(int argc, char* argv[]) {
12451251

12461252
sapp_desc d;
12471253
memset(&d, 0, sizeof(d));
1248-
1254+
1255+
d.high_dpi = true;
12491256
d.width = 1024;
12501257
d.height = 768;
12511258
d.init_cb = app_init;

0 commit comments

Comments
 (0)