• Advertisement
Sign in to follow this  

[.net] Fastest way to render to a window?

This topic is 4146 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

OK, so I'm developing a high-performance 2D game in a System.Windows.Forms.Form window, and I need to use a very fast method of updating the window, so my FPS doesn't suffer. What is the fastest way to display something in a window? Currently I'm using a pictureBox; is there anything faster?

Share this post


Link to post
Share on other sites
Advertisement
DirectX? I think a panel would have less overhead than a PictureBox.

What you should do is make it so your code just takes a Control to render into. This way you can easily switch between the two once you have some non-trivial rendering going on and see if it makes any difference.

Share this post


Link to post
Share on other sites
I don't want to make DirectX or OpenGL a dependency, I'd like to do without any external APIs. At least for this game (others I use MDX).

Share this post


Link to post
Share on other sites
I assume you are using System.Drawing, in which case you can just draw right on the form, no other controls are necessary. You just need to handle the paint event.

Share this post


Link to post
Share on other sites
If you want to draw an image it would be something like this in your form.
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.DrawImageUnscaled(image, x, y);
}

You can also use all the other GDI+ drawing methods. You don't want to do direct pixel manipulation (it would be really slow).

Share this post


Link to post
Share on other sites
I am assuming that you would use a back buffer to do all your drawing before showing it to the user.

You could have a System.Drawing.Bitmap object as your back buffer.
You could draw stuff on it using the Graphics object or if you wanted pixel access, you could lock your Bitmap object and access the data through the returned pointer.

*EDITED* - Added some codes just incase you need it.


System.Drawing.Bitmap backBuffer = new Bitmap(myWin.Width, myWin.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

//Lock bitmap object.
System.Drawing.Imaging.BitmapData bmpData = backBuffer.LockBits(new System.Drawing.Rectangle(0, 0, backBuffer.Width, backBuffer.Height);

//Get pointer to pixel data and do some stuff to it.
byte* pPixelData = bmpData.Scan0.ToPointer();

//Remember to unlock it.
backBuffer.UnlockBits(bmpData);

Share this post


Link to post
Share on other sites
As a side note, the above code uses unsafe (unmanaged) pointers.

If you don't wish to use them, you can use an array to work in (as normal) then use Marshal.Copy to dump the data back into the Bitmap (using bmpData.Scan0 as the destination).

Here's some sample code that I wrote to illustrate that (VB, I'm afraid).

Share this post


Link to post
Share on other sites
A few notes:
* Bitmap.Width/Height are extremely slow in .NET 2.0, cache them where possible.
* Use a dirty rect system to avoid re-rendering areas that haven't changed.
* AGG is better/faster than GDI+.
* You can also create a plugin render system so that Direct3D is used if the HW is there, otherwise use software.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement