Sunday, 8 June 2008

Copying a Dynamically Created Bitmap to a Texture2D with XNA

I am sure someone has done it before, but I couldn't find any complete example in the web, so I wrote my own code: essentially it allows you to create a texture from a dynamically created Bitmap object. So you don't have to load the Bitmap from a file but you can dynamically create one in memory, do some GDI+ drawing on it (e.g for procedural textures) and then load it to a texture and use it. This can be useful if you want to programmatically create textures. Even though I have not tried it yet, you should be able to even update the texture before every frame using this method. This is the code:

using SD = System.Drawing;
using SDI = System.Drawing.Imaging;

// ...

protected override void LoadContent()
{
// Create Bitmap that we want edit and transfer to a texture
SD.Bitmap bitmap = new SD.Bitmap(100, 100);

// Create Graphics for the bitmap
SD.Graphics bitmapGraphics = SD.Graphics.FromImage(bitmap);

// Do some drawing with GDI+, e.g.
bitmapGraphics.Clear(SD.Color.CornflowerBlue);
// ... what ever you want to do :)

// Get rid of the graphcis object
bitmapGraphics.Dispose();

// Initialize our custom texture (should be defined in the class)
customTexture = new Texture2D(this.GraphicsDevice, bitmap.Width, bitmap.Height, 0, TextureUsage.None, SurfaceFormat.Color);

// Lock the bitmap data
SDI.BitmapData data = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, bitmap.PixelFormat);

// calculate the byte size: for PixelFormat.Format32bppArgb (standard for GDI bitmaps) it's the hight * stride
int bufferSize = data.Height * data.Stride; // stride already incorporates 4 bytes per pixel

// create buffer
byte[] bytes = new byte[bufferSize];

// copy bitmap data into buffer
Marshal.Copy(data.Scan0, bytes, 0, bytes.Length);

// copy our buffer to the texture
customTexture.SetData(bytes);

// unlock the bitmap data
bitmap.UnlockBits(data);
}

Using a standard Windows.Form for XNA Projects

Something that I found very helpful was the article by Pedro Güida. It tells you how to render the standard XNA output to a standard Windows.Form. Why would you want to do that? Well, some say it's good for writing level editors, but in my case it was useful since I wanted to override the Forms WinProc, which isn't possible in the Game class delivered by the XNA templates. There are some other solutions that accomplish that, but Pedro's solution is the only one that let's you still use the comfortable Game's class in conjunction with a standard Form. So here you go :) Thanks Pedro for this elegant and simple solution!

Saturday, 2 February 2008

Deactivate Flash in Firefox

Firefox can cause high amount of CPU-load when displaying multiple tabs with flash content. A very nice little plug-in for Firefox is Flashblock (must be good, considering it's name ;). You can either download it from the developer's website, or via the firefox addon-page. What it does is initially replacing every flash animation on a web page you load with a little spacer. Clicking on the spacer loads the appropriate flash movie. The effect is that it dramatically reduces the CPU load, as it suppresses all flash movies by default (a nice side-effect is that lots of adds disappear, as they are often implemented in flash). It also supports a white-list of pages on which flash should be loaded as usual, so you can add pages like youtube.com if you don't want to click on the spacer every time.