Im migrating my game from Pygame to Monogame. I'm having trouble to create a Texture2D based on a cut of another big image.
I have one picture with all my assets and i want to create diferent Textures for each one and saved them in memory like a bufffer.
I tried this
Texture2D sprite = new Texture2D(game.Get_GraphicsDevice(), width, height);
Color[] data = new Color [width * height];
Texture2D sheet = texture;
sheet.GetData(0, new Microsoft.Xna.Framework.Rectangle(x, y, width, height), data, 0, data.Length);
sprite.SetData(data);
but im getting this code error
System.ArgumentException: 'Type T is of an invalid size for the format of this texture. (Parameter 'T')''
This is not the best way to do this as you will have tons of texture switches which then lead to seperate draw calls for each sprite (FPS killer). I assume you're going to draw your sprites with SpriteBatch, which actually can draw a part of a texture (also with the Rectangle struct).
Best course of action would be to create a simple custom sprite class, where you have fields or properties for the position and width/height in the big texture (yout atlas) or even better, a Rectangle struct and then use e.g. this function of SpriteBatch:
public void Draw(Texture2D texture, Vector2 position, Rectangle? sourceRectangle, Color color) {...}
Typically, you would then keep those 'Sprites' in a Dictionary or Array to access them later with your game objects or entities.
This way, as long as they share the same texture, all your sprites will be drawn in a single draw call by SpriteBatch's internal batching system. So your starting point, working with a bigger atlas, is actually much better. Of course, several big texture atlases are also no problem.