Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Monogame - Change the size of a sprite

Tags:

c#

xna

monogame

I have created a background image for a game that is 300x135 pixels. When loaded into my game, I need it to start at point (0,0), which it does:

position = new Vector2(0, 0);

However I want to enlarge this image so it spreads to 1200x540 pixels, however I'm not quite sure how to do this, although I'm aware I'm meant to use a Rectangle or something similar.

Here are the componets for my background image:

Vector2 position;
Texture2D texture;
like image 276
toadflax Avatar asked Feb 07 '23 03:02

toadflax


1 Answers

If Monogame truly mimicked XNA, there should be a SpriteBatch.Draw(...) overload with a destinationRectangle parameter where you can set the size.

destinationRectangle
Type: Rectangle
A rectangle that specifies (in screen coordinates) the destination for drawing the sprite. If this rectangle is not the same size as the source rectangle, the sprite will be scaled to fit.

https://msdn.microsoft.com/en-us/library/ff433987.aspx

yourBatch.Draw(texture, 
               new Rectangle(position.X, position.Y, 1200, 540), 
               new Rectangle(0,0, texturesOriginalWidth, texturesOriginalHeight), 
               Color.White);
like image 75
TyCobb Avatar answered Feb 15 '23 16:02

TyCobb