views:

239

answers:

3

I'm trying to write an application (winforms) that can demonstrate how two oscillating colors will result in a third color. For this I need to be able to switch between two colors very fast (at >50 fps). I'm really hoping to do this in managed code.

Right now I'm drawing two small rectangular bitmaps with solid colors on top of each other. Using GDI+ DrawImage with two in-memory bitmaps in a doublebuffering enabled control doesn't cut it and results in flickering/tearing at high speeds. A timer connected to a slider triggers the switching.

  1. Is this a sensible approach?
  2. Will GDI and BitBLT be better?
  3. Does WPF perform better?
  4. What about DirectX or other technologies?

I would really appreciate feedback, TIA!

+1  A: 

I have never had good luck with GDI to do high speed graphics, so I used DirectX, but MS has dropped support for Managed DirectX, so you may need to do this in unmanaged C++.

Just write your controller in C#, then have a very thin layer of managed C++ that just calls to the unmanaged C++ DLL that has DirectX support.

You will need to get exclusive control of the computer, so that no other application can really use the cpu, otherwise you will find that your framerate can dropped, or at least no be very consistent.

If you use an older version of DirectX, such as DirectX 9.0c, that may still have support for .NET, and I used that to get a framerate for a music program of about 70 frames/second.

James Black
I haven't written C++ in years (since college actually), but if I can get away with a minimum of C++ code, that would be great.
chriscena
Unfortunately if you used a newer version of DirectX you would be doing a lot more C++, so just look for the version I mentioned.
James Black
Late feedback, but anyway: This ended up as the solution, due to some tearing problems I couldn't find a fast solution for with SDL. Found the DirectX 9.0c version, created a winform control which did the graphics magic. To avoid the additional DirectX installation, I just bundled the MDX DLLs needed in the app solution. Thanks.
chriscena
+1  A: 

Flicker should be avoidable with a double-buffered approach (and by this I don't mean just setting the rendering control's DoubleBuffered property to True - ironically, this will have no effect on flicker).

Tearing can be dealt with via DirectX, but only if you synchronize your frame rate with your monitor's refresh rate. This may not be possible, especially if you need to achieve a specific frame rate (and it doesn't happen to be your monitor's refresh rate).

I don't think WPF gets around the fundamental tearing problem (but I could be wrong).

MusiGenesis
Thanks for your feedback.
chriscena
+1  A: 

This will work with GDI, but you won't be able to control flicker, so it's kind of out of the question. Direct X may be a lot of extra fluff just for showing two non-flickering images. Perhaps SDL will work well enough? It's cross platform and you can literally code this effect in less than 30 lines of code.

http://cs-sdl.sourceforge.net/index.php/SimpleExample

Brandon Pelfrey
I had forgotten about SDL. Definitely going to test this, since there is a .NET library available. This might just be the solution.
chriscena