Skip to content

Commit

Permalink
Fix mouse input
Browse files Browse the repository at this point in the history
  • Loading branch information
Jhonnyg committed May 16, 2024
1 parent 8e00b19 commit 33cc206
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
26 changes: 17 additions & 9 deletions engine/platform/src/platform_window_glfw3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ namespace dmPlatform
void* m_GamepadEventCallbackUserData;
} g_GLFW3Context;

static void UpdateWindowSize(HWindow window)
{
glfwGetFramebufferSize(window->m_Window, &window->m_Width, &window->m_Height);
glfwGetWindowSize(window->m_Window, &window->m_WidthScreen, &window->m_HeightScreen);
}

static void OnError(int error, const char* description)
{
dmLogError("GLFW Error: %s\n", description);
Expand All @@ -42,7 +48,7 @@ namespace dmPlatform
static void OnWindowResize(GLFWwindow* glfw_window, int width, int height)
{
HWindow window = (HWindow) glfwGetWindowUserPointer(glfw_window);
glfwGetFramebufferSize(window->m_Window, &window->m_Width, &window->m_Height);
UpdateWindowSize(window);

if (window->m_ResizeCallback != 0x0)
{
Expand Down Expand Up @@ -234,9 +240,10 @@ namespace dmPlatform
glfwSetCharCallback(window->m_Window, OnAddCharacterCallback);
glfwSetMarkedTextCallback(window->m_Window, OnMarkedTextCallback);

glfwGetFramebufferSize(window->m_Window, &window->m_Width, &window->m_Height);
glfwSetJoystickCallback(OnJoystick);

UpdateWindowSize(window);

SetSwapInterval(window, 1);

// glfwSetWindowBackgroundColor does not exist in GLFW3, but we could potentially
Expand Down Expand Up @@ -306,15 +313,12 @@ namespace dmPlatform
void SetWindowSize(HWindow window, uint32_t width, uint32_t height)
{
glfwSetWindowSize(window->m_Window, (int) width, (int) height);
int window_width, window_height;
glfwGetFramebufferSize(window->m_Window, &window_width, &window_height);
window->m_Width = window_width;
window->m_Height = window_height;
UpdateWindowSize(window);

// The callback is not called from glfw when the size is set manually
if (window->m_ResizeCallback)
{
window->m_ResizeCallback(window->m_ResizeCallbackUserData, window_width, window_height);
window->m_ResizeCallback(window->m_ResizeCallbackUserData, window->m_Width, window->m_Height);
}
}

Expand Down Expand Up @@ -403,8 +407,12 @@ namespace dmPlatform
{
double xpos, ypos;
glfwGetCursorPos(window->m_Window, &xpos, &ypos);
*x = (int32_t) xpos;
*y = (int32_t) ypos;

float w_scale = (float) window->m_Width / (float) window->m_WidthScreen;
float h_scale = (float) window->m_Height / (float) window->m_HeightScreen;

*x = (int32_t) (xpos * w_scale);
*y = (int32_t) (ypos * h_scale);
}

bool GetDeviceState(HWindow window, DeviceState state, int32_t op1)
Expand Down
2 changes: 2 additions & 0 deletions engine/platform/src/platform_window_glfw3_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ namespace dmPlatform
double m_MouseScrollY;
int32_t m_Width;
int32_t m_Height;
int32_t m_WidthScreen;
int32_t m_HeightScreen;
uint32_t m_Samples : 8;
uint32_t m_HighDPI : 1;
uint32_t m_SwapIntervalSupported : 1;
Expand Down

0 comments on commit 33cc206

Please sign in to comment.