views:

63

answers:

2

I want to create a simple video renderer to play around, and do stuff like creating what would be a mobile OS just for fun. My father told me that in the very first computers, you would edit a specific memory address and the screen would update. I would like to simulate this inside a window in Windows. Is there any way I can do this with C#?

+2  A: 

It's a full featured topic that's outside the scope (it won't fit, not that i won't ramble about it for hours :-) of this answer..but this should get you started with drawing in C#

http://www.geekpedia.com/tutorial50_Drawing-with-Csharp.html

Things have come a bit from the old days of direct memory manipulation..although everything is still tied to pixels.

Edit: Oh, and if you run into flickering problems and get stuck, drop me a line and i'll send you a DoubleBuffered panel to paint with.

Caladain
In WinForms > v2.0, double buffering can be turned on with the setting of the DoubleBuffered property: http://msdn.microsoft.com/en-us/library/system.windows.forms.control.doublebuffered.aspx.
codekaizen
But not on a panel or any managed control :-D Go ahead and try it..it actively ignores your attempt because the DoubleBuffered Property is a protected value.
Caladain
@Caladain: Right. I guess I forgot since it is so easy to just subclass the panel and set that property in the constructor. At any rate, DoubleBuffered is just a shortcut for setting the control style to ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, so alternatively you can just call SetStyle() with that value.
codekaizen
Thanks! I'll stick with drawing rectangles, much simpler (I'm still very unexperienced at c#)
Daniel S
+4  A: 

This used to be done because you could get direct access to the video buffer. This is typically not available with today's systems, as the video memory is managed by the video driver and OS. Further, there really isn't a 1:1 mapping of video memory buffer and what is displayed anymore. With so much memory available, it became possible to have multiple buffers and switch between them. The currently displayed buffer is called the "front buffer" and other, non-displayed buffers are called "back buffers" (for more, see http://en.wikipedia.org/wiki/Multiple_buffering). We typically write to back buffers and then have the video system update the front buffer for us. This provides smooth updates, as the video driver synchronizes the update with the scan rate of the monitor.

To write to back buffers using C#, my favorite technique is to use the WPF WritableBitmap. I've also used the System.Drawing.Bitmap to update the screen by writing pixels to it via LockBits.

codekaizen
Although I'm sure this is a better solution, I don't understand the code completely and knowing myself, I would mess up big time with unmanaged code :D
Daniel S
@Daniel - this isn't unmanaged code... unsafe, sure, but there isn't really any other way to have it be fast. You can use GetPixel/SetPixel, but you can't have fast. ;)
codekaizen