开发者

Rendering to the screen in VB.net

I'm creating my own 2D engine. I planned on just using the .net drawing functions. However, this is extremely slow and the screen flashes a lot. I have to draw it to a window, however...

开发者_StackOverflow中文版

What is the best way to do this? How would I add buffers to the situation?


Assuming you're using GDI, the "screen" flashing problem is due to writing directly over the primary surface. You can avoid this by creating a "Bitmap" buffer, by doing something like this:

Bitmap buffer = New Bitmap(Me.ClientSize.Width, Me.ClientSize.Height)

Then creating a managed "Graphics" object from that buffer,

Using gfx As Graphics = New Graphics.FromImage( buffer )

// draw with gfx object

End Using

To display the updated back buffer on the primary surface, something like:

PrimaryGraphics.DrawImageUnscaled(buffer, 0, 0) or you can P/Invoke "BitBlt", which I believe is a bit faster.

If you were using C# I would suggest using "unsafe" code blocks to manipulate the buffer directly via pointers, which can speed things up quite a bit. However, I don't believe VB.net supports them.

Edit:

GDI+ is not really going to be "blazing" fast no matter what due to the way it's implemented using standard system CPU / Memory resources.

You could look into using SlimDX, which has a managed wrapper around Direct2D, a hardware accelerated 2D API. There are some tutorials available for getting started.

Edit 2:

If you stick with GDI, you can use "LockBits" on your back buffer to do some "optimized" memory operations, here is a great article on the subject.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜