From 4b1ab4db9705248f9242d5b1260a22740c1b72f4 Mon Sep 17 00:00:00 2001 From: Martin Fouilleul Date: Wed, 23 Aug 2023 12:35:25 +0200 Subject: [PATCH] expose oc_surface_contents_scaling() to get dpi scaling from a surface --- samples/fluid/src/main.c | 28 +++++++++++++++------------- samples/glesTriangle/src/main.c | 4 +++- src/wasmbind/surface_api.json | 8 ++++++++ 3 files changed, 26 insertions(+), 14 deletions(-) diff --git a/samples/fluid/src/main.c b/samples/fluid/src/main.c index 82a9d4a..348b600 100644 --- a/samples/fluid/src/main.c +++ b/samples/fluid/src/main.c @@ -140,6 +140,8 @@ frame_buffer divBuffer[4]; GLuint vertexBuffer; +oc_surface surface; + //---------------------------------------------------------------- //NOTE(martin): initialization //---------------------------------------------------------------- @@ -419,10 +421,10 @@ ORCA_EXPORT void oc_on_mouse_up(int button) ORCA_EXPORT void oc_on_mouse_move(float x, float y, float dx, float dy) { - mouseInput.x = x * 2; - mouseInput.y = y * 2; - mouseInput.deltaX = dx * 2; - mouseInput.deltaY = dy * 2; + mouseInput.x = x; + mouseInput.y = y; + mouseInput.deltaX = dx; + mouseInput.deltaY = dy; } void init_color_checker() @@ -580,19 +582,20 @@ void input_splat(float t) //NOTE: apply force and dye if(mouseInput.down && (mouseInput.deltaX || mouseInput.deltaY)) { + oc_vec2 scaling = oc_surface_contents_scaling(surface); // account for margin float margin = 32; float offset = margin / texWidth; float ratio = 1 - 2 * margin / texWidth; - float splatPosX = (mouseInput.x / frameWidth) * ratio + offset; - float splatPosY = (1 - mouseInput.y / frameHeight) * ratio + offset; + float splatPosX = (mouseInput.x * scaling.x / frameWidth) * ratio + offset; + float splatPosY = (1 - mouseInput.y * scaling.y / frameHeight) * ratio + offset; - float splatVelX = (10000. * DELTA * mouseInput.deltaX / frameWidth) * ratio; - float splatVelY = (-10000. * DELTA * mouseInput.deltaY / frameWidth) * ratio; + float splatVelX = (10000. * DELTA * mouseInput.deltaX * scaling.x / frameWidth) * ratio; + float splatVelY = (-10000. * DELTA * mouseInput.deltaY * scaling.y / frameWidth) * ratio; - float intensity = 100 * sqrtf(square(ratio * mouseInput.deltaX / frameWidth) + square(ratio * mouseInput.deltaY / frameHeight)); + float intensity = 100 * sqrtf(square(ratio * mouseInput.deltaX * scaling.x / frameWidth) + square(ratio * mouseInput.deltaY * scaling.y / frameHeight)); float r = intensity * (sinf(2 * M_PI * 0.1 * t) + 1); float g = 0.5 * intensity * (cosf(2 * M_PI * 0.1 / M_E * t + 654) + 1); @@ -609,8 +612,6 @@ void input_splat(float t) float testDiv[texWidth / 2][texWidth / 2][4]; -oc_surface surface; - ORCA_EXPORT void oc_on_init() { oc_log_info("Hello, world (from C)"); @@ -682,8 +683,9 @@ ORCA_EXPORT void oc_on_init() ORCA_EXPORT void oc_on_resize(u32 width, u32 height) { - frameWidth = width * 2; - frameHeight = height * 2; + oc_vec2 scaling = oc_surface_contents_scaling(surface); + frameWidth = width * scaling.x; + frameHeight = height * scaling.y; } ORCA_EXPORT void oc_on_frame_refresh() diff --git a/samples/glesTriangle/src/main.c b/samples/glesTriangle/src/main.c index 4517302..9ccb81a 100644 --- a/samples/glesTriangle/src/main.c +++ b/samples/glesTriangle/src/main.c @@ -96,7 +96,9 @@ ORCA_EXPORT void oc_on_frame_refresh(void) static float alpha = 0; - glViewport(0, 0, frameSize.x * 2, frameSize.y * 2); + oc_vec2 scaling = oc_surface_contents_scaling(surface); + + glViewport(0, 0, frameSize.x * scaling.x, frameSize.y * scaling.y); GLfloat matrix[] = { cosf(alpha) / aspect, sinf(alpha), 0, 0, -sinf(alpha) / aspect, cosf(alpha), 0, 0, diff --git a/src/wasmbind/surface_api.json b/src/wasmbind/surface_api.json index 942f8bd..3166c0d 100644 --- a/src/wasmbind/surface_api.json +++ b/src/wasmbind/surface_api.json @@ -52,6 +52,14 @@ {"name": "surface", "type": {"name": "oc_surface", "tag": "S"}}] }, +{ + "name": "oc_surface_contents_scaling", + "cname": "oc_surface_contents_scaling", + "ret": {"name": "oc_vec2", "tag": "S"}, + "args": [ + {"name": "surface", + "type": {"name": "oc_surface", "tag": "S"}}] +}, { "name": "oc_surface_render_commands", "cname": "orca_surface_render_commands", -- 2.25.1