views:

3923

answers:

6

Are there any other way to render graphics in C# beyond GDI+ and XNA? (For the development of a tile map editor)

+7  A: 

http://cs-sdl.sourceforge.net/index.php/Main_Page is the solution I've come to love. If you need 3d on top of it, you can use Tao.OpenGL to render inside it. It's fast, industry standard (SDL, that is), and cross-platform.

Cody Brocious
Awesome! I'm actually familiar with SDL. I'm going to look into this.
David McGraw
+2  A: 

Yes, I have written a Windows Forms control that wraps DirectX 9.0 and provides direct pixel level manipulation of the video surface.

I actually wrote another post on StackOverflow asking if there are other better approaches, search on "Unsafe C# and pointers for 2d rendering, good or bad?"

While it is relatively high performance, it requires the unsafe compiler option as it uses pointers to access the memory efficiently. Hence the reason for this earlier post.

This is a high level of the required steps:

  1. Download the DirectX SDK.
  2. Create a new C# winforms project and reference the installed Microsoft DirectX assembly.
  3. Initialize a new DirectX Device object with Presentation Parameters (windowed, back buffering etc) you require.
  4. Create the Device, taking care to record the surface "Pitch" and current display mode (bits per pixel).
  5. When you need to display something, Lock the backbuffer surface and store the returned pointer to the start of surface memory.
  6. Use pointer arithmetic, calculate the actual pixel position in the data based on the surface pitch, bits per pixel and the actual x/y pixel coordinate.
  7. In my case for simplicity I am sticking to 32bpp, meaning setting a pixel is as simple as: *(surfacePointer + (y * pitch + x))=Color.FromARGB(255,0,0);
  8. When finished drawing, Unlock the back buffer surface. Present the surface.
  9. Repeat from step 5 as required.

Be aware that taking this approach you need to be very careful about checking the current display mode (pitch and bits per pxiel) of the target surface. Also you will need to have a strategy in place to deal with window resizing or changes of screen format while your program is running.

Ash
+1  A: 

You could try looking into WPF, using Visual Studio and/or Expression Blend. I'm not sure how sophisticated you're trying to get, but it should be able to handle a simple editor. Check out this MSDN Article for more info.

tbreffni
+2  A: 
  • Managed DirectX (Microsoft.DirectX namespace) for faster 3D graphics. It's a solid .NET wrapper over DirectX API, which comes with a bit of performance hit for creating .NET objects and marshalling. Unless you are writing a full featured modern 3D engine, it will work fine.

  • Window Presentation Foundation (WPF) (Windows.Media namespace) - best choice for 2D graphics. Also has limited 3D abilities. Aimed to replace Windows Forms with vector, hardware accelerated resolution-independent framework. Very convenient, supports several flavours of custom controls, resources, data binding, events and commands... also has a few WTFs. Speed is usually faster than GDI and slower than DirectX, and depends greatly on how you do things (seen something to work 60 times faster after rewriting in a sensible way). We had a success implementing 3 1280x1024 screens full of real-time indicators, graphs and plots on a single (and not the best) PC.

ima
A: 

You might look into the Cairo graphics library, the Mono project has bindings for C#.

AngelBlaZe
A: 

Cairo is an option: http://igorbrejc.net/development/c/welcome-to-cairo. I'm currently rewriting my mapping software using both GDI+ and Cairo. It has a tile map generator, among other features.

Igor Brejc