[LibGDX,Desktop]Remeber the window position

I want to remember the position the of the game window when closed. Thus, when starting the game again, the window is moved to its previews position.

Doing some tests, I get odd results. Consider the following line:

Display.setLocation(Display.getX(), Display.getY());

This shouldnt move the window, but it does.

is the window a JFrame ?

Save the position of the window when you close it to a save file you have and when next time you open the window set it’s position to that from file.

Sorry, accidental medal, browsing on mobile. Back to topic he means LWJGL display. LibGDX LWJGL backend.

Yes, its a LWJGL window.

Thats what I am doing. The problem is when I am moving the window to the saved position. The position if completely wrong.

Well then split your problem.

Test if setting the position is wrong, or you saving the position is wrong.

The saving is not wrong. I added a shutdown hook which stores Display.getX() and Display.getY() in a Vector2, which is serialized and exported to a file with ObjectOutputStream.

When starting the game, the saved Vector2 is read with ObjectInputStream. I then call Display.setLocation(int x, int y). The setLocation works as it moves the actual window. But the position is wrong. getX() and getY() is either giving me wrong values, or, setLocation does not work as expected. Both are poorly documented.

Is the shut-down hook being called after the display is closed? Try printing the vector before quitting. Then while loading, print it again and see if the values are matching.

I did, they are the same.

I did some more tests, and the problem is definitely the values returned by getX() and getY().

For example, when the window is leftmost, getX() return a value between 60 and 90. When I move the window rightmost, the value is still around 60 and 90.
When it is in the middle, it return a value around 280.

The width of my screen is 1920.

Is it a bug in LWJGL or a misunderstanding from my part?

That behaviour is confusing, it’s weird. Can you post the code? Are you directly using Display.getX() and Display.getY() or using a wrapper provided by LibGDX? Does LibGDX has a window resize hook?

If not, try adding this code to the update logic and move the window.


if (Display.wasResized())
{
    System.out.println(Display.getX() + " " + Display.getY());
}

You have to resize the window after moving, there is no way to know when the window is moved in LWJGL 2. Let’s see what this prints out.

@SHC Libgdx has a resize(int width, int height) method, which gets called, when you resize the window.
I guess thats the resize hook :slight_smile:

There is not much code to show. While in the menu, I replaced the action performed by the Exit button with System.out.println(Display.getX() + " " + Display.getY());. No LibGDX wrapper(those dont exist as far as I know).

Why are we talking about resizing now?