How can I replace game objects when I resize game window?

Sep 20, 2020 at 9:29am
Hello!
I would like make possibility of resize game window for the player.
I use this code in my Win32GUI program:

case WM_SIZING:
GetClientRect(hwnd,&Client_Rect);
windowwidth = Client_Rect.right - Client_Rect.left;
windowheight = Client_Rect.bottom - Client_Rect.top;

player.xplace *= (windowwidth/oldwindowwidth);
player.yplace *= (windowheight/oldwindowheight);

oldwindowwidth = windowwidth;
oldwindowheight = windowheight;
break;

But when I run this code, the player coordinates are hardly greater when the new window size is greater than its previous size, and when the new window size is smaller than its previous size, the player's place is always in the left-top corner of the window.
What's wrong with my code?
Sep 20, 2020 at 10:06pm
what is the type of windowwidth?
Sep 21, 2020 at 6:33am
Hi, Jonnin!

The type of windowwidth (and the other window variables) is int.
Sep 21, 2020 at 10:40am
The type of windowwidth (and the other window variables) is int.
Which means that that you have a interger division. You should cast one of the value to floating point/double:

player.xplace *= (static_cast<double>(windowwidth)/oldwindowwidth);
Sep 21, 2020 at 10:43am
If .xplace is also int, then you might need to also round rather than truncate.
Sep 24, 2020 at 8:16am
I tried your suggestions, but my program still not work. For example, when I move to the right edge of the window, then resize it, the player's spaceship stay on its old place, although it should be at the edge of the bigger window.
Sep 24, 2020 at 10:50am
the player's spaceship stay on its old place
Maybe that means GetClientRect(...) does not provide the actually size during WM_SIZING.

You could try WM_SIZE. See:

https://docs.microsoft.com/en-us/windows/win32/winmsg/wm-size
Sep 24, 2020 at 12:20pm
Yeah! Now it works! In case of window width there is some difference from the right edge, but this difference is acceptable.
Thank you, Coder777!
Now this is my piece of code:

1
2
3
4
5
6
7
8
9
10
case WM_SIZE:
            windowwidth = LOWORD(lParam);
            windowheight = HIWORD(lParam);

            player.xplace *= (static_cast<double>(windowwidth)/oldwindowwidth);
            player.yplace *= (static_cast<double>(windowheight)/oldwindowheight);

            oldwindowwidth = windowwidth;
            oldwindowheight = windowheight;
break; 
Topic archived. No new replies allowed.