Scaling to different Resolutions

Member
Posts: 196
Joined: 2003.10
Post: #1
Sorry 'bout all the information request posts, folks. I'm trying to get back into game dev - I figure the best way is to look at all the places I got stuck or bogged down last time and go at it again...

My code, last time I started coding a basic "engine," got very complex very quickly when I tried to make all the positions relative to the window. I basically placed all objects on a scale of 0-100 with the idea that it would represent a percentage for position. I.e. the position { 3, 20 } would represent an object who's center was located 3% from the left, and 20% from the bottom (assuming an origin in the bottom-left). The idea was that scaling the window to different resolutions would be simple - Just modify the original parameters when switching resolutions and the code would do the rest.

Thing is, this got very confusing very quickly. When variables are abstract like that, they lose their meaning and visualising code's results (what I find to be a fundamental part of programming) becomes close to impossible. Think build-run-repeat. Think bad.

So how to people handle this? Does someone have a nice, tidy way of handling this? There must be an agreed-upon style...
Quote this message in a reply
Moderator
Posts: 613
Joined: 2004.09
Post: #2
Personally i use fixed window sizes regardless of monitor resoultion so 640x480 would be the upper left regardless of what size screen the user has selected. It may not be the best way to do it but its been working well for me so far.

Kyle Richter
DragonForged.com
Twitter: @kylerichter
Quote this message in a reply
Member
Posts: 184
Joined: 2004.07
Post: #3
blobbo Wrote:So how to people handle this? Does someone have a nice, tidy way of handling this? There must be an agreed-upon style...

Most old-school programmers will probably use pixel coordinates, because then it becomes obvious in screen-space where the object should be drawn (just lob off the fraction, or round it.) This is not really important if you are using OpenGL and don't care if your textured sprites end up on pixel boundaries or not.

I use the method you mention for my coordinate systems, going from 0.0 to 1.0, which works fine. One thing to remember that if both your width and height go from 0.0 to 1.0, if your window is not a perfect square, there will be distortion. Your aspect ratio should probably be close to that of the screen in case you want to allow your game to be run in full screen mode. As such, I usually have my y coordinate range 1.0 and x coordinate range 1.333 (4/3). I allow distortion if the user has specficially resized the window to be a square (other developers like to handle this situation differently, or only have a constant number of available window sizes.)

Note that if you have a big world, the whole world doesn't have to range from 0.0 to 1.0, though it makes it easier if the scale is the same (that is, you are picking a box of size 1.0 by 1.3333 from your world.)

I'm a bit confused about what got difficult for you using this method- this method is just as good as any other coordinate system, so you will have the same problems no matter what- could you mention what it was that was difficult?
Quote this message in a reply
Member
Posts: 196
Joined: 2003.10
Post: #4
I guess the problem was that I was constantly having to convert between the two. Every time I'd have to draw something, I'd have to interpretFractionToResolution(num) on every figure I had. It got rather tiresome...
Quote this message in a reply
Moderator
Posts: 365
Joined: 2002.04
Post: #5
phydeaux Wrote:Your aspect ratio should probably be close to that of the screen in case you want to allow your game to be run in full screen mode. As such, I usually have my y coordinate range 1.0 and x coordinate range 1.333 (4/3). I allow distortion if the user has specficially resized the window to be a square (other developers like to handle this situation differently, or only have a constant number of available window sizes.)
Your aspect ratio may as well be exactly that of the screen or window. Calculate the aspect ratio as (float)width/(float)height whenever the window size changes, and you're done.

blobbo Wrote:I guess the problem was that I was constantly having to convert between the two. Every time I'd have to draw something, I'd have to interpretFractionToResolution(num) on every figure I had. It got rather tiresome...
I'd recommend going with OpenGL and not bothering with exact pixel alignment; that way you can forget about this whole issue and just use transforms to ensure that things are drawn where you want and whatever size you want. You also get rotation, scaling and 3D thrown in for free. Wink

Neil Carter
Nether - Mac games and comic art
Quote this message in a reply
Member
Posts: 184
Joined: 2004.07
Post: #6
NCarter Wrote:Your aspect ratio may as well be exactly that of the screen or window. Calculate the aspect ratio as (float)width/(float)height whenever the window size changes, and you're done.

That's right, though if you change the dimensions so you can see a different proportion of the world in the x or y directions, you are changing the gameplay, which is why I keep it constant. Obviously, if your game is 3D, this doesn't matter as much.
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #7
I normally have the Y axis go from -1.0 to 1.0, and the X axis go from -ratio to ratio, where ratio is the window's width divided by its height. To accommodate different aspect ratios, I make it so that an object can be relative to either the left side of the window, the right side of the window, or the center. With a bit of care, this system allows me to accommodate just about any window size the user throws at me.

A less pretty but easier solution would be to pick an aspect ratio (4:3, probably) and stick with it. If the user tries to resize the window, constrain its aspect ratio. If you're running fullscreen and the display's ratio isn't 4:3, resize your viewport.

- Alex Diener
Quote this message in a reply
Member
Posts: 196
Joined: 2003.10
Post: #8
Quote:I'd recommend going with OpenGL and not bothering with exact pixel alignment; that way you can forget about this whole issue

I'm afraid I don't understand.

So basically I was on the right track?! Funny - it always felt so awkward.
Quote this message in a reply
Moderator
Posts: 365
Joined: 2002.04
Post: #9
blobbo Wrote:So basically I was on the right track?! Funny - it always felt so awkward.
No, basically you're on the wrong track. Rasp

You're trying to calculate the scale of everything as you draw it. I'm saying you should draw everything at a constant size and use OpenGL's transforms to scale things to the size you want.

Neil Carter
Nether - Mac games and comic art
Quote this message in a reply
Member
Posts: 196
Joined: 2003.10
Post: #10
Ah, I get it. Draw everything to, say 800x600 scale and force people to accept that aspect ratio? Specifically what transforms should I be looking at for scaling drawing commands?
Quote this message in a reply
Moderator
Posts: 365
Joined: 2002.04
Post: #11
blobbo Wrote:Ah, I get it. Draw everything to, say 800x600 scale and force people to accept that aspect ratio?
That'd work.

Quote:Specifically what transforms should I be looking at for scaling drawing commands?
glScale(), oddly enough. Wink

Neil Carter
Nether - Mac games and comic art
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Obtaining Supported Screen Resolutions Volte 4 4,277 Sep 11, 2005 03:05 AM
Last Post: Volte
  scaling of textures WhatMeWorry 1 2,213 Jul 30, 2005 03:41 AM
Last Post: NCarter
  windows getting resized/moved after switching resolutions aarku 5 4,862 Nov 30, 2003 03:56 AM
Last Post: aarku
  Scaling down the no. of bitsÖ sealfin 7 3,520 Jul 2, 2003 01:22 AM
Last Post: sealfin