Write in C++ and expose to C# or write directly in C#?
Asked Answered
I

3

7

I am doing a bit of research before I am going to write my own 2D (and maybe some 3D) game engine. I have made an engine before using C# and XNA but I want to go cross platform this time by making my new engine in C++ using opengl etc.

But ... I still want the fast iteration times from C# and have access to the game engine there.

So I have a few options here.

  1. Write the engine and a CLI wrapper in C++
  2. Write the whole thing directly in C#, no C++
  3. Write in C++ and use Mono to load C# Code / Assemblies into the engine.
  4. No C#
  5. ... Probably something I didnt think of yet

I think I do need the speed for culling, scenegraph stuff, Matrix calculations, particle systems etc.

Wat are the Pros and Cons? What do you suggest?

Irrelievable answered 10/8, 2010 at 19:23 Comment(2)
Possibly stupid question since I don't know much about C#: Is it feasible to write something like this in a GC'd language like C# or would GC pauses eat you alive?Statism
It is feasible and the performance is acceptable. C++ is and will always be (I didn't say that!) faster. There are several C# game engines floating around already. Flat Red Ball for example.Irrelievable
D
2

If you want cross platform, you can't use C++/CLI. This isn't supported on any platforms other than Windows.

That being said, for cross platform, I'd either use C# with Tao, or use C++ to make the engine a library, and the use Platform Invoke to "use" the library from within your C# code. This will provide you the speed and control you need in the core engine (C++), with the flexibility of game design in C#.

This provides a clean, cross platform means of developing a game engine that can be exposed to C#.

Desdamona answered 10/8, 2010 at 19:30 Comment(5)
Hmm Tao is a little stale looking (no updates in 5 years!) and their domain (taoframework.com) has been taken over by a squatter... Is it still current or has it fallen out of date?Sorci
If you split the engine code into C++, make sure you're not making unnecessarily 'chatty' calls across the the native/managed boundary.Hunsinger
@FrustratedWithFormsDesigner: It's in the mono core now, though. Since it's a direct OpenGL wrapper, and very stable, it's still a good option. Otherwise, you can use OpenTK, but that includes other things as well (which may be useful for a game engine).Desdamona
I am really leaning towards writing and engine library using C++ and use Platform Invoke. I will be doing some experiments with P/Invoke first before diving in. Thanks for the suggestions :)Irrelievable
@WoutervD: If you want a C++ engine, and want cross platform, this is the only real option. All C++ calls from C# will need to be done using P/Invoke, since C++/CLI is windows only.Desdamona
I
1

Tao framework is dead, try OpenTK instead

Intersexual answered 10/8, 2010 at 19:44 Comment(0)
P
1

Optimized C++ code will be significantly faster (20% in my case for ODE's in 32-bit, and almost 40% in 64-bit; C# is slower in 64-bit than C# in 32-bit, but that's another post). I would take advantage of heavy math in a C++ library (integration, differential equations, etc).

Plataea answered 10/8, 2010 at 20:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.