1
\$\begingroup\$

I've just recently switched from XNA to MonoGame and now I've run into something that has never happened to me in XNA before.

I've created a 2x2 texture and filled it with a color by doing this:

//Generates a texture from the color.
Texture2D texture = new Texture2D(Constants.GAME.GraphicsDevice, 2, 2, false, SurfaceFormat.Color);
texture.SetData<Color>(new Color[] { alternateColor });

AlternateColor = texture;

What I use this texture for is for drawing rectangles of different sizes using the specified color. This has usually worked just fine for me in XNA but when I do this in MonoGame I get this:

enter image description here

The screen is suppose to be black here as I'm scaling that 2x2 texture up to fill the entire screen.

I've tried turning mipmaps off, I've tried turning multisampling off and I've tried setting my sample state [0] to use PointWrap but I just can't seem to get this to work.

Any suggestions would be much welcomed.

Thanks!

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Welcome to MonoGame be prepared to learn enough to have a basic understanding of all the underlying technoligies so you can fix the many platform dependent bugs throughout the library. \$\endgroup\$ Commented Apr 2, 2014 at 15:27
  • 1
    \$\begingroup\$ It seems like there's a few things you have to do differently, in XNA it just works :/ \$\endgroup\$
    – Spiderhund
    Commented Apr 2, 2014 at 16:28
  • \$\begingroup\$ What is the value of the alternateColor variable? Why aren't you calling SetData with a 2x2 (or 4x1, whatever XNA wants) array of elements? \$\endgroup\$
    – ashes999
    Commented May 2, 2014 at 18:26

3 Answers 3

3
\$\begingroup\$

Another solution that also explains the problem you ran in to:

Texture2D texture = new Texture2D(Constants.GAME.GraphicsDevice, 2, 2, false, SurfaceFormat.Color);
texture.SetData<Color>(new Color[] {
    alternateColor,
    alternateColor,
    alternateColor,
    alternateColor 
});

One color for each pixel in the texture (2x2 = 4)

\$\endgroup\$
1
\$\begingroup\$

I ran into this (two years later) and found a different solution. The way you're creating your texture is fine (you can even create a 1x1 texture), and then specify a Rectangle when you draw it.

Creation:

var texture = new Texture2D(this.graphicsDevice, 1, 1, false, SurfaceFormat.Color);
texture.SetData<Color>(new Color[] { Color.White });

Drawing:

spriteBatch.draw(texture, null, new Rectangle(x, y, width, height), null, null, 0, Vector2.One, Color.MyChoiceOfColorHere)

As @Felsir pointed out, this is more efficient, because it shares a single white texture across all your coloured rectangles.

The four default-valued parameters preceeding the colour are the source rectangle, the origin, rotation, and scale.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Instead of creating a texture with a specific color; make it white and use the spritebatch color parameter. That way you have one texture that you can reuse instead of creating one for each color. \$\endgroup\$
    – Felsir
    Commented Sep 19, 2016 at 5:34
  • \$\begingroup\$ That's a great idea @Felsir, I updated my sample appropriately. \$\endgroup\$
    – ashes999
    Commented Sep 20, 2016 at 20:24
0
\$\begingroup\$

I changed the texture size to 1x1 instead of 2x2 and that seemed to fix it.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Any idea why that fixed it? \$\endgroup\$
    – Anko
    Commented Apr 2, 2014 at 20:05
  • \$\begingroup\$ I can only assume it has something to do with the 4 pixels not getting the exact same color since if I used a nearest-neighbour interpolation when scaling the texture up I got 4 squares, one black, one kinda greenish, one dark purple and one bluish. When there is only one pixel (when using a 1x1 texture) I can be sure that there are no other colors other than the one I specified for the texture. \$\endgroup\$
    – Spiderhund
    Commented Apr 2, 2014 at 21:33
  • \$\begingroup\$ This is exactly the reason. You defined a 2x2 texture but only set the top left pixel. \$\endgroup\$
    – Felsir
    Commented Sep 19, 2016 at 5:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .