Hello Guest

[BUG] Mouse coordinates wrong in windowed mode

  • 3 Replies
  • 7187 Views
[BUG] Mouse coordinates wrong in windowed mode
« on: April 14, 2011, 19:13:34 »
The mouse Y-coordinate appears to be off when retrieving it with Mouse.getEventY() in windowed mode, at least on Windows (XP). This seems to occur when the window is (slightly) higher than the screen resolution minus the height of the task bar. The problem can be reproduced with the code below. When you run this and click right below the top of the window you'll see the Y-coordinate is wrong (should be near 3000). It seems like it's limited somehow by the actual screen height minus that of the task bar.

Code: [Select]
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.PixelFormat;

public class App
{
    public static void main( String[] args ) throws LWJGLException
    {
        Display.setTitle("MouseProb");
        Display.setFullscreen(false);

        Display.setDisplayMode(new DisplayMode(600, 3000));

        Display.setInitialBackground(0, 0, 0);
        PixelFormat pixelFormat = new PixelFormat(24, 8, 16, 0, 0); // bpp, alfa, dep, sten, fsaa
        Display.create(pixelFormat);

        for(;;)
        {
            // Draw stuff here

            Display.update(false);

            Display.sync(30);

            Display.processMessages();
            if(Display.isCloseRequested()) break;

            while(Mouse.next())
            {
                if(Mouse.getEventButton() >= 0 && Mouse.getEventButtonState())
                {
                    int x = Mouse.getEventX();
                    int y = Mouse.getEventY();

                    System.out.println("Mouse clicked at X=" + x + " Y=" + y);
                }
            }
        }
       
        Display.destroy();
    }
}

*

Offline Matzon

  • *****
  • 2242
Re: [BUG] Mouse coordinates wrong in windowed mode
« Reply #1 on: April 16, 2011, 19:03:51 »
On my windows 7 machine, the window created is: 616x1050

That is, 600 width as you specify + 2x8px border.

The height is cropped to 1050, since that is my max resolution. That 1050 is including a 30 px top border and a 8 px bottom border, which leaves 1012 px left.

The max value I can select is: 1037, meaning 1038 pixels is "selectable".

I am not sure why these values don't add up, but I can't see anything wrong in the LWJGL code. It seems to be the window manager in windows that does some magic.

I tried using org.lwjgl.opengl.Window.undecorated that allowed me to create a window with absolute size (ie. bigger than screen) and that gives back correct values.

*

Offline kappa

  • *****
  • 1314
Re: [BUG] Mouse coordinates wrong in windowed mode
« Reply #2 on: September 28, 2011, 22:19:05 »
Not too familiar with the Win API but some googling on this issue found this article.

Seems that there is a function that returns the client area (GetClientRect) and another which returns window area (GetWindowRect), a quick look at the native part of the LWJGL windows implementation shows that GetClientRect is used but didn't find any references to GetWindowRect, so might be that we need to incorporate that method somehow to calculate and compensate for title/border when running the Display in decorated windowed mode.
« Last Edit: September 29, 2011, 09:45:26 by kappa »

Re: [BUG] Mouse coordinates wrong in windowed mode
« Reply #3 on: September 29, 2011, 07:49:54 »
Wow thanks for looking into this  :)