Microsoft Game Technologies: XNA SetPixel() functionality

  • twonjosh / 100 / Sun, 31 Jan 2010 21:40:00 GMT / Comments (3)
  • I'm trying to create a blank 2D texture to use to manually be able to set a pixel on the screen a certain color. This is mainly for school so I can use it to draw math splines and what-have-you.

    The best/fastest way I could come up with so far is creating a Texture2D the size of the screen then setting the pixels using:

    SetData<Color>(pixelData);

    The problem is "clearing" the data next loop. I'd have to basically loop thru all pixels and reset the values to a clear color; which is causing major slow-down. Is there a texture fill method, or a memset-like process I can use to reset the colors in this monster Color array?

  • Keywords:

    xna, setpixel, functionality, microsoft, game

  • http://msdn.itags.org/microsoft-game/244566/«« Last Thread - Next Thread »»
    1. A better approach would be to use the version of SetData() that allows you to specify the rectangle to set. If you set only one pixel, you specify a rectangle of one pixel, and pass in an array of a single Color.

      However, to draw splines, you're probably better off just drawing a list of lines in a vertex buffer. The benefit of line lists is that it's 3D geometry, so you can rotate around it if you want :-)

      jonwatte | Mon, 03 Sep 2007 23:46:00 GMT |

    2. This seemed to do the trick. Thanks again!

      using System;
      using System.Collections.Generic;
      using Microsoft.Xna.Framework;
      using Microsoft.Xna.Framework.Audio;
      using Microsoft.Xna.Framework.Graphics;
      using Microsoft.Xna.Framework.Input;
      using Microsoft.Xna.Framework.Storage;
      using Microsoft.Xna.Framework.Content;

      namespace Xbox360Game1
      {
      public class Rasterizer2D
      {
      Texture2D pixels;
      Color[] pixelData_clear;
      Game GameInstance;
      SpriteBatch batch;

      public Rasterizer2D(Game game)
      {
      GameInstance = game;

      IGraphicsDeviceService graphicsService = (IGraphicsDeviceService)GameInstance.Services.GetService(typeof(IGraphicsDeviceService));
      GraphicsDevice device = graphicsService.GraphicsDevice;

      if (game != null)
      {
      batch = new SpriteBatch(graphicsService.GraphicsDevice);
      }

      pixels = new Texture2D(device, device.Viewport.Width, device.Viewport.Height, 0, ResourceUsage.None, SurfaceFormat.Color, ResourceManagementMode.Automatic);
      pixelData_clear = new Color[device.Viewport.Width * device.Viewport.Height];

      for (int i = 0; i < device.Viewport.Width * device.Viewport.Height; ++i)
      pixelData_clearIdea = new Color(new Vector4(1.0f, 1.0f, 1.0f, 0.0f)); // b,g,r,a

      pixels.SetData<Color>(pixelData_clear);
      }

      public void SetPixel(int x, int y, Color c)
      {
      Rectangle r = new Rectangle(x, y, 1, 1);
      Color[] color = new Color[1];
      color[0] = c;

      pixels.SetData<Color>(0, r, color, 0, 1, SetDataOptions.None);
      }

      public void Clear()
      {
      pixels.SetData<Color>(pixelData_clear);
      }

      public void Draw()
      {
      batch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Texture, SaveStateMode.None);
      batch.Draw(pixels, new Vector2(0, 0), null, Color.White);
      batch.End();

      }
      }
      }

      twonjosh | Mon, 03 Sep 2007 23:47:00 GMT |

    3. Pretty sure you solved it allready, but here's my toughts:
      use a List of MyPoint(int X, int Y, Color Color) where you want to draw the spline.
      clear the screen
      iterate for each item in the List, and draw the pixel onscreen.

      as in:
      class DrawableSpline: DrawableGameComponent {
      private static Texture2D pixel;

      private SpriteBatch batch;
      private List<MyPoint> points = new List<MyPoint>();
      private Color bg;
      public DrawableSpline(Game game, Color backgroundColor): base(game) {
      pixel = new Texture2D(Game.GraphicsDevice, 1, 1, 0, ResourceUsage.None, SurfaceFormat.Color, ResourceManagementMode.Automatic);
      pixel.SetData<Color>(new Color[] { new Color(255, 255, 255, 255); });
      batch = new SpriteBatch(Game.GraphicsDevice);
      bg = backgroundColor;
      }

      public void AddPixel(int x, int y, Color c) {
      points.Add(new MyPoint(x, y, c);
      }

      public override void Draw(GameTime gameTime) {
      Game.GraphicsDevice.Clear(bg);
      batch.Begin();
      foreach(MyPoint mp in points)
      batch.Draw(pixel, new Vector2(mp.X, mp.Y), mp.Color);
      batch.End();
      }
      }

      hsilva | Fri, 25 Jan 2008 07:18:00 GMT |

  • Microsoft Game Technologies Questions

    • xna creators club online

      when i sign in i have been presented with a fill out formand a botton that saysjoin now!but the bott...

      By michaelhansen

    • Here we go!!

      http://www.microsoft.com/downloads/details.aspx?familyid=21e979e3-b8ae-4ea6-8e65-393ea7684d6c&di...

      By gms0012

    • XNA Overview Webcast

      Register ...

      By jimperry

    • XNA Parallax Engine

      Hi,I hope this doesn't break any of the rules, I have checked and I don't think that it does.The par...

      By kinlan

    • XNA doomed?

      XNA may be the greatest thing since sliced bread, but I don't see it getting a lot of support until ...

      By cachefriendly, 14 Comments

    • XNA Quake bin release 2

      New release of my Quake port to XNA.This one as a lot of new features like player walk, weapons (axe...

      By perpixel, 3 Comments

    • XNA certification exam

      I apologize if this is not the right place to ask, but I've seen some requests for DirectX certifica...

      By johanhiemstra, 7 Comments

    • XNA Breakout

      Hello, I have about a year of experence with C# and I have tried a simple game using XNA, I made bre...

      By joshmackey, 14 Comments

    • XNA in Internet Explorer?

      I was surprised to find out that XNA doesn't work in Internet Explorer as a WinForm Control. I know ...

      By johnlieurance, 14 Comments