Bad mouse position when clicking left button

Hello, I’ve got a pretty big problem. When I click, the mouse coords are really weird, screenshots below will tell all the stuff.

How I get the coords:

How I call it:

Hi @Pulzerino, welcome to the GLFW forums!

Can you replicate the issue with the GLFW test events.c?

Cheers,

Doug.

Ok, I couldn’t replicate the issue, tested it with cursor.c too. So I guess, the problem is somewhere in my window class, if the getting of cursor is written properly. So I’ll post my window class here:

#include "pzpch.h"

#include "GLFWWindow.h"

namespace Pulzine
{
	GLFWWindow::GLFWWindow(uint32_t width, uint32_t height, const std::string& windowName)
	{
		m_Data.width = width;
		m_Data.height = height;
		m_Data.name = windowName;

		Init();
	}

	GLFWWindow::~GLFWWindow()
	{
	}

	void GLFWWindow::Init()
	{
		PZ_ASSERT(glfwInit(), "Failed to initialize GLFW!");

		glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GLFW_TRUE);

		m_Window = glfwCreateWindow(m_Data.width, m_Data.height, m_Data.name.c_str(), NULL, NULL);

		PZ_ASSERT(m_Window, "Failed to create window!");
		PZ_CORE_INFO("Created window [{0}] ({1}, {2})", m_Data.name, m_Data.width, m_Data.height);

		m_Context = Context::Create(m_Window);
		m_Context->Init();
		glfwSetWindowUserPointer(m_Window, &m_Data);

		SetVSync(true);

		glfwSetWindowSizeCallback(m_Window, [](GLFWwindow* window, int width, int height)
		{
			WindowData& data = *(WindowData*)glfwGetWindowUserPointer(window);

			data.width = width;
			data.height = height;
			
			WindowResizeEvent e(width, height);
			data.callback(e);
		});

		glfwSetWindowCloseCallback(m_Window, [](GLFWwindow* window)
		{
			WindowData& data = *(WindowData*)glfwGetWindowUserPointer(window);

			WindowCloseEvent e;
			data.callback(e);
		});

		glfwSetKeyCallback(m_Window, [](GLFWwindow* window, int keycode, int scancode, int action, int mods)
		{
			WindowData& data = *(WindowData*)glfwGetWindowUserPointer(window);

			switch (action)
			{
				case GLFW_PRESS:
				{
					KeyPressedEvent e(keycode, 0);
					data.callback(e);
					break;
				}
				case GLFW_RELEASE:
				{
					KeyReleasedEvent e(keycode);
					data.callback(e);
					break;
				}
				case GLFW_REPEAT:
				{
					KeyPressedEvent e(keycode, 1);
					data.callback(e);
					break;
				}
			}
		});

		glfwSetCharCallback(m_Window, [](GLFWwindow* window, unsigned int keycode)
		{
			WindowData& data = *(WindowData*)glfwGetWindowUserPointer(window);
			KeyRepeatEvent e(keycode);
			data.callback(e);
		});

		glfwSetCursorPosCallback(m_Window, [](GLFWwindow* window, double xPos, double yPos)
		{
			WindowData& data = *(WindowData*)glfwGetWindowUserPointer(window);
			MouseMovedEvent e((float)xPos, (float)yPos);
			data.callback(e);
		});

		glfwSetMouseButtonCallback(m_Window, [](GLFWwindow* window, int button, int action, int mods)
		{
			WindowData& data = *(WindowData*)glfwGetWindowUserPointer(window);

			switch (action)
			{
				case GLFW_PRESS:
				{
					MouseButtonPressedEvent e(button);
					data.callback(e);
					break;
				}
				case GLFW_RELEASE:
				{
					MouseButtonReleasedEvent e(button);
					data.callback(e);
					break;
				}
			}
		});

		glfwSetScrollCallback(m_Window, [](GLFWwindow* window, double xOffset, double yOffset)
		{
			WindowData& data = *(WindowData*)glfwGetWindowUserPointer(window);
			MouseScrolledEvent e((float)xOffset, (float)yOffset);
			data.callback(e);
		});
	}

	void GLFWWindow::OnUpdate()
	{
		m_Context->SwapBuffers();
		glfwPollEvents();
	}

	void GLFWWindow::SetVSync(bool state)
	{
		glfwSwapInterval(state);
		m_Data.vSync = state;
	}
}

It’s difficult to debug partial code from a program. My suggestion would be to either set debug breakpoints in the relevant areas of code or add debug output to the callback functions and event processing code to check.

I mean, afaik everything works just fine in my window, just this is weird, the direct output from glfwGetCursorPos is giving me these numbers. Also this window class is the only place where is glfw

I don’t know what is your issue, but I noticed that your code is pretty similar to the Hazel engine. Not that it’s a problem, I just wanted to know if it’s true :).