January 16, 2005
Review of the benefits of C# for games programming
I have just about reached the limits of what I can hope to achieve with my C# to C++ translator project. There is so much more I would like to do but it is getting to the stage where I cannot make significant progress in weekend time alone. I'm also extremely busy at work and have little energy left. Maybe I'll pick up where I left off in a few months.
I did pretty well in the time available I think. I can translate simple C# programs to C++. The build process is well integrated into my IDE. I can do C# source level debugging using a standard C++ debugger. It took 6 days solid work to get this far.
This is a summary of what I have learned and what I think the benefits of C# would be in console game development. I'll probably round it off with a review of the disadvantages and the alternatives once my enthusiasm has had a change to die down a little!
Reflection
- Custom metadata annotations
- Can be queried at compile time for automatic code generation or at runtime
- Supports automatic serialization and deserialization
- Class factories
- Can be used to eliminate redundant code, e.g. visitor pattern or prototype pattern
- Automatic file format / schema generation
- Automatic binding of game code to tools such as RDBMS or level editor
- Automatic binding of game code to scripting languages such as Lua
- Automatic generation of multiplayer network protocol
Faster build times
- Game code can be run, tested and debugged on a .NET virtual machine for full rebuild times probably less than 1 minute
- Generated C++ code has minimal physical dependencies and automatically applies the pimpl idiom without programmer intervention
Reuse existing C# and C++ development tools
- Visual Studio 2005 would be a suitable IDE
- It provides basic editing like syntax highlighting, error checking as you type and code navigation
- Integrate into IDE using custom build rules, NAnt or Jam
- Automatic refactoring tools for rename, extract method, encapsulate field, etc
- C# source level debugging with any C++ debugger on any platform
- Profile C# program with any C++ profiler on any platform
- C# programs can be disassembled to native assembly language for optimization purposes
Testing and test driven development
- C# unit testing tools have a slightly more concise syntax than those of C++
- Support for automatically generated mock objects
- Automatic code coverage analysis
Fast, safe and fully automatic memory management
- Corrects flaws in reference counting by means of weak references and compile time analysis
- Memory leaks (unreachable allocations) are impossible
- Dangling pointers are impossible
- Better performance than garbage collection but not as flexible
- Reference counting constraints will not prevent the program from running in a garbage collected VM
Comprehensive class library
- Modify key classes in the Mono class library to work with reference counting
- Unrestrictive MIT license
- Easier to use than STL and boost (IMHO)
Compiled code should have similar performance to C++
- There is no reason to expect worse performance for constructs that have a direct equivalent in C++. They will be translated into exactly their C++ equivalents.
- Some constructs are higher level than C++, e.g. arrays are always variable length. Expect poorer performance in these cases.
- Expect improved performance for some constructs. For example, C#'s simplified inheritance model can support more efficient reference conversions. This is essential for effective use of interfaces.
Higher level language features
- First class language support for the observer pattern through events and delegates
- First class language support for thread synchronization constructs
- Support for interfaces
- Memory leaks and dangling pointers are impossible
- Runtime checking on array bounds, null dereference, arithmetic overflow
- These can be disabled in optimized builds
- Generics are an alternative to templates. There are pros and cons. Templates support meta-programming / generics reduce code bloat
Automatic code generation framework
- Compiler plugins generates code from C# metadata at build time
- Serialization
- Bindings
- Diagnostic stack traces
- Remote procedure call
- Other schema languages / DDLs
C++ / C# interoperation
- Methods can be implemented in C++ by using the "extern" qualifier
- Classes can be defined in C# so that their metadata is reflected but then actually implemented in C++
- C++ can call C# methods directly and access C# fields and properties directly because they are translated into C++
Miscellaneous
- C# game code can be reused directly in a C# tool, such as a level editor
- Translator could actually translate any .NET language to C++, e.g. F#, VB.NET or even Managed C++ or C++/CLI
what new project you're going to present us with?
:-)
platinum mum
http://www.gamesfromwithin.com/articles/0412/000054.html
<< Home