Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


How do you achieve resolution independence?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
13 replies to this topic

#1 noatom   Members   -  Reputation: 785

Like
0Likes
Like

Posted 16 February 2013 - 07:17 AM

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?



Sponsor:

#2 cozzie   Members   -  Reputation: 1654

Like
0Likes
Like

Posted 16 February 2013 - 07:42 AM

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



#3 nfactorial   Members   -  Reputation: 730

Like
1Likes
Like

Posted 16 February 2013 - 08:23 AM

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!



#4 noatom   Members   -  Reputation: 785

Like
0Likes
Like

Posted 16 February 2013 - 10:10 AM

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



#5 BornToCode   Members   -  Reputation: 935

Like
3Likes
Like

Posted 16 February 2013 - 10:48 AM

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.


Edited by BornToCode, 16 February 2013 - 10:51 AM.


#6 Cornstalks   Crossbones+   -  Reputation: 6991

Like
1Likes
Like

Posted 16 February 2013 - 10:59 AM

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.


[ 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 ]

#7 noatom   Members   -  Reputation: 785

Like
-1Likes
Like

Posted 16 February 2013 - 11:01 AM

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


Edited by noatom, 16 February 2013 - 11:01 AM.


#8 Waterlimon   Crossbones+   -  Reputation: 2599

Like
0Likes
Like

Posted 16 February 2013 - 11:06 AM

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


#9 BornToCode   Members   -  Reputation: 935

Like
1Likes
Like

Posted 16 February 2013 - 11:08 AM

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



#10 Norman Barrows   Crossbones+   -  Reputation: 2204

Like
1Likes
Like

Posted 16 February 2013 - 11:30 AM

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 1988"

 

rocklandsoftware.net

 


#11 NightCreature83   Crossbones+   -  Reputation: 2932

Like
1Likes
Like

Posted 16 February 2013 - 02:25 PM

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.

That will still cause you to have aspect ratio artifacts when it changes from 4:3 to 16:9 or 16:10 or anything else though. Your best bet is to render into a -1..1 range for your render target and then use the projection matrix on the rendertarget and final render to correct how square the pixels in each step need to be.
Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, Mad Max

#12 Styves   Members   -  Reputation: 1053

Like
0Likes
Like

Posted 22 February 2013 - 11:23 AM

Or apply the aspect ratio to the width or height (depending on whether you rely on horizontal fov or vertical) to correct it. For example if (like most games these days) your 16:9 image sees more on the sides than a 4:3 image, apply w/h to h to get a correct, square result.



#13 mhagain   Crossbones+   -  Reputation: 8138

Like
0Likes
Like

Posted 22 February 2013 - 12:29 PM

Why not just use an ortho projection?  Build the correct projection matrix, load it, draw.  There's no rule that says that the inputs to such a projection must be the same as your screen resolution, so you can use this to achieve a virtual coordinate system entirely on the GPU.  Dear old Quake did it in 1996, you can do it today.

 

@NormanBarrows : video RAM is not slow.  Transfers between the two different types of memory (system and video) can be slow, but if everything is kept in video RAM all of the time it is not slow.  Why else do you think vertex buffers, texture objects, etc exist?


It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.


#14 BornToCode   Members   -  Reputation: 935

Like
0Likes
Like

Posted 22 February 2013 - 02:06 PM

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.

That will still cause you to have aspect ratio artifacts when it changes from 4:3 to 16:9 or 16:10 or anything else though. Your best bet is to render into a -1..1 range for your render target and then use the projection matrix on the rendertarget and final render to correct how square the pixels in each step need to be.

That makes no sense, because first of all when you render your quad to be screen aligned you will need to set up an orthographic projection which will use the screen resolution. So when you create your quad that is the same Width and Height as your resolution it will be an 1:1 representation, so when you apply the texture it will mapped 1:1 as well. So i do not see the scale issue you are talking about.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS