Sign in to follow this  

Resolution Independence

This topic is 3843 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 .

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
Sorry to bring this topic back from the dead, but I was thinking...

Couldn't I deal with all the game logic (collisions, movement) etc. in what I would consider the "natural graphical resolution", then just scale the graphics up (or down) when drawing based on the currentWidth / naturalWidth & currentHeight / naturalHeight ?

Share this post


Link to post
Share on other sites

This topic is 3843 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this