How do you achieve resolution independence?

Started by
12 comments, last by BornToCode 11 years, 2 months ago

Say I have this:

My resolution is 800x600.I make a game that has a sprite at 400,300,that means the middle of the screen.I give that game to a friend,that has a resolution of 1920x1080.

What will happen? the sprite will appear very far from the middle of the screen,but very close to the left side of the screen.

So how can I solve this?

Advertisement

Hi.

I'd say save the screen resolution in some class member (Variable), and when creating the sprite, make it "screenwidth/2" with "screenheight/2". I'm assuming now that you're working in fullscreen mode and the user can select a screen resolution. If yet set the 800x600 resolution hard in your code, then there won't be an issue, only the window will appear smaller since he has a higher resolution :)

good luck

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

Just store the x,y position in the [0...1] range and scale it to the display. It's not the entirety of it, as you'll need to handle different aspect ratio's. But thats the basics.

n!

cozzie..that doesn't make any sense.What will happen when I'll have 90 sprites? the same goes for nfactorial...

The way i have handle that in my 2d engine i wrote 2 years ago whas to rendered the whole screen into an texture. then Create a Screen aligned quad that is the same size as the display you want. Then apply the texture to fit the whole quad. That way you can always render your game in an 800x600 resolution and you can upscale or downscale with that technique.

cozzie..that doesn't make any sense.What will happen when I'll have 90 sprites? the same goes for nfactorial...

nfactorial's suggestion does make sense. You can make your rendering window so it's scaled to be a size of 1x1, so that if you want to put something in the center, it's position is (0.5, 0.5). If you want the top left, it's (0, 0). If you want the bottom right, it's (1, 1). Like he mentioned, if you want different aspect ratios to be supported, you might have to be a bit more clever than that.

[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

clever how?! I can't believe there is nothing on google about this subject!

Basically what you do is, you have all your game objects positioned using a custom coordinate system.

Then you can lets say create 2 objects, both 1*1 units, even if the first one is 10*10 pixels and the second 256*256. All you need to do is scale them. Though you probably will want the same precision on all objects.

Now, with your custom coordinate system, you can say that 1 unit = 1 screen height. So if you have a 256*256 sprite, and you have it 1*1 units in your game coordinates, it will always be scaled to be 1 screen height regardless of how many pixels the screen has. Same with position (eg. 0.333 will always be 1 third of the screen)

On different aspect ratios you will have different amount of objects visible because 1 screen shows more of your world on the X axis than another (unless you use a black border of some type, or decide that 1 unit=1 screen width, so bigger aspect ratio would show LESS of the world on Y axis)

Of course, what this means that if you have your 16*16 sprite, it will not be 16*16 pixels, which might produce visual artifacts. You may try to avoid that by scaling so that its always some multiple of the real size (so 1 unit= some multiple of lets say 16 pixels, so 16, 32, 48, 64...) and choose the best scaling for the screen (so it always shows approximately the right amount of the world, sometimes less sometimes more)

For GUIs you might want something different, because you might not want them to be the same scale on all screens. A GUI might might look ok on a 800*600 screen might look blurry and hard to read on a 1920*1080 screen. What ive seen often used is a system where the positions and sizes are combinations of (Unit, Pixel offset) so (0.5,100) would be "100 pixels from the center". The values can be negative so you can start the count from either side of the screen.

o3o

Did you take a look at the solution i posted. That is the easist way to go about doing it without having to change any of your code. And it will work with any resolution

you have 2 basic approaches to choose from, both of which have been mentioned:

1. virtual coordinates. you code everything using virtual coordinates, such as 0 to 1.0 by 0 to 1.0 or 10,000x10,000 or whatever.

everything you draw, each time you draw, you convert the coords to the current hardware resolution.

2. stretchblit. you code everything at one resolution (such as your 800x600), draw to an offscreen surface, then strechblit the offscreen surface to the screen.

the first method, you have 1 float mul (or one int mul and one int div) for each coordinate and scale to be converted.

the second method you have the overhead of an offscreen render, and a stretchblit every frame.

You'd have to test to determine which would be faster. method #1 i suspect, as its just int mul's and div's in fast system ram, vs a strechblit in slow vidram.

However, method 2 is easier to implement, especially since you didn't address this issue when you first started your project (as you should have, but hey, now you know, right?).

Norm Barrows

Rockland Software Productions

"Building PC games since 1989"

rocklandsoftware.net

PLAY CAVEMAN NOW!

http://rocklandsoftware.net/beta.php

This topic is closed to new replies.

Advertisement