Home » Category » Microsoft Game Technologies

Microsoft Game Technologies: XNA SetPixel() functionality

100| Fri, 28 Dec 2007 04:00:00 GMT| twonjosh| 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 & Tags: xna, setpixel, functionality, microsoft, game

URL: http://msdn.itags.org/microsoft-game/244566/
 
«« Prev - Next »» 3 helpful answers below.
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 |

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 |

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 Hot Answers

Microsoft Game Technologies New questions

Microsoft Game Technologies Related Categories