Resolution Independence

Started by
11 comments, last by superpig 16 years, 11 months ago
I have a book on XNA that I've been reading, and the author uses resolution independent code by using a 0.0f to 1.0f coordinate system (where 0.0f is the top and left, and 1.0f is the bottom and right), then sort of projects that onto the screen by converting it into the current resolution. I understand the concept, but he doesn't really explain what he's doing or how he's doing it. Does anyone know of an article about this, or would like to explain it to me? His code is really messy and confusing to me, and he seems to use a lot of 'magic' or, precalculated numbers that I don't know what they're for.
Advertisement
Try not to complicate things too much. All that he's doing is a simple ratio/proportion. Instead of keeping fixed coordinates you specify your positioning as relative to the width/height.

Let's use an example.

You have a widget placed at (relative) 0.25 x.

(translates to: place this widget 1/4 of the way across the screen)

If the resolution is 800x600, it would be placed at (absolute) 800 * 0.25 = 200 x (1/4 of the way across the screen).

If the resolution is 1600x1200, it would be placed at (absolute) 1600 * 0.25 = 400 x (1/4 of the way across the screen).
Okay, so I understand that.

But what about scaling the sprite? I think that's what's really confusing me. How do I decide how big the sprite should be? It seems like it would be simple, but I confused myself at some point and now I'm lost.

And same thing for collision detection.
If you know the size of the sprite in pixels, and the size of the screen in pixels, it's simple math to find out how much to scale it so that it covers say, half the window.

Sprite width in pixels = 64
Screen width in pixels = 1024
Desired sprite width in relative coordinates: 0.5
Desired sprite width in pixels = x
Scale = y

0.5 = x/1024
x = 1024*0.5 = 512

y = 512/64 = 8

Therefore, the sprite's width must be scaled 8 times larger to be half the width of the screen.
Okay, in the following code:

// Rescale to fit resolution
new Rectangle(
sprite.rect.X * width / 1024,
sprite.rect.Y * height / 768,
sprite.rect.Width * width / 1024,
sprite.rect.Height * height / 768)

'width' is the current screen width and 'height' is the current screen height.

So what is '1024' and '768'? This is from that book, and I'm not sure what the point of those numbers are, but it doesn't work without them.

Is that like, the 'native resolution' of the images? The reason I think that is because if 'sprite.rect.Width' is 512, and 'width' is 1024, then 512 * 1024 / 1024 = 512.
Just from that snippet of code, I can't tell. If I were to guess, I'd say that "width" and "height" are the desired width and height of the sprite in pixels after scaling, while 1024 and 768 are the size of the screen in pixels.

That's just a guess though. This is why I don't bother with most graphics programming books :P .
Quote:'width' is the current screen width and 'height' is the current screen height.
I read that, but I was wondering if you (or the book) had it backwards, because 1024x768 is a pretty typical screen size, while also being a very unusual sprite size.
Quote:Original post by Uphoreum
Quote:'width' is the current screen width and 'height' is the current screen height.


Right, so it looks at first glance that the book assumes 1024x768 is the 'natural' resolution that the sprites are designed for. They're not using 0.0 to 1.1 they're using 1024x768 as "1.0" and the ratio of current resolution to that resolution as the proportion to scale the sprite.

What book is that btw?
I was pretty sure that's what was going on, but I just wanted to check.

It's "Professional XNA Game Programming For Xbox 360 and Windows"

This topic is closed to new replies.

Advertisement