Reputation:
I'm starting to learn how to use XNA to make a simple tile-based game, but I found myself with a problem. With OpenGL, when I define the coordinates to draw, for example, a rectangle, those coordinates refered to the viewport I made (ortho or perspective). So, for exmaple, the point of coordinates 0,0 was placed at the center of the screen.
With XNA all dimensions and coordinates are expressed in pixels, so if I draw a 10x10 rectangle it's going to be a 10 pixels x 10 pixels rectangle, with the same going on for the origin's coordinates. This way, if I change the resolution, the proportions change (in OpenGL, they didn't, all have the same size). How can I use XNA without being bound by the pixel's proportions? Can anybody tell me where I'm doing wrong or how I can go about this?
Upvotes: 2
Views: 1033
Reputation: 22133
I'm going to assume you are using Spritebatch.Draw(Texture2D texture, Vector2 position, Color color) to draw 2d sprites that represent tiles. Yes, in this case the "Vector2 position" is indeed a pair of coordinates in the window space. But there are many other ways to draw in XNA, including doing exactly what you were doing in OpenGL, i.e. create polygons and transform them using world/view matrices. See for instance this tutorial on MSDN.
That said, you could keep using Spritebatch and perform the scaling "manually" using Spritebatch.Draw(Texture2D texture, Rectangle destinationRectangle, Color color). Say a tile should always occupy 5% of the screen's width and 6% of the screen's height, then the scaling would look like:
var destinationRectangle = new Rectangle(
positionX,
positionY,
0.05 * screenWidth,
0.06 * screenHeight);
spritebatch.Draw(tileTexture, destinationRectangle, Color.White);
You can get the screen's width and height using GraphicsDevice.Viewport.Width/Height.
More generally speaking, there's nothing to prevent you from scaling coordinates however you see fit using the Matrix class and Vector2/3.Transform methods, whether you want to perform the calculation via a shader or on the cpu, or even do the transforms via custom linear algebra or whatever.
Upvotes: 0