locked
XNA SetPixel() functionality RRS feed

  • Question

  • 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?

    Saturday, December 23, 2006 2:54 AM

Answers

  • 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();

            }
        }
    }
    Saturday, December 23, 2006 6:10 AM

All replies

  • 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 :-)

    Saturday, December 23, 2006 4:17 AM
  • 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();

            }
        }
    }
    Saturday, December 23, 2006 6:10 AM