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

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?
what is the type of windowwidth?
Hi, Jonnin!

The type of windowwidth (and the other window variables) is int.
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);
If .xplace is also int, then you might need to also round rather than truncate.
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.
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
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; 
Registered users can post here. Sign in or register to post.