Scaling sprites

Started by
6 comments, last by MasterWorks 18 years, 9 months ago
I'm working through the obligitory tetris, breakout, and pong style games, and wondering about scaling images when resolutions change. For example, if I'm using 64x64 blocks in 1024x768 mode, I may want to use only 32x32 blocks in 800x600 mode. Of course I can alter this with a transform matrix and a call to the sprite interface's setTransform method, but I was wondering if there was another (possibly more elegant and/or efficient) way to do these scalings based on screen resolution? Thanks for any input, Antrim
Advertisement
To make resolution-independent sprites, I use a "virtual screen size" which I pass into the D3DXMatrixOrthoOffCenterLH() function. For example, say my "virtual" screen is 800x600. I'll pass this to the ortho function:

D3DXMatrixOrthoOffCenterLH(&matProj,0,800,0,600,zNear,zFar);

Then I set the viewport to cover the entire screen. This way, no matter what resolution I'm using, and no matter what size viewport I use, the sprites will still use an 800x600 grid for placement and such. They'll be scaled to match the screen as well. :)
_______________________________________________________________________Hoo-rah.
Quote:Original post by Drakex
To make resolution-independent sprites, I use a "virtual screen size" which I pass into the D3DXMatrixOrthoOffCenterLH() function. For example, say my "virtual" screen is 800x600. I'll pass this to the ortho function:

D3DXMatrixOrthoOffCenterLH(&matProj,0,800,0,600,zNear,zFar);

Then I set the viewport to cover the entire screen. This way, no matter what resolution I'm using, and no matter what size viewport I use, the sprites will still use an 800x600 grid for placement and such. They'll be scaled to match the screen as well. :)



This is definitely the way to do it -- set up a virtual resolution that you're comfortable with and then design the entire game around that resolution. If the ACTUAL resolution is, say, 1.5 times greater than your virtual resolution, then multiply every coordinate, vertex, etc. by 1.5.
Maybe I'm misunderstanding something, but it seems like both solutions are different.

From what I'm gathering, I can create the whole game with my virtual resolution, and then have it resized.

Where I'm a bit confused is at the resizing...it seems like Drakex is saying that by setting the actual viewport to the user desired resolution and using the virtual resolution that I created, the sprites will be automatically resized. From what MasterWorks is saying though, it seems like I need to do a lot more calculating as to the ratio of the users desired size to my virtual size and perform quite a few adjustments to get the proper scaling done.

Maybe you are both saying the same thing and I'm misunderstanding one of you...or maybe they are just 2 different solutions.

Thanks for the info, and any clarification you can give

Antrim
It seems like MasterWorks is thinking of something else, as well. Using my method, all coordinates remain the same - so if your game is running at 2048x1536, and you use a virtual resolution of 1024x768, placing a sprite at 512,384 will place it in the middle of the screen.
_______________________________________________________________________Hoo-rah.
Quote:Original post by Drakex
It seems like MasterWorks is thinking of something else, as well. Using my method, all coordinates remain the same - so if your game is running at 2048x1536, and you use a virtual resolution of 1024x768, placing a sprite at 512,384 will place it in the middle of the screen.


That is the same thing I was saying. I used transformed/lit vertices for 2D stuff, so the multiplication is the VERY LAST step, one that only your render class or whatever will care about. Coordinates throughout the game engine, in data files, etc. should all be relative to your virtual resolution. Converting from the virtual resolution to the actual resolution is the very last step before something appears onscreen, if you aren't doing it the '3d way'.
Are you manually transforming the vertices and multiplying the coordinates? I'm not sure if that's where we're differing. I just use the projection matrix.
_______________________________________________________________________Hoo-rah.
Quote:Original post by Drakex
Are you manually transforming the vertices and multiplying the coordinates? I'm not sure if that's where we're differing. I just use the projection matrix.


Not manually transforming AND multiplying, just manually multiplying. :) Can you use a projection matrix to do this even when using T&L vertices? Either way, our solution is the same... another thing is be sure to use floats/singles/etc and not integers for your coordinates.

This topic is closed to new replies.

Advertisement