Repositioning and resizing when i change the size of my frame

5.6k Views Asked by At

Moderator's Note: This question is cross-posted on gamedev.SE.

I am trying to write a game.

When someone resizes the window of my game, I need all the graphics i have drawn on the screen to reposition correctly so that the ratio of the graphics' width and height remain correct and that the new position determined by x and y, have been adjusted for the new size screen.

In the following image, the top left corner is 0,0... all coordinates / widths / heights are measured from this point.

enter image description here

What do I need to do to x, y, width and height from the variable width and height of my screen in order to keep this aspect ratio correct?

SIDE NOTE: the aspect ratio of the screen will always be 16:9!

1

There are 1 best solutions below

1
On BEST ANSWER

Here's an idea, assign each pixel as follows: $$ x_1=\left\lfloor x_0\cdot \frac{1376}{1920}\right\rfloor \\ y_1=\left\lfloor y_0\cdot \frac{768}{1080}\right\rfloor $$

where $(x_0,y_0)$ are the large screen coordinates, and $(x_1,y_1)$ are the small screen coordinates.

Obviously, there are more pixels in the bigger screen than in the small screen, so some pixels map to the same location. In those cases, you need some way of choosing which pixel to take.

This method will change the proportions very slightly.