Sign in to follow this  
Daichi

XNA Small Solid Texture2D

Recommended Posts

So, with XNA. Anyway to go about making a small solid Texture2D without loading an image? Like generate a 2x2 white image to use for drawing rectangles or whatever.

Share this post


Link to post
Share on other sites
Off the top of my head you could use a render target for a spritebatch drawing nothing but a solid fill color then resolve the render target into a texture. I'm sure there is a better way to do this but that will work

Share this post


Link to post
Share on other sites
First you just create a texture using the constructor (http://msdn.microsoft.com/en-us/library/bb195755.aspx). Then you can use SetData to set some pixels to it (http://msdn.microsoft.com/en-us/library/bb198834.aspx).

So basically like this:


Texture2D texture = new Texture2D(
graphicsDevice,
2,
2,
0,
ResourceUsage.None,
SurfaceFormat.Color);

Color[] pixels = new Color[4];
for (int i = 0; i < pixels.Length; i++)
pixels[i] = Color.White;

texture.SetData(pixels);


Share this post


Link to post
Share on other sites
Quote:
Original post by Daichi
It seems to be TextureUsage now, but yeah. Thank you that did the trick. :D


Oops. I'm embarrassed now. I accidentally pulled up the 1.0 Refresh docs. I guess it was a little late last night. :)

Glad you got it working, though.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this