I have a normal game window which is 640*480, and everything is fine, but when I try to maximize the window, the objects translate to different positions on the screen, for example If I have a circle which is drawn at the center in the normal window, when I try to maximize it, it shifts away from the center of the screen. How do I adjust it so it draws at the center in both normal window and maximized window ?
-
\$\begingroup\$ it sounds like more of the screen is being drawn, maybe you just need to change the focus point of your camera? \$\endgroup\$– Benjamin Danger JohnsonOct 23, 2012 at 22:14
2 Answers
Is this is a 2D game, you need to position your objects based on ratios or percentages.
For example instead of putting your object at (320, 240)
to center it. Put it at (.5f*ScreenWidth, .5f*ScreenHeight)
. Make sure you update any bufferer vertices when resizing so that their positions are updated.
If it's a 3D game, your camera class should expand the view port from the center out. Meaning, keep the camera in the same position, with the same target vector.
-
\$\begingroup\$ In addition, it may be worthwhile to create some shortcut functions that draw things at a position based on the resolution. You might set it up so draw(.5, .5) draws at the center of the screen, or you might code with a specific resolution in mind and have the function do the adjusting for you (i.e. draw(320, 240)). This is probably easier in the context of games. \$\endgroup\$ Oct 24, 2012 at 1:28
This may not be the most efficient method, but it's certainly easy. If you can make canvases/surfaces/framebuffers/whatever you call them, then you can just code the game for a specific resolution (may as well stick with 640x480), draw everything gameplay-related to the canvas, and then draw the canvas scaled based on the window width and/or height.
For a game that is usually 640x480:
x_scale = 480 / window_height
y_scale = 480 / window_height